Skip to content

Commit c5d726a

Browse files
f0sselclaude
andcommitted
Refactor audit tests to use buffer-based logging and add constant for common reason
- Replace test logger that discarded output with buffer-based approach for proper verification - Add ReasonNoMatchingRules constant to avoid string duplication - Enhance tests to verify log levels and content fields - Tests now actually validate that audit logging works correctly 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent cdb7563 commit c5d726a

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

audit/audit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"net/http"
66
)
77

8+
const (
9+
ReasonNoMatchingRules = "no matching allow rules"
10+
)
11+
812
// Request represents information about an HTTP request for auditing
913
type Request struct {
1014
Method string

audit/audit_test.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
11
package audit
22

33
import (
4+
"bytes"
45
"log/slog"
56
"net/http"
7+
"strings"
68
"testing"
79
)
810

911
func TestLoggingAuditor(t *testing.T) {
10-
// Create a logger that discards output during tests
11-
logger := slog.New(slog.NewTextHandler(nil, &slog.HandlerOptions{
12-
Level: slog.LevelError + 1, // Higher than any level to suppress all logs
13-
}))
14-
15-
auditor := NewLoggingAuditor(logger)
16-
1712
tests := []struct {
18-
name string
19-
request *Request
13+
name string
14+
request *Request
15+
expectedLevel string
16+
expectedFields []string
2017
}{
2118
{
2219
name: "allow request",
@@ -26,22 +23,46 @@ func TestLoggingAuditor(t *testing.T) {
2623
Allowed: true,
2724
Rule: "allow github.com",
2825
},
26+
expectedLevel: "INFO",
27+
expectedFields: []string{"ALLOW", "GET", "https://github.com", "allow github.com"},
2928
},
3029
{
3130
name: "deny request",
3231
request: &Request{
3332
Method: "POST",
3433
URL: "https://example.com",
3534
Allowed: false,
36-
Reason: "no matching allow rules",
35+
Reason: ReasonNoMatchingRules,
3736
},
37+
expectedLevel: "WARN",
38+
expectedFields: []string{"DENY", "POST", "https://example.com", ReasonNoMatchingRules},
3839
},
3940
}
4041

4142
for _, tt := range tests {
4243
t.Run(tt.name, func(t *testing.T) {
43-
// Should not panic
44+
var buf bytes.Buffer
45+
logger := slog.New(slog.NewTextHandler(&buf, &slog.HandlerOptions{
46+
Level: slog.LevelDebug,
47+
}))
48+
49+
auditor := NewLoggingAuditor(logger)
4450
auditor.AuditRequest(tt.request)
51+
52+
logOutput := buf.String()
53+
if logOutput == "" {
54+
t.Fatalf("expected log output, got empty string")
55+
}
56+
57+
if !strings.Contains(logOutput, tt.expectedLevel) {
58+
t.Errorf("expected log level %s, got: %s", tt.expectedLevel, logOutput)
59+
}
60+
61+
for _, field := range tt.expectedFields {
62+
if !strings.Contains(logOutput, field) {
63+
t.Errorf("expected log to contain %q, got: %s", field, logOutput)
64+
}
65+
}
4566
})
4667
}
4768
}

0 commit comments

Comments
 (0)