Files
popertots 3029fd5179
CI / Nginx config check (push) Successful in 13s
CI / Env docs check (push) Successful in 15s
CI / Docker compose check (push) Successful in 15s
CI / Frontend major deps (push) Failing after 24s
CI / Frontend deps check (push) Successful in 30s
CI / Secrets scan (push) Successful in 38s
CI / Go build (push) Successful in 39s
CI / Frontend build (push) Successful in 1m3s
CI / Knip (push) Successful in 45s
CI / Go vet (prod) (push) Failing after 1m42s
CI / Frontend a11y check (push) Successful in 2m34s
CI / Go vet (dev) (push) Successful in 2m29s
CI / Staticcheck (prod) (push) Failing after 2m38s
CI / go mod tidy (push) Successful in 1m3s
CI / Staticcheck (dev) (push) Successful in 2m55s
CI / Frontend QC (audit) (push) Successful in 51s
CI / golangci-lint (push) Successful in 3m22s
CI / Go vulnerabilities (push) Successful in 1m26s
CI / Frontend QC (typecheck) (push) Successful in 2m18s
CI / Security scan (prod) (push) Successful in 4m18s
CI / Security scan (dev) (push) Successful in 4m40s
CI / Tests (prod) (push) Has been skipped
CI / Tests (dev) (push) Has been skipped
CI / Race (prod) (push) Has been skipped
CI / Race (dev) (push) Has been skipped
CI / Frontend QC (lint) (push) Successful in 2m18s
CI / Svelte strict check (push) Successful in 43s
test: add coverage tests across backend + fix mock for PENDING checkout support
New test files cover previously untested paths across DAV, validators,
S3, Square, mw, bookings, user, and payments packages.

Includes mock fix: HoldCheckouts flag on MockClient allows tests to
pause auto-complete goroutine for testing PENDING checkout states.

Coverage: 50.4% → 65.0% (+14.6pp)
2026-07-10 18:13:44 +01:00

334 lines
10 KiB
Go

//go:build test
package admin
import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"
"crussell/testutils"
"crussell/testutils/fixtures"
"github.com/go-chi/chi/v5"
)
func TestPatchTests_CRUD(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
// Create a service to link
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
req := CreatePatchTestRequest{
Name: "Test Patch Test",
Description: strPtr("Description"),
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", req, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("expected 201, got %d", w.Code)
}
var created struct {
ID string `json:"id"`
}
parseResponseBody(w, &created)
w = makeAdminRequest(http.HandlerFunc(GetPatchTests), "GET", "/api/admin/patch-tests", nil, ctx)
if w.Code != http.StatusOK {
t.Fatalf("expected 200, got %d", w.Code)
}
var list []PatchTest
parseResponseBody(w, &list)
if len(list) != 1 {
t.Errorf("expected 1 patch test, got %d", len(list))
}
newName := "Updated Name"
updateReq := UpdatePatchTestRequest{Name: &newName}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d, body: %s", w.Code, w.Body.String())
}
w = makeAdminRequest(http.HandlerFunc(DeletePatchTest), "DELETE", "/api/admin/patch-tests/"+created.ID, nil, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d", w.Code)
}
}
func strPtr(s string) *string {
return &s
}
// =============================================================================
// Create Patch Test - Validation Tests
// =============================================================================
func TestCreatePatchTest_ValidationErrors(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
handler := http.HandlerFunc(CreatePatchTest)
t.Run("empty_name", func(t *testing.T) {
req := CreatePatchTestRequest{
Name: "",
NoticeDurationHours: 24,
ExpiryMonths: 6,
}
w := makeAdminRequest(handler, "POST", "/api/admin/patch-tests", req, ctx)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400 for empty name, got %d. body: %s", w.Code, w.Body.String())
}
})
_, err := tx.Exec(ctx, `SELECT 1`)
if err != nil {
t.Fatalf("tx check failed: %v", err)
}
}
// =============================================================================
// Update Patch Test - Error Tests
// =============================================================================
func TestUpdatePatchTest_NotFound(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
newName := "Updated"
updateReq := UpdatePatchTestRequest{Name: &newName}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/nonexistent-id", updateReq, ctx)
if w.Code != http.StatusNotFound {
t.Errorf("expected 404 for nonexistent patch test, got %d. body: %s", w.Code, w.Body.String())
}
}
// =============================================================================
// Delete Patch Test - Error Tests
// =============================================================================
func TestDeletePatchTest_NotFound(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
w = makeAdminRequest(http.HandlerFunc(DeletePatchTest), "DELETE", "/api/admin/patch-tests/nonexistent-id", nil, ctx)
if w.Code != http.StatusNotFound {
t.Errorf("expected 404 for nonexistent patch test, got %d. body: %s", w.Code, w.Body.String())
}
}
// =============================================================================
// Update Patch Test - Individual Field Update Tests
// =============================================================================
func TestUpdatePatchTest_UpdateDescription(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
var created struct {
ID string `json:"id"`
}
parseResponseBody(w, &created)
updateReq := UpdatePatchTestRequest{Description: strPtr("Updated Description")}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d, body: %s", w.Code, w.Body.String())
}
}
func TestUpdatePatchTest_UpdateNoticeDuration(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
var created struct {
ID string `json:"id"`
}
parseResponseBody(w, &created)
noticeDuration := 48
updateReq := UpdatePatchTestRequest{NoticeDurationHours: &noticeDuration}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d, body: %s", w.Code, w.Body.String())
}
}
func TestUpdatePatchTest_UpdateExpiryMonths(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
var created struct {
ID string `json:"id"`
}
parseResponseBody(w, &created)
expiryMonths := 12
updateReq := UpdatePatchTestRequest{ExpiryMonths: &expiryMonths}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d, body: %s", w.Code, w.Body.String())
}
}
func TestUpdatePatchTest_UpdateServiceIDs(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
var created struct {
ID string `json:"id"`
}
parseResponseBody(w, &created)
updateReq := UpdatePatchTestRequest{ServiceIDs: []string{serviceID}}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusNoContent {
t.Fatalf("expected 204, got %d, body: %s", w.Code, w.Body.String())
}
}
func TestUpdatePatchTest_InvalidJSON(t *testing.T) {
ctx, _ := testutils.SetupTestTx(t)
req := httptest.NewRequest("PUT", "/api/admin/patch-tests/abcdef123456", bytes.NewReader([]byte(`{invalid}`)))
req.Header.Set("Content-Type", "application/json")
rctx := chi.NewRouteContext()
rctx.URLParams.Add("id", "abcdef123456")
ctx = context.WithValue(ctx, chi.RouteCtxKey, rctx)
req = req.WithContext(ctx)
w := httptest.NewRecorder()
http.HandlerFunc(UpdatePatchTest).ServeHTTP(w, req)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400 for invalid JSON, got %d. body: %s", w.Code, w.Body.String())
}
}
func TestUpdatePatchTest_ValidationError(t *testing.T) {
ctx, tx := testutils.SetupTestTx(t)
serviceID, err := fixtures.CreateTestService(tx)
if err != nil {
t.Fatalf("failed to create service: %v", err)
}
createReq := CreatePatchTestRequest{
Name: "Test",
NoticeDurationHours: 24,
ExpiryMonths: 6,
ServiceIDs: []string{serviceID},
}
w := makeAdminRequest(http.HandlerFunc(CreatePatchTest), "POST", "/api/admin/patch-tests", createReq, ctx)
if w.Code != http.StatusCreated {
t.Fatalf("failed to create patch test: %d", w.Code)
}
var created struct {
ID string `json:"id"`
}
parseResponseBody(w, &created)
var longName string
for i := 0; i < 201; i++ {
longName += "a"
}
updateReq := UpdatePatchTestRequest{Name: &longName}
w = makeAdminRequest(http.HandlerFunc(UpdatePatchTest), "PUT", "/api/admin/patch-tests/"+created.ID, updateReq, ctx)
if w.Code != http.StatusBadRequest {
t.Errorf("expected 400 for validation error, got %d. body: %s", w.Code, w.Body.String())
}
}