| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/home/connect" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | ) |
| 13 | |
| 14 | func sheetPath(id uint64) string { return "/sheets/" + strconv.FormatUint(id, 10) } |
| 15 | |
| 16 | // teamSheet makes a sheet as somebody else on the instance, so the only |
| 17 | // way the person under test reaches it is the team rung. |
| 18 | func teamSheet(t *testing.T, f *teamFixture, title string) SheetSummary { |
| 19 | t.Helper() |
| 20 | other, err := f.app.memberFor("colleague@example.com") |
| 21 | if err != nil { |
| 22 | t.Fatal(err) |
| 23 | } |
| 24 | s, err := f.app.createSheet(nil, title, other) |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | return s |
| 29 | } |
| 30 | |
| 31 | func memberCount(t *testing.T, f *teamFixture) int64 { |
| 32 | t.Helper() |
| 33 | var n int64 |
| 34 | if err := f.app.db.Model(&Member{}).Count(&n).Error; err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | return n |
| 38 | } |
| 39 | |
| 40 | // A roster member with no account here signs in with Home alone, works |
| 41 | // as a team member, and — removed on Home — loses the team on the next |
| 42 | // request once the roster refreshes, on the session they already hold. |
| 43 | // Their own sheet goes with it: its creator's grant is the team rung's. |
| 44 | func TestTeamRemovalRevokesAHomeOnlyMembersExistingSession(t *testing.T) { |
| 45 | f := newTeamFixture(t) |
| 46 | plan := teamSheet(t, f, "Team plan") |
| 47 | a := teamSubject('a') |
| 48 | f.dir.Set(a, "member") |
| 49 | |
| 50 | // Asked to choose first (Home spec 2026-11-14-oficina-id-native.md |
| 51 | // §8); "I'm new here" lands on the default path, as connect's choice |
| 52 | // keeps no return path. |
| 53 | if fin := f.signInHomeChoosing(t, a); fin.Code != http.StatusSeeOther || fin.Header().Get("Location") != connect.ChoosePath { |
| 54 | t.Fatalf("roster member's Home sign-in = %d %q, want the choice", fin.Code, fin.Header().Get("Location")) |
| 55 | } |
| 56 | var before int64 |
| 57 | f.app.db.Model(&Member{}).Where("address IS NULL").Count(&before) |
| 58 | if before != 0 { |
| 59 | t.Fatalf("Home-only members = %d before the choice, want 0: nothing is made until they choose", before) |
| 60 | } |
| 61 | if fin := f.choose(t, "new"); fin.Code != http.StatusSeeOther || fin.Header().Get("Location") != "/" { |
| 62 | t.Fatalf("I'm new here = %d %q", fin.Code, fin.Header().Get("Location")) |
| 63 | } |
| 64 | var addressless int64 |
| 65 | f.app.db.Model(&Member{}).Where("address IS NULL").Count(&addressless) |
| 66 | if addressless != 1 { |
| 67 | t.Fatalf("Home-only members = %d, want 1", addressless) |
| 68 | } |
| 69 | created := f.request(t, "POST", "/sheets", url.Values{"title": {"Mine"}}, false) |
| 70 | if created.Code != http.StatusSeeOther { |
| 71 | t.Fatalf("member create = %d", created.Code) |
| 72 | } |
| 73 | mine := created.Header().Get("Location") |
| 74 | for _, p := range []string{"/sheets", sheetPath(plan.ID), mine} { |
| 75 | if rec := f.get(t, p); rec.Code != http.StatusOK { |
| 76 | t.Fatalf("member GET %s = %d", p, rec.Code) |
| 77 | } |
| 78 | } |
| 79 | session := f.jar[f.app.homeSessions.CookieName()] |
| 80 | |
| 81 | f.dir.Set(a, "") |
| 82 | f.refreshed(t) |
| 83 | if f.jar[f.app.homeSessions.CookieName()] != session { |
| 84 | t.Fatal("the test lost the session it means to keep using") |
| 85 | } |
| 86 | for _, p := range []string{sheetPath(plan.ID), mine} { |
| 87 | if rec := f.get(t, p); rec.Code != http.StatusNotFound { |
| 88 | t.Errorf("removed member GET %s = %d, want 404", p, rec.Code) |
| 89 | } |
| 90 | } |
| 91 | if list := f.get(t, "/sheets"); list.Code != http.StatusOK || strings.Contains(list.Body.String(), "Team plan") || strings.Contains(list.Body.String(), "Mine") { |
| 92 | t.Errorf("removed member's list = %d, still showing team sheets", list.Code) |
| 93 | } else if strings.Contains(list.Body.String(), "data-partial") { |
| 94 | t.Error("a NotMember's list is marked partial: a guest's shares are their whole list") |
| 95 | } |
| 96 | if rec := f.request(t, "POST", "/sheets", url.Values{"title": {"After"}}, false); rec.Code != http.StatusForbidden { |
| 97 | t.Errorf("removed member create = %d, want 403", rec.Code) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // The known Sheets bug, under team mode: an address on SHEETS_OWNERS with |
| 102 | // a stored team_role of owner has no team standing at all unless the |
| 103 | // roster says so, and loses it when the roster stops saying so. Roles come |
| 104 | // from the roster. Shares from somebody else survive removal, and keep |
| 105 | // working while the roster cannot be read. |
| 106 | func TestTeamStandingIsTheRostersNeverTheStoredRole(t *testing.T) { |
| 107 | f := newTeamFixture(t, signedInAddress) |
| 108 | plan := teamSheet(t, f, "Team plan") |
| 109 | f.local(t, signedInAddress) |
| 110 | var stored Member |
| 111 | if err := f.app.db.Where("address = ?", signedInAddress).First(&stored).Error; err != nil || stored.TeamRole != string(TeamGuest) { |
| 112 | t.Fatalf("SHEETS_OWNERS promoted a member in team mode: %q %v", stored.TeamRole, err) |
| 113 | } |
| 114 | if err := f.app.db.Model(&Member{}).Where("address = ?", signedInAddress).Update("team_role", "owner").Error; err != nil { |
| 115 | t.Fatal(err) |
| 116 | } |
| 117 | if rec := f.get(t, sheetPath(plan.ID)); rec.Code != http.StatusNotFound { |
| 118 | t.Fatalf("stored owner without roster standing = %d, want 404", rec.Code) |
| 119 | } |
| 120 | |
| 121 | p := teamSubject('p') |
| 122 | f.dir.Set(p, "owner") |
| 123 | f.link(t, p) |
| 124 | // Off the allowlist and holding no grant: the link still resolves, |
| 125 | // because in team mode the roster, not admission, is the team. |
| 126 | f.app.admit = Admits(nil) |
| 127 | if rec := f.get(t, sheetPath(plan.ID)); rec.Code != http.StatusOK { |
| 128 | t.Fatalf("linked roster owner = %d, want 200", rec.Code) |
| 129 | } |
| 130 | // And signing in with Home alone reaches the same account. |
| 131 | if fin := f.signInHome(t, p); fin.Code != http.StatusSeeOther { |
| 132 | t.Fatalf("linked owner's Home sign-in = %d", fin.Code) |
| 133 | } |
| 134 | if rec := f.get(t, sheetPath(plan.ID)); rec.Code != http.StatusOK { |
| 135 | t.Fatalf("linked roster owner by Home session = %d, want 200", rec.Code) |
| 136 | } |
| 137 | paul := f.viewer(t) |
| 138 | if !f.app.stillReaches(t.Context(), plan.ID, paul.ID) { |
| 139 | t.Error("the share screen says a roster owner cannot reach a team sheet") |
| 140 | } |
| 141 | if m := f.viewer(t); m.TeamRole != string(TeamOwner) { |
| 142 | t.Errorf("roster owner's team role = %q", m.TeamRole) |
| 143 | } |
| 144 | f.dir.Set(p, "member") |
| 145 | f.refreshed(t) |
| 146 | if m := f.viewer(t); m.TeamRole != string(TeamMember) { |
| 147 | t.Errorf("roster member's team role = %q", m.TeamRole) |
| 148 | } |
| 149 | |
| 150 | colleague, _ := f.app.memberFor("colleague@example.com") |
| 151 | shared := teamSheet(t, f, "Shared") |
| 152 | if _, err := f.app.grant(nil, shared.ID, signedInAddress, SheetViewer, colleague); err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | |
| 156 | f.expired(t) |
| 157 | missing := f.get(t, "/sheets/999999") |
| 158 | team := f.get(t, sheetPath(plan.ID)) |
| 159 | if team.Code != http.StatusServiceUnavailable || missing.Code != team.Code || missing.Body.String() != team.Body.String() { |
| 160 | t.Errorf("Unknown standing: team sheet %d, missing sheet %d; want identical 503s", team.Code, missing.Code) |
| 161 | } |
| 162 | if rec := f.get(t, sheetPath(shared.ID)); rec.Code != http.StatusOK { |
| 163 | t.Errorf("a share under Unknown standing = %d, want 200", rec.Code) |
| 164 | } |
| 165 | // Paul's ruling (2026-10-28): the list under Unknown is the shares, |
| 166 | // marked partial, never a 503 for the whole page and never the team's. |
| 167 | if rec := f.get(t, "/sheets"); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "Shared") || |
| 168 | strings.Contains(rec.Body.String(), "Team plan") || !strings.Contains(rec.Body.String(), "data-partial") || |
| 169 | !strings.Contains(rec.Body.String(), "Only sheets shared with you are shown.") { |
| 170 | t.Errorf("list under Unknown standing = %d, want the shares marked partial", rec.Code) |
| 171 | } |
| 172 | native := httptest.NewRequest("GET", testOrigin+"/sheets", nil) |
| 173 | native.Header.Set("Accept", "application/json") |
| 174 | for _, c := range f.jar { |
| 175 | native.AddCookie(c) |
| 176 | } |
| 177 | if rec := do(t, f.app, native); rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `"Partial":true`) || strings.Contains(rec.Body.String(), "Team plan") { |
| 178 | t.Errorf("native list under Unknown standing = %d %s, want the shares marked partial", rec.Code, rec.Body.String()) |
| 179 | } |
| 180 | if rec := f.request(t, "POST", "/sheets", url.Values{"title": {"x"}}, false); rec.Code != http.StatusServiceUnavailable { |
| 181 | t.Errorf("create under Unknown standing = %d, want 503", rec.Code) |
| 182 | } |
| 183 | |
| 184 | f.dir.SetDown(false) |
| 185 | f.dir.Set(p, "") |
| 186 | f.refreshed(t) |
| 187 | if rec := f.get(t, sheetPath(plan.ID)); rec.Code != http.StatusNotFound { |
| 188 | t.Errorf("removed linked member = %d, want 404", rec.Code) |
| 189 | } |
| 190 | if f.app.stillReaches(t.Context(), plan.ID, paul.ID) { |
| 191 | t.Error("the share screen says a removed member still reaches a team sheet") |
| 192 | } |
| 193 | if rec := f.get(t, sheetPath(shared.ID)); rec.Code != http.StatusOK { |
| 194 | t.Errorf("removed member's share = %d, want 200", rec.Code) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // A member's lease is sixty seconds. With the directory down, standing |
| 199 | // holds until then and not a moment after: Unknown, retryable. |
| 200 | func TestTeamStandingEndsAtTheLeaseUnderAFailingRefresh(t *testing.T) { |
| 201 | f := newTeamFixture(t) |
| 202 | plan := teamSheet(t, f, "Team plan") |
| 203 | a := teamSubject('a') |
| 204 | f.dir.Set(a, "member") |
| 205 | f.signInHome(t, a) |
| 206 | f.dir.SetDown(true) |
| 207 | f.clock.Step(59 * time.Second) |
| 208 | if rec := f.get(t, sheetPath(plan.ID)); rec.Code != http.StatusOK { |
| 209 | t.Fatalf("inside the lease = %d, want 200", rec.Code) |
| 210 | } |
| 211 | f.clock.Step(2 * time.Second) |
| 212 | rec := f.get(t, sheetPath(plan.ID)) |
| 213 | if rec.Code != http.StatusServiceUnavailable || rec.Header().Get("Retry-After") == "" { |
| 214 | t.Fatalf("past the lease = %d (Retry-After %q), want a retryable 503", rec.Code, rec.Header().Get("Retry-After")) |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | // Home sign-in allocates only for a subject the roster says is a member |
| 219 | // right now; not for a stranger, not when the roster is unknown, and not |
| 220 | // when somebody else is already signed in. |
| 221 | func TestTeamSignInAllocatesOnlyForACurrentMember(t *testing.T) { |
| 222 | f := newTeamFixture(t) |
| 223 | if fin := f.signInHome(t, teamSubject('z')); fin.Code != http.StatusForbidden || !strings.Contains(fin.Body.String(), "/signin/local") { |
| 224 | t.Fatalf("stranger's Home sign-in = %d", fin.Code) |
| 225 | } |
| 226 | if _, ok := f.jar[f.app.homeSessions.CookieName()]; ok || memberCount(t, f) != 0 { |
| 227 | t.Fatal("a stranger got a session or a member") |
| 228 | } |
| 229 | |
| 230 | f.dir.Set(teamSubject('a'), "member") |
| 231 | f.dir.Set(teamSubject('b'), "member") |
| 232 | f.refreshed(t) // the stranger's sign-in fetched the roster before they were added |
| 233 | if fin := f.signInHome(t, teamSubject('a')); fin.Code != http.StatusSeeOther { |
| 234 | t.Fatalf("member's Home sign-in = %d", fin.Code) |
| 235 | } |
| 236 | start := f.request(t, "GET", "/auth/start?return_to=%2Fsheets", nil, false) |
| 237 | // The exchange itself refuses a different subject over a Home session |
| 238 | // (400, as connect's does), before anything here runs. |
| 239 | if fin := f.exchange(t, start, teamSubject('b')); fin.Code != http.StatusBadRequest { |
| 240 | t.Errorf("a second identity over a live Home session = %d, want 400", fin.Code) |
| 241 | } |
| 242 | // Over a local session it reaches connect's completion, which refuses an |
| 243 | // unknown arrival before allocating anything. |
| 244 | f.jar = map[string]*http.Cookie{} |
| 245 | f.local(t, "local@example.com") |
| 246 | start = f.request(t, "GET", "/auth/start?return_to=%2Fsheets", nil, false) |
| 247 | if fin := f.exchange(t, start, teamSubject('b')); fin.Code != http.StatusForbidden { |
| 248 | t.Errorf("an unknown identity over a live local session = %d, want 403", fin.Code) |
| 249 | } |
| 250 | if n := memberCount(t, f); n != 2 { |
| 251 | t.Errorf("members = %d after refused arrivals, want 2 (a, and the local account)", n) |
| 252 | } |
| 253 | |
| 254 | f.expired(t) |
| 255 | if fin := f.signInHome(t, teamSubject('b')); fin.Code != http.StatusServiceUnavailable { |
| 256 | t.Errorf("unknown roster at first sign-in = %d, want 503", fin.Code) |
| 257 | } |
| 258 | if n := memberCount(t, f); n != 2 { |
| 259 | t.Errorf("members = %d after an Unknown arrival, want 2", n) |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | // Paul's ruling with nothing to show: a Home-only member with no shares, |
| 264 | // under Unknown, gets the partial notice and an empty list — not a 503, |
| 265 | // not the team's sheets, and not "No sheets yet", which would read as an |
| 266 | // answer (Fable, #53 Required 3). |
| 267 | func TestTeamEmptyPartialListSaysPartialNotEmpty(t *testing.T) { |
| 268 | f := newTeamFixture(t) |
| 269 | teamSheet(t, f, "Team plan") |
| 270 | a := teamSubject('a') |
| 271 | f.dir.Set(a, "member") |
| 272 | f.signInHome(t, a) |
| 273 | f.expired(t) |
| 274 | rec := f.get(t, "/sheets") |
| 275 | body := rec.Body.String() |
| 276 | if rec.Code != http.StatusOK || !strings.Contains(body, "data-partial") || strings.Contains(body, "Team plan") || strings.Contains(body, "No sheets yet") { |
| 277 | t.Errorf("empty list under Unknown = %d (partial %v, team %v, empty state %v)", rec.Code, |
| 278 | strings.Contains(body, "data-partial"), strings.Contains(body, "Team plan"), strings.Contains(body, "No sheets yet")) |
| 279 | } |
| 280 | } |
| 281 | |
| 282 | // viewer is the request's actor as a handler sees it. |
| 283 | func (f *teamFixture) viewer(t *testing.T) Member { |
| 284 | t.Helper() |
| 285 | r := httptest.NewRequest("GET", testOrigin+"/sheets", nil) |
| 286 | for _, c := range f.jar { |
| 287 | r.AddCookie(c) |
| 288 | } |
| 289 | m, ok := f.app.actor(r) |
| 290 | if !ok { |
| 291 | t.Fatal("no actor") |
| 292 | } |
| 293 | return m |
| 294 | } |
| 295 | |