refactor(backend): replace resetTestData with SetupTestDB and add new tests

Migrate all test files from resetTestData(t) to testutils.SetupTestDB(t) for isolated per-package test databases.

- Add new feature tests: name history assertions, referral discount preview,
  time blockers, email validation, GDPR export, loyalty manual redemption
- Update existing tests to use batch queries and SetupTestDB
- Remove test_helpers.go resetTestData infrastructure
- Add comprehensive user profile tests (442 new lines)

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
This commit is contained in:
2026-06-20 16:57:36 +01:00
co-authored by Sisyphus
parent 9c68918c20
commit b03c4f6247
36 changed files with 1735 additions and 859 deletions
@@ -14,6 +14,7 @@ import (
"time"
"crussell/db"
"crussell/testutils"
"crussell/mw"
"github.com/go-chi/chi/v5"
@@ -58,9 +59,9 @@ func createTestUser(t *testing.T) string {
return userID
}
func createNotification(t *testing.T, reason, userID string, acknowledged bool) int {
func createNotification(t *testing.T, reason, userID string, acknowledged bool) string {
t.Helper()
var notificationID int
var notificationID string
if userID == "" {
query := `INSERT INTO admin_notifications (reason) VALUES ($1) RETURNING id`
if acknowledged {
@@ -88,7 +89,7 @@ func createNotification(t *testing.T, reason, userID string, acknowledged bool)
// =============================================================================
func TestNotifications_IncludeAcknowledged_Default(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, false)
@@ -108,7 +109,7 @@ func TestNotifications_IncludeAcknowledged_Default(t *testing.T) {
}
func TestNotifications_IncludeAcknowledged_True(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, false)
@@ -128,7 +129,7 @@ func TestNotifications_IncludeAcknowledged_True(t *testing.T) {
}
func TestNotifications_IncludeAcknowledged_ResponseHasAcknowledgedAt(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, true)
@@ -155,7 +156,7 @@ func TestNotifications_IncludeAcknowledged_ResponseHasAcknowledgedAt(t *testing.
// =============================================================================
func TestNotifications_PriorityOrdering(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
// Create notifications in reverse priority order
@@ -191,7 +192,7 @@ func TestNotifications_PriorityOrdering(t *testing.T) {
}
func TestNotifications_PriorityOrdering_OldestFirstWithinPriority(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
// Create two pending_booking notifications with a time gap
@@ -218,7 +219,7 @@ func TestNotifications_PriorityOrdering_OldestFirstWithinPriority(t *testing.T)
}
func TestNotifications_AllNotifications_NewestFirst(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, false)
@@ -248,7 +249,7 @@ func TestNotifications_AllNotifications_NewestFirst(t *testing.T) {
// =============================================================================
func TestNotifications_UnreadCount(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, false)
@@ -273,7 +274,7 @@ func TestNotifications_UnreadCount(t *testing.T) {
}
func TestNotifications_UnreadCount_Zero(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, true)
@@ -292,7 +293,7 @@ func TestNotifications_UnreadCount_Zero(t *testing.T) {
}
func TestNotifications_UnreadCount_Empty(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
handler := http.HandlerFunc(GetUnreadCount)
w := makeExtendedAdminRequest(handler, "GET", "/api/admin/notifications/unread-count", nil)
@@ -312,7 +313,7 @@ func TestNotifications_UnreadCount_Empty(t *testing.T) {
// =============================================================================
func TestNotifications_NewBookingReason(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "new_booking", userID, false)
@@ -335,7 +336,7 @@ func TestNotifications_NewBookingReason(t *testing.T) {
}
func TestNotifications_EditRequestedReason(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "edit_requested", userID, false)
@@ -358,7 +359,7 @@ func TestNotifications_EditRequestedReason(t *testing.T) {
}
func TestNotifications_Priority_NewBookingBelowPendingBooking(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "new_booking", userID, false)
@@ -386,7 +387,7 @@ func TestNotifications_Priority_NewBookingBelowPendingBooking(t *testing.T) {
// =============================================================================
func TestNotifications_IncludeAcknowledgedWithReasonFilter(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
createNotification(t, "pending_booking", userID, false)
@@ -411,7 +412,7 @@ func TestNotifications_IncludeAcknowledgedWithReasonFilter(t *testing.T) {
// =============================================================================
func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create a service
var serviceID string
@@ -471,7 +472,7 @@ func TestNotifications_ResponseEnriched_WithUserAndBooking(t *testing.T) {
}
func TestNotifications_ResponseEnriched_NoUserOrBooking(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create notification without user_id or booking_id
createNotification(t, "1_week_no_pay", "", false)
@@ -502,13 +503,13 @@ func TestNotifications_ResponseEnriched_NoUserOrBooking(t *testing.T) {
// =============================================================================
func TestNotifications_Acknowledge_ViaExtendedHandler(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
notifID := createNotification(t, "pending_booking", userID, false)
handler := http.HandlerFunc(AcknowledgeNotification)
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%d/acknowledge", notifID), nil)
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notifID), nil)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
@@ -527,13 +528,13 @@ func TestNotifications_Acknowledge_ViaExtendedHandler(t *testing.T) {
}
func TestNotifications_Acknowledge_AlreadyAcknowledged_Extended(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
userID := createTestUser(t)
notifID := createNotification(t, "pending_booking", userID, true)
handler := http.HandlerFunc(AcknowledgeNotification)
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%d/acknowledge", notifID), nil)
w := makeExtendedAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notifID), nil)
if w.Code != http.StatusNotFound {
t.Errorf("expected status 404 for already acknowledged, got %d", w.Code)
@@ -541,9 +542,9 @@ func TestNotifications_Acknowledge_AlreadyAcknowledged_Extended(t *testing.T) {
}
// createNotificationWithBooking creates a notification with both user_id and booking_id
func createNotificationWithBooking(t *testing.T, reason, userID, bookingID string, acknowledged bool) int {
func createNotificationWithBooking(t *testing.T, reason, userID, bookingID string, acknowledged bool) string {
t.Helper()
var notificationID int
var notificationID string
query := `INSERT INTO admin_notifications (reason, user_id, booking_id) VALUES ($1, $2, $3) RETURNING id`
if acknowledged {
query = `INSERT INTO admin_notifications (reason, user_id, booking_id, acknowledged_at) VALUES ($1, $2, $3, NOW()) RETURNING id`
@@ -21,35 +21,18 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
"crussell/db"
"crussell/testutils"
"crussell/mw"
"crussell/testutils/fixtures"
"crussell/testutils/jwt"
"crussell/testutils/testdb"
"github.com/go-chi/chi/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
func TestMain(m *testing.M) {
pool, _ := testdb.NewPool("")
testdb.Migrate(&testing.T{}, pool)
db.DB = pool
jwt.Init()
code := m.Run()
pool.Close()
os.Exit(code)
}
func resetTestData(t *testing.T) {
t.Helper()
testdb.TruncateTables(t, db.DB)
}
// makeAdminRequest creates a request with admin context
func makeAdminRequest(handler http.Handler, method, path string, body interface{}) *httptest.ResponseRecorder {
return makeRequestWithContext(handler, method, path, body, "admin001", "admin")
@@ -108,7 +91,7 @@ func extractIDFromPath(path string) (string, string) {
// TestNotifications_List tests that an admin can list all unacknowledged notifications
func TestNotifications_List(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user for notification reference
var userID string
@@ -122,7 +105,7 @@ func TestNotifications_List(t *testing.T) {
}
// Create a notification
var notificationID int
var notificationID string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO admin_notifications (reason, user_id)
VALUES ('pending_booking', $1)
@@ -153,14 +136,14 @@ func TestNotifications_List(t *testing.T) {
t.Errorf("expected reason 'pending_booking', got %s", resp.Notifications[0].Reason)
}
if resp.Notifications[0].ID != notificationID {
t.Errorf("expected notification ID %d, got %d", notificationID, resp.Notifications[0].ID)
t.Errorf("expected notification ID %s, got %s", notificationID, resp.Notifications[0].ID)
}
}
}
// TestNotifications_ListEmpty tests that an empty list is returned when no notifications exist
func TestNotifications_ListEmpty(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
handler := http.HandlerFunc(GetNotifications)
w := makeAdminRequest(handler, "GET", "/api/admin/notifications", nil)
@@ -186,7 +169,7 @@ func TestNotifications_ListEmpty(t *testing.T) {
// TestNotifications_ListFilterByReason tests that notifications can be filtered by reason
func TestNotifications_ListFilterByReason(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user
var userID string
@@ -236,7 +219,7 @@ func TestNotifications_ListFilterByReason(t *testing.T) {
// TestNotifications_ListPagination tests that pagination works correctly
func TestNotifications_ListPagination(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user
var userID string
@@ -285,7 +268,7 @@ func TestNotifications_ListPagination(t *testing.T) {
// TestNotifications_ListExcludesAcknowledged tests that acknowledged notifications are not returned
func TestNotifications_ListExcludesAcknowledged(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user
var userID string
@@ -308,7 +291,7 @@ func TestNotifications_ListExcludesAcknowledged(t *testing.T) {
}
// Create unacknowledged notification
var unackID int
var unackID string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO admin_notifications (reason, user_id)
VALUES ('cancelled_booking', $1)
@@ -336,7 +319,7 @@ func TestNotifications_ListExcludesAcknowledged(t *testing.T) {
}
if len(resp.Notifications) > 0 && resp.Notifications[0].ID != unackID {
t.Errorf("expected unacknowledged notification ID %d, got %d", unackID, resp.Notifications[0].ID)
t.Errorf("expected unacknowledged notification ID %s, got %s", unackID, resp.Notifications[0].ID)
}
}
@@ -346,7 +329,7 @@ func TestNotifications_ListExcludesAcknowledged(t *testing.T) {
// TestNotifications_Acknowledge tests that an admin can acknowledge a notification
func TestNotifications_Acknowledge(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user
var userID string
@@ -360,7 +343,7 @@ func TestNotifications_Acknowledge(t *testing.T) {
}
// Create notification
var notificationID int
var notificationID string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO admin_notifications (reason, user_id)
VALUES ('pending_booking', $1)
@@ -371,7 +354,7 @@ func TestNotifications_Acknowledge(t *testing.T) {
}
handler := http.HandlerFunc(AcknowledgeNotification)
w := makeAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%d/acknowledge", notificationID), nil)
w := makeAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notificationID), nil)
if w.Code != http.StatusOK {
t.Errorf("expected status 200, got %d. body: %s", w.Code, w.Body.String())
@@ -393,10 +376,10 @@ func TestNotifications_Acknowledge(t *testing.T) {
// TestNotifications_AcknowledgeNotFound tests that acknowledging a non-existent notification returns 404
func TestNotifications_AcknowledgeNotFound(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
handler := http.HandlerFunc(AcknowledgeNotification)
w := makeAdminRequest(handler, "POST", "/api/admin/notifications/99999/acknowledge", nil)
w := makeAdminRequest(handler, "POST", "/api/admin/notifications/ffffffffffff/acknowledge", nil)
if w.Code != http.StatusNotFound {
t.Errorf("expected status 404 for non-existent notification, got %d", w.Code)
@@ -405,7 +388,7 @@ func TestNotifications_AcknowledgeNotFound(t *testing.T) {
// TestNotifications_AcknowledgeAlreadyAcknowledged tests that acknowledging an already-acknowledged notification returns 404
func TestNotifications_AcknowledgeAlreadyAcknowledged(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user
var userID string
@@ -419,7 +402,7 @@ func TestNotifications_AcknowledgeAlreadyAcknowledged(t *testing.T) {
}
// Create already-acknowledged notification
var notificationID int
var notificationID string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO admin_notifications (reason, user_id, acknowledged_at)
VALUES ('pending_booking', $1, NOW())
@@ -430,7 +413,7 @@ func TestNotifications_AcknowledgeAlreadyAcknowledged(t *testing.T) {
}
handler := http.HandlerFunc(AcknowledgeNotification)
w := makeAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%d/acknowledge", notificationID), nil)
w := makeAdminRequest(handler, "POST", fmt.Sprintf("/api/admin/notifications/%s/acknowledge", notificationID), nil)
if w.Code != http.StatusNotFound {
t.Errorf("expected status 404 for already acknowledged notification, got %d", w.Code)
@@ -439,7 +422,7 @@ func TestNotifications_AcknowledgeAlreadyAcknowledged(t *testing.T) {
// TestNotifications_AcknowledgeInvalidID tests that invalid notification IDs are handled
func TestNotifications_AcknowledgeInvalidID(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
handler := http.HandlerFunc(AcknowledgeNotification)
@@ -478,7 +461,7 @@ func TestNotifications_AcknowledgeInvalidID(t *testing.T) {
// TestNotifications_AcknowledgeMissingID tests that missing ID returns 400
func TestNotifications_AcknowledgeMissingID(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create a custom request with no ID in path
req := httptest.NewRequest("POST", "/api/admin/notifications//acknowledge", nil)
@@ -506,7 +489,7 @@ func TestNotifications_AcknowledgeMissingID(t *testing.T) {
// TestNotifications_WithBookingReference tests that notifications include booking_id when applicable
func TestNotifications_WithBookingReference(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
// Create test user
var userID string
@@ -542,7 +525,7 @@ func TestNotifications_WithBookingReference(t *testing.T) {
}
// Create notification with booking reference
var notificationID int
var notificationID string
err = db.DB.QueryRow(context.Background(), `
INSERT INTO admin_notifications (reason, booking_id, user_id)
VALUES ('pending_booking', $1, $2)
@@ -583,7 +566,7 @@ var _ = func() *pgxpool.Pool { return nil }
// =============================================================================
func TestAcknowledgePendingBookingNotification_Success(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
ctx := context.Background()
userID, err := fixtures.CreateTestUser(db.DB)
@@ -642,7 +625,7 @@ func TestAcknowledgePendingBookingNotification_Success(t *testing.T) {
}
func TestAcknowledgePendingBookingNotification_Idempotent(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
ctx := context.Background()
userID, err := fixtures.CreateTestUser(db.DB)
@@ -687,7 +670,7 @@ func TestAcknowledgePendingBookingNotification_Idempotent(t *testing.T) {
}
func TestAcknowledgePendingBookingNotification_NoNotification(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
ctx := context.Background()
userID, err := fixtures.CreateTestUser(db.DB)
@@ -723,7 +706,7 @@ func TestAcknowledgePendingBookingNotification_NoNotification(t *testing.T) {
}
func TestAcknowledgePendingBookingNotification_NonTxCaller(t *testing.T) {
resetTestData(t)
testutils.SetupTestDB(t)
ctx := context.Background()
userID, err := fixtures.CreateTestUser(db.DB)