//go:build test // +build test package admin import ( "context" "encoding/json" "net/http" "testing" "crussell/db" "crussell/handlers/notifications" "crussell/handlers/today" "crussell/mw" ) func TestAdminToday_CurrentNext(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 booking for today (in_progress) _, err = db.DB.Exec(context.Background(), ` INSERT INTO bookings (user_id, start_time, status, created_at) VALUES ($1, NOW(), 'in_progress', NOW()) `, userID) if err != nil { t.Fatalf("failed to create booking: %v", err) } // Get the booking ID var bookingID string err = db.DB.QueryRow(context.Background(), ` SELECT id FROM bookings WHERE user_id = $1 ORDER BY created_at DESC LIMIT 1 `, userID).Scan(&bookingID) if err != nil { t.Fatalf("failed to get booking ID: %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) } 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()) } 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.Errorf("expected current appointment, got nil") } if response.Current != nil && response.Current.ID != bookingID { t.Errorf("expected booking ID %s, got %s", bookingID, response.Current.ID) } } func TestAdminToday_Appointments(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 booking for today _, err = db.DB.Exec(context.Background(), ` INSERT INTO bookings (user_id, start_time, status, created_at) VALUES ($1, NOW(), 'confirmed', NOW()) `, userID) if err != nil { t.Fatalf("failed to create booking: %v", err) } // Get the booking ID var bookingID string err = db.DB.QueryRow(context.Background(), ` SELECT id FROM bookings WHERE user_id = $1 ORDER BY created_at DESC LIMIT 1 `, userID).Scan(&bookingID) if err != nil { t.Fatalf("failed to get booking ID: %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) } 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()) } var response today.TodayAppointmentsResponse if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil { t.Fatalf("failed to unmarshal response: %v", err) } if len(response.Appointments) != 1 { t.Errorf("expected 1 appointment, got %d", len(response.Appointments)) } if len(response.Appointments) > 0 && response.Appointments[0].ID != bookingID { t.Errorf("expected booking ID %s, got %s", bookingID, response.Appointments[0].ID) } } func TestAdminToday_PendingApprovals(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 pending booking _, err = db.DB.Exec(context.Background(), ` INSERT INTO bookings (user_id, start_time, status, created_at) VALUES ($1, NOW() + INTERVAL '1 day', 'pending', NOW()) `, userID) if err != nil { t.Fatalf("failed to create booking: %v", err) } // Get the booking ID var bookingID string err = db.DB.QueryRow(context.Background(), ` SELECT id FROM bookings WHERE user_id = $1 ORDER BY created_at DESC LIMIT 1 `, userID).Scan(&bookingID) if err != nil { t.Fatalf("failed to get booking ID: %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) } handler := http.HandlerFunc(today.GetPendingApprovalsHandler) w := makeAdminRequest(handler, "GET", "/api/admin/today/pending-approvals", nil) if w.Code != http.StatusOK { t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String()) } var response today.PendingApprovalsResponse if err := json.Unmarshal(w.Body.Bytes(), &response); err != nil { t.Fatalf("failed to unmarshal response: %v", err) } if len(response.Approvals) != 1 { t.Errorf("expected 1 pending approval, got %d", len(response.Approvals)) } if len(response.Approvals) > 0 && response.Approvals[0].ID != bookingID { t.Errorf("expected booking ID %s, got %s", bookingID, response.Approvals[0].ID) } } func TestAdminNotifications_List(t *testing.T) { t.Skip("Skipping - WIP handler") } func TestAdminNotifications_Acknowledge(t *testing.T) { t.Skip("Skipping - WIP handler") } func TestAdminToday_NonAdmin(t *testing.T) { cleanup := setupTestDB(t) defer cleanup() // Test current-next endpoint currentNextHandler := mw.RequireAdmin(http.HandlerFunc(today.GetCurrentAndNextHandler)) w := makeUserRequest(currentNextHandler, "GET", "/api/admin/today/current-next", nil) 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) 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) 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) 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) if w.Code != http.StatusForbidden { t.Errorf("Notifications Acknowledge: expected status 403, got %d", w.Code) } }