MCPcopy Create free account
hub / github.com/astercloud/aster / TestInMemoryAuditLog_PurgeEvents

Function TestInMemoryAuditLog_PurgeEvents

pkg/security/audit_test.go:219–263  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

217}
218
219func TestInMemoryAuditLog_PurgeEvents(t *testing.T) {
220 auditLog := NewInMemoryAuditLog(nil)
221 defer func() { _ = auditLog.Close() }()
222
223 now := time.Now()
224 yesterday := now.Add(-24 * time.Hour)
225
226 // 添加旧事件和新事件
227 _ = auditLog.LogEvent(AuditEvent{
228 Type: AuditTypeUserLogin,
229 UserID: "user1",
230 Message: "Old login",
231 Timestamp: yesterday.Add(-1 * time.Hour),
232 })
233 _ = auditLog.LogEvent(AuditEvent{
234 Type: AuditTypeUserLogin,
235 UserID: "user2",
236 Message: "Recent login",
237 Timestamp: now,
238 })
239
240 // 清除昨天之前的事件
241 count, err := auditLog.PurgeEvents(context.Background(), yesterday)
242 if err != nil {
243 t.Fatalf("PurgeEvents failed: %v", err)
244 }
245
246 if count != 1 {
247 t.Errorf("expected 1 event purged, got %d", count)
248 }
249
250 // 使用GetStatus验证剩余事件数
251 status := auditLog.GetStatus()
252 if status.TotalEvents != 1 {
253 t.Errorf("expected 1 event remaining, got %d", status.TotalEvents)
254 }
255
256 // 使用GetEventsByUser验证user2的事件还在
257 events, _ := auditLog.GetEventsByUser("user2", 10)
258 if len(events) == 0 {
259 t.Error("expected user2 event to remain")
260 } else if events[0].UserID != "user2" {
261 t.Errorf("expected user2 to remain, got %s", events[0].UserID)
262 }
263}
264
265func TestInMemoryAuditLog_GetStatus(t *testing.T) {
266 auditLog := NewInMemoryAuditLog(nil)

Callers

nothing calls this directly

Calls 8

CloseMethod · 0.95
LogEventMethod · 0.95
PurgeEventsMethod · 0.95
GetStatusMethod · 0.95
GetEventsByUserMethod · 0.95
NewInMemoryAuditLogFunction · 0.85
AddMethod · 0.65
ErrorMethod · 0.65

Tested by

no test coverage detected