package httpapi import ( "context" "net/http" "net/http/httptest" "testing" "github.com/alexedwards/scs/v2" "github.com/descrybe/descrybe-v2/apps/api/internal/auth" "github.com/descrybe/descrybe-v2/apps/api/internal/config" "github.com/descrybe/descrybe-v2/apps/api/internal/platformsettings" "github.com/google/uuid" ) // TestRouterAdminSettingsMounted locks GET /api/admin/settings after session + // platform-admin gates (503 with nil pool / nil service path, not chi 404). func TestRouterAdminSettingsMounted(t *testing.T) { t.Parallel() sm := scs.New() sm.Cookie.Name = "descrybe_session" uid := uuid.MustParse("aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa") s := &Server{ Config: config.Config{ CSRFCookieName: "descrybe_csrf", WebOrigin: "http://localhost:5173", }, Sessions: sm, Auth: &auth.Service{}, PlatformSettings: platformsettings.NewService(nil, platformsettings.EnvConfig{}), testPlatformAdmin: func(_ context.Context, got uuid.UUID) (bool, error) { return got == uid, nil }, } var token string seed := LoadSession(sm)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { sm.Put(r.Context(), auth.SessionUserIDKey, uid.String()) w.WriteHeader(http.StatusNoContent) })) seedRec := httptest.NewRecorder() seed.ServeHTTP(seedRec, httptest.NewRequest(http.MethodGet, "/seed", nil)) for _, c := range seedRec.Result().Cookies() { if c.Name == sm.Cookie.Name { token = c.Value } } if token == "" { t.Fatal("expected session cookie from seed request") } h := s.Router() unauth := httptest.NewRecorder() h.ServeHTTP(unauth, httptest.NewRequest(http.MethodGet, "/api/admin/settings", nil)) if unauth.Code != http.StatusUnauthorized { t.Fatalf("unauth status=%d want 401 body=%s", unauth.Code, unauth.Body.String()) } mounted := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/api/admin/settings", nil) req.AddCookie(&http.Cookie{Name: sm.Cookie.Name, Value: token}) h.ServeHTTP(mounted, req) if mounted.Code == http.StatusNotFound { t.Fatalf("settings not mounted: status=404 body=%s", mounted.Body.String()) } if mounted.Code != http.StatusOK { t.Fatalf("mounted status=%d want 200 body=%s", mounted.Code, mounted.Body.String()) } }