fix: improve test infrastructure and add ID validation

- Add TestMain to set test env vars and testdb.TruncateTables for test
  isolation
- Add chi routing context to test helpers for path parameter extraction
- Fix SQL error handling to use errors.Is() instead of ==
- Add validators package with ID validation
- Fix admin test middleware chain (RequireAdmin wrapper)
- Update test user inserts to include phone and date_of_birth fields
- Update service delete test to check soft-delete (is_active=false)
- Update holiday hours test to use new schema (weekday, is_open)
- Add phone number validation tests for UK mobile numbers
This commit is contained in:
2026-02-23 00:59:32 +00:00
parent 355e8a26c1
commit df3439bd70
30 changed files with 1081 additions and 360 deletions
+3 -11
View File
@@ -25,6 +25,7 @@ func setupTestDB(t *testing.T) func() {
pool := testdb.Pool(t)
testdb.Migrate(t, pool)
testdb.TruncateTables(t, pool)
originalDB := db.DB
db.DB = pool
@@ -302,7 +303,6 @@ func TestScheduling_CreateExceptionalGroup_NonAdmin(t *testing.T) {
userToken := jwt.GenerateUserToken("user-123")
newGroup := ExceptionalGroup{
Name: "Summer Hours",
Description: "Extended summer schedule",
@@ -345,18 +345,12 @@ func TestScheduling_DeleteExceptionalGroup_Admin(t *testing.T) {
}
handler := http.HandlerFunc(DeleteExceptionalGroup)
req := httptest.NewRequest("DELETE", "/api/scheduling/exceptional-groups?id="+string(rune(groupID+'0')), nil)
// Use proper URL query with strconv.Itoa
req := httptest.NewRequest("DELETE", "/api/scheduling/exceptional-groups?id="+strconv.Itoa(groupID), nil)
req.Header.Set("Authorization", "Bearer "+adminToken)
w := httptest.NewRecorder()
handler.ServeHTTP(w, req)
// The handler expects id as query param but as a proper int
// Let's use proper URL query
req = httptest.NewRequest("DELETE", "/api/scheduling/exceptional-groups?id="+strconv.Itoa(groupID), nil)
req.Header.Set("Authorization", "Bearer "+adminToken)
w = httptest.NewRecorder()
handler.ServeHTTP(w, req)
if w.Code != http.StatusNoContent {
t.Errorf("expected status 204, got %d. body: %s", w.Code, w.Body.String())
}
@@ -520,5 +514,3 @@ func TestScheduling_UpdateExceptionalApplications_NonAdmin(t *testing.T) {
t.Errorf("expected status 403, got %d. body: %s", w.Code, w.Body.String())
}
}