64 lines
1.5 KiB
Go
64 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAuditStoreAppendAndRecent(t *testing.T) {
|
|
store, err := newAuditStore(filepath.Join(t.TempDir(), "audit.log"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.append(auditEvent{Actor: "admin", Action: "login", Detail: "successful login"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.append(auditEvent{Actor: "admin", Action: "user_update", TargetID: "abc"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
events, err := store.recent(10)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(events) != 2 {
|
|
t.Fatalf("events = %+v", events)
|
|
}
|
|
if events[0].Action != "user_update" || events[1].Action != "login" {
|
|
t.Fatalf("unexpected ordering: %+v", events)
|
|
}
|
|
if events[0].Timestamp.IsZero() || events[1].Timestamp.IsZero() {
|
|
t.Fatal("expected timestamps to be populated")
|
|
}
|
|
}
|
|
|
|
func TestActionLabel(t *testing.T) {
|
|
if got := actionLabel("camera_view"); got != "camera_view" {
|
|
t.Fatalf("actionLabel = %q", got)
|
|
}
|
|
}
|
|
|
|
func TestAuditStoreRecentEmpty(t *testing.T) {
|
|
store, err := newAuditStore(filepath.Join(t.TempDir(), "audit.log"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
events, err := store.recent(5)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(events) != 0 {
|
|
t.Fatalf("expected no events, got %+v", events)
|
|
}
|
|
}
|
|
|
|
func TestAuditStoreAppendWithoutPath(t *testing.T) {
|
|
store, err := newAuditStore("")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := store.append(auditEvent{Timestamp: time.Now()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|