| 1 | package plan3test |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/equipo" |
| 13 | "amadan.net/oficina/equipo/internal/equipo/personapi" |
| 14 | ) |
| 15 | |
| 16 | func TestPersonAPIBrowserCSRF(t *testing.T) { |
| 17 | f := newFixture(t) |
| 18 | apiRouter(t, f, func(_ context.Context, token string) (personapi.PersonPrincipal, error) { |
| 19 | if token != "native-fixture" { |
| 20 | return personapi.PersonPrincipal{}, equipo.ErrForbidden |
| 21 | } |
| 22 | return personapi.PersonPrincipal{Address: owner.Address, CredentialID: "native-fixture", SessionEpoch: time.Now().Unix()}, nil |
| 23 | }) |
| 24 | for _, tc := range []struct { |
| 25 | name, auth, site string |
| 26 | cookie bool |
| 27 | status int |
| 28 | }{ |
| 29 | {"native", "Bearer native-fixture", "", false, 200}, |
| 30 | {"cookie", "", origin, true, 200}, |
| 31 | {"cross-cookie", "", "https://evil.example", true, 403}, |
| 32 | {"missing", "", origin, false, 401}, |
| 33 | {"invalid", "Bearer invalid", origin, false, 401}, |
| 34 | {"mixed", "Bearer native-fixture", origin, true, 401}, |
| 35 | {"cross-native", "Bearer native-fixture", "https://evil.example", false, 403}, |
| 36 | } { |
| 37 | t.Run(tc.name, func(t *testing.T) { |
| 38 | before := f.count(t, "SELECT count(*) FROM conversation_messages") |
| 39 | r := httptest.NewRequest("POST", "/api/v1/conversation/messages", strings.NewReader(`{"body":"`+tc.name+`"}`)) |
| 40 | r.Header.Set("Content-Type", "application/json") |
| 41 | r.Header.Set("Idempotency-Key", tc.name) |
| 42 | if tc.auth != "" { |
| 43 | r.Header.Set("Authorization", tc.auth) |
| 44 | } |
| 45 | if tc.site != "" { |
| 46 | r.Header.Set("Origin", tc.site) |
| 47 | } |
| 48 | if tc.cookie { |
| 49 | r.AddCookie(f.Cookies[owner.Address]) |
| 50 | } |
| 51 | w := httptest.NewRecorder() |
| 52 | f.Handler.ServeHTTP(w, r) |
| 53 | expectHTTP(t, w, tc.status) |
| 54 | after := f.count(t, "SELECT count(*) FROM conversation_messages") |
| 55 | if tc.status != 200 && before != after { |
| 56 | t.Fatal("rejected request wrote data") |
| 57 | } |
| 58 | }) |
| 59 | } |
| 60 | r := httptest.NewRequest("POST", "/conversation", strings.NewReader("body=bad&request_id=bad")) |
| 61 | r.Header.Set("Authorization", "Bearer native-fixture") |
| 62 | r.Header.Set("Origin", origin) |
| 63 | w := httptest.NewRecorder() |
| 64 | f.Handler.ServeHTTP(w, r) |
| 65 | if w.Code < 400 { |
| 66 | t.Fatal("bearer on cookie-only route accepted") |
| 67 | } |
| 68 | } |
| 69 | func TestPersonAPIRevisionReplay(t *testing.T) { |
| 70 | f := newFixture(t) |
| 71 | path := "/api/v1/me/settings" |
| 72 | expectHTTP(t, apiRequest(t, f, owner, "PATCH", path, `{"landing":"work"}`, "settings", ""), 428) |
| 73 | w := apiRequest(t, f, owner, "PATCH", path, `{"landing":"work"}`, "settings", `"1"`) |
| 74 | expectHTTP(t, w, 200) |
| 75 | replay := apiRequest(t, f, owner, "PATCH", path, `{"landing":"work"}`, "settings", `"1"`) |
| 76 | expectHTTP(t, replay, 200) |
| 77 | if w.Body.String() != replay.Body.String() { |
| 78 | t.Fatal("replay changed result") |
| 79 | } |
| 80 | expectHTTP(t, apiRequest(t, f, owner, "PATCH", path, `{"landing":"conversation"}`, "settings", `"1"`), 409) |
| 81 | expectHTTP(t, apiRequest(t, f, owner, "PATCH", path, `{"landing":"conversation"}`, "new", `"1"`), 409) |
| 82 | } |
| 83 | func TestPersonAPIConversationParity(t *testing.T) { |
| 84 | f := newFixture(t) |
| 85 | p := proposed(t, f) |
| 86 | _, err := f.Store.SendMessage(ctx, owner, "", "private text", equipo.Mutation{RequestID: "private"}) |
| 87 | must(t, err) |
| 88 | _, err = f.Store.SendMessage(ctx, owner, p.TaskID, "shared text", equipo.Mutation{RequestID: "shared"}) |
| 89 | must(t, err) |
| 90 | w := apiRequest(t, f, peer, "GET", "/api/v1/conversation", "", "", "") |
| 91 | expectHTTP(t, w, 200) |
| 92 | if strings.Contains(w.Body.String(), "private text") { |
| 93 | t.Fatal("private conversation leaked") |
| 94 | } |
| 95 | w = apiRequest(t, f, peer, "GET", "/api/v1/tasks/"+p.TaskID+"/thread", "", "", "") |
| 96 | expectHTTP(t, w, 200) |
| 97 | if !strings.Contains(w.Body.String(), "shared text") { |
| 98 | t.Fatal("shared thread absent") |
| 99 | } |
| 100 | w = apiRequest(t, f, owner, "GET", "/api/v1/conversation/cards", "", "", "") |
| 101 | expectHTTP(t, w, 200) |
| 102 | if !strings.Contains(w.Body.String(), p.TaskID) { |
| 103 | t.Fatal("task card absent") |
| 104 | } |
| 105 | if w.Header().Get("Cache-Control") != "no-store" || w.Header().Get("ETag") == "" { |
| 106 | t.Fatal("missing privacy/revision headers") |
| 107 | } |
| 108 | } |
| 109 | func TestPersonAPIPeopleReadOnly(t *testing.T) { |
| 110 | f := newFixture(t) |
| 111 | w := apiRequest(t, f, peer, "GET", "/api/v1/people", "", "", "") |
| 112 | expectHTTP(t, w, 200) |
| 113 | var page struct{ Items []map[string]any } |
| 114 | must(t, json.Unmarshal(w.Body.Bytes(), &page)) |
| 115 | if len(page.Items) != 2 { |
| 116 | t.Fatal("missing members", page) |
| 117 | } |
| 118 | for _, item := range page.Items { |
| 119 | for k := range item { |
| 120 | if k != "address" && k != "display_identity" && k != "active" && k != "role" { |
| 121 | t.Fatal("unsafe people field", k) |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | for _, method := range []string{"POST", "PUT", "PATCH", "DELETE"} { |
| 126 | expectHTTP(t, apiRequest(t, f, owner, method, "/api/v1/people", `{}`, "write", ""), 405) |
| 127 | } |
| 128 | f.exec(t, "UPDATE members SET active=0 WHERE address=?", peer.Address) |
| 129 | expectHTTP(t, apiRequest(t, f, peer, "GET", "/api/v1/people", "", "", ""), 403) |
| 130 | } |
| 131 | func TestPersonAPIRequiredReads(t *testing.T) { |
| 132 | f := newFixture(t) |
| 133 | p := proposed(t, f) |
| 134 | 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"} { |
| 135 | t.Run(path, func(t *testing.T) { |
| 136 | w := apiRequest(t, f, owner, "GET", "/api/v1"+path, "", "", "") |
| 137 | expectHTTP(t, w, 200) |
| 138 | if w.Header().Get("ETag") == "" { |
| 139 | t.Fatal("missing ETag", fmt.Sprint(w.Header())) |
| 140 | } |
| 141 | }) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | func TestPersonAPISessionRevocationAndReplayAuthority(t *testing.T) { |
| 146 | f := newFixture(t) |
| 147 | w := apiRequest(t, f, owner, "POST", "/api/v1/preferences", `{"text":"plain"}`, "preference", "") |
| 148 | expectHTTP(t, w, 200) |
| 149 | f.exec(t, "UPDATE members SET session_epoch=? WHERE address=?", time.Now().Add(time.Hour).Unix(), owner.Address) |
| 150 | expectHTTP(t, apiRequest(t, f, owner, "POST", "/api/v1/preferences", `{"text":"plain"}`, "preference", ""), 403) |
| 151 | expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/capabilities", "", "", ""), 403) |
| 152 | f.exec(t, "UPDATE members SET session_epoch=0,active=0 WHERE address=?", owner.Address) |
| 153 | expectHTTP(t, apiRequest(t, f, owner, "GET", "/api/v1/capabilities", "", "", ""), 403) |
| 154 | } |
| 155 | |