| 1 | package docs |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "context" |
| 6 | "crypto/sha256" |
| 7 | "encoding/base64" |
| 8 | "encoding/hex" |
| 9 | "encoding/json" |
| 10 | "errors" |
| 11 | "io" |
| 12 | "log/slog" |
| 13 | "net/http" |
| 14 | "net/http/httptest" |
| 15 | "net/url" |
| 16 | "path/filepath" |
| 17 | "regexp" |
| 18 | "strings" |
| 19 | "sync" |
| 20 | "testing" |
| 21 | "time" |
| 22 | |
| 23 | "amadan.net/oficina/home/connect" |
| 24 | "amadan.net/oficina/home/roster" |
| 25 | family "amadan.net/rastrillo/rastrillo/crypto" |
| 26 | "amadan.net/rastrillo/rastrillo/sessions" |
| 27 | "github.com/carlosframework/docs/internal/docs/principalstore" |
| 28 | "gorm.io/gorm" |
| 29 | ) |
| 30 | |
| 31 | type teamRouterDirectory struct { |
| 32 | mu sync.Mutex |
| 33 | key *family.Keypair |
| 34 | bindings roster.Bindings |
| 35 | generation string |
| 36 | epoch int64 |
| 37 | members []roster.MemberEntry |
| 38 | fail bool |
| 39 | } |
| 40 | |
| 41 | func (d *teamRouterDirectory) RoundTrip(r *http.Request) (*http.Response, error) { |
| 42 | d.mu.Lock() |
| 43 | defer d.mu.Unlock() |
| 44 | if d.fail { |
| 45 | return nil, errors.New("directory unavailable") |
| 46 | } |
| 47 | q := r.URL.Query() |
| 48 | if r.URL.Scheme+"://"+r.URL.Host != d.bindings.Directory || r.URL.Path != "/roster" || q.Get("team") != d.bindings.Team || !roster.ValidChallenge(q.Get("fetch")) || q.Get("cursor") != "" || r.Header.Get("Authorization") != "Bearer "+base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{7}, 32)) { |
| 49 | return nil, errors.New("wrong roster request scope") |
| 50 | } |
| 51 | index, complete := 0, true |
| 52 | members := append([]roster.MemberEntry{}, d.members...) |
| 53 | p := roster.Page{Version: 1, Type: "page", Directory: d.bindings.Directory, Consumer: d.bindings.Consumer, Team: d.bindings.Team, Audience: d.bindings.Audience, Fetch: q.Get("fetch"), RequestCursorHash: roster.CursorHash(""), Generation: d.generation, Epoch: d.epoch, Index: &index, Members: &members, Complete: &complete} |
| 54 | raw, e := roster.SignPage(d.key, "roster1", p) |
| 55 | if e != nil { |
| 56 | return nil, e |
| 57 | } |
| 58 | return &http.Response{StatusCode: 200, Header: http.Header{"Content-Type": {"application/json"}}, Body: io.NopCloser(bytes.NewReader(raw))}, nil |
| 59 | } |
| 60 | func (d *teamRouterDirectory) change(members []roster.MemberEntry, fail bool) { |
| 61 | d.mu.Lock() |
| 62 | defer d.mu.Unlock() |
| 63 | d.members = members |
| 64 | d.epoch++ |
| 65 | d.fail = fail |
| 66 | } |
| 67 | |
| 68 | type teamRouterFixture struct { |
| 69 | *routerHomeFixture |
| 70 | directory *teamRouterDirectory |
| 71 | clock *teamTestClock |
| 72 | cfg Config |
| 73 | alice, bob int64 |
| 74 | own, shared, hidden *Document |
| 75 | } |
| 76 | |
| 77 | func newTeamRouterFixture(t *testing.T) *teamRouterFixture { |
| 78 | return newTeamRouterFixtureWithDashboard(t, false) |
| 79 | } |
| 80 | func newTeamRouterFixtureWithDashboard(t *testing.T, dashboard bool) *teamRouterFixture { |
| 81 | return newTeamRouterFixtureWith(t, dashboard, false) |
| 82 | } |
| 83 | |
| 84 | // newTeamRouterFixtureWith can also run the team instance with warm |
| 85 | // handles and content sealing on (warm_test.go): rows sealed as production |
| 86 | // stores them, and DOCS_WARM_HANDLE on. |
| 87 | func newTeamRouterFixtureWith(t *testing.T, dashboard, warmSealed bool) *teamRouterFixture { |
| 88 | t.Helper() |
| 89 | ctx := t.Context() |
| 90 | credential, e := family.Generate() |
| 91 | if e != nil { |
| 92 | t.Fatal(e) |
| 93 | } |
| 94 | digest := sha256.Sum256(credential.SignPub()) |
| 95 | kid := hex.EncodeToString(digest[:]) |
| 96 | manifest, _ := json.Marshal(map[string]string{kid: base64.StdEncoding.EncodeToString(credential.SignPub())}) |
| 97 | home := HomeConfig{Issuer: bridgeIssuer, ConsumerID: "router-docs", KeysBase64: base64.StdEncoding.EncodeToString(manifest), DEKBase64: base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{83}, 32))} |
| 98 | cfg := Config{DBPath: filepath.Join(t.TempDir(), "team.db"), Env: "development", Origin: bridgeOrigin, Home: &home, AllowedAddresses: "alice@example.com,bob@example.com", SampleDocuments: "0", Mailer: navigationMail{}, Logger: slog.New(slog.NewTextHandler(io.Discard, nil))} |
| 99 | if warmSealed { |
| 100 | cfg.ContentKey = base64.StdEncoding.EncodeToString(bytes.Repeat([]byte{71}, 32)) |
| 101 | cfg.WarmHandle = true |
| 102 | // Sealing is activated offline, as activatedContentApp does. |
| 103 | first, e := New(cfg) |
| 104 | if e != nil { |
| 105 | t.Fatal(e) |
| 106 | } |
| 107 | if e = first.Close(); e != nil { |
| 108 | t.Fatal(e) |
| 109 | } |
| 110 | cipher, e := newContentCipher(cfg.ContentKey, "production") |
| 111 | if e != nil { |
| 112 | t.Fatal(e) |
| 113 | } |
| 114 | if e = MaintainContent(ctx, ContentMaintenanceConfig{DBPath: cfg.DBPath, ContentKey: cfg.ContentKey, ExpectedFingerprint: cipher.fingerprint}); e != nil { |
| 115 | t.Fatal(e) |
| 116 | } |
| 117 | } |
| 118 | app, e := New(cfg) |
| 119 | if e != nil { |
| 120 | t.Fatal(e) |
| 121 | } |
| 122 | f := &teamRouterFixture{cfg: cfg, clock: &teamTestClock{}} |
| 123 | f.alice, e = app.store.EnsurePerson(ctx, "alice@example.com") |
| 124 | if e != nil { |
| 125 | t.Fatal(e) |
| 126 | } |
| 127 | f.bob, e = app.store.EnsurePerson(ctx, "bob@example.com") |
| 128 | if e != nil { |
| 129 | t.Fatal(e) |
| 130 | } |
| 131 | f.own, e = app.store.createDocument(ctx, f.alice, "Alice original", "", VisibilityPrivate) |
| 132 | if e != nil { |
| 133 | t.Fatal(e) |
| 134 | } |
| 135 | f.shared, e = app.store.createDocument(ctx, f.bob, "Bob shared", "", VisibilityPrivate) |
| 136 | if e != nil { |
| 137 | t.Fatal(e) |
| 138 | } |
| 139 | f.hidden, e = app.store.createDocument(ctx, f.bob, "Bob private", "", VisibilityPrivate) |
| 140 | if e != nil { |
| 141 | t.Fatal(e) |
| 142 | } |
| 143 | if e = app.store.Grant(ctx, f.bob, f.shared.Sqid, "alice@example.com", RoleWriter); e != nil { |
| 144 | t.Fatal(e) |
| 145 | } |
| 146 | for _, d := range []*Document{f.own, f.shared, f.hidden} { |
| 147 | address := "alice@example.com" |
| 148 | if d.OwnerID == f.bob { |
| 149 | address = "bob@example.com" |
| 150 | } |
| 151 | if _, e = app.store.ApplyUpdate(ctx, d, localActor(d.OwnerID, address), validUpdate(t)); e != nil { |
| 152 | t.Fatal(e) |
| 153 | } |
| 154 | } |
| 155 | if e = app.Close(); e != nil { |
| 156 | t.Fatal(e) |
| 157 | } |
| 158 | operator, e := family.Generate() |
| 159 | if e != nil { |
| 160 | t.Fatal(e) |
| 161 | } |
| 162 | signer, e := family.Generate() |
| 163 | if e != nil { |
| 164 | t.Fatal(e) |
| 165 | } |
| 166 | fixed := func(v byte) string { return base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{v}, 32)) } |
| 167 | binding := roster.Bindings{Issuer: bridgeIssuer, Directory: "https://home.example", Consumer: "router-docs", App: "docs", Team: "TeamA", Audience: bridgeOrigin} |
| 168 | directory := &teamRouterDirectory{key: signer, bindings: binding, generation: fixed(1), epoch: 1, members: []roster.MemberEntry{{Subject: teamSubject('a'), Role: "member"}, {Subject: teamSubject('b'), Role: "member"}, {Subject: teamSubject('c'), Role: "member"}}} |
| 169 | record := roster.Provisioning{Version: 1, Operation: "team-provision", Sequence: 1, Bindings: binding, Generation: directory.generation, MinEpoch: 1, Keys: []roster.Key{{ID: "roster1", Public: base64.RawURLEncoding.EncodeToString(signer.SignPub())}}} |
| 170 | wire, e := roster.SignProvisioning(operator, "operator1", record) |
| 171 | if e != nil { |
| 172 | t.Fatal(e) |
| 173 | } |
| 174 | keys, _ := json.Marshal([]roster.Key{{ID: "operator1", Public: base64.RawURLEncoding.EncodeToString(operator.SignPub())}}) |
| 175 | cfg.Team = &TeamConfig{TeamID: binding.Team, DirectoryOrigin: binding.Directory, ProvisioningBase64: base64.StdEncoding.EncodeToString(wire), OperatorKeysBase64: base64.StdEncoding.EncodeToString(keys), RosterToken: fixed(7), clock: f.clock, transport: directory} |
| 176 | if dashboard { |
| 177 | t.Setenv("OFICINA_DASHBOARD_TEAM", binding.Team) |
| 178 | cfg.Directory = binding.Directory |
| 179 | } |
| 180 | app, e = New(cfg) |
| 181 | if e != nil { |
| 182 | t.Fatal(e) |
| 183 | } |
| 184 | t.Cleanup(func() { |
| 185 | if e := app.Close(); e != nil { |
| 186 | t.Error(e) |
| 187 | } |
| 188 | }) |
| 189 | sessionStore, e := sessions.New(sessions.Config{DB: app.db.Writer(), Origin: bridgeOrigin}) |
| 190 | if e != nil { |
| 191 | t.Fatal(e) |
| 192 | } |
| 193 | f.routerHomeFixture = &routerHomeFixture{app: app, key: credential, keyID: kid, sessions: sessionStore} |
| 194 | f.directory = directory |
| 195 | f.cfg = cfg |
| 196 | return f |
| 197 | } |
| 198 | func teamSubject(c byte) string { return "pr_" + strings.Repeat(string(c), 26) } |
| 199 | |
| 200 | var chooseToken = regexp.MustCompile(`name="token" value="([A-Za-z0-9_-]{43})"`) |
| 201 | |
| 202 | // choosePage follows a roster member's first finish (Home spec |
| 203 | // 2026-11-14-oficina-id-native.md §8) to the choose page, and returns its |
| 204 | // form token. Nothing is allocated before a POST. |
| 205 | func (b *routerBrowser) choosePage(t *testing.T, finish *httptest.ResponseRecorder) string { |
| 206 | t.Helper() |
| 207 | routerRedirect(t, finish, connect.ChoosePath) |
| 208 | page := b.do("GET", connect.ChoosePath, "") |
| 209 | body := page.Body.String() |
| 210 | // Tests render catalog keys (NewForTest wires no BaseCatalog); the |
| 211 | // copy itself is TestChooseCopyIsTheSpecs. |
| 212 | for _, want := range []string{"docs.home.choose_title", "docs.home.choose_body", `name="choice" value="connect"`, `name="choice" value="new"`} { |
| 213 | if page.Code != 200 || !strings.Contains(body, want) { |
| 214 | t.Fatalf("choose page=%d lacks %s\n%s", page.Code, want, body) |
| 215 | } |
| 216 | } |
| 217 | m := chooseToken.FindStringSubmatch(body) |
| 218 | if m == nil { |
| 219 | t.Fatal("choose page carries no form token") |
| 220 | } |
| 221 | return m[1] |
| 222 | } |
| 223 | |
| 224 | func (b *routerBrowser) choose(choice, token string) *httptest.ResponseRecorder { |
| 225 | return b.do("POST", connect.ChoosePath, url.Values{"choice": {choice}, "token": {token}}.Encode()) |
| 226 | } |
| 227 | |
| 228 | // chooseNew is a member's first finish, then "I'm new here". |
| 229 | func (b *routerBrowser) chooseNew(t *testing.T, finish *httptest.ResponseRecorder) { |
| 230 | t.Helper() |
| 231 | routerRedirect(t, b.choose("new", b.choosePage(t, finish)), "/") |
| 232 | } |
| 233 | |
| 234 | func (b *routerBrowser) update(doc *Document, body []byte) int { |
| 235 | r := b.request("POST", "/d/"+doc.Sqid+"/updates", string(body)) |
| 236 | r.Header.Set("Content-Type", "application/octet-stream") |
| 237 | return b.serve(r).Code |
| 238 | } |
| 239 | func TestTeamRouterLocalLinkMembershipRemovalAndIndependentGrant(t *testing.T) { |
| 240 | f := newTeamRouterFixture(t) |
| 241 | b := f.browser() |
| 242 | b.localSignin(t, "alice@example.com") |
| 243 | if r := b.do("GET", "/d/"+f.own.Sqid, ""); r.Code != 404 { |
| 244 | t.Fatal("email admission became team standing", r.Code) |
| 245 | } |
| 246 | if r := b.do("GET", "/d/"+f.shared.Sqid, ""); r.Code != 200 { |
| 247 | t.Fatal("unlinked guest lost exact grant", r.Code) |
| 248 | } |
| 249 | cookie := b.cookies[f.sessions.CookieName()].Value |
| 250 | b.linkProof(t, teamSubject('a'), "alice@example.com") |
| 251 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 252 | if b.cookies[f.sessions.CookieName()].Value != cookie { |
| 253 | t.Fatal("link rotated local session") |
| 254 | } |
| 255 | for _, d := range []*Document{f.own, f.shared} { |
| 256 | if r := b.do("GET", "/d/"+d.Sqid, ""); r.Code != 200 { |
| 257 | t.Fatal("linked member lost access", r.Code) |
| 258 | } |
| 259 | } |
| 260 | if r := b.do("GET", "/d/"+f.hidden.Sqid, ""); r.Code != 404 { |
| 261 | t.Fatal("membership opened another owner private document", r.Code) |
| 262 | } |
| 263 | if r := b.do("GET", "/d/"+f.shared.Sqid+"/share", ""); r.Code != 404 { |
| 264 | t.Fatal("writer grant became owner administration", r.Code) |
| 265 | } |
| 266 | if r := b.do("GET", "/d/"+f.own.Sqid+"/share", ""); r.Code != 200 { |
| 267 | t.Fatal("member owner cannot administer", r.Code) |
| 268 | } |
| 269 | f.clock.step(roster.RetryAfter) |
| 270 | f.directory.change([]roster.MemberEntry{{Subject: teamSubject('b'), Role: "member"}, {Subject: teamSubject('c'), Role: "member"}}, false) |
| 271 | if e := f.app.store.team.cache.Refresh(t.Context()); e != nil { |
| 272 | t.Fatal(e) |
| 273 | } |
| 274 | for _, path := range []string{"/d/" + f.own.Sqid, "/d/nonexistent", "/d/" + f.own.Sqid + "/share"} { |
| 275 | if r := b.do("GET", path, ""); r.Code != 404 { |
| 276 | t.Fatal("removed owner retained item access", path, r.Code) |
| 277 | } |
| 278 | } |
| 279 | if r := b.do("POST", "/documents", url.Values{"title": {"refused"}}.Encode()); r.Code != 403 { |
| 280 | t.Fatal("removed member created document", r.Code) |
| 281 | } |
| 282 | if code := b.update(f.own, validUpdate(t)); code != 404 { |
| 283 | t.Fatal("removed owner update not definitely refused", code) |
| 284 | } |
| 285 | if code := b.update(f.shared, validUpdate(t)); code != 204 { |
| 286 | t.Fatal("independent writer grant lost after membership removal", code) |
| 287 | } |
| 288 | f.directory.change(nil, true) |
| 289 | f.clock.step(60 * time.Second) |
| 290 | if e := f.app.store.team.cache.Refresh(t.Context()); e == nil { |
| 291 | t.Fatal("directory failure accepted") |
| 292 | } |
| 293 | for _, path := range []string{"/d/" + f.own.Sqid + "/share", "/d/nonexistent/share"} { |
| 294 | if r := b.do("GET", path, ""); r.Code != 503 { |
| 295 | t.Fatal("unknown owner administration became permanent or leaked existence", r.Code) |
| 296 | } |
| 297 | } |
| 298 | missing := b.do("GET", "/d/nonexistent", "") |
| 299 | private := b.do("GET", "/d/"+f.own.Sqid, "") |
| 300 | if missing.Code != 503 || private.Code != 503 || missing.Body.String() != private.Body.String() { |
| 301 | t.Fatal("unknown standing leaked existence or became permanent refusal") |
| 302 | } |
| 303 | if r := b.do("GET", "/d/"+f.shared.Sqid, ""); r.Code != 200 { |
| 304 | t.Fatal("grant unavailable during roster failure", r.Code) |
| 305 | } |
| 306 | if r := b.do("GET", "/", ""); r.Code != 200 || !strings.Contains(r.Body.String(), `rst-tone="warning"`) || !strings.Contains(r.Body.String(), `<p>docs.team.partial</p>`) || strings.Contains(r.Body.String(), "Alice original") { |
| 307 | t.Fatal("partial list misrepresented unavailable owned documents", r.Code) |
| 308 | } |
| 309 | if r := b.do("GET", "/documents/search?q=shared", ""); r.Code != 200 || r.Header().Get("Docs-Catalog-Partial") != "1" { |
| 310 | t.Fatal("partial search lost status", r.Code) |
| 311 | } |
| 312 | if code := b.update(f.own, validUpdate(t)); code != 503 { |
| 313 | t.Fatal("uncertain update became permanent failure", code) |
| 314 | } |
| 315 | if code := b.update(f.shared, validUpdate(t)); code != 204 { |
| 316 | t.Fatal("writer grant depended on roster availability", code) |
| 317 | } |
| 318 | signoutChained(t, b.do("POST", "/signout", "")) |
| 319 | nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| 320 | authTime := time.Now().Add(-2 * time.Hour).Truncate(time.Second) |
| 321 | routerRedirect(t, b.exchange(t, nonce, teamSubject('a'), authTime), "/") |
| 322 | session, ok := f.sessions.From(b.request("GET", "/", "")) |
| 323 | if !ok || session.Method != homeAssertionMethod || !session.AuthTime.Equal(authTime) { |
| 324 | t.Fatal("ordinary Home sign-in changed authentication time") |
| 325 | } |
| 326 | if r := b.do("GET", "/d/"+f.shared.Sqid, ""); r.Code != 200 { |
| 327 | t.Fatal("linked Home identity lost current verified grant address", r.Code) |
| 328 | } |
| 329 | p, e := f.app.store.team.principals.Resolve(context.Background(), bridgeIssuer, teamSubject('a')) |
| 330 | if e != nil || p.ID != f.alice || p.Kind != principalstore.Local { |
| 331 | t.Fatal("existing local ownership identity changed", e) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | func TestTeamRouterHomeOnlyFirstEntryAndConflict(t *testing.T) { |
| 336 | f := newTeamRouterFixture(t) |
| 337 | b := f.browser() |
| 338 | nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| 339 | b.chooseNew(t, b.exchange(t, nonce, teamSubject('b'), time.Now().Add(-time.Hour))) |
| 340 | p, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('b')) |
| 341 | if e != nil || p.Kind != principalstore.Home || p.VerifiedAddress != nil { |
| 342 | t.Fatal("first Home entry did not create addressless principal", e) |
| 343 | } |
| 344 | var count int64 |
| 345 | if e = f.app.store.db.Model(&Person{}).Count(&count).Error; e != nil || count != 2 { |
| 346 | t.Fatal("Home entry created legacy email person", count, e) |
| 347 | } |
| 348 | if r := b.do("GET", "/", ""); r.Code != 200 || strings.Contains(r.Body.String(), "Alice original") || strings.Contains(r.Body.String(), "Bob private") { |
| 349 | t.Fatal("new Home principal inherited local ownership", r.Code) |
| 350 | } |
| 351 | r := b.do("POST", "/documents", url.Values{"title": {"Home document"}}.Encode()) |
| 352 | target := routerRedirect(t, r, "") |
| 353 | if !strings.HasPrefix(target, "/d/") { |
| 354 | t.Fatal("member could not create document") |
| 355 | } |
| 356 | var d Document |
| 357 | if e = f.app.store.db.Where("sqid = ?", strings.TrimPrefix(target, "/d/")).First(&d).Error; e != nil || d.OwnerID != p.ID { |
| 358 | t.Fatal("Home-created document has wrong owner", e) |
| 359 | } |
| 360 | nonce = b.start(t, "/auth/start?return_to=%2F", "") |
| 361 | if r = b.exchange(t, nonce, teamSubject('c'), time.Now()); r.Code != 400 { |
| 362 | t.Fatal("existing browser switched principal", r.Code) |
| 363 | } |
| 364 | if _, e = f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('c')); !errors.Is(e, principalstore.ErrRefused) { |
| 365 | t.Fatal("conflict allocated unknown principal", e) |
| 366 | } |
| 367 | if r = b.do("GET", target, ""); r.Code != 200 { |
| 368 | t.Fatal("conflict replaced current session", r.Code) |
| 369 | } |
| 370 | outsider := f.browser() |
| 371 | nonce = outsider.start(t, "/auth/start?return_to=%2F", "") |
| 372 | if r = outsider.exchange(t, nonce, teamSubject('z'), time.Now()); r.Code != 403 { |
| 373 | t.Fatal("nonmember first entry accepted", r.Code) |
| 374 | } |
| 375 | if _, e = f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('z')); !errors.Is(e, principalstore.ErrRefused) { |
| 376 | t.Fatal("nonmember allocated principal", e) |
| 377 | } |
| 378 | if r = b.do("GET", "/account/home", ""); r.Code != 303 || r.Header().Get("Location") != "/signin/local?reauth=1" { |
| 379 | t.Fatal("addressless account cannot begin explicit local reauthentication", r.Code, r.Header().Get("Location")) |
| 380 | } |
| 381 | } |
| 382 | |
| 383 | func TestTeamRouterActivatedDatabaseRefusesMissingAndMismatchedConfiguration(t *testing.T) { |
| 384 | f := newTeamRouterFixture(t) |
| 385 | cfg := f.cfg |
| 386 | cfg.Team = nil |
| 387 | if app, e := New(cfg); e == nil { |
| 388 | app.Close() |
| 389 | t.Fatal("activated database reopened without TeamMode") |
| 390 | } |
| 391 | mismatch := *f.cfg.Team |
| 392 | mismatch.TeamID = "TeamB" |
| 393 | cfg.Team = &mismatch |
| 394 | if app, e := New(cfg); e == nil { |
| 395 | app.Close() |
| 396 | t.Fatal("activated database accepted foreign team configuration") |
| 397 | } |
| 398 | cfg = f.cfg |
| 399 | cfg.Home = nil |
| 400 | if app, e := New(cfg); e == nil { |
| 401 | app.Close() |
| 402 | t.Fatal("activated database accepted missing Home identity configuration") |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | // The partial notice is asked after the list is built, from the same |
| 407 | // Access (Paul's ruling on list pages, 2026-10-28, condition 1): a lease |
| 408 | // that lapses after the page's first look at it, and before the list is |
| 409 | // scoped, must still mark the shares-only list partial. |
| 410 | func TestTeamListAsksPartialAfterListing(t *testing.T) { |
| 411 | f := newTeamRouterFixture(t) |
| 412 | b := f.browser() |
| 413 | b.localSignin(t, "alice@example.com") |
| 414 | b.linkProof(t, teamSubject('a'), "alice@example.com") |
| 415 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 416 | if r := b.do("GET", "/", ""); r.Code != 200 || strings.Contains(r.Body.String(), `<p>docs.team.partial</p>`) || !strings.Contains(r.Body.String(), "Alice original") { |
| 417 | t.Fatal("member list", r.Code) |
| 418 | } |
| 419 | f.directory.change(nil, true) |
| 420 | // Armed when the request resolves its person, before admission: the |
| 421 | // roster clock's first reading is admission's decision (Member), the |
| 422 | // second the handler's first look at it (Member), and the lease has |
| 423 | // lapsed by the third. |
| 424 | reads := 0 |
| 425 | var count func() |
| 426 | count = func() { |
| 427 | if reads++; reads == 3 { |
| 428 | f.clock.elapsed += 60 * time.Second |
| 429 | return |
| 430 | } |
| 431 | f.clock.next = count |
| 432 | } |
| 433 | if e := f.app.store.db.Callback().Query().Before("gorm:query").Register("test:arm-lapse", func(tx *gorm.DB) { |
| 434 | if tx.Statement.Table == "people" && reads == 0 { |
| 435 | f.clock.mu.Lock() |
| 436 | f.clock.next = count |
| 437 | f.clock.mu.Unlock() |
| 438 | } |
| 439 | }); e != nil { |
| 440 | t.Fatal(e) |
| 441 | } |
| 442 | defer f.app.store.db.Callback().Query().Remove("test:arm-lapse") |
| 443 | r := b.do("GET", "/", "") |
| 444 | if reads < 3 || r.Code != 200 || strings.Contains(r.Body.String(), "Alice original") || !strings.Contains(r.Body.String(), "Bob shared") { |
| 445 | t.Fatal("the lease did not lapse between the first look and the list", reads, r.Code) |
| 446 | } |
| 447 | if !strings.Contains(r.Body.String(), `<p>docs.team.partial</p>`) { |
| 448 | t.Fatal("a shares-only list built under a lapsed lease was not marked partial") |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | // A first Home entry the roster cannot decide is a retryable 503 that |
| 453 | // allocates nothing: the allocation gate is Runtime.AdmitUnlinked, and only |
| 454 | // its definite "not a member" is the 403 "unknown". |
| 455 | func TestTeamRouterHomeOnlyFirstEntryUnderUnknownIsRetryable(t *testing.T) { |
| 456 | f := newTeamRouterFixture(t) |
| 457 | f.directory.change(nil, true) |
| 458 | f.clock.step(61 * time.Second) |
| 459 | b := f.browser() |
| 460 | nonce := b.start(t, "/auth/start?return_to=%2F", "") |
| 461 | if r := b.exchange(t, nonce, teamSubject('c'), time.Now()); r.Code != 503 { |
| 462 | t.Fatal("undecided first entry was not retryable", r.Code) |
| 463 | } |
| 464 | if _, e := f.app.store.team.principals.Resolve(t.Context(), bridgeIssuer, teamSubject('c')); !errors.Is(e, principalstore.ErrRefused) { |
| 465 | t.Fatal("undecided first entry allocated a principal", e) |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | // The native list renders no partial notice, so under Unknown it is |
| 470 | // unavailable rather than a short list of shares (Fable on #100). |
| 471 | func TestTeamNativeListUnderUnknownIsUnavailableNotShort(t *testing.T) { |
| 472 | f := newTeamRouterFixture(t) |
| 473 | b := f.browser() |
| 474 | b.localSignin(t, "alice@example.com") |
| 475 | b.linkProof(t, teamSubject('a'), "alice@example.com") |
| 476 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 477 | native := func(path string) *httptest.ResponseRecorder { |
| 478 | r := b.request("GET", path, "") |
| 479 | r.Header.Set("Accept", "application/json") |
| 480 | return b.serve(r) |
| 481 | } |
| 482 | if r := native("/"); r.Code != 200 || !strings.Contains(r.Body.String(), "Alice original") || !strings.Contains(r.Body.String(), "Bob shared") { |
| 483 | t.Fatal("member native list", r.Code) |
| 484 | } |
| 485 | f.directory.change(nil, true) |
| 486 | f.clock.step(61 * time.Second) |
| 487 | for _, path := range []string{"/", "/?q=Bob"} { |
| 488 | if r := native(path); r.Code != 503 || strings.Contains(r.Body.String(), "Bob shared") { |
| 489 | t.Fatal("native list under Unknown was short, not unavailable", path, r.Code, r.Body.String()) |
| 490 | } |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | // Under Unknown the list page is the partial list with the notice even when |
| 495 | // it is empty: the empty state and the notice, not a 503 (Paul's ruling on |
| 496 | // list pages, 2026-10-28). NotMember's empty list is complete, so it carries |
| 497 | // no notice; item pages and the dashboard feed are unchanged. |
| 498 | func TestTeamEmptyListUnderUnknownIsPartialNotUnavailable(t *testing.T) { |
| 499 | f := newTeamRouterFixture(t) |
| 500 | b := f.browser() |
| 501 | b.localSignin(t, "bob@example.com") |
| 502 | // Bob signed in locally with no Home link is a guest, and nothing is |
| 503 | // shared with him: an empty list, complete, with no notice. |
| 504 | if r := b.do("GET", "/", ""); r.Code != 200 || strings.Contains(r.Body.String(), `<p>docs.team.partial</p>`) || !strings.Contains(r.Body.String(), "docs.list.empty.title") { |
| 505 | t.Fatal("guest's empty list", r.Code) |
| 506 | } |
| 507 | b.linkProof(t, teamSubject('b'), "bob@example.com") |
| 508 | routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/")) |
| 509 | if r := b.do("GET", "/", ""); r.Code != 200 || !strings.Contains(r.Body.String(), "Bob private") { |
| 510 | t.Fatal("member list", r.Code) |
| 511 | } |
| 512 | f.directory.change(nil, true) |
| 513 | f.clock.step(61 * time.Second) |
| 514 | for _, path := range []string{"/", "/?q=Bob"} { |
| 515 | r := b.do("GET", path, "") |
| 516 | body := r.Body.String() |
| 517 | if r.Code != 200 || !strings.Contains(body, `<p>docs.team.partial</p>`) || strings.Contains(body, "Bob private") || strings.Contains(body, "Bob shared") { |
| 518 | t.Fatal("empty list under Unknown was not partial", path, r.Code) |
| 519 | } |
| 520 | } |
| 521 | if r := b.do("GET", "/", ""); !strings.Contains(r.Body.String(), "docs.list.empty.title") { |
| 522 | t.Fatal("empty partial list lost its empty state") |
| 523 | } |
| 524 | if r := b.do("GET", "/d/"+f.hidden.Sqid, ""); r.Code != 503 { |
| 525 | t.Fatal("own item under Unknown was not a retryable 503", r.Code) |
| 526 | } |
| 527 | // The native list renders no notice, so it is still unavailable. |
| 528 | native := b.request("GET", "/", "") |
| 529 | native.Header.Set("Accept", "application/json") |
| 530 | if r := b.serve(native); r.Code != 503 { |
| 531 | t.Fatal("native list under Unknown was not unavailable", r.Code) |
| 532 | } |
| 533 | } |
| 534 | |