| 1 | package slides |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "net/http/httptest" |
| 9 | "net/url" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | |
| 14 | "amadan.net/oficina/home/dashboard" |
| 15 | ) |
| 16 | |
| 17 | // probe answers GET /api/team through the whole router, unauthenticated. |
| 18 | func probe(t *testing.T, mux *http.ServeMux) (*httptest.ResponseRecorder, map[string]any) { |
| 19 | t.Helper() |
| 20 | w := httptest.NewRecorder() |
| 21 | mux.ServeHTTP(w, httptest.NewRequest("GET", homeTestOrigin+"/api/team", nil)) |
| 22 | var body map[string]any |
| 23 | if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil { |
| 24 | t.Fatalf("probe body %q: %v", w.Body.String(), err) |
| 25 | } |
| 26 | return w, body |
| 27 | } |
| 28 | |
| 29 | // Team mode off: the probe says only that. |
| 30 | func TestTeamProbeOffSaysOnlyThat(t *testing.T) { |
| 31 | mux, _, _ := homeFixture(t) |
| 32 | w, body := probe(t, mux) |
| 33 | if w.Code != 200 || w.Header().Get("Cache-Control") != "no-store" || len(body) != 1 || body["team_mode"] != false { |
| 34 | t.Fatalf("probe off = %d %v %v", w.Code, w.Header(), body) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Team mode on: unknown before any roster, ok once one is held, stale |
| 39 | // once it ages out — the same body whoever asks, and no account data. |
| 40 | func TestTeamProbeStatesAndNoAccountData(t *testing.T) { |
| 41 | f := newTeamFixture(t) |
| 42 | f.dir.SetDown(true) |
| 43 | _, body := probe(t, f.mux) |
| 44 | if body["roster"] != "unknown" || body["team_mode"] != true || body["consumer"] != teamTestConsumer || len(body) != 3 { |
| 45 | t.Fatalf("probe before any roster = %v", body) |
| 46 | } |
| 47 | f.dir.SetDown(false) |
| 48 | f.clock.Step(10 * time.Second) |
| 49 | f.dir.Set(teamSubject('a'), "member") |
| 50 | if _, body = probe(t, f.mux); body["roster"] != "ok" { |
| 51 | t.Fatalf("probe with a roster = %v", body) |
| 52 | } |
| 53 | f.member(teamSubject('a'), "member") |
| 54 | _, signedOut := probe(t, f.mux) |
| 55 | w := f.do("GET", "/api/team", "", "", "") |
| 56 | if w.Body.String() == "" || !strings.Contains(w.Body.String(), `"roster":"ok"`) || strings.Contains(w.Body.String(), teamSubject('a')) || strings.Contains(w.Body.String(), "TeamA") { |
| 57 | t.Fatalf("probe signed in = %s", w.Body.String()) |
| 58 | } |
| 59 | if len(signedOut) != 3 { |
| 60 | t.Fatalf("probe body = %v", signedOut) |
| 61 | } |
| 62 | f.expired() |
| 63 | if _, body = probe(t, f.mux); body["roster"] != "stale" { |
| 64 | t.Fatalf("probe aged out = %v", body) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Paul's ruling on #33 (2026-10-30): on an orphan — a team-rung deck |
| 69 | // whose owner the roster says is gone — a roster owner may share it |
| 70 | // (the share page, minting and revoking links) and change its title and |
| 71 | // theme; may not delete it, nor narrow its rung (private hides it from |
| 72 | // everybody: a soft-delete). A member may do none of it, a live owner's |
| 73 | // deck is not an orphan, nor is a departed member's private deck, even |
| 74 | // one the roster owner holds a read grant on. |
| 75 | func TestTeamRosterOwnerSharesAndRetitlesOrphansOnly(t *testing.T) { |
| 76 | f := newTeamFixture(t) |
| 77 | departed := f.member(teamSubject('d'), "member") |
| 78 | orphan := f.deck(departed, "Departed's team deck", rungTeam) |
| 79 | private := f.deck(departed, "Departed's private deck", rungPrivate) |
| 80 | live := f.member(teamSubject('l'), "member") |
| 81 | kept := f.deck(live, "Live owner's deck", rungTeam) |
| 82 | f.member(teamSubject('m'), "member") |
| 83 | rosterOwner := f.member(teamSubject('o'), "owner") |
| 84 | f.grant(private, rosterOwner, departed) |
| 85 | f.dir.Set(teamSubject('d'), "") |
| 86 | f.refreshed() |
| 87 | |
| 88 | // Share: the page, a link minted and revoked. |
| 89 | if got := f.get(deckPath(orphan) + "/share"); got.Code != 200 { |
| 90 | t.Fatalf("roster owner's share page on an orphan = %d, want 200", got.Code) |
| 91 | } |
| 92 | if got := f.post(deckPath(orphan)+"/share", nil); got.Code != http.StatusSeeOther { |
| 93 | t.Fatalf("roster owner minting a link on an orphan = %d", got.Code) |
| 94 | } |
| 95 | var link ShareLink |
| 96 | if err := f.d.G.Where("deck_id = ?", orphan.ID).Take(&link).Error; err != nil { |
| 97 | t.Fatal("no link minted") |
| 98 | } |
| 99 | if got := f.post(fmt.Sprintf("%s/share/%d/revoke", deckPath(orphan), link.ID), nil); got.Code != http.StatusSeeOther { |
| 100 | t.Fatalf("roster owner revoking a link on an orphan = %d", got.Code) |
| 101 | } |
| 102 | // Title and theme: the settings page, with no rung and no delete. |
| 103 | page := f.get(deckPath(orphan) + "/settings") |
| 104 | if page.Code != 200 || strings.Contains(page.Body.String(), `name="rung"`) || strings.Contains(page.Body.String(), deckPath(orphan)+"/delete") { |
| 105 | t.Fatalf("roster owner's settings on an orphan = %d (rung or delete offered?)", page.Code) |
| 106 | } |
| 107 | // The viewer offers Share… in the bar, and never the settings form |
| 108 | // (the editor's inspector holds those, for everybody). |
| 109 | if got := f.get(viewerPath(orphan)); strings.Contains(got.Body.String(), deckPath(orphan)+"/settings") || !strings.Contains(got.Body.String(), deckPath(orphan)+"/share") { |
| 110 | t.Fatal("the roster owner's viewer of an orphan offers no share, or links the settings form") |
| 111 | } |
| 112 | // The share page shows the rung and offers no way to change it, and |
| 113 | // the access POST is refused like any other owner screen. |
| 114 | if got := f.get(deckPath(orphan) + "/share"); strings.Contains(got.Body.String(), `name="rung"`) || !strings.Contains(got.Body.String(), "<section slides-share-access") { |
| 115 | t.Fatalf("roster owner's share page on an orphan offers the rung:\n%s", got.Body.String()) |
| 116 | } |
| 117 | if got := f.post(deckPath(orphan)+"/share/access", url.Values{"rung": {rungPrivate}}); got.Code != 404 { |
| 118 | t.Fatalf("roster owner making an orphan private from the share page = %d, want 404", got.Code) |
| 119 | } |
| 120 | if got := f.post(deckPath(orphan)+"/settings", url.Values{"title": {"Rescued"}, "theme": {"ink"}}); got.Code != http.StatusSeeOther { |
| 121 | t.Fatalf("roster owner retitling an orphan = %d %s", got.Code, got.Body.String()) |
| 122 | } |
| 123 | var row Deck |
| 124 | f.d.G.Take(&row, orphan.ID) |
| 125 | if row.Title != "Rescued" || row.Theme != "ink" || row.Rung != rungTeam { |
| 126 | t.Fatalf("orphan after retitling = %q %q %q", row.Title, row.Theme, row.Rung) |
| 127 | } |
| 128 | // Not the rung, not delete. |
| 129 | if got := f.post(deckPath(orphan)+"/settings", url.Values{"title": {"Hidden"}, "theme": {"ink"}, "rung": {rungPrivate}}); got.Code != http.StatusForbidden { |
| 130 | t.Fatalf("roster owner making an orphan private = %d, want 403", got.Code) |
| 131 | } |
| 132 | if got := f.get(deckPath(orphan) + "/delete"); got.Code != 404 { |
| 133 | t.Fatalf("roster owner's delete page on an orphan = %d, want 404", got.Code) |
| 134 | } |
| 135 | if got := f.post(deckPath(orphan)+"/delete", nil); got.Code != 404 { |
| 136 | t.Fatalf("roster owner deleting an orphan = %d, want 404", got.Code) |
| 137 | } |
| 138 | f.d.G.Take(&row, orphan.ID) |
| 139 | if row.Rung != rungTeam || row.Title != "Rescued" { |
| 140 | t.Fatalf("a refused narrowing changed the orphan: %q %q", row.Title, row.Rung) |
| 141 | } |
| 142 | // A departed member's private deck, readable by grant: no owner powers. |
| 143 | if got := f.get(viewerPath(private)); got.Code != 200 || strings.Contains(got.Body.String(), deckPath(private)+"/share") { |
| 144 | t.Fatalf("roster owner's granted private orphan = %d (owner mode?)", got.Code) |
| 145 | } |
| 146 | for _, p := range []string{"/settings", "/share", "/delete"} { |
| 147 | if got := f.get(deckPath(private) + p); got.Code != 404 { |
| 148 | t.Fatalf("roster owner's %s on a private orphan = %d, want 404", p, got.Code) |
| 149 | } |
| 150 | } |
| 151 | if got := f.get(deckPath(kept) + "/settings"); got.Code != 404 { |
| 152 | t.Fatalf("roster owner's settings on a live owner's deck = %d, want 404", got.Code) |
| 153 | } |
| 154 | |
| 155 | // A member is no owner of anything but their own. |
| 156 | f.freshHomeSignIn(teamSubject('m')) |
| 157 | for _, p := range []string{"/settings", "/share"} { |
| 158 | if got := f.get(deckPath(orphan) + p); got.Code != 404 { |
| 159 | t.Fatalf("a member's %s on an orphan = %d, want 404", p, got.Code) |
| 160 | } |
| 161 | } |
| 162 | if got := f.post(deckPath(orphan)+"/share/access", url.Values{"rung": {rungPrivate}}); got.Code != 404 { |
| 163 | t.Fatalf("a member's rung change on an orphan = %d, want 404", got.Code) |
| 164 | } |
| 165 | |
| 166 | // The departed owner re-added: the deck has a live owner again. |
| 167 | f.dir.Set(teamSubject('d'), "member") |
| 168 | f.refreshed() |
| 169 | f.freshHomeSignIn(teamSubject('o')) |
| 170 | if got := f.get(deckPath(orphan) + "/share"); got.Code != 404 { |
| 171 | t.Fatalf("roster owner after the owner's return = %d, want 404", got.Code) |
| 172 | } |
| 173 | |
| 174 | // And the owner themselves still has every power, delete included. |
| 175 | f.freshHomeSignIn(teamSubject('d')) |
| 176 | if got := f.get(deckPath(orphan) + "/share"); !strings.Contains(got.Body.String(), `name="rung"`) { |
| 177 | t.Fatalf("the owner's share page offers no rung:\n%s", got.Body.String()) |
| 178 | } |
| 179 | if got := f.post(deckPath(orphan)+"/share/access", url.Values{"rung": {rungPrivate}}); got.Code != http.StatusSeeOther { |
| 180 | t.Fatalf("the owner's own rung change = %d", got.Code) |
| 181 | } |
| 182 | f.d.G.Take(&row, orphan.ID) |
| 183 | if row.Rung != rungPrivate { |
| 184 | t.Fatalf("the owner's rung change did not land: %q", row.Rung) |
| 185 | } |
| 186 | if got := f.post(deckPath(orphan)+"/settings", url.Values{"title": {"Mine"}, "theme": {"ink"}, "rung": {rungTeam}}); got.Code != http.StatusSeeOther { |
| 187 | t.Fatalf("the owner's own rung change through the settings POST = %d", got.Code) |
| 188 | } |
| 189 | if got := f.post(deckPath(orphan)+"/delete", nil); got.Code != http.StatusSeeOther { |
| 190 | t.Fatalf("the owner's own delete = %d", got.Code) |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // The dashboard feed follows the roster: a member's team decks, a removed |
| 195 | // member's grants alone, and under Unknown unavailable — not a short list. |
| 196 | func TestTeamDashboardFeedFollowsTheRoster(t *testing.T) { |
| 197 | f := newTeamFixture(t) |
| 198 | colleague := f.member(teamSubject('b'), "member") |
| 199 | f.deck(colleague, "Team plan", rungTeam) |
| 200 | shared := f.deck(colleague, "Shared", rungPrivate) |
| 201 | u := f.member(teamSubject('a'), "member") |
| 202 | f.grant(shared, u, colleague) |
| 203 | ctx := context.Background() |
| 204 | titles := func(r dashboard.Response) string { |
| 205 | var out []string |
| 206 | for _, it := range r.Items { |
| 207 | out = append(out, it.Title) |
| 208 | } |
| 209 | return strings.Join(out, ",") |
| 210 | } |
| 211 | got, err := f.a.dashboardDocuments(ctx, u.ID, teamSubject('a'), time.Now()) |
| 212 | if err != nil || !strings.Contains(titles(got), "Team plan") || !strings.Contains(titles(got), "Shared") { |
| 213 | t.Fatalf("member's feed = %v %v", titles(got), err) |
| 214 | } |
| 215 | f.dir.Set(teamSubject('a'), "") |
| 216 | f.refreshed() |
| 217 | got, err = f.a.dashboardDocuments(ctx, u.ID, teamSubject('a'), time.Now()) |
| 218 | if err != nil || titles(got) != "Shared" { |
| 219 | t.Fatalf("removed member's feed = %q %v, want the grant alone", titles(got), err) |
| 220 | } |
| 221 | f.dir.Set(teamSubject('a'), "member") |
| 222 | f.refreshed() |
| 223 | f.expired() |
| 224 | if got, err = f.a.dashboardDocuments(ctx, u.ID, teamSubject('a'), time.Now()); err != dashboard.ErrUnavailable || len(got.Items) != 0 { |
| 225 | t.Fatalf("Unknown feed = %v %v, want unavailable", titles(got), err) |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | // The drive API stands on its operator's standing: a member's agent |
| 230 | // lists and creates; under Unknown the JSON list is unavailable (it |
| 231 | // cannot carry the notice) and create is a 503; a removed operator's |
| 232 | // agent reaches their grants and creates nothing. |
| 233 | func TestTeamAgentStandsOnItsOperator(t *testing.T) { |
| 234 | f := newTeamFixture(t) |
| 235 | colleague := f.member(teamSubject('b'), "member") |
| 236 | f.deck(colleague, "Team plan", rungTeam) |
| 237 | shared := f.deck(colleague, "Shared", rungPrivate) |
| 238 | u := f.member(teamSubject('a'), "member") |
| 239 | f.grant(shared, u, colleague) |
| 240 | token := f.agent(u) |
| 241 | |
| 242 | list := f.api(token, "GET", apiPrefix+"/decks", "") |
| 243 | if list.Code != 200 || !strings.Contains(list.Body.String(), "Team plan") { |
| 244 | t.Fatalf("member's agent list = %d %s", list.Code, list.Body.String()) |
| 245 | } |
| 246 | if got := f.api(token, "POST", apiPrefix+"/decks", `{"title":"By agent"}`); got.Code != http.StatusCreated { |
| 247 | t.Fatalf("member's agent create = %d", got.Code) |
| 248 | } |
| 249 | |
| 250 | f.expired() |
| 251 | if got := f.api(token, "GET", apiPrefix+"/decks", ""); got.Code != http.StatusServiceUnavailable || got.Header().Get("Retry-After") == "" { |
| 252 | t.Fatalf("agent list under Unknown = %d, want a retryable 503", got.Code) |
| 253 | } |
| 254 | if got := f.api(token, "POST", apiPrefix+"/decks", `{"title":"x"}`); got.Code != http.StatusServiceUnavailable { |
| 255 | t.Fatalf("agent create under Unknown = %d, want 503", got.Code) |
| 256 | } |
| 257 | if got := f.api(token, "GET", apiPrefix+"/decks/"+shared.Sqid, ""); got.Code != 200 { |
| 258 | t.Fatalf("agent read of a grant under Unknown = %d", got.Code) |
| 259 | } |
| 260 | |
| 261 | f.dir.Set(teamSubject('a'), "") |
| 262 | f.renewed() |
| 263 | list = f.api(token, "GET", apiPrefix+"/decks", "") |
| 264 | if list.Code != 200 || strings.Contains(list.Body.String(), "Team plan") || !strings.Contains(list.Body.String(), "Shared") { |
| 265 | t.Fatalf("removed operator's agent list = %d %s, want the grant alone", list.Code, list.Body.String()) |
| 266 | } |
| 267 | if got := f.api(token, "POST", apiPrefix+"/decks", `{"title":"x"}`); got.Code != http.StatusForbidden { |
| 268 | t.Fatalf("removed operator's agent create = %d, want 403", got.Code) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // A Home-only person is named by their ref wherever an address would be: |
| 273 | // the deck list's last change, presence, and an agent's principal. |
| 274 | func TestTeamHomeOnlyPeopleAreNamedByTheirRef(t *testing.T) { |
| 275 | f := newTeamFixture(t) |
| 276 | u := f.member(teamSubject('a'), "member") |
| 277 | plan := f.deck(u, "Team plan", rungTeam) |
| 278 | want := homeOnlyLabel(teamSubject('a')) |
| 279 | if !strings.HasSuffix(want, "…aaaa") { |
| 280 | t.Fatalf("label = %q", want) |
| 281 | } |
| 282 | labels := f.a.actorLabels(context.Background(), []string{actorFor(u)}) |
| 283 | if labels[actorFor(u)] != want { |
| 284 | t.Fatalf("actor label = %q, want %q", labels[actorFor(u)], want) |
| 285 | } |
| 286 | if got := f.get("/"); !strings.Contains(got.Body.String(), want) { |
| 287 | t.Fatal("the deck list does not name the Home-only author") |
| 288 | } |
| 289 | if got := f.do("POST", deckPath(plan)+"/presence", `{"client":"c1","slide":"","block":""}`, "application/json", homeTestOrigin); got.Code != http.StatusNoContent { |
| 290 | t.Fatalf("presence = %d", got.Code) |
| 291 | } |
| 292 | set := f.a.live.Snapshot(plan.Sqid) |
| 293 | if len(set) != 1 || set[0].Name != want { |
| 294 | t.Fatalf("presence = %+v, want named %q", set, want) |
| 295 | } |
| 296 | f.agent(u) |
| 297 | if got := f.get("/agents"); !strings.Contains(got.Body.String(), "claude~"+want) { |
| 298 | t.Fatal("an agent's principal does not name its Home-only operator") |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | // A local account linked to a Home identity, on a team instance with no |
| 303 | // allowlist: the link names who they are, and the roster decides what |
| 304 | // they reach — neither needs SLIDES_ALLOWED_ADDRESSES. |
| 305 | func TestTeamLinkedAccountStandsOnTheRoster(t *testing.T) { |
| 306 | f := newTeamFixture(t) |
| 307 | colleague := f.member(teamSubject('b'), "member") |
| 308 | plan := f.deck(colleague, "Team plan", rungTeam) |
| 309 | linked := f.localUser("linked@example.com") |
| 310 | homeLinkedSubject(t, f.a, linked, teamSubject('k')) |
| 311 | f.dir.Set(teamSubject('k'), "member") |
| 312 | f.refreshed() |
| 313 | if got := f.freshHomeSignIn(teamSubject('k')); got.Code != http.StatusSeeOther { |
| 314 | t.Fatalf("linked member's Home sign-in = %d %s", got.Code, got.Body.String()) |
| 315 | } |
| 316 | if f.userFor(teamSubject('k')).ID != linked.ID { |
| 317 | t.Fatal("the linked account signed in as somebody else") |
| 318 | } |
| 319 | page := f.get(viewerPath(plan)) |
| 320 | if page.Code != 200 || !strings.Contains(page.Body.String(), "linked@example.com") { |
| 321 | t.Fatalf("linked member on a team deck = %d (signed in as the account?)", page.Code) |
| 322 | } |
| 323 | f.dir.Set(teamSubject('k'), "") |
| 324 | f.refreshed() |
| 325 | if got := f.get(viewerPath(plan)); got.Code != 404 { |
| 326 | t.Fatalf("linked account after removal = %d, want 404", got.Code) |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | // A Home-only member exports like anybody: their jobs are keyed on the |
| 331 | // user, since there is no address to key on, and so are their agents'. |
| 332 | func TestTeamHomeOnlyMemberExports(t *testing.T) { |
| 333 | f := newTeamFixture(t) |
| 334 | u := f.member(teamSubject('a'), "member") |
| 335 | plan := f.deck(u, "Team plan", rungTeam) |
| 336 | got := f.post(deckPath(plan)+"/export", nil) |
| 337 | if got.Code != http.StatusSeeOther || !strings.Contains(got.Header().Get("Location"), "/export/") { |
| 338 | t.Fatalf("Home-only member's export = %d %s", got.Code, got.Body.String()) |
| 339 | } |
| 340 | status := f.get(got.Header().Get("Location")) |
| 341 | if status.Code != 200 { |
| 342 | t.Fatalf("their export's status page = %d", status.Code) |
| 343 | } |
| 344 | token := f.agent(u) |
| 345 | if got := f.api(token, "POST", apiPrefix+"/decks/"+plan.Sqid+"/export", ""); got.Code != http.StatusAccepted { |
| 346 | t.Fatalf("their agent's export = %d %s", got.Code, got.Body.String()) |
| 347 | } |
| 348 | if owner := f.a.apiExportOwner(principal{operator: u}); owner != teamJobOwner(u.ID) { |
| 349 | t.Fatalf("agent export owner = %q", owner) |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | // A presigned GET never outlives the standing it was minted on: the |
| 354 | // Member lease, and never beyond the roster's 60 s otherwise. |
| 355 | func TestTeamPresignedURLsAreCappedAtTheLease(t *testing.T) { |
| 356 | f := newTeamFixture(t) |
| 357 | f.member(teamSubject('a'), "member") |
| 358 | u := f.userFor(teamSubject('a')) |
| 359 | u.team = &teamAccess{Access: f.a.team.Access(context.Background(), teamSubject('a'))} |
| 360 | if ttl := f.a.presignTTLFor(u); ttl <= 0 || ttl > 60*time.Second { |
| 361 | t.Fatalf("member's presign TTL = %v", ttl) |
| 362 | } |
| 363 | f.clock.Step(50 * time.Second) |
| 364 | if ttl := f.a.presignTTLFor(u); ttl > 10*time.Second { |
| 365 | t.Fatalf("presign TTL with 10 s of lease left = %v", ttl) |
| 366 | } |
| 367 | u.team = &teamAccess{Access: f.a.team.Access(context.Background(), "")} |
| 368 | if ttl := f.a.presignTTLFor(u); ttl != 60*time.Second { |
| 369 | t.Fatalf("guest's presign TTL = %v, want the roster's 60 s", ttl) |
| 370 | } |
| 371 | } |
| 372 | |