Files
CollabTable/shared/backend/auth/store_integration_test.go
T

104 lines
3.4 KiB
Go
Raw Normal View History

2026-09-01 21:51:15 +02:00
package auth
import (
"context"
"os"
"testing"
"time"
"repos.gfranz.ch/Gabriel20xx/shared/backend/postgres"
)
// TestStoreIntegration exercises Store against a real PostgreSQL database,
// covering exactly what the fake stores unit tests (in each consumer's own
// httpapi package) use instead cannot: a mismatch between a query's column
// list and Scan's destination count only surfaces against a real driver
// round-trip. Skipped unless DATABASE_URL points at a scratch database -
// never run by default in CI; point it at a local/throwaway database to run
// it, e.g.:
//
// DATABASE_URL=postgres://user:pass@localhost:5432/scratch?sslmode=disable go test ./auth/... -run TestStoreIntegration
func TestStoreIntegration(t *testing.T) {
url := os.Getenv("DATABASE_URL")
if url == "" {
t.Skip("DATABASE_URL not set; skipping Postgres integration test")
}
ctx := context.Background()
pool, err := postgres.Open(ctx, postgres.DefaultConfig(url))
if err != nil {
t.Fatalf("open pool: %v", err)
}
// Registered before the DeleteAllUsers cleanup below so it runs after
// that one (t.Cleanup runs in LIFO order).
t.Cleanup(pool.Close)
if err := EnsureSchema(ctx, pool, SchemaOptions{SingleUser: false, APITokens: true}); err != nil {
t.Fatalf("EnsureSchema() error = %v", err)
}
store := NewStore(pool)
if err := store.DeleteAllUsers(ctx); err != nil {
t.Fatalf("DeleteAllUsers() (pre-test cleanup) error = %v", err)
}
t.Cleanup(func() {
if err := store.DeleteAllUsers(context.Background()); err != nil {
t.Logf("cleanup: DeleteAllUsers() error = %v", err)
}
})
hash, err := HashPassword("correct horse battery staple")
if err != nil {
t.Fatalf("HashPassword() error = %v", err)
}
// The very first account is promoted to superadmin; this also exercises
// CreateUser's own Scan (7 RETURNING columns), separately from find's.
created, err := store.CreateUser(ctx, "root", "root@example.com", hash)
if err != nil {
t.Fatalf("CreateUser() error = %v", err)
}
if created.Role != RoleSuperAdmin {
t.Fatalf("first CreateUser() role = %q, want %q", created.Role, RoleSuperAdmin)
}
byIdentifier, err := store.FindByIdentifier(ctx, "root")
if err != nil {
t.Fatalf("FindByIdentifier() error = %v", err)
}
if byIdentifier == nil || byIdentifier.ID != created.ID || byIdentifier.Role != RoleSuperAdmin {
t.Fatalf("FindByIdentifier() = %+v, want a superadmin match for %+v", byIdentifier, created)
}
byID, err := store.FindByID(ctx, created.ID)
if err != nil {
t.Fatalf("FindByID() error = %v", err)
}
if byID == nil || byID.Username != "root" || byID.Role != RoleSuperAdmin {
t.Fatalf("FindByID() = %+v, want username %q and superadmin role", byID, "root")
}
_, hashedToken, err := NewSessionToken()
if err != nil {
t.Fatalf("NewSessionToken() error = %v", err)
}
if err := store.CreateSession(ctx, created.ID, hashedToken, time.Now().Add(time.Hour)); err != nil {
t.Fatalf("CreateSession() error = %v", err)
}
bySession, err := store.FindSessionUser(ctx, hashedToken)
if err != nil {
t.Fatalf("FindSessionUser() error = %v", err)
}
if bySession == nil || bySession.ID != created.ID {
t.Fatalf("FindSessionUser() = %+v, want a match for %+v", bySession, created)
}
users, err := store.ListUsers(ctx)
if err != nil {
t.Fatalf("ListUsers() error = %v", err)
}
if len(users) != 1 || users[0].ID != created.ID {
t.Fatalf("ListUsers() = %+v, want exactly the created user", users)
}
}