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:
@@ -20,8 +20,8 @@ func TestAdminServices_Create(t *testing.T) {
|
||||
|
||||
// Create admin user in DB first
|
||||
_, err := db.DB.Exec(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Admin', 'User', 'admin@test.com', 'hash', 'admin', 'email')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Admin', 'User', 'admin@test.com', '+447123456789', '1990-01-01', 'hash', 'admin', 'email')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create admin user: %v", err)
|
||||
@@ -179,14 +179,14 @@ func TestAdminServices_Delete(t *testing.T) {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify service is deleted
|
||||
var count int
|
||||
err = db.DB.QueryRow(context.Background(), "SELECT COUNT(*) FROM services WHERE id = $1", serviceID).Scan(&count)
|
||||
// Verify service is soft deleted (is_active = false)
|
||||
var isActive bool
|
||||
err = db.DB.QueryRow(context.Background(), "SELECT is_active FROM services WHERE id = $1", serviceID).Scan(&isActive)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to check service: %v", err)
|
||||
}
|
||||
if count != 0 {
|
||||
t.Error("expected service to be deleted")
|
||||
if isActive {
|
||||
t.Error("expected service to be soft deleted (is_active = false)")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -196,8 +196,8 @@ func TestAdminServices_NonAdmin(t *testing.T) {
|
||||
|
||||
// Create regular user in DB
|
||||
_, err := db.DB.Exec(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Regular', 'User', 'user@test.com', 'hash', 'verified_email', 'email')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Regular', 'User', 'user@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user