Some checks failed
Go / build (.exe, 386, windows, windows-386) (push) Has been cancelled
Go / build (.exe, amd64, windows, windows-amd64) (push) Has been cancelled
Go / build (.exe, arm64, windows, windows-arm64) (push) Has been cancelled
Go / build (386, freebsd, freebsd-386) (push) Has been cancelled
Go / build (386, linux, linux-386) (push) Has been cancelled
Go / build (386, netbsd, netbsd-386) (push) Has been cancelled
Go / build (386, openbsd, openbsd-386) (push) Has been cancelled
Go / build (386, plan9, plan9-386) (push) Has been cancelled
Go / build (amd64, darwin, darwin-amd64) (push) Has been cancelled
Go / build (amd64, dragonfly, dragonfly-amd64) (push) Has been cancelled
Go / build (amd64, freebsd, freebsd-amd64) (push) Has been cancelled
Go / build (amd64, illumos, illumos-amd64) (push) Has been cancelled
Go / build (amd64, linux, linux-amd64) (push) Has been cancelled
Go / build (amd64, netbsd, netbsd-amd64) (push) Has been cancelled
Go / build (amd64, openbsd, openbsd-amd64) (push) Has been cancelled
Go / build (amd64, plan9, plan9-amd64) (push) Has been cancelled
Go / build (amd64, solaris, solaris-amd64) (push) Has been cancelled
Go / build (arm, 6, linux, linux-armv6) (push) Has been cancelled
Go / build (arm, 7, linux, linux-armv7) (push) Has been cancelled
Go / build (arm, freebsd, freebsd-arm) (push) Has been cancelled
Go / build (arm, netbsd, netbsd-arm) (push) Has been cancelled
Go / build (arm, openbsd, openbsd-arm) (push) Has been cancelled
Go / build (arm, plan9, plan9-arm) (push) Has been cancelled
Go / build (arm64, darwin, darwin-arm64) (push) Has been cancelled
Go / build (arm64, freebsd, freebsd-arm64) (push) Has been cancelled
Go / build (arm64, linux, linux-arm64) (push) Has been cancelled
Go / build (arm64, netbsd, netbsd-arm64) (push) Has been cancelled
Go / build (arm64, openbsd, openbsd-arm64) (push) Has been cancelled
Go / build (loong64, linux, linux-loong64) (push) Has been cancelled
Go / build (mips, linux, linux-mips) (push) Has been cancelled
Go / build (mips64, linux, linux-mips64) (push) Has been cancelled
Go / build (mips64le, linux, linux-mips64le) (push) Has been cancelled
Go / build (mipsle, linux, linux-mipsle) (push) Has been cancelled
Go / build (ppc64, aix, aix-ppc64) (push) Has been cancelled
Go / build (ppc64, linux, linux-ppc64) (push) Has been cancelled
Go / build (ppc64, openbsd, openbsd-ppc64) (push) Has been cancelled
Go / build (ppc64le, linux, linux-ppc64le) (push) Has been cancelled
Go / build (riscv64, freebsd, freebsd-riscv64) (push) Has been cancelled
Go / build (riscv64, linux, linux-riscv64) (push) Has been cancelled
Go / build (riscv64, openbsd, openbsd-riscv64) (push) Has been cancelled
Go / build (s390x, linux, linux-s390x) (push) Has been cancelled
Docker Image / docker (push) Has been cancelled
Go / merge-artifacts (push) Has been cancelled
67 lines
2.0 KiB
Go
67 lines
2.0 KiB
Go
// internal/adminaudit/audit_test.go 包含用于约束 audit 行为的测试。
|
|
|
|
package adminaudit
|
|
|
|
import (
|
|
"context"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/tursom/mc-gateway/internal/admindb"
|
|
)
|
|
|
|
func TestRepositoryRecordAndList(t *testing.T) {
|
|
db, err := admindb.Open(filepath.Join(t.TempDir(), "gateway.sqlite3"))
|
|
if err != nil {
|
|
t.Fatalf("Open() error = %v", err)
|
|
}
|
|
defer db.Close()
|
|
if err := admindb.Migrate(db); err != nil {
|
|
t.Fatalf("Migrate() error = %v", err)
|
|
}
|
|
|
|
now := time.Unix(123, 0)
|
|
repo := NewRepositoryWithClock(db, func() time.Time { return now })
|
|
|
|
if err := repo.Record(context.Background(), "admin", "127.0.0.1", "route_upsert", "route", "play.example", true, "route saved"); err != nil {
|
|
t.Fatalf("Record(success) error = %v", err)
|
|
}
|
|
now = time.Unix(124, 0)
|
|
if err := repo.Record(context.Background(), "admin", "127.0.0.1", "route_delete", "route", "play.example", false, "route missing"); err != nil {
|
|
t.Fatalf("Record(failure) error = %v", err)
|
|
}
|
|
|
|
logs, err := repo.List(context.Background(), DefaultListLimit)
|
|
if err != nil {
|
|
t.Fatalf("List() error = %v", err)
|
|
}
|
|
if len(logs) != 2 {
|
|
t.Fatalf("List() len = %d, want 2", len(logs))
|
|
}
|
|
if logs[0].Action != "route_delete" || logs[0].Success {
|
|
t.Fatalf("logs[0] = %+v, want latest failure", logs[0])
|
|
}
|
|
if logs[0].CreatedAt != 124 {
|
|
t.Fatalf("logs[0].CreatedAt = %d, want 124", logs[0].CreatedAt)
|
|
}
|
|
if logs[1].Action != "route_upsert" || !logs[1].Success {
|
|
t.Fatalf("logs[1] = %+v, want earlier success", logs[1])
|
|
}
|
|
|
|
limited, err := repo.List(context.Background(), 1)
|
|
if err != nil {
|
|
t.Fatalf("List(limit) error = %v", err)
|
|
}
|
|
if len(limited) != 1 || limited[0].Action != "route_delete" {
|
|
t.Fatalf("List(limit) = %+v", limited)
|
|
}
|
|
}
|
|
|
|
func TestRepositoryRecordIgnoresNilDB(t *testing.T) {
|
|
repo := NewRepository(nil)
|
|
if err := repo.Record(context.Background(), "admin", "127.0.0.1", "login", "user", "admin", true, "ok"); err != nil {
|
|
t.Fatalf("Record(nil DB) error = %v", err)
|
|
}
|
|
}
|