@@ -709,15 +709,56 @@ func TestResetCapture(t *testing.T) {
709
709
}
710
710
711
711
func TestRelevantAuditLogging (t * testing.T ) {
712
- tx := makeTransaction (t )
713
- tx .WAF .AuditLogRelevantStatus = regexp .MustCompile (`(403)` )
714
- tx .variables .responseStatus .Set ("403" )
715
- tx .AuditEngine = types .AuditEngineRelevantOnly
716
- // tx.WAF.auditLogger = auditlog.NewAuditLogger()
717
- tx .ProcessLogging ()
718
- // TODO how do we check if the log was writen?
719
- if err := tx .Close (); err != nil {
720
- t .Error (err )
712
+ tests := []struct {
713
+ name string
714
+ status string
715
+ interruption * types.Interruption
716
+ relevantLog bool
717
+ }{
718
+ {
719
+ name : "TestRelevantAuditLogging" ,
720
+ status : "403" ,
721
+ interruption : nil ,
722
+ relevantLog : true ,
723
+ },
724
+ {
725
+ name : "TestNotRelevantAuditLogging" ,
726
+ status : "200" ,
727
+ interruption : nil ,
728
+ relevantLog : false ,
729
+ },
730
+ {
731
+ name : "TestRelevantAuditLoggingWithInterruption" ,
732
+ interruption : & types.Interruption {
733
+ Status : 403 ,
734
+ Action : "deny" ,
735
+ },
736
+ relevantLog : true ,
737
+ },
738
+ }
739
+
740
+ for _ , tt := range tests {
741
+ t .Run (tt .name , func (t * testing.T ) {
742
+ tx := makeTransaction (t )
743
+ debugLog := bytes.Buffer {}
744
+ tx .debugLogger = debuglog .Default ().WithLevel (debuglog .LevelDebug ).WithOutput (& debugLog )
745
+ tx .WAF .AuditLogRelevantStatus = regexp .MustCompile (`(403)` )
746
+ tx .variables .responseStatus .Set (tt .status )
747
+ tx .interruption = tt .interruption
748
+ tx .AuditEngine = types .AuditEngineRelevantOnly
749
+ tx .audit = true // Mimics that there is something to audit
750
+ tx .ProcessLogging ()
751
+ // TODO how do we check if the log was written?
752
+ if err := tx .Close (); err != nil {
753
+ t .Error (err )
754
+ }
755
+ if tt .relevantLog && strings .Contains (debugLog .String (), "Transaction status not marked for audit logging" ) {
756
+ t .Errorf ("unexpected debug log: %q. Transaction status should be marked for audit logging" , debugLog .String ())
757
+ }
758
+ if ! tt .relevantLog && ! strings .Contains (debugLog .String (), "Transaction status not marked for audit logging" ) {
759
+ t .Errorf ("missing debug log. Transaction status should be not marked for audit logging not being relevant" )
760
+ }
761
+ })
721
762
}
722
763
}
723
764
0 commit comments