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,11 +20,11 @@ func TestAdminUsers_List(t *testing.T) {
|
||||
|
||||
// Create test users
|
||||
_, err := db.DB.Exec(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES
|
||||
('Alice', 'Smith', 'alice@test.com', 'hash1', 'admin', 'standard'),
|
||||
('Bob', 'Jones', 'bob@test.com', 'hash2', 'verified_email', 'standard'),
|
||||
('Charlie', 'Brown', 'charlie@test.com', 'hash3', 'verified_email', 'vip')
|
||||
('Alice', 'Smith', 'alice@test.com', '+447123456789', '1990-01-01', 'hash1', 'admin', 'email'),
|
||||
('Bob', 'Jones', 'bob@test.com', '+447123456789', '1990-01-01', 'hash2', 'verified_email', 'email'),
|
||||
('Charlie', 'Brown', 'charlie@test.com', '+447123456789', '1990-01-01', 'hash3', 'verified_email', 'email')
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create users: %v", err)
|
||||
@@ -58,8 +58,8 @@ func TestAdminUsers_Get(t *testing.T) {
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', 'hash', 'verified_email', 'vip')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
@@ -82,8 +82,8 @@ func TestAdminUsers_Get(t *testing.T) {
|
||||
t.Errorf("expected user ID %s, got %s", userID, response.ID)
|
||||
}
|
||||
|
||||
if response.AccountType != "vip" {
|
||||
t.Errorf("expected account type 'vip', got %s", response.AccountType)
|
||||
if response.AccountType != "email" {
|
||||
t.Errorf("expected account type 'email', got %s", response.AccountType)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,7 +92,8 @@ func TestAdminUsers_Get_NotFound(t *testing.T) {
|
||||
defer cleanup()
|
||||
|
||||
handler := http.HandlerFunc(user.GetAdminUserHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/users/nonexistent-id", nil)
|
||||
// Use 12-char or less ID to avoid CHAR(12) constraint error
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/users/nonexist", nil)
|
||||
|
||||
if w.Code != http.StatusNotFound {
|
||||
t.Errorf("expected status 404, got %d", w.Code)
|
||||
@@ -106,8 +107,8 @@ func TestAdminUsers_PatchTests_Eligible(t *testing.T) {
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', 'hash', 'verified_email', 'standard')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
@@ -152,8 +153,8 @@ func TestAdminUsers_PatchTests_Eligible_WithExisting(t *testing.T) {
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', 'hash', 'verified_email', 'standard')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
@@ -218,8 +219,8 @@ func TestAdminUsers_AddPatchTest(t *testing.T) {
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', 'hash', 'verified_email', 'standard')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
@@ -267,8 +268,8 @@ func TestAdminUsers_AddPatchTest_InvalidService(t *testing.T) {
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', 'hash', 'verified_email', 'standard')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Test', 'User', 'testuser@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
@@ -302,8 +303,8 @@ func TestAdminUsers_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', 'standard')
|
||||
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)
|
||||
@@ -312,8 +313,8 @@ func TestAdminUsers_NonAdmin(t *testing.T) {
|
||||
// Create test user for GET
|
||||
var targetUserID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO users (n_first_name, n_last_name, email, password_hash, account_role, account_type)
|
||||
VALUES ('Target', 'User', 'target@test.com', 'hash', 'verified_email', 'standard')
|
||||
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
|
||||
VALUES ('Target', 'User', 'target@test.com', '+447123456789', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&targetUserID)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user