oficina / sheets Public

Sheets: split the guard tests under the 300-line cap

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev a779d57135d71d5f978652b76eaf87fef15ab54a parent 7bae5e3
2 files changed, +122 −105
  • internal/sheets/team_guard_more_test.go +122 −0
  • internal/sheets/team_guard_test.go +0 −105

CI passed — run details

diff --git a/internal/sheets/team_guard_more_test.go b/internal/sheets/team_guard_more_test.go
new file mode 100644
index 0000000..1f72b30
--- /dev/null
+++ b/internal/sheets/team_guard_more_test.go
@@ -0,0 +1,122 @@
+package sheets
+
+// Team mode's write guard and identity, continued from team_guard_test.go:
+// the cases Fable's review on #53 added, and the storage failures.
+
+import (
+ "context"
+ "errors"
+ "fmt"
+ "net/http"
+ "net/url"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/home/connect"
+ "amadan.net/oficina/home/roster"
+ "amadan.net/oficina/home/teammode"
+ rdb "amadan.net/rastrillo/rastrillo/db"
+)
+
+// Fable's note 2 on #53: a lower share from somebody else does not carry
+// a write the team rung admitted through a lapse. A share outranks the team
+// rung in roleFor, so the one place a lower share sits under a higher role
+// is the roster-owner rule: a roster owner holding a viewer share on an
+// orphaned sheet may share it as owner, and that rests on standing.
+func TestTeamLowerShareDoesNotCarryALapsingWrite(t *testing.T) {
+ f := newTeamFixture(t)
+ o, a := teamSubject('o'), teamSubject('a')
+ f.dir.Set(o, "owner")
+ f.dir.Set(a, "member")
+ f.signInHome(t, a)
+ created := f.request(t, "POST", "/sheets", url.Values{"title": {"Orphan"}}, false)
+ if created.Code != http.StatusSeeOther {
+ t.Fatalf("create = %d", created.Code)
+ }
+ orphan := created.Header().Get("Location")
+ var sheetID uint64
+ fmt.Sscanf(orphan, "/sheets/%d", &sheetID)
+ f.dir.Set(a, "")
+ f.refreshed(t)
+
+ f.signInHome(t, o)
+ _, owner := f.teamWriter(t)
+ colleague, _ := f.app.memberFor("colleague@example.com")
+ if err := f.app.db.Create(&Grant{SheetID: sheetID, MemberID: owner.ID, Role: string(SheetViewer), GrantedBy: colleague.ID}).Error; err != nil {
+ t.Fatal(err)
+ }
+ if rec := f.get(t, orphan+"/share"); rec.Code != http.StatusOK {
+ t.Fatalf("roster owner with a viewer share on an orphaned sheet's sharing = %d, want 200", rec.Code)
+ }
+ teamGuard = func(ctx context.Context, authorize func(roster.State) error, write func() error) error {
+ return teammode.Guard(ctx, authorize, func() error {
+ if err := write(); err != nil {
+ return err
+ }
+ f.clock.Step(roster.MaximumAge + time.Second)
+ return nil
+ })
+ }
+ t.Cleanup(func() { teamGuard = teammode.Guard })
+ if rec := f.request(t, "POST", orphan+"/share", url.Values{"address": {"new@example.com"}, "role": {"viewer"}}, false); rec.Code != http.StatusServiceUnavailable {
+ t.Errorf("an owner's share resting on standing, lease lapsing = %d, want 503", rec.Code)
+ }
+ teamGuard = teammode.Guard
+ var n int64
+ if f.app.db.Model(&Grant{}).Joins("JOIN members ON members.id = grants.member_id").Where("members.address = ?", "new@example.com").Count(&n); n != 0 {
+ t.Errorf("a refused share committed %d grant(s)", n)
+ }
+}
+
+// Fable's note 1 on #53: storage that cannot say who a session is answers
+// a retryable 503, and keeps the session; it never signs the person out.
+func TestTeamIdentityStorageFailureIsUnavailableNotSignOut(t *testing.T) {
+ f := newTeamFixture(t, "x@example.com")
+ f.local(t, "x@example.com")
+ session := f.jar[f.app.homeSessions.CookieName()]
+ closed, err := rdb.Open(filepath.Join(t.TempDir(), "closed.db"), quietLogger())
+ if err != nil {
+ t.Fatal(err)
+ }
+ closed.Close()
+ f.app.team.db = closed.Writer()
+ if rec := f.get(t, "/sheets"); rec.Code != http.StatusServiceUnavailable {
+ t.Errorf("identity over unreadable storage = %d, want 503", rec.Code)
+ }
+ if f.jar[f.app.homeSessions.CookieName()] != session {
+ t.Error("a storage failure signed the person out")
+ }
+}
+
+func mustList(t *testing.T, f *teamFixture) []SheetSummary {
+ t.Helper()
+ _, me := f.teamWriter(t)
+ list, err := f.app.listSheets(me)
+ if err != nil {
+ t.Fatal(err)
+ }
+ return list
+}
+
+// A Home-only lookup that cannot read storage is "cannot decide", never
+// "nobody": connect would otherwise go on to allocate a member for a
+// subject that may already have one.
+func TestTeamHomeOnlyStorageFailureIsNotNobody(t *testing.T) {
+ f := newTeamFixture(t)
+ d, err := f.app.db.DB()
+ if err != nil {
+ t.Fatal(err)
+ }
+ closed, err := rdb.Open(filepath.Join(t.TempDir(), "closed.db"), quietLogger())
+ if err != nil {
+ t.Fatal(err)
+ }
+ closed.Close()
+ if _, err := f.app.teamHomeOnly(closed.Writer())(t.Context(), homeTestIssuer, teamSubject('a')); err == nil || errors.Is(err, connect.ErrNoPerson) {
+ t.Errorf("HomeOnly over unreadable storage = %v, want unavailable", err)
+ }
+ if _, err := f.app.teamHomeOnly(d)(t.Context(), homeTestIssuer, teamSubject('a')); !errors.Is(err, connect.ErrNoPerson) {
+ t.Errorf("HomeOnly for nobody = %v, want ErrNoPerson", err)
+ }
+}
diff --git a/internal/sheets/team_guard_test.go b/internal/sheets/team_guard_test.go
index c681524..c312fba 100644
--- a/internal/sheets/team_guard_test.go
+++ b/internal/sheets/team_guard_test.go
@@ -7,14 +7,11 @@ import (
"net/http"
"net/http/httptest"
"net/url"
- "path/filepath"
"testing"
"time"
- "amadan.net/oficina/home/connect"
"amadan.net/oficina/home/roster"
"amadan.net/oficina/home/teammode"
- rdb "amadan.net/rastrillo/rastrillo/db"
"amadan.net/oficina/sheets/internal/sheets/store"
)
@@ -267,105 +264,3 @@ func TestTeamEveryWriteRouteRefusesALapseBeforeCommit(t *testing.T) {
t.Errorf("sizes after a refused resize = %v %v, want none", cols, rows)
}
}
-
-// Fable's note 2 on #53: a lower share from somebody else does not carry
-// a write the team rung admitted through a lapse. A share outranks the team
-// rung in roleFor, so the one place a lower share sits under a higher role
-// is the roster-owner rule: a roster owner holding a viewer share on an
-// orphaned sheet may share it as owner, and that rests on standing.
-func TestTeamLowerShareDoesNotCarryALapsingWrite(t *testing.T) {
- f := newTeamFixture(t)
- o, a := teamSubject('o'), teamSubject('a')
- f.dir.Set(o, "owner")
- f.dir.Set(a, "member")
- f.signInHome(t, a)
- created := f.request(t, "POST", "/sheets", url.Values{"title": {"Orphan"}}, false)
- if created.Code != http.StatusSeeOther {
- t.Fatalf("create = %d", created.Code)
- }
- orphan := created.Header().Get("Location")
- var sheetID uint64
- fmt.Sscanf(orphan, "/sheets/%d", &sheetID)
- f.dir.Set(a, "")
- f.refreshed(t)
-
- f.signInHome(t, o)
- _, owner := f.teamWriter(t)
- colleague, _ := f.app.memberFor("colleague@example.com")
- if err := f.app.db.Create(&Grant{SheetID: sheetID, MemberID: owner.ID, Role: string(SheetViewer), GrantedBy: colleague.ID}).Error; err != nil {
- t.Fatal(err)
- }
- if rec := f.get(t, orphan+"/share"); rec.Code != http.StatusOK {
- t.Fatalf("roster owner with a viewer share on an orphaned sheet's sharing = %d, want 200", rec.Code)
- }
- teamGuard = func(ctx context.Context, authorize func(roster.State) error, write func() error) error {
- return teammode.Guard(ctx, authorize, func() error {
- if err := write(); err != nil {
- return err
- }
- f.clock.Step(roster.MaximumAge + time.Second)
- return nil
- })
- }
- t.Cleanup(func() { teamGuard = teammode.Guard })
- if rec := f.request(t, "POST", orphan+"/share", url.Values{"address": {"new@example.com"}, "role": {"viewer"}}, false); rec.Code != http.StatusServiceUnavailable {
- t.Errorf("an owner's share resting on standing, lease lapsing = %d, want 503", rec.Code)
- }
- teamGuard = teammode.Guard
- var n int64
- if f.app.db.Model(&Grant{}).Joins("JOIN members ON members.id = grants.member_id").Where("members.address = ?", "new@example.com").Count(&n); n != 0 {
- t.Errorf("a refused share committed %d grant(s)", n)
- }
-}
-
-// Fable's note 1 on #53: storage that cannot say who a session is answers
-// a retryable 503, and keeps the session; it never signs the person out.
-func TestTeamIdentityStorageFailureIsUnavailableNotSignOut(t *testing.T) {
- f := newTeamFixture(t, "x@example.com")
- f.local(t, "x@example.com")
- session := f.jar[f.app.homeSessions.CookieName()]
- closed, err := rdb.Open(filepath.Join(t.TempDir(), "closed.db"), quietLogger())
- if err != nil {
- t.Fatal(err)
- }
- closed.Close()
- f.app.team.db = closed.Writer()
- if rec := f.get(t, "/sheets"); rec.Code != http.StatusServiceUnavailable {
- t.Errorf("identity over unreadable storage = %d, want 503", rec.Code)
- }
- if f.jar[f.app.homeSessions.CookieName()] != session {
- t.Error("a storage failure signed the person out")
- }
-}
-
-func mustList(t *testing.T, f *teamFixture) []SheetSummary {
- t.Helper()
- _, me := f.teamWriter(t)
- list, err := f.app.listSheets(me)
- if err != nil {
- t.Fatal(err)
- }
- return list
-}
-
-// A Home-only lookup that cannot read storage is "cannot decide", never
-// "nobody": connect would otherwise go on to allocate a member for a
-// subject that may already have one.
-func TestTeamHomeOnlyStorageFailureIsNotNobody(t *testing.T) {
- f := newTeamFixture(t)
- d, err := f.app.db.DB()
- if err != nil {
- t.Fatal(err)
- }
- closed, err := rdb.Open(filepath.Join(t.TempDir(), "closed.db"), quietLogger())
- if err != nil {
- t.Fatal(err)
- }
- closed.Close()
- if _, err := f.app.teamHomeOnly(closed.Writer())(t.Context(), homeTestIssuer, teamSubject('a')); err == nil || errors.Is(err, connect.ErrNoPerson) {
- t.Errorf("HomeOnly over unreadable storage = %v, want unavailable", err)
- }
- if _, err := f.app.teamHomeOnly(d)(t.Context(), homeTestIssuer, teamSubject('a')); !errors.Is(err, connect.ErrNoPerson) {
- t.Errorf("HomeOnly for nobody = %v, want ErrNoPerson", err)
- }
-}