Tests
This commit is contained in:
@@ -9,12 +9,11 @@ package admin
|
||||
// - GetCurrentAndNextHandler: GET /api/admin/today/current-next - Get current & next booking
|
||||
// - GetTodayAppointmentsHandler: GET /api/admin/today/appointments - Get today's bookings
|
||||
// - GetPendingApprovalsHandler: GET /api/admin/today/pending-approvals - Get pending bookings
|
||||
// - GetNotifications: GET /api/admin/notifications - List notifications (WIP - skipped)
|
||||
// - AcknowledgeNotification: POST /api/admin/notifications/{id}/ack - Acknowledge (WIP - skipped)
|
||||
// - Auto-status transitions: Silent background updates on GET requests
|
||||
//
|
||||
// Authentication: All endpoints require admin role (403 for non-admins).
|
||||
// WIP: Notification tests are skipped pending handler implementation.package admin
|
||||
|
||||
//
|
||||
// Note: Notification tests are in handlers/notifications/notifications_test.go
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
@@ -255,6 +254,310 @@ func TestAdminToday_PendingApprovals(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// =============================================================================
|
||||
// Auto-Status Transition Tests
|
||||
// =============================================================================
|
||||
|
||||
// TestAdminToday_AutoTransition_ConfirmedToInProgress verifies that a confirmed booking
|
||||
// that has started but not yet ended is automatically transitioned to in_progress
|
||||
// when fetching today's appointments.
|
||||
//
|
||||
// The transition happens silently in the background during GET requests, not via cron.
|
||||
func TestAdminToday_AutoTransition_ConfirmedToInProgress(t *testing.T) {
|
||||
cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
// Create 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 ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
// Create service with 30 minute duration
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
`).Scan(&serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
|
||||
// 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(), `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() - INTERVAL '15 minutes', 'confirmed', NOW())
|
||||
RETURNING id
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to add service to booking: %v", err)
|
||||
}
|
||||
|
||||
// Call the handler - this should trigger auto-transition
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify the booking status was changed to in_progress
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query booking status: %v", err)
|
||||
}
|
||||
|
||||
if status != "in_progress" {
|
||||
t.Errorf("expected status 'in_progress' after auto-transition, got '%s'", status)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminToday_AutoTransition_InProgressToCompleted verifies that an in_progress
|
||||
// booking that has ended is automatically transitioned to completed when fetching
|
||||
// today's appointments.
|
||||
//
|
||||
// The transition happens silently in the background during GET requests, not via cron.
|
||||
func TestAdminToday_AutoTransition_InProgressToCompleted(t *testing.T) {
|
||||
cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
// Create 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 ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
// Create service with 30 minute duration
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
`).Scan(&serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
|
||||
// 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(), `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() - INTERVAL '40 minutes', 'in_progress', NOW())
|
||||
RETURNING id
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to add service to booking: %v", err)
|
||||
}
|
||||
|
||||
// Call the handler - this should trigger auto-transition
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify the booking status was changed to completed
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query booking status: %v", err)
|
||||
}
|
||||
|
||||
if status != "completed" {
|
||||
t.Errorf("expected status 'completed' after auto-transition, got '%s'", status)
|
||||
}
|
||||
}
|
||||
|
||||
// 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) {
|
||||
cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
// Create 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 ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
`).Scan(&serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
|
||||
// Create CONFIRMED booking that starts in 1 hour (should NOT transition)
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() + INTERVAL '1 hour', 'confirmed', NOW())
|
||||
RETURNING id
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to add service to booking: %v", err)
|
||||
}
|
||||
|
||||
// Call the handler
|
||||
handler := http.HandlerFunc(today.GetTodayAppointmentsHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/appointments", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d", w.Code)
|
||||
}
|
||||
|
||||
// Verify the booking status is still 'confirmed' (not changed)
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query booking status: %v", err)
|
||||
}
|
||||
|
||||
if status != "confirmed" {
|
||||
t.Errorf("expected status 'confirmed' (no auto-transition before start), got '%s'", status)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminToday_AutoTransition_CurrentNextHandler verifies that auto-transition
|
||||
// also works when calling GetCurrentAndNextHandler (not just appointments handler)
|
||||
func TestAdminToday_AutoTransition_CurrentNextHandler(t *testing.T) {
|
||||
cleanup := setupTestDB(t)
|
||||
defer cleanup()
|
||||
|
||||
// Create 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 ('Test', 'User', 'testuser@test.com', '+1234567890', '1990-01-01', 'hash', 'verified_email', 'email')
|
||||
RETURNING id
|
||||
`).Scan(&userID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create user: %v", err)
|
||||
}
|
||||
|
||||
// Create service
|
||||
var serviceID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO services (name, description, price, duration_minutes, is_active)
|
||||
VALUES ('Manicure', 'Basic manicure', 25.00, 30, true)
|
||||
RETURNING id
|
||||
`).Scan(&serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create service: %v", err)
|
||||
}
|
||||
|
||||
// Create CONFIRMED booking that's currently in progress
|
||||
var bookingID string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO bookings (user_id, start_time, status, created_at)
|
||||
VALUES ($1, NOW() - INTERVAL '10 minutes', 'confirmed', NOW())
|
||||
RETURNING id
|
||||
`, userID).Scan(&bookingID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create booking: %v", err)
|
||||
}
|
||||
|
||||
// Add service to booking
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
INSERT INTO booking_services (booking_id, service_id)
|
||||
VALUES ($1, $2)
|
||||
`, bookingID, serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to add service to booking: %v", err)
|
||||
}
|
||||
|
||||
// Call GetCurrentAndNextHandler - should trigger auto-transition
|
||||
handler := http.HandlerFunc(today.GetCurrentAndNextHandler)
|
||||
w := makeAdminRequest(handler, "GET", "/api/admin/today/current-next", nil)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify auto-transition happened
|
||||
var status string
|
||||
err = db.DB.QueryRow(context.Background(), `
|
||||
SELECT status FROM bookings WHERE id = $1
|
||||
`, bookingID).Scan(&status)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query booking status: %v", err)
|
||||
}
|
||||
|
||||
if status != "in_progress" {
|
||||
t.Errorf("expected 'in_progress' after current-next handler, got '%s'", status)
|
||||
}
|
||||
|
||||
// Verify the response includes the booking as 'current'
|
||||
var response today.CurrentNextResponse
|
||||
if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil {
|
||||
t.Fatalf("failed to unmarshal response: %v", err)
|
||||
}
|
||||
|
||||
if response.Current == nil {
|
||||
t.Error("expected current booking in response")
|
||||
} else if response.Current.ID != bookingID {
|
||||
t.Errorf("expected current booking ID %s, got %s", bookingID, response.Current.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// TestAdminNotifications_List is skipped (WIP) - tests that an admin
|
||||
// can list all their notifications.
|
||||
func TestAdminNotifications_List(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user