Files
Agent-System/internal/server/freeform_create_authz_test.go
weichen a9c9a9182e refactor(server): remove 24 mirrored Runtime fields via nil-safe accessors; fix Shutdown-vs-background-loop DATA RACE and goose 00071 duplicate version
- Server no longer mirrors app.Runtime services (gov/svc/pwSvc/...): 574 call sites now use accessors in runtime_accessors.go
- backgroundLoop (mutex+sync.Once) stops sweeper/publisher/reminder/wsGC goroutines race-free (baseline 44161d2a reproduced DATA RACE; now green under -race)
- goose 00071 duplicate (dialogue merge 1da17a28 brought two 00071): context-conflict-adjudications renumbered to 00072; dev DB applied only through 00070 so zero data migration
- task_finalize tests build minimal app.Runtime; test injection via serverTestOverrides + setOverride* (explicit-nil markers for fail-closed cases)
2026-09-25 13:17:22 +00:00

69 lines
2.0 KiB
Go

package server
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func postFreeformCreate(t *testing.T, srv *Server, token string, body map[string]any) *httptest.ResponseRecorder {
t.Helper()
raw, _ := json.Marshal(body)
req := httptest.NewRequest(http.MethodPost, "/api/v1/freeform-documents", bytes.NewReader(raw))
withBearerAuth(req, token)
w := httptest.NewRecorder()
srv.Routes().ServeHTTP(w, req)
return w
}
func TestCreateFreeformDoc_RejectsMissingProjectID(t *testing.T) {
srv := newTestServer(t)
token := loginTestAdmin(t, srv)
w := postFreeformCreate(t, srv, token, map[string]any{
"project_id": 0,
"title": "x",
"content_markdown": "# x\n",
})
if w.Code != http.StatusBadRequest {
t.Fatalf("want 400 got %d %s", w.Code, w.Body.String())
}
if !bytes.Contains(w.Body.Bytes(), []byte("project_id")) {
t.Fatalf("want project_id error, got %s", w.Body.String())
}
}
func TestCreateFreeformDoc_RejectsDirectProjectShared(t *testing.T) {
srv := newTestServer(t)
token := loginTestAdmin(t, srv)
w := postFreeformCreate(t, srv, token, map[string]any{
"project_id": 1,
"title": "x",
"content_markdown": "# x\n",
"space": "project_shared",
})
if w.Code != http.StatusBadRequest {
t.Fatalf("want 400 got %d %s", w.Code, w.Body.String())
}
if !bytes.Contains(w.Body.Bytes(), []byte("my_private")) {
t.Fatalf("want my_private-only error, got %s", w.Body.String())
}
}
func TestCreateFreeformDoc_FailClosedWhenAuthorizerNil(t *testing.T) {
srv := newTestServer(t)
srv.cfg.AllowInMemory = false
srv.setOverrideAuthorizer(nil)
token := loginTestAdmin(t, srv)
w := postFreeformCreate(t, srv, token, map[string]any{
"project_id": 1,
"title": "x",
"content_markdown": "# x\n",
"space": "my_private",
})
if w.Code != http.StatusForbidden && w.Code != http.StatusUnauthorized {
t.Fatalf("fail-closed authorizer=nil want 403/401 got %d %s", w.Code, w.Body.String())
}
}