| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "net/url" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/home/dashboard" |
| 14 | "github.com/go-json-experiment/json" |
| 15 | |
| 16 | "amadan.net/oficina/sheets/internal/sheets/teamstate" |
| 17 | ) |
| 18 | |
| 19 | // Tests added for Fable's review of #52: each kills a mutant that had |
| 20 | // survived the first round. |
| 21 | |
| 22 | func (f *teamFixture) getHTML(t *testing.T, path string) *httptest.ResponseRecorder { |
| 23 | t.Helper() |
| 24 | r := httptest.NewRequest("GET", testOrigin+path, nil) |
| 25 | r.Header.Set("Accept", "text/html") |
| 26 | for _, c := range f.jar { |
| 27 | r.AddCookie(c) |
| 28 | } |
| 29 | w := httptest.NewRecorder() |
| 30 | f.app.Router().ServeHTTP(w, r) |
| 31 | return w |
| 32 | } |
| 33 | |
| 34 | // A Home-only member is signed in as far as the chrome is concerned: the |
| 35 | // account menu and its Sign out render, and /signin sends them on. |
| 36 | func TestTeamHomeOnlyMemberGetsSignedInChrome(t *testing.T) { |
| 37 | f := newTeamFixture(t) |
| 38 | f.dir.Set(teamSubject('a'), "member") |
| 39 | f.signInHome(t, teamSubject('a')) |
| 40 | page := f.getHTML(t, "/sheets") |
| 41 | if page.Code != http.StatusOK || !strings.Contains(page.Body.String(), `action="/signout"`) { |
| 42 | t.Fatalf("Home-only member's list = %d without the account menu's sign out", page.Code) |
| 43 | } |
| 44 | if strings.Contains(page.Body.String(), `href="/signin`) { |
| 45 | t.Error("a signed-in Home-only member was offered Sign in") |
| 46 | } |
| 47 | if rec := f.get(t, "/signin"); rec.Code != http.StatusSeeOther { |
| 48 | t.Errorf("/signin for a signed-in Home-only member = %d, want 303", rec.Code) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | // The dashboard feed asks the roster: team sheets while a member, shares |
| 53 | // only once removed, unavailable — not short — under Unknown. |
| 54 | func TestTeamDashboardFeedFollowsTheRoster(t *testing.T) { |
| 55 | t.Setenv("OFICINA_DASHBOARD_TEAM", "blue") |
| 56 | f := newTeamFixtureWith(t, "https://oficina.example.test") |
| 57 | a := teamSubject('a') |
| 58 | f.dir.Set(a, "member") |
| 59 | f.signInHome(t, a) |
| 60 | teamSheet(t, f, "Team plan") |
| 61 | shared := teamSheet(t, f, "Shared") |
| 62 | id, err := teamstate.HomeMember(t.Context(), f.app.team.db, homeTestIssuer, a) |
| 63 | if err != nil { |
| 64 | t.Fatal(err) |
| 65 | } |
| 66 | colleague, _ := f.app.memberFor("colleague@example.com") |
| 67 | if err := f.app.db.Create(&Grant{SheetID: shared.ID, MemberID: uint64(id), Role: string(SheetViewer), GrantedBy: colleague.ID}).Error; err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | feed := func() (int, []string) { |
| 71 | jti, _ := dashboard.ID() |
| 72 | nonce, _ := dashboard.ID() |
| 73 | now := time.Now().Unix() |
| 74 | token, err := dashboard.Sign(f.key, dashboard.Claims{Version: 1, Issuer: homeTestIssuer, Audience: testOrigin, Directory: "https://oficina.example.test", Subject: a, Team: "blue", Purpose: "dashboard-read", KeyID: "key", ID: jti, Nonce: nonce, Issued: now, Expires: now + 120}) |
| 75 | if err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | r := httptest.NewRequest("GET", testOrigin+dashboard.Path, nil) |
| 79 | r.Header.Set("Authorization", "Bearer "+token) |
| 80 | w := httptest.NewRecorder() |
| 81 | f.app.Router().ServeHTTP(w, r) |
| 82 | var data dashboard.Response |
| 83 | var titles []string |
| 84 | if w.Code == http.StatusOK && json.Unmarshal(w.Body.Bytes(), &data) == nil { |
| 85 | for _, it := range data.Items { |
| 86 | titles = append(titles, it.Title) |
| 87 | } |
| 88 | } |
| 89 | return w.Code, titles |
| 90 | } |
| 91 | if code, titles := feed(); code != http.StatusOK || len(titles) != 2 { |
| 92 | t.Fatalf("member's feed = %d %v, want both sheets", code, titles) |
| 93 | } |
| 94 | f.dir.Set(a, "") |
| 95 | f.refreshed(t) |
| 96 | if code, titles := feed(); code != http.StatusOK || len(titles) != 1 || titles[0] != "Shared" { |
| 97 | t.Fatalf("removed member's feed = %d %v, want the share only", code, titles) |
| 98 | } |
| 99 | f.dir.Set(a, "member") |
| 100 | f.refreshed(t) |
| 101 | f.expired(t) |
| 102 | if code, titles := feed(); code == http.StatusOK || len(titles) != 0 { |
| 103 | t.Fatalf("feed under Unknown = %d %v, want unavailable", code, titles) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | // A link stored under another issuer refuses activation, and leaves no |
| 108 | // marker behind. |
| 109 | func TestTeamCensusRefusesAForeignIssuer(t *testing.T) { |
| 110 | f := newTeamApp(t, "", signedInAddress) |
| 111 | app := f.app |
| 112 | m, err := app.memberFor("linked@example.com") |
| 113 | if err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | if err := app.db.Exec("INSERT INTO home_identity_links(issuer,subject,person_id) VALUES(?,?,?)", "https://id.other.example", teamSubject('f'), m.ID).Error; err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | if err := app.ConfigureTeam(); !errors.Is(err, errTeamConfig) { |
| 120 | t.Fatalf("ConfigureTeam over a foreign issuer's link = %v, want refusal", err) |
| 121 | } |
| 122 | d, _ := app.db.DB() |
| 123 | if _, activated, err := teamState.Required(t.Context(), d); err != nil || activated { |
| 124 | t.Fatal("a refused activation left a marker") |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // connect's same-person rule: a known Home-only member arriving over |
| 129 | // somebody else's live local session is refused, and that session stays. |
| 130 | func TestTeamKnownArrivalOverAnotherSessionIsRefused(t *testing.T) { |
| 131 | f := newTeamFixture(t) |
| 132 | a := teamSubject('a') |
| 133 | f.dir.Set(a, "member") |
| 134 | f.signInHome(t, a) |
| 135 | f.jar = map[string]*http.Cookie{} |
| 136 | f.local(t, "x@example.com") |
| 137 | local := f.jar[f.app.homeSessions.CookieName()] |
| 138 | start := f.request(t, "GET", "/auth/start?return_to=%2Fsheets", nil, false) |
| 139 | if fin := f.exchange(t, start, a); fin.Code != http.StatusForbidden { |
| 140 | t.Fatalf("known member over another local session = %d, want 403", fin.Code) |
| 141 | } |
| 142 | if got := f.jar[f.app.homeSessions.CookieName()]; got == nil || got.Value != local.Value { |
| 143 | t.Fatal("the local session did not survive the refused arrival") |
| 144 | } |
| 145 | if m := f.viewer(t); m.Address != "x@example.com" { |
| 146 | t.Fatalf("the session now acts as %q", m.Label()) |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Paul, 2026-10-28: team owners can share orphaned sheets. A roster owner |
| 151 | // holds SheetOwner on a sheet with no live owner grant; a roster member |
| 152 | // does not; a sheet with a live owner is untouched; and the rule stops |
| 153 | // applying the moment the sheet has a live owner again. Home-only members |
| 154 | // are labelled with their ref. |
| 155 | func TestTeamRosterOwnerSharesOrphanedSheets(t *testing.T) { |
| 156 | f := newTeamFixture(t) |
| 157 | o, a, m := teamSubject('o'), teamSubject('a'), teamSubject('m') |
| 158 | f.dir.Set(o, "owner") |
| 159 | f.dir.Set(a, "member") |
| 160 | f.dir.Set(m, "member") |
| 161 | create := func(subject, title string) string { |
| 162 | f.signInHome(t, subject) |
| 163 | rec := f.request(t, "POST", "/sheets", url.Values{"title": {title}}, false) |
| 164 | if rec.Code != http.StatusSeeOther { |
| 165 | t.Fatalf("%s create = %d", title, rec.Code) |
| 166 | } |
| 167 | return rec.Header().Get("Location") |
| 168 | } |
| 169 | orphan := create(a, "Orphan") |
| 170 | live := create(m, "Live") |
| 171 | f.dir.Set(a, "") |
| 172 | f.refreshed(t) |
| 173 | |
| 174 | f.signInHome(t, m) |
| 175 | if rec := f.get(t, orphan+"/share"); rec.Code != http.StatusNotFound { |
| 176 | t.Errorf("roster member on an orphaned sheet's sharing = %d, want 404", rec.Code) |
| 177 | } |
| 178 | f.signInHome(t, o) |
| 179 | if rec := f.get(t, live+"/share"); rec.Code != http.StatusNotFound { |
| 180 | t.Errorf("roster owner on a live-owned sheet's sharing = %d, want 404", rec.Code) |
| 181 | } |
| 182 | page := f.getHTML(t, orphan+"/share") |
| 183 | if page.Code != http.StatusOK { |
| 184 | t.Fatalf("roster owner on an orphaned sheet's sharing = %d, want 200", page.Code) |
| 185 | } |
| 186 | if !strings.Contains(page.Body.String(), teamLabel+" …"+a[len(a)-4:]) { |
| 187 | t.Error("the departed Home-only owner is not labelled with their ref") |
| 188 | } |
| 189 | if rec := f.request(t, "POST", orphan+"/share", url.Values{"address": {"new@example.com"}, "role": {"owner"}}, false); rec.Code != http.StatusSeeOther { |
| 190 | t.Fatalf("roster owner sharing an orphaned sheet = %d", rec.Code) |
| 191 | } |
| 192 | if rec := f.get(t, orphan+"/share"); rec.Code != http.StatusNotFound { |
| 193 | t.Errorf("after giving it a live owner, the roster owner's sharing = %d, want 404", rec.Code) |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Revoking a current Home-only member's grant names them in the notice by |
| 198 | // label and ref, never by an address they do not have. |
| 199 | func TestTeamRevokeNoticeNamesAHomeOnlyMember(t *testing.T) { |
| 200 | f := newTeamFixture(t) |
| 201 | a := teamSubject('a') |
| 202 | f.dir.Set(a, "member") |
| 203 | f.signInHome(t, a) |
| 204 | rec := f.request(t, "POST", "/sheets", url.Values{"title": {"Mine"}}, false) |
| 205 | sheet := rec.Header().Get("Location") |
| 206 | if rec := f.request(t, "POST", sheet+"/share", url.Values{"address": {"new@example.com"}, "role": {"owner"}}, false); rec.Code != http.StatusSeeOther { |
| 207 | t.Fatalf("share = %d", rec.Code) |
| 208 | } |
| 209 | id, err := teamstate.HomeMember(t.Context(), f.app.team.db, homeTestIssuer, a) |
| 210 | if err != nil { |
| 211 | t.Fatal(err) |
| 212 | } |
| 213 | f.jar = map[string]*http.Cookie{} |
| 214 | f.local(t, "new@example.com") |
| 215 | revoke := f.request(t, "POST", sheet+"/share/revoke", url.Values{"member": {strconv.FormatInt(id, 10)}}, false) |
| 216 | if revoke.Code != http.StatusSeeOther { |
| 217 | t.Fatalf("revoke = %d", revoke.Code) |
| 218 | } |
| 219 | page := f.getHTML(t, revoke.Header().Get("Location")) |
| 220 | want := "since " + teamLabel + " …" + a[len(a)-4:] + " is on the team" |
| 221 | if !strings.Contains(page.Body.String(), want) { |
| 222 | t.Fatalf("revoke notice does not name the Home-only member as %q", want) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // O3: the roster-owner rule never reaches a sheet that does not exist — |
| 227 | // deleted or still filling — so it cannot resurrect a deleted sheet's |
| 228 | // sharing. |
| 229 | func TestTeamRosterOwnerRuleSkipsDeletedSheets(t *testing.T) { |
| 230 | f := newTeamFixture(t) |
| 231 | o := teamSubject('o') |
| 232 | f.dir.Set(o, "owner") |
| 233 | gone := teamSheet(t, f, "Gone") // its creator is not on the roster: orphaned |
| 234 | f.signInHome(t, o) |
| 235 | if rec := f.get(t, sheetPath(gone.ID)+"/share"); rec.Code != http.StatusOK { |
| 236 | t.Fatalf("roster owner on an orphaned sheet's sharing = %d, want 200", rec.Code) |
| 237 | } |
| 238 | if err := f.app.db.Exec("UPDATE sheets SET deleted_at = ? WHERE id = ?", time.Now().UTC(), gone.ID).Error; err != nil { |
| 239 | t.Fatal(err) |
| 240 | } |
| 241 | if rec := f.get(t, sheetPath(gone.ID)+"/share"); rec.Code != http.StatusNotFound { |
| 242 | t.Errorf("roster owner on a deleted sheet's sharing = %d, want 404", rec.Code) |
| 243 | } |
| 244 | if f.app.mayShare(gone.ID, f.viewer(t)) { |
| 245 | t.Error("the roster-owner rule reached a deleted sheet") |
| 246 | } |
| 247 | } |
| 248 | |