refactor(backend): update test files for PoolProxy and per-test transactions
Migrate all test files from SetupTestDB/db.DB pattern to per-test transactions: - Replace SetupTestDB(t) with SetupTestTx(t) for context + transaction - Replace db.DB.Query/QueryRow/Exec with tx.Query/QueryRow/Exec - Replace context.Background() with context from SetupTestTx - Replace defer rows.Close() pattern with explicit rows.Close() - Add testdb.SeedBaseline(pool) to all TestMain functions - Wire db.Conn = db.NewPoolProxy(pool) in all TestMain functions Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
@@ -18,13 +18,11 @@ package admin
|
||||
//
|
||||
// Note: Notification tests are in handlers/notifications/notifications_test.go
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"crussell/db"
|
||||
"crussell/testutils"
|
||||
"crussell/handlers/notifications"
|
||||
"crussell/handlers/today"
|
||||
@@ -34,11 +32,11 @@ import (
|
||||
// TestAdminToday_CurrentNext verifies that an admin can retrieve the currently
|
||||
// in-progress booking and the next upcoming booking for the dashboard.
|
||||
func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -49,7 +47,7 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -59,7 +57,7 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
}
|
||||
|
||||
// Create booking for today (in_progress)
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW(), 'in_progress', NOW())
|
||||
`, userID)
|
||||
@@ -69,7 +67,7 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
|
||||
// Get the booking ID
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT id FROM bookings WHERE user_id = $1 ORDER BY created_at DESC LIMIT 1
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
@@ -77,7 +75,7 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -86,7 +84,7 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -109,7 +107,7 @@ func TestAdminToday_CurrentNext(t *testing.T) {
|
||||
// TestAdminToday_CurrentNext_ClosingTime verifies that the current-next endpoint
|
||||
// returns the closing time for today.
|
||||
func TestAdminToday_CurrentNext_ClosingTime(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Seed working hours for today (DB uses 0=Monday, 6=Sunday)
|
||||
todayWeekday := int(time.Now().Weekday())
|
||||
@@ -118,7 +116,7 @@ func TestAdminToday_CurrentNext_ClosingTime(t *testing.T) {
|
||||
} else {
|
||||
todayWeekday -= 1
|
||||
}
|
||||
_, err := db.DB.Exec(context.Background(), `
|
||||
_, err := tx.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '18:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '18:00', is_open = true
|
||||
@@ -128,7 +126,7 @@ func TestAdminToday_CurrentNext_ClosingTime(t *testing.T) {
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -155,11 +153,11 @@ func TestAdminToday_CurrentNext_ClosingTime(t *testing.T) {
|
||||
// TestAdminToday_Appointments tests that an admin can get a list of all
|
||||
// bookings scheduled for today with their details.
|
||||
func TestAdminToday_Appointments(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -170,7 +168,7 @@ func TestAdminToday_Appointments(t *testing.T) {
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -180,7 +178,7 @@ func TestAdminToday_Appointments(t *testing.T) {
|
||||
}
|
||||
|
||||
// Create booking for today
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW(), 'confirmed', NOW())
|
||||
`, userID)
|
||||
@@ -190,7 +188,7 @@ func TestAdminToday_Appointments(t *testing.T) {
|
||||
|
||||
// Get the booking ID
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT id FROM bookings WHERE user_id = $1 ORDER BY created_at DESC LIMIT 1
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
@@ -198,7 +196,7 @@ func TestAdminToday_Appointments(t *testing.T) {
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -207,7 +205,7 @@ func TestAdminToday_Appointments(t *testing.T) {
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -230,11 +228,11 @@ func TestAdminToday_Appointments(t *testing.T) {
|
||||
// TestAdminToday_PendingApprovals verifies that an admin can see all pending
|
||||
// bookings that require approval/confirmation.
|
||||
func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -245,7 +243,7 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -255,7 +253,7 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
}
|
||||
|
||||
// Create pending booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() + INTERVAL '1 day', 'pending', NOW())
|
||||
`, userID)
|
||||
@@ -265,7 +263,7 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
|
||||
// Get the booking ID
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT id FROM bookings WHERE user_id = $1 ORDER BY created_at DESC LIMIT 1
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
@@ -273,7 +271,7 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -282,7 +280,7 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetPendingApprovalsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/pending-approvals", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/pending-approvals", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -312,11 +310,11 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
//
|
||||
// The transition happens silently in the background during GET requests, not via cron.
|
||||
func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -327,7 +325,7 @@ func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
|
||||
// Create service with 30 minute duration
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -339,7 +337,7 @@ func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
// Create CONFIRMED booking that started 15 minutes ago (should be in progress)
|
||||
// Start time = NOW - 15 minutes, duration = 30 minutes, so still ongoing
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() - INTERVAL '15 minutes', 'confirmed', NOW())
|
||||
RETURNING id
|
||||
@@ -349,7 +347,7 @@ func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -359,7 +357,7 @@ func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
|
||||
// Call the handler - this should trigger auto-transition
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -367,7 +365,7 @@ func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
|
||||
// Verify the booking status was changed to in_progress
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
@@ -385,11 +383,11 @@ func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
//
|
||||
// The transition happens silently in the background during GET requests, not via cron.
|
||||
func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -400,7 +398,7 @@ func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
|
||||
// Create service with 30 minute duration
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -412,7 +410,7 @@ func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
// Create IN_PROGRESS booking that ended 10 minutes ago
|
||||
// Start time = NOW - 40 minutes, duration = 30 minutes, so ended 10 mins ago
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() - INTERVAL '40 minutes', 'in_progress', NOW())
|
||||
RETURNING id
|
||||
@@ -422,7 +420,7 @@ func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -432,7 +430,7 @@ func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
|
||||
// Call the handler - this should trigger auto-transition
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -440,7 +438,7 @@ func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
|
||||
// Verify the booking status was changed to completed
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
@@ -455,11 +453,11 @@ func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
// TestAdminToday_NoAutoTransition_BeforeStartTime verifies that a confirmed
|
||||
// booking that hasn't started yet is NOT transitioned to in_progress.
|
||||
func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -470,7 +468,7 @@ func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -481,7 +479,7 @@ func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
|
||||
// Create CONFIRMED booking that starts in 1 hour (should NOT transition)
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() + INTERVAL '1 hour', 'confirmed', NOW())
|
||||
RETURNING id
|
||||
@@ -491,7 +489,7 @@ func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -501,7 +499,7 @@ func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
|
||||
// Call the handler
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
@@ -509,7 +507,7 @@ func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
|
||||
// Verify the booking status is still 'confirmed' (not changed)
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
@@ -524,11 +522,11 @@ func TestAdminToday_NoAutoTransition_BeforeStartTime(t *testing.T) {
|
||||
// TestAdminToday_AutoTransition_CurrentNextHandler verifies that auto-transition
|
||||
// also works when calling GetCurrentAndNextHandler (not just appointments handler)
|
||||
func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -539,7 +537,7 @@ func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
@@ -548,19 +546,21 @@ func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
|
||||
// Create CONFIRMED booking that's currently in progress
|
||||
// Create CONFIRMED booking that started a few minutes ago (still in progress).
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
now := time.Now()
|
||||
bookingStart := now.Add(-5 * time.Minute) // 5 min ago — within today, started before now, still in progress (30min service)
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() - INTERVAL '10 minutes', 'confirmed', NOW())
|
||||
VALUES ($1, $2, 'confirmed', NOW())
|
||||
RETURNING id
|
||||
`, userID).Scan(&bookingID)
|
||||
`, userID, bookingStart).Scan(&bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
@@ -570,7 +570,7 @@ func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
|
||||
// Call GetCurrentAndNextHandler - should trigger auto-transition
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -578,7 +578,7 @@ func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
|
||||
// Verify auto-transition happened
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
err = tx.QueryRow(ctx, `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
@@ -612,16 +612,14 @@ func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
// - total_bookings counts non-cancelled bookings, excluding cancelled/no_show
|
||||
// - The range includes bookings from both the closed day and prior open days
|
||||
func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
now := time.Now()
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
yesterdayStart := todayStart.AddDate(0, 0, -1)
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(ctx, `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -637,7 +635,7 @@ func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
} else {
|
||||
todayWeekday -= 1
|
||||
}
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '00:00', '00:00', false)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '00:00', end_time = '00:00', is_open = false
|
||||
@@ -648,7 +646,7 @@ func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
// Mark all other weekdays as open
|
||||
for wd := 0; wd <= 6; wd++ {
|
||||
if wd != todayWeekday {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '17:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '17:00', is_open = true
|
||||
@@ -681,7 +679,7 @@ func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
}
|
||||
|
||||
for _, b := range yesterdayBookings {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
`, userID, b.startTime, b.status)
|
||||
@@ -690,7 +688,7 @@ func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
}
|
||||
}
|
||||
for _, b := range todayBookings {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
`, userID, b.startTime, b.status)
|
||||
@@ -700,7 +698,7 @@ func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -733,15 +731,13 @@ func TestAdminToday_ClosedDay_Summary(t *testing.T) {
|
||||
// - summary_scope = "day" (today's summary)
|
||||
// - week_summary is present with summary_scope = "week"
|
||||
func TestAdminToday_WeekSummary_TomorrowClosed(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
now := time.Now()
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
|
||||
// Create test user
|
||||
var userID string
|
||||
err := db.DB.QueryRow(ctx, `
|
||||
err := tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -770,7 +766,7 @@ func TestAdminToday_WeekSummary_TomorrowClosed(t *testing.T) {
|
||||
startTime = "00:00"
|
||||
endTime = "00:00"
|
||||
}
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = $2, end_time = $3, is_open = $4
|
||||
@@ -782,7 +778,7 @@ func TestAdminToday_WeekSummary_TomorrowClosed(t *testing.T) {
|
||||
_ = sundayGo // unused but kept for clarity
|
||||
|
||||
// Create a completed booking for today (so we're done-for-day but today is open)
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, 'completed', NOW())
|
||||
`, userID, todayStart.Add(9*time.Hour))
|
||||
@@ -791,7 +787,7 @@ func TestAdminToday_WeekSummary_TomorrowClosed(t *testing.T) {
|
||||
}
|
||||
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -824,9 +820,7 @@ func TestAdminToday_WeekSummary_TomorrowClosed(t *testing.T) {
|
||||
// a closed day, even when default working_hours says today is open.
|
||||
// This tests the column name fix: monday_week_start → week_start.
|
||||
func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
|
||||
ctx := context.Background()
|
||||
ctx, tx := testutils.SetupTestTx(t)
|
||||
now := time.Now()
|
||||
todayStart := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
|
||||
|
||||
@@ -839,7 +833,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
}
|
||||
|
||||
// Seed DEFAULT working_hours: today is OPEN (this should be overridden by exceptional hours)
|
||||
_, err := db.DB.Exec(ctx, `
|
||||
_, err := tx.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '17:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '17:00', is_open = true
|
||||
@@ -851,7 +845,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
// Make all other weekdays open too
|
||||
for wd := 0; wd <= 6; wd++ {
|
||||
if wd != todayWeekday {
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO working_hours (weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, '09:00', '17:00', true)
|
||||
ON CONFLICT (weekday) DO UPDATE SET start_time = '09:00', end_time = '17:00', is_open = true
|
||||
@@ -873,7 +867,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
mondayStr := monday.Format("2006-01-02")
|
||||
|
||||
var groupID int
|
||||
err = db.DB.QueryRow(ctx, `
|
||||
err = tx.QueryRow(ctx, `
|
||||
INSERT INTO exceptional_working_hours_groups (name, description)
|
||||
VALUES ('Test Closure', 'Exceptional closure for test')
|
||||
RETURNING id
|
||||
@@ -882,7 +876,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
t.Fatalf("failed to create exceptional hours group: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO exceptional_working_hours (group_id, weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, $2, '00:00', '00:00', false)
|
||||
`, groupID, todayWeekday)
|
||||
@@ -890,7 +884,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
t.Fatalf("failed to seed exceptional hours: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO exceptional_group_applications (group_id, week_start)
|
||||
VALUES ($1, $2::date)
|
||||
`, groupID, mondayStr)
|
||||
@@ -900,7 +894,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
|
||||
// Create a completed booking on today (to populate summary)
|
||||
var userID string
|
||||
err = db.DB.QueryRow(ctx, `
|
||||
err = tx.QueryRow(ctx, `
|
||||
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', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
@@ -909,7 +903,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
_, err = db.DB.Exec(ctx, `
|
||||
_, err = tx.Exec(ctx, `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, $2, 'completed', NOW())
|
||||
`, userID, todayStart.Add(9*time.Hour))
|
||||
@@ -920,7 +914,7 @@ func TestAdminToday_ExceptionalHours_ClosedDay(t *testing.T) {
|
||||
// Call the handler — with exceptional hours making today closed,
|
||||
// it should use the closed-day branch (summary_scope = "week")
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Fatalf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
@@ -964,39 +958,39 @@ func TestAdminNotifications_Acknowledge(t *testing.T) {
|
||||
// TestAdminToday_NonAdmin verifies that non-admin users receive HTTP 403
|
||||
// when accessing today's dashboard endpoints.
|
||||
func TestAdminToday_NonAdmin(t *testing.T) {
|
||||
testutils.SetupTestDB(t)
|
||||
ctx, _ := testutils.SetupTestTx(t)
|
||||
|
||||
// Test current-next endpoint
|
||||
currentNextHandler := mw.RequireAdmin(http.HandlerFunc(today.GetCurrentAndNextHandler))
|
||||
w := makeUserRequest(currentNextHandler, "GET", "/api/admin/today/current-next", nil)
|
||||
w := makeUserRequest(currentNextHandler, "GET", "/api/admin/today/current-next", nil, ctx)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("CurrentNext: expected status 403, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Test appointments endpoint
|
||||
appointmentsHandler := mw.RequireAdmin(http.HandlerFunc(today.GetTodayAppointmentsHandler))
|
||||
w = makeUserRequest(appointmentsHandler, "GET", "/api/admin/today/appointments", nil)
|
||||
w = makeUserRequest(appointmentsHandler, "GET", "/api/admin/today/appointments", nil, ctx)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("Appointments: expected status 403, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Test pending-approvals endpoint
|
||||
pendingApprovalsHandler := mw.RequireAdmin(http.HandlerFunc(today.GetPendingApprovalsHandler))
|
||||
w = makeUserRequest(pendingApprovalsHandler, "GET", "/api/admin/today/pending-approvals", nil)
|
||||
w = makeUserRequest(pendingApprovalsHandler, "GET", "/api/admin/today/pending-approvals", nil, ctx)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("PendingApprovals: expected status 403, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Test notifications list endpoint
|
||||
notificationsHandler := mw.RequireAdmin(http.HandlerFunc(notifications.GetNotifications))
|
||||
w = makeUserRequest(notificationsHandler, "GET", "/api/admin/notifications", nil)
|
||||
w = makeUserRequest(notificationsHandler, "GET", "/api/admin/notifications", nil, ctx)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("Notifications List: expected status 403, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Test notifications acknowledge endpoint
|
||||
ackHandler := mw.RequireAdmin(http.HandlerFunc(notifications.AcknowledgeNotification))
|
||||
w = makeUserRequest(ackHandler, "POST", "/api/admin/notifications/1/acknowledge", nil)
|
||||
w = makeUserRequest(ackHandler, "POST", "/api/admin/notifications/1/acknowledge", nil, ctx)
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("Notifications Acknowledge: expected status 403, got %d", w.Code)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user