test(improve): use dedicated test domain and improve cleanup

- Change test email domain from @example.com to @tests.bookhoard.internal
- This prevents accidental deletion of real user data when self-hosters run tests
- Improve test cleanup: delete ALL users with test domains before each test
- Ensures complete test isolation by cleaning up users from previous tests
- Handles edge cases where tests promote users to admin or modify accounts

The @tests.bookhoard.internal domain is clearly for testing only and
won't conflict with real user emails.
This commit is contained in:
2026-02-22 11:20:15 -05:00
parent 334af53749
commit cbf80037c4
+17 -14
View File
@@ -212,13 +212,13 @@ func setupDeviceTest(t *testing.T) *TestDeviceSetup {
// createTestUserOnce returns the pre-created test user info
func createTestUserOnce(t *testing.T, db *database.Queries) UserTestData {
ctx := context.Background()
user, err := db.GetUserByEmail(ctx, "testuser@example.com")
user, err := db.GetUserByEmail(ctx, "testuser@tests.bookhoard.internal")
require.NoError(t, err, "Test user should exist (created by setupTestServer)")
userUUID, err := uuid.FromBytes(user.ID.Bytes[0:16])
require.NoError(t, err, "Should parse user UUID")
return UserTestData{
ID: userUUID,
Email: "testuser@example.com",
Email: "testuser@tests.bookhoard.internal",
Username: "testuser",
Password: "Test@Pass123!",
}
@@ -228,7 +228,7 @@ func createTestUserOnce(t *testing.T, db *database.Queries) UserTestData {
func createRegularUserOnce(t *testing.T, db *database.Queries) UserTestData {
ctx := context.Background()
uniqueID := uuid.New().String()[:8]
email := fmt.Sprintf("regularuser-%s@example.com", uniqueID)
email := fmt.Sprintf("regularuser-%s@tests.bookhoard.internal", uniqueID)
username := fmt.Sprintf("regularuser-%s", uniqueID)
passwordHash := "$2a$10$JjAtK7PPa1WexQC3AUGe8OXLeuseZ/haN1Mz7emMo6CfOvMiTVXWq"
@@ -518,18 +518,21 @@ func setupTestServer(t *testing.T) *TestServerSetup {
ctx := context.Background()
// Delete existing test users if they exist (cascades to delete collections, media items, etc.)
for _, email := range []string{"testuser@example.com", "testregularuser@example.com"} {
existingUser, err := queries.GetUserByEmail(ctx, email)
if err == nil {
queries.DeleteUser(ctx, existingUser.ID)
// Delete ALL test users (any user with test email domains) to ensure clean state
// This handles users created during tests that may have been promoted to admin, etc.
allUsers, err := queries.ListUsers(ctx)
if err == nil {
for _, user := range allUsers {
if strings.HasSuffix(user.Email, "@example.com") || strings.HasSuffix(user.Email, "@tests.bookhoard.internal") {
queries.DeleteUser(ctx, user.ID)
}
}
}
// Create fresh admin test user
passwordHash := "$2a$10$JjAtK7PPa1WexQC3AUGe8OXLeuseZ/haN1Mz7emMo6CfOvMiTVXWq"
adminUser, err := queries.CreateUser(ctx, database.CreateUserParams{
Email: "testuser@example.com",
Email: "testuser@tests.bookhoard.internal",
Username: "testuser",
PasswordHash: passwordHash,
FirstName: pgtype.Text{String: "Test", Valid: true},
@@ -544,7 +547,7 @@ func setupTestServer(t *testing.T) *TestServerSetup {
// Create fresh regular test user
regularUser, err := queries.CreateUser(ctx, database.CreateUserParams{
Email: "testregularuser@example.com",
Email: "testregularuser@tests.bookhoard.internal",
Username: "testregularuser",
PasswordHash: passwordHash,
FirstName: pgtype.Text{String: "Regular", Valid: true},
@@ -558,8 +561,8 @@ func setupTestServer(t *testing.T) *TestServerSetup {
createDefaultCollectionsForUser(t, queries, pgtype.UUID{Bytes: regularUUID, Valid: true})
// Login to get tokens
adminToken := loginWithCredentials(t, ts, "testuser@example.com", "Test@Pass123!")
regularToken := loginWithCredentials(t, ts, "testregularuser@example.com", "Test@Pass123!")
adminToken := loginWithCredentials(t, ts, "testuser@tests.bookhoard.internal", "Test@Pass123!")
regularToken := loginWithCredentials(t, ts, "testregularuser@tests.bookhoard.internal", "Test@Pass123!")
// Create TestServerSetup struct with all resources
setup := &TestServerSetup{
@@ -615,7 +618,7 @@ func loginWithCredentials(t *testing.T, ts *httptest.Server, email, password str
func getTestUserID(t *testing.T, db *database.Queries) uuid.UUID {
ctx := context.Background()
user, err := db.GetUserByEmail(ctx, "testuser@example.com")
user, err := db.GetUserByEmail(ctx, "testuser@tests.bookhoard.internal")
require.NoError(t, err, "Test user should exist")
userUUID, err := uuid.FromBytes(user.ID.Bytes[:])
require.NoError(t, err, "Failed to parse user UUID")
@@ -624,7 +627,7 @@ func getTestUserID(t *testing.T, db *database.Queries) uuid.UUID {
func getRegularUserID(t *testing.T, db *database.Queries) uuid.UUID {
ctx := context.Background()
user, err := db.GetUserByEmail(ctx, "testregularuser@example.com")
user, err := db.GetUserByEmail(ctx, "testregularuser@tests.bookhoard.internal")
require.NoError(t, err, "Regular user should exist")
userUUID, err := uuid.FromBytes(user.ID.Bytes[:])
require.NoError(t, err, "Failed to parse user UUID")