diff --git a/cmd/server/tests/test_helpers_test.go b/cmd/server/tests/test_helpers_test.go index d7a86ab..9a4b593 100644 --- a/cmd/server/tests/test_helpers_test.go +++ b/cmd/server/tests/test_helpers_test.go @@ -544,6 +544,10 @@ func setupTestServer(t *testing.T) *TestServerSetup { AnnotationService: annotationService, DeviceAuthMiddleware: deviceAuthMiddleware, LoginTracker: loginAttemptTracker, + // RegisterRoutes dereferences Settings for the auth rate limit; + // the main binary wires database.NewSettingsRegistry(queries) — + // the test harness must too or route registration nil-panics. + Settings: database.NewSettingsRegistry(queries), } router.RegisterRoutes(routerConfig) @@ -588,21 +592,31 @@ func setupTestServer(t *testing.T) *TestServerSetup { } } - // Create fresh admin test user + // The admin test user doubles as the shared dev admin and is + // deliberately PRESERVED by the cleanup above — reuse it when it + // already exists instead of re-creating (a blind INSERT hits + // users_email_key on every run after the first). passwordHash := "$2a$10$JjAtK7PPa1WexQC3AUGe8OXLeuseZ/haN1Mz7emMo6CfOvMiTVXWq" - adminUser, err := queries.CreateUser(ctx, database.CreateUserParams{ - Email: "testuser@tests.bookhoard.internal", - Username: "testuser", - PasswordHash: passwordHash, - FirstName: pgtype.Text{String: "Test", Valid: true}, - LastName: pgtype.Text{String: "User", Valid: true}, - Role: "admin", - }) - require.NoError(t, err, "Failed to create admin test user") - - adminUUID, err := uuid.FromBytes(adminUser.ID.Bytes[:]) - require.NoError(t, err, "Failed to parse admin user UUID") - createDefaultCollectionsForUser(t, queries, pgtype.UUID{Bytes: adminUUID, Valid: true}) + var adminUUID uuid.UUID + if existing, gerr := queries.GetUserByEmail(ctx, "testuser@tests.bookhoard.internal"); gerr == nil { + adminUUID, err = uuid.FromBytes(existing.ID.Bytes[:]) + require.NoError(t, err, "Failed to parse admin user UUID") + } else { + adminUser, cerr := queries.CreateUser(ctx, database.CreateUserParams{ + Email: "testuser@tests.bookhoard.internal", + Username: "testuser", + PasswordHash: passwordHash, + FirstName: pgtype.Text{String: "Test", Valid: true}, + LastName: pgtype.Text{String: "User", Valid: true}, + Role: "admin", + }) + require.NoError(t, cerr, "Failed to create admin test user") + adminUUID, err = uuid.FromBytes(adminUser.ID.Bytes[:]) + require.NoError(t, err, "Failed to parse admin user UUID") + // Default collections are created exactly once per user; the + // preserved dev admin already has its set from the first run. + createDefaultCollectionsForUser(t, queries, pgtype.UUID{Bytes: adminUUID, Valid: true}) + } // Create fresh regular test user regularUser, err := queries.CreateUser(ctx, database.CreateUserParams{