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
@@ -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)