fix: improve test infrastructure and add ID validation

- Add TestMain to set test env vars and testdb.TruncateTables for test
  isolation
- Add chi routing context to test helpers for path parameter extraction
- Fix SQL error handling to use errors.Is() instead of ==
- Add validators package with ID validation
- Fix admin test middleware chain (RequireAdmin wrapper)
- Update test user inserts to include phone and date_of_birth fields
- Update service delete test to check soft-delete (is_active=false)
- Update holiday hours test to use new schema (weekday, is_open)
- Add phone number validation tests for UK mobile numbers
This commit is contained in:
2026-02-23 00:59:32 +00:00
parent 355e8a26c1
commit df3439bd70
30 changed files with 1081 additions and 360 deletions
+18 -5
View File
@@ -11,12 +11,19 @@ import (
"golang.org/x/crypto/bcrypt"
)
// Global counter for unique emails in tests
var testEmailCounter int64
func CreateTestAdminUser(pool *pgxpool.Pool) (string, error) {
return createTestUser(pool, "Admin", "User", "admin@test.com", "admin")
return createTestUser(pool, "Admin", "User", "", "admin")
}
func CreateTestUser(pool *pgxpool.Pool) (string, error) {
return createTestUser(pool, "Test", "User", "user@test.com", "verified_email")
return createTestUser(pool, "Test", "User", "", "verified_email")
}
func CreateTestUserWithEmail(pool *pgxpool.Pool, email, role string) (string, error) {
return createTestUser(pool, "Test", "User", email, role)
}
func createTestUser(pool *pgxpool.Pool, firstName, lastName, email, role string) (string, error) {
@@ -25,13 +32,19 @@ func createTestUser(pool *pgxpool.Pool, firstName, lastName, email, role string)
return "", fmt.Errorf("failed to hash password: %w", err)
}
// Generate unique email if not provided
if email == "" {
testEmailCounter++
email = fmt.Sprintf("%s.%s.%d@test.com", firstName, lastName, testEmailCounter)
}
ctx := context.Background()
var userID string
err = pool.QueryRow(ctx, `
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
VALUES ($1, $2, $3, $4, $5, 'email')
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
VALUES ($1, $2, $3, $4, $5, $6, $7, 'email')
RETURNING id
`, firstName, lastName, email, string(passwordHash), role).Scan(&userID)
`, firstName, lastName, email, "+447123456789", "1990-01-01", string(passwordHash), role).Scan(&userID)
if err != nil {
return "", fmt.Errorf("failed to create user: %w", err)