test fixes

This commit is contained in:
2026-03-01 16:05:02 +00:00
parent e00740a8c9
commit 803aab7073
6 changed files with 1652 additions and 4 deletions
+145
View File
@@ -397,3 +397,148 @@ func TestAdminUsers_NonAdmin(t *testing.T) {
t.Errorf("ADD: expected status 403, got %d", w.Code)
}
}
func TestAdminUsers_Get_Success(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
// Create a test user
var userID string
err := db.DB.QueryRow(context.Background(), `
INSERT INTO users (n_first_name, n_last_name, email, phone, date_of_birth, password_hash, account_role, account_type)
VALUES ('John', 'Doe', 'john.doe@test.com', '+447700900000', '1990-01-01', 'hash', 'verified_email', 'email')
RETURNING id
`).Scan(&userID)
if err != nil {
t.Fatalf("failed to create test user: %v", err)
}
// Call admin get user endpoint
handler := http.HandlerFunc(user.GetAdminUserHandler)
w := makeAdminRequest(handler, "GET", "/api/admin/users/"+userID, nil)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
t.Logf("response body: %s", w.Body.String())
return
}
var resp user.AdminUserDetail
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to unmarshal response: %v", err)
}
if resp.ID != userID {
t.Errorf("expected user ID %s, got %s", userID, resp.ID)
}
if resp.FirstName != "John" {
t.Errorf("expected first name 'John', got '%s'", resp.FirstName)
}
if resp.LastName != "Doe" {
t.Errorf("expected last name 'Doe', got '%s'", resp.LastName)
}
if resp.Email == nil || *resp.Email != "john.doe@test.com" {
t.Errorf("expected email 'john.doe@test.com', got '%v'", resp.Email)
}
if resp.AccountRole != "verified_email" {
t.Errorf("expected account_role 'verified_email', got '%s'", resp.AccountRole)
}
}
func TestAdminUsers_Get_WithBookings(t *testing.T) {
cleanup := setupTestDB(t)
defer cleanup()
// Create a test user
var userID string
err := db.DB.QueryRow(context.Background(), `
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', '+447700900000', '1990-01-01', 'hash', 'verified_email', 'email')
RETURNING id
`).Scan(&userID)
if err != nil {
t.Fatalf("failed to create test user: %v", err)
}
// Create a service
var serviceID string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO services (name, description, price, duration_minutes, is_active)
VALUES ('Test Service', 'Test description', 50.00, 60, true)
RETURNING id
`).Scan(&serviceID)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
// Create bookings for the user
var bookingID1 string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO bookings (user_id, start_time, status, notes)
VALUES ($1, '2099-12-31 10:00:00+00', 'completed', 'Past booking')
RETURNING id
`, userID).Scan(&bookingID1)
if err != nil {
t.Fatalf("failed to create booking: %v", err)
}
// Link service to booking
_, err = db.DB.Exec(context.Background(),
"INSERT INTO booking_services (booking_id, service_id) VALUES ($1, $2)",
bookingID1, serviceID)
if err != nil {
t.Fatalf("failed to link service to booking: %v", err)
}
var bookingID2 string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO bookings (user_id, start_time, status, notes)
VALUES ($1, '2099-12-31 14:00:00+00', 'pending', 'Upcoming booking')
RETURNING id
`, userID).Scan(&bookingID2)
if err != nil {
t.Fatalf("failed to create second booking: %v", err)
}
// Link service to second booking
_, err = db.DB.Exec(context.Background(),
"INSERT INTO booking_services (booking_id, service_id) VALUES ($1, $2)",
bookingID2, serviceID)
if err != nil {
t.Fatalf("failed to link service to second booking: %v", err)
}
// Call admin get user endpoint
handler := http.HandlerFunc(user.GetAdminUserHandler)
w := makeAdminRequest(handler, "GET", "/api/admin/users/"+userID, nil)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d", w.Code)
t.Logf("response body: %s", w.Body.String())
return
}
// Note: The current implementation doesn't include bookings in the response
// This test verifies the user is retrieved correctly
var resp user.AdminUserDetail
if err := json.Unmarshal(w.Body.Bytes(), &resp); err != nil {
t.Fatalf("failed to unmarshal response: %v", err)
}
if resp.ID != userID {
t.Errorf("expected user ID %s, got %s", userID, resp.ID)
}
// Verify bookings exist in database (we can't verify via response since it's not included)
var bookingCount int
err = db.DB.QueryRow(context.Background(),
"SELECT COUNT(*) FROM bookings WHERE user_id = $1", userID).Scan(&bookingCount)
if err != nil {
t.Fatalf("failed to count bookings: %v", err)
}
if bookingCount != 2 {
t.Errorf("expected 2 bookings in DB, got %d", bookingCount)
}
}