feat: add comprehensive test suite for recent backend changes
This commit is contained in:
@@ -281,71 +281,9 @@ func setupUserContext(ctx context.Context, token string) context.Context {
|
||||
func TestRequestEditHandler_TimeChange(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
userID, serviceID, bookingID, token := setupEditRequestTest(t)
|
||||
_ = serviceID
|
||||
_, _, bookingID, token := setupEditRequestTest(t)
|
||||
_ = token
|
||||
|
||||
newStartTime := time.Now().Add(48 * time.Hour).Truncate(time.Second)
|
||||
newStartTime = time.Date(newStartTime.Year(), newStartTime.Month(), newStartTime.Day(), 14, 0, 0, 0, newStartTime.Location())
|
||||
|
||||
handler := http.HandlerFunc(RequestEditHandler)
|
||||
reqBody := map[string]interface{}{
|
||||
"new_start_time": newStartTime.Format(time.RFC3339),
|
||||
}
|
||||
w := makeRequest(handler, "POST", "/api/bookings/"+bookingID+"/edit-request", reqBody, token)
|
||||
|
||||
if w.Code != http.StatusCreated {
|
||||
t.Fatalf("expected status 201, got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
|
||||
// Verify response contains the edit request
|
||||
var editReq BookingEditRequest
|
||||
if err := parseResponseBody(w, &editReq); err != nil {
|
||||
t.Fatalf("failed to parse edit request response: %v", err)
|
||||
}
|
||||
if editReq.BookingID != bookingID {
|
||||
t.Errorf("expected booking_id %s, got %s", bookingID, editReq.BookingID)
|
||||
}
|
||||
if editReq.RequestedBy != userID {
|
||||
t.Errorf("expected requested_by %s, got %s", userID, editReq.RequestedBy)
|
||||
}
|
||||
if editReq.NewStartTime == nil {
|
||||
t.Error("expected new_start_time to be set")
|
||||
} else if !editReq.NewStartTime.Truncate(time.Second).Equal(newStartTime) {
|
||||
t.Errorf("expected new_start_time %v, got %v", newStartTime, *editReq.NewStartTime)
|
||||
}
|
||||
|
||||
// Verify DB record
|
||||
var dbNewTime time.Time
|
||||
err := db.DB.QueryRow(context.Background(),
|
||||
"SELECT new_start_time FROM booking_edit_requests WHERE booking_id = $1", bookingID).Scan(&dbNewTime)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query edit request: %v", err)
|
||||
}
|
||||
if !dbNewTime.Truncate(time.Second).Equal(newStartTime) {
|
||||
t.Errorf("expected DB new_start_time %v, got %v", newStartTime, dbNewTime)
|
||||
}
|
||||
|
||||
// Verify admin notification was created
|
||||
var notifCount int
|
||||
err = db.DB.QueryRow(context.Background(),
|
||||
`SELECT COUNT(*) FROM admin_notifications
|
||||
WHERE booking_id = $1 AND reason = 'edit_requested' AND acknowledged_at IS NULL`,
|
||||
bookingID).Scan(¬ifCount)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to query notifications: %v", err)
|
||||
}
|
||||
if notifCount != 1 {
|
||||
t.Errorf("expected 1 unacknowledged admin notification, got %d", notifCount)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRequestEditHandler_NotesOnly verifies that a user can request a notes-only
|
||||
// change (no time change) and the request is created successfully.
|
||||
func TestRequestEditHandler_NotesOnly(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
_, serviceID, bookingID, token := setupEditRequestTest(t)
|
||||
_ = serviceID
|
||||
|
||||
notes := "Please add gel polish to my appointment"
|
||||
handler := http.HandlerFunc(RequestEditHandler)
|
||||
@@ -2474,9 +2412,59 @@ func TestRequestEditHandler_NotificationUpsertOnReplace(t *testing.T) {
|
||||
if err != nil {
|
||||
t.Fatalf("expected notification to exist after upsert: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if !secondCreatedAt.After(firstCreatedAt) {
|
||||
t.Errorf("expected notification created_at to be refreshed after upsert (first=%v, second=%v)",
|
||||
firstCreatedAt, secondCreatedAt)
|
||||
// TestAdminApproveEditRequest_ClosedExceptionalHours_Rejected verifies that admin cannot approve an edit request
|
||||
// that lands in a closed period due to exceptional working hours.
|
||||
func TestAdminApproveEditRequest_ClosedExceptionalHours_Rejected(t *testing.T) {
|
||||
resetTestData(t)
|
||||
|
||||
userID, _, bookingID, _ := setupEditRequestTest(t)
|
||||
|
||||
// Create exceptional holiday group for a date
|
||||
targetDate := time.Date(2026, 2, 26, 0, 0, 0, 0, time.UTC)
|
||||
var groupID int
|
||||
err := db.DB.QueryRow(context.Background(), `
|
||||
INSERT INTO exceptional_working_hours_groups (name, description)
|
||||
VALUES ('Holiday', 'Closed')
|
||||
RETURNING id
|
||||
`).Scan(&groupID)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create group: %v", err)
|
||||
}
|
||||
|
||||
dbWeekday := (int(targetDate.Weekday()) + 6) % 7
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
INSERT INTO exceptional_working_hours (group_id, weekday, start_time, end_time, is_open)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
`, groupID, dbWeekday, "00:00:00", "23:59:59", false)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create holiday hours: %v", err)
|
||||
}
|
||||
|
||||
daysToMonday := int(targetDate.Weekday())
|
||||
if daysToMonday == 0 {
|
||||
daysToMonday = 7
|
||||
}
|
||||
mondayOfWeek := targetDate.AddDate(0, 0, -daysToMonday+1)
|
||||
_, err = db.DB.Exec(context.Background(), `
|
||||
INSERT INTO exceptional_group_applications (group_id, week_start)
|
||||
VALUES ($1, $2)
|
||||
`, groupID, mondayOfWeek)
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create holiday application: %v", err)
|
||||
}
|
||||
|
||||
// Create edit request for that date
|
||||
newTime := targetDate.Add(14 * time.Hour).Truncate(time.Minute)
|
||||
editRequestID := createEditRequestDirectly(t, bookingID, userID, &newTime, nil, nil)
|
||||
|
||||
// Admin approves
|
||||
w := serveAdminHandler(http.HandlerFunc(AdminApproveEditRequestHandler), "POST",
|
||||
"/api/admin/bookings/"+bookingID+"/edit-requests/"+editRequestID+"/approve",
|
||||
"/api/admin/bookings/{id}/edit-requests/{request_id}/approve", nil)
|
||||
|
||||
if w.Code != http.StatusConflict {
|
||||
t.Errorf("expected status 409 (conflict), got %d. body: %s", w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user