Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
56 lines
2.0 KiB
Go
56 lines
2.0 KiB
Go
package store
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestAuditEventsAreKeptAndFound(t *testing.T) {
|
|
db := capacityStore(t)
|
|
ctx := t.Context()
|
|
now := time.Now()
|
|
for i, e := range []AuditEvent{
|
|
{At: now.Add(-2 * time.Hour), Actor: "ilker", Source: "api", Action: "POST /api/v1/models", Target: "m1", Status: 200},
|
|
{At: now.Add(-time.Hour), Actor: "ann@example.com", Source: "web", Action: "POST /admin/users/u1/suspend", Target: "bob@example.com", Status: 200},
|
|
{At: now, Actor: "ilker", Source: "api", Action: "DELETE /api/v1/nodes/n1", Status: 409},
|
|
} {
|
|
if err := db.WriteAudit(ctx, e); err != nil {
|
|
t.Fatal(i, err)
|
|
}
|
|
}
|
|
all, err := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-24 * time.Hour)})
|
|
if err != nil || len(all) != 3 || all[0].Action != "DELETE /api/v1/nodes/n1" {
|
|
t.Fatalf("%+v %v", all, err)
|
|
}
|
|
if got, _ := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-24 * time.Hour), Actor: "ilker"}); len(got) != 2 {
|
|
t.Errorf("by actor: %d", len(got))
|
|
}
|
|
if got, _ := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-24 * time.Hour), Contains: "BOB@"}); len(got) != 1 {
|
|
t.Errorf("by target: %d", len(got))
|
|
}
|
|
if got, _ := db.AuditEvents(ctx, AuditFilter{Since: now.Add(-90 * time.Minute)}); len(got) != 2 {
|
|
t.Errorf("since: %d", len(got))
|
|
}
|
|
}
|
|
|
|
func TestAStaffRoleIsGivenAndTakenAway(t *testing.T) {
|
|
db := capacityStore(t)
|
|
ctx := t.Context()
|
|
if err := db.CreateUser(ctx, User{ID: "u1", Email: "v@example.com", Name: "v", PasswordHash: "x",
|
|
State: UserActive, CreatedAt: time.Now()}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, role := range []string{RoleViewer, RoleOperator, RoleBilling, RoleAdmin, ""} {
|
|
if err := db.SetStaffRole(ctx, "u1", role); err != nil {
|
|
t.Fatal(role, err)
|
|
}
|
|
u, err := db.UserByID(ctx, "u1")
|
|
if err != nil || u.StaffRole() != role || u.Staff() != (role != "") || u.IsAdmin != (role == RoleAdmin) {
|
|
t.Errorf("%q: %+v %v", role, u, err)
|
|
}
|
|
}
|
|
if db.SetStaffRole(ctx, "u1", "god") == nil {
|
|
t.Error("an unknown role was taken")
|
|
}
|
|
}
|