package plan3test import ( "context" "encoding/json" "fmt" "net/http/httptest" "strings" "testing" "time" "amadan.net/oficina/equipo/internal/equipo" "amadan.net/oficina/equipo/internal/equipo/personapi" ) func TestPersonAPIBrowserCSRF(t *testing.T) { f := newFixture(t) apiRouter(t, f, func(_ context.Context, token string) (personapi.PersonPrincipal, error) { if token != "native-fixture" { return personapi.PersonPrincipal{}, equipo.ErrForbidden } return personapi.PersonPrincipal{Address: owner.Address, CredentialID: "native-fixture", SessionEpoch: time.Now().Unix()}, nil }) for _, tc := range []struct { name, auth, site string cookie bool status int }{ {"native", "Bearer native-fixture", "", false, 200}, {"cookie", "", origin, true, 200}, {"cross-cookie", "", "https://evil.example", true, 403}, {"missing", "", origin, false, 401}, {"invalid", "Bearer invalid", origin, false, 401}, {"mixed", "Bearer native-fixture", origin, true, 401}, {"cross-native", "Bearer native-fixture", "https://evil.example", false, 403}, } { t.Run(tc.name, func(t *testing.T) { before := f.count(t, "SELECT count(*) FROM conversation_messages") r := httptest.NewRequest("POST", "/api/v1/conversation/messages", strings.NewReader(`{"body":"`+tc.name+`"}`)) r.Header.Set("Content-Type", "application/json") r.Header.Set("Idempotency-Key", tc.name) if tc.auth != "" { r.Header.Set("Authorization", tc.auth) } if tc.site != "" { r.Header.Set("Origin", tc.site) } if tc.cookie { r.AddCookie(f.Cookies[owner.Address]) } w := httptest.NewRecorder() f.Handler.ServeHTTP(w, r) expectHTTP(t, w, tc.status) after := f.count(t, "SELECT count(*) FROM conversation_messages") if tc.status != 200 && before != after { t.Fatal("rejected request wrote data") } }) } r := httptest.NewRequest("POST", "/conversation", strings.NewReader("body=bad&request_id=bad")) r.Header.Set("Authorization", "Bearer native-fixture") r.Header.Set("Origin", origin) w := httptest.NewRecorder() f.Handler.ServeHTTP(w, r) if w.Code < 400 { t.Fatal("bearer on cookie-only route accepted") } } func TestPersonAPIRevisionReplay(t *testing.T) { f := newFixture(t) path := "/api/v1/me/settings" expectHTTP(t, apiRequest(t, f, owner, "PATCH", path, `{"landing":"work"}`, "settings", ""), 428) w := apiRequest(t, f, owner, "PATCH", path, `{"landing":"work"}`, "settings", `"1"`) expectHTTP(t, w, 200) replay := apiRequest(t, f, owner, "PATCH", path, `{"landing":"work"}`, "settings", `"1"`) expectHTTP(t, replay, 200) if w.Body.String() != replay.Body.String() { t.Fatal("replay changed result") } expectHTTP(t, apiRequest(t, f, owner, "PATCH", path, `{"landing":"conversation"}`, "settings", `"1"`), 409) expectHTTP(t, apiRequest(t, f, owner, "PATCH", path, `{"landing":"conversation"}`, "new", `"1"`), 409) } func TestPersonAPIConversationParity(t *testing.T) { f := newFixture(t) p := proposed(t, f) _, err := f.Store.SendMessage(ctx, owner, "", "private text", equipo.Mutation{RequestID: "private"}) must(t, err) _, err = f.Store.SendMessage(ctx, owner, p.TaskID, "shared text", equipo.Mutation{RequestID: "shared"}) must(t, err) w := apiRequest(t, f, peer, "GET", "/api/v1/conversation", "", "", "") expectHTTP(t, w, 200) if strings.Contains(w.Body.String(), "private text") { t.Fatal("private conversation leaked") } w = apiRequest(t, f, peer, "GET", "/api/v1/tasks/"+p.TaskID+"/thread", "", "", "") expectHTTP(t, w, 200) if !strings.Contains(w.Body.String(), "shared text") { t.Fatal("shared thread absent") } w = apiRequest(t, f, owner, "GET", "/api/v1/conversation/cards", "", "", "") expectHTTP(t, w, 200) if !strings.Contains(w.Body.String(), p.TaskID) { t.Fatal("task card absent") } if w.Header().Get("Cache-Control") != "no-store" || w.Header().Get("ETag") == "" { t.Fatal("missing privacy/revision headers") } } func TestPersonAPIPeopleReadOnly(t *testing.T) { f := newFixture(t) w := apiRequest(t, f, peer, "GET", "/api/v1/people", "", "", "") expectHTTP(t, w, 200) var page struct{ Items []map[string]any } must(t, json.Unmarshal(w.Body.Bytes(), &page)) if len(page.Items) != 2 { t.Fatal("missing members", page) } for _, item := range page.Items { for k := range item { if k != "address" && k != "display_identity" && k != "active" && k != "role" { t.Fatal("unsafe people field", k) } } } for _, method := range []string{"POST", "PUT", "PATCH", "DELETE"} { expectHTTP(t, apiRequest(t, f, owner, method, "/api/v1/people", `{}`, "write", ""), 405) } f.exec(t, "UPDATE members SET active=0 WHERE address=?", peer.Address) expectHTTP(t, apiRequest(t, f, peer, "GET", "/api/v1/people", "", "", ""), 403) } func TestPersonAPIRequiredReads(t *testing.T) { f := newFixture(t) p := proposed(t, f) for _, path := range []string{"/me", "/capabilities", "/me/settings", "/people", "/conversation", "/conversation/cards", "/tasks", "/tasks/" + p.TaskID, "/tasks/" + p.TaskID + "/plan", "/tasks/" + p.TaskID + "/thread", "/journey", "/needs-you", "/preferences", "/coordinator"} { t.Run(path, func(t *testing.T) { w := apiRequest(t, f, owner, "GET", "/api/v1"+path, "", "", "") expectHTTP(t, w, 200) if w.Header().Get("ETag") == "" { t.Fatal("missing ETag", fmt.Sprint(w.Header())) } }) } } func TestPersonAPISessionRevocationAndReplayAuthority(t *testing.T) { f := newFixture(t) w := apiRequest(t, f, owner, "POST", "/api/v1/preferences", `{"text":"plain"}`, "preference", "") expectHTTP(t, w, 200) f.exec(t, "UPDATE members SET session_epoch=? WHERE address=?", time.Now().Add(time.Hour).Unix(), owner.Address) expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/preferences", `{"text":"plain"}`, "preference", ""), 403) expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/capabilities", "", "", ""), 403) f.exec(t, "UPDATE members SET session_epoch=0,active=0 WHERE address=?", owner.Address) expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/capabilities", "", "", ""), 403) }