|
| 1 | +package fetch |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestAppendFetchAudit_SingleEntry(t *testing.T) { |
| 17 | + logPath := filepath.Join(t.TempDir(), "audit.jsonl") |
| 18 | + |
| 19 | + entry := FetchAuditEntry{ |
| 20 | + TraceID: "trace-001", |
| 21 | + FetchTime: time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC), |
| 22 | + URL: "https://example.com/resource", |
| 23 | + SHA256: "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", |
| 24 | + FetchType: "http_get", |
| 25 | + AllowedBy: "allowlist-rule-1", |
| 26 | + CacheHit: false, |
| 27 | + } |
| 28 | + |
| 29 | + err := AppendFetchAudit(logPath, entry) |
| 30 | + require.NoError(t, err) |
| 31 | + |
| 32 | + data, err := os.ReadFile(logPath) |
| 33 | + require.NoError(t, err) |
| 34 | + |
| 35 | + var got FetchAuditEntry |
| 36 | + err = json.Unmarshal([]byte(strings.TrimSpace(string(data))), &got) |
| 37 | + require.NoError(t, err) |
| 38 | + |
| 39 | + assert.Equal(t, "trace-001", got.TraceID) |
| 40 | + assert.Equal(t, time.Date(2025, 1, 15, 10, 30, 0, 0, time.UTC), got.FetchTime) |
| 41 | + assert.Equal(t, "https://example.com/resource", got.URL) |
| 42 | + assert.Equal(t, "abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890", got.SHA256) |
| 43 | + assert.Equal(t, "http_get", got.FetchType) |
| 44 | + assert.Equal(t, "allowlist-rule-1", got.AllowedBy) |
| 45 | + assert.False(t, got.CacheHit) |
| 46 | +} |
| 47 | + |
| 48 | +func TestAppendFetchAudit_MultipleEntries(t *testing.T) { |
| 49 | + logPath := filepath.Join(t.TempDir(), "audit.jsonl") |
| 50 | + |
| 51 | + for i := range 3 { |
| 52 | + entry := FetchAuditEntry{ |
| 53 | + TraceID: fmt.Sprintf("trace-%03d", i), |
| 54 | + FetchTime: time.Now().UTC(), |
| 55 | + URL: fmt.Sprintf("https://example.com/resource/%d", i), |
| 56 | + SHA256: "deadbeef", |
| 57 | + FetchType: "http_get", |
| 58 | + AllowedBy: "allowlist", |
| 59 | + CacheHit: i > 0, |
| 60 | + } |
| 61 | + err := AppendFetchAudit(logPath, entry) |
| 62 | + require.NoError(t, err) |
| 63 | + } |
| 64 | + |
| 65 | + data, err := os.ReadFile(logPath) |
| 66 | + require.NoError(t, err) |
| 67 | + |
| 68 | + lines := strings.Split(strings.TrimSpace(string(data)), "\n") |
| 69 | + assert.Len(t, lines, 3) |
| 70 | + |
| 71 | + for _, line := range lines { |
| 72 | + var got FetchAuditEntry |
| 73 | + err := json.Unmarshal([]byte(line), &got) |
| 74 | + assert.NoError(t, err) |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +func TestAppendFetchAudit_CreatesParentDirs(t *testing.T) { |
| 79 | + logPath := filepath.Join(t.TempDir(), "a", "b", "c", "audit.jsonl") |
| 80 | + |
| 81 | + entry := FetchAuditEntry{ |
| 82 | + TraceID: "trace-nested", |
| 83 | + FetchTime: time.Now().UTC(), |
| 84 | + URL: "https://example.com/nested", |
| 85 | + SHA256: "cafebabe", |
| 86 | + FetchType: "http_get", |
| 87 | + AllowedBy: "allowlist", |
| 88 | + CacheHit: true, |
| 89 | + } |
| 90 | + |
| 91 | + err := AppendFetchAudit(logPath, entry) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + _, err = os.Stat(logPath) |
| 95 | + assert.NoError(t, err) |
| 96 | +} |
| 97 | + |
| 98 | +func TestAppendFetchAudit_JSONFields(t *testing.T) { |
| 99 | + entry := FetchAuditEntry{ |
| 100 | + TraceID: "trace-fields", |
| 101 | + FetchTime: time.Now().UTC(), |
| 102 | + URL: "https://example.com/fields", |
| 103 | + SHA256: "fieldhash", |
| 104 | + FetchType: "http_get", |
| 105 | + AllowedBy: "allowlist", |
| 106 | + CacheHit: false, |
| 107 | + } |
| 108 | + |
| 109 | + data, err := json.Marshal(entry) |
| 110 | + require.NoError(t, err) |
| 111 | + |
| 112 | + var m map[string]interface{} |
| 113 | + err = json.Unmarshal(data, &m) |
| 114 | + require.NoError(t, err) |
| 115 | + |
| 116 | + expectedKeys := []string{"trace_id", "fetch_time", "url", "sha256", "fetch_type", "allowed_by", "cache_hit"} |
| 117 | + for _, key := range expectedKeys { |
| 118 | + assert.Contains(t, m, key) |
| 119 | + } |
| 120 | + assert.Len(t, m, len(expectedKeys)) |
| 121 | +} |
0 commit comments