Add crussell_test database creation and schema seeding to dev script

- Create crussell_test database after PostgreSQL reset
- Seed test DB schema from init-script.sql so tests can run
- This fixes the TLS connection errors in test runs

Also:
- Fixed color variables in script (C_RESET, C_GREEN, etc.)
This commit is contained in:
2026-02-22 00:06:03 +00:00
parent 44cac94f64
commit 9ca102153b
4 changed files with 28 additions and 7 deletions
@@ -23,6 +23,9 @@ func setupTestDB(t *testing.T) func() {
pool := testdb.Pool(t) pool := testdb.Pool(t)
testdb.Migrate(t, pool) testdb.Migrate(t, pool)
// Truncate tables to ensure clean state
testdb.TruncateTables(t, pool)
originalDB := db.DB originalDB := db.DB
db.DB = pool db.DB = pool
@@ -85,7 +85,7 @@ func makeRequest(handler http.HandlerFunc, method, path string, body interface{}
return w return w
} }
func makeAuthRequest(handler http.HandlerFunc, method, path, token string, body interface{}) *httptest.ResponseRecorder { func makeAuthRequest(handler http.Handler, method, path, token string, body interface{}) *httptest.ResponseRecorder {
var req *http.Request var req *http.Request
if body != nil { if body != nil {
bodyBytes, _ := json.Marshal(body) bodyBytes, _ := json.Marshal(body)
@@ -197,7 +197,6 @@ func TestScheduling_UpdateDefaultHours_NonAdmin(t *testing.T) {
defer cleanup() defer cleanup()
userToken := jwt.GenerateUserToken("user-123") userToken := jwt.GenerateUserToken("user-123")
handler := http.HandlerFunc(UpdateDefaultHours)
newHours := []DefaultHours{ newHours := []DefaultHours{
{Weekday: 0, StartTime: "08:00", EndTime: "18:00", IsOpen: true}, {Weekday: 0, StartTime: "08:00", EndTime: "18:00", IsOpen: true},
@@ -210,7 +209,7 @@ func TestScheduling_UpdateDefaultHours_NonAdmin(t *testing.T) {
} }
// Wrap handler with RequireAdmin middleware // Wrap handler with RequireAdmin middleware
w := makeAuthRequest(mw.RequireAdmin(UpdateDefaultHours), "PUT", "/api/scheduling/default-hours", userToken, newHours) w := makeAuthRequest(mw.RequireAdmin(http.HandlerFunc(UpdateDefaultHours)), "PUT", "/api/scheduling/default-hours", userToken, newHours)
if w.Code != http.StatusForbidden { if w.Code != http.StatusForbidden {
t.Errorf("expected status 403, got %d. body: %s", w.Code, w.Body.String()) t.Errorf("expected status 403, got %d. body: %s", w.Code, w.Body.String())
@@ -318,7 +317,7 @@ func TestScheduling_CreateExceptionalGroup_NonAdmin(t *testing.T) {
WeekStarts: []string{"2026-06-01"}, WeekStarts: []string{"2026-06-01"},
} }
w := makeAuthRequest(mw.RequireAdmin(CreateExceptionalGroup), "POST", "/api/scheduling/exceptional-groups", userToken, newGroup) w := makeAuthRequest(mw.RequireAdmin(http.HandlerFunc(CreateExceptionalGroup)), "POST", "/api/scheduling/exceptional-groups", userToken, newGroup)
if w.Code != http.StatusForbidden { if w.Code != http.StatusForbidden {
t.Errorf("expected status 403, got %d. body: %s", w.Code, w.Body.String()) t.Errorf("expected status 403, got %d. body: %s", w.Code, w.Body.String())
@@ -378,7 +377,7 @@ func TestScheduling_DeleteExceptionalGroup_NonAdmin(t *testing.T) {
userToken := jwt.GenerateUserToken("user-123") userToken := jwt.GenerateUserToken("user-123")
handler := mw.RequireAdmin(DeleteExceptionalGroup) handler := mw.RequireAdmin(http.HandlerFunc(DeleteExceptionalGroup))
req := httptest.NewRequest("DELETE", "/api/scheduling/exceptional-groups?id=1", nil) req := httptest.NewRequest("DELETE", "/api/scheduling/exceptional-groups?id=1", nil)
req.Header.Set("Authorization", "Bearer "+userToken) req.Header.Set("Authorization", "Bearer "+userToken)
w := httptest.NewRecorder() w := httptest.NewRecorder()
@@ -507,7 +506,7 @@ func TestScheduling_UpdateExceptionalApplications_NonAdmin(t *testing.T) {
defer cleanup() defer cleanup()
userToken := jwt.GenerateUserToken("user-123") userToken := jwt.GenerateUserToken("user-123")
handler := mw.RequireAdmin(UpdateExceptionalApplications) handler := mw.RequireAdmin(http.HandlerFunc(UpdateExceptionalApplications))
reqBody := map[string]interface{}{ reqBody := map[string]interface{}{
"groupId": 1, "groupId": 1,
+2
View File
@@ -132,6 +132,8 @@ func TruncateTables(t *testing.T, pool *pgxpool.Pool) {
"exceptional_working_hours", "exceptional_working_hours",
"exceptional_working_hours_groups", "exceptional_working_hours_groups",
"users", "users",
"images",
"tags",
} }
for _, table := range tables { for _, table := range tables {
+18 -1
View File
@@ -14,6 +14,12 @@ C_GREEN=$'\033[32m'
C_RED=$'\033[31m' C_RED=$'\033[31m'
C_BLUE=$'\033[34m' C_BLUE=$'\033[34m'
C_YELLOW=$'\033[33m' C_YELLOW=$'\033[33m'
# Color codes for output
C_RESET=$'\033[0m'
C_GREEN=$'\033[32m'
C_RED=$'\033[31m'
C_BLUE=$'\033[34m'
C_YELLOW=$'\033[33m'
log_info() { echo "🔹 $1" } log_info() { echo "🔹 $1" }
log_success() { echo "$1" } log_success() { echo "$1" }
log_error() { echo "$1" } log_error() { echo "$1" }
@@ -49,7 +55,18 @@ docker compose up postgres -d > /dev/null 2>&1
log_success "PostgreSQL reset complete" log_success "PostgreSQL reset complete"
sleep 3 sleep 3
# --- 3b. Rustfs (don't wipe data, just restart) --- # --- 3b. Create Test Database ---
log_step "Setting up test database (crussell_test)..."
docker exec postgres psql -U myuser -d mydb -c "CREATE DATABASE crussell_test;" 2>/dev/null || true
log_success "Test database ready"
sleep 1
# --- 3c. Seed Test Database Schema ---
log_step "Seeding test database schema..."
docker exec -i postgres psql -U myuser -d crussell_test < init-scripts/init-script.sql > /dev/null 2>&1 || true
log_success "Test database schema ready"
# --- 3d. Rustfs (don't wipe data, just restart) ---
log_step "Restarting Rustfs (S3-compatible storage)..." log_step "Restarting Rustfs (S3-compatible storage)..."
docker compose restart rustfs > /dev/null 2>&1 docker compose restart rustfs > /dev/null 2>&1
log_success "Rustfs restarted" log_success "Rustfs restarted"