| 1 | package sheets |
| 2 | |
| 3 | // Team mode's write guard and identity, continued from team_guard_test.go: |
| 4 | // the cases Fable's review on #53 added, and the storage failures. |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "fmt" |
| 10 | "net/http" |
| 11 | "net/url" |
| 12 | "path/filepath" |
| 13 | "testing" |
| 14 | "time" |
| 15 | |
| 16 | "amadan.net/oficina/home/connect" |
| 17 | "amadan.net/oficina/home/roster" |
| 18 | "amadan.net/oficina/home/teammode" |
| 19 | rdb "amadan.net/rastrillo/rastrillo/db" |
| 20 | ) |
| 21 | |
| 22 | // Fable's note 2 on #53: a lower share from somebody else does not carry |
| 23 | // a write the team rung admitted through a lapse. A share outranks the team |
| 24 | // rung in roleFor, so the one place a lower share sits under a higher role |
| 25 | // is the roster-owner rule: a roster owner holding a viewer share on an |
| 26 | // orphaned sheet may share it as owner, and that rests on standing. |
| 27 | func TestTeamLowerShareDoesNotCarryALapsingWrite(t *testing.T) { |
| 28 | f := newTeamFixture(t) |
| 29 | o, a := teamSubject('o'), teamSubject('a') |
| 30 | f.dir.Set(o, "owner") |
| 31 | f.dir.Set(a, "member") |
| 32 | f.signInHome(t, a) |
| 33 | created := f.request(t, "POST", "/sheets", url.Values{"title": {"Orphan"}}, false) |
| 34 | if created.Code != http.StatusSeeOther { |
| 35 | t.Fatalf("create = %d", created.Code) |
| 36 | } |
| 37 | orphan := created.Header().Get("Location") |
| 38 | var sheetID uint64 |
| 39 | fmt.Sscanf(orphan, "/sheets/%d", &sheetID) |
| 40 | f.dir.Set(a, "") |
| 41 | f.refreshed(t) |
| 42 | |
| 43 | f.signInHome(t, o) |
| 44 | _, owner := f.teamWriter(t) |
| 45 | colleague, _ := f.app.memberFor("colleague@example.com") |
| 46 | if err := f.app.db.Create(&Grant{SheetID: sheetID, MemberID: owner.ID, Role: string(SheetViewer), GrantedBy: colleague.ID}).Error; err != nil { |
| 47 | t.Fatal(err) |
| 48 | } |
| 49 | if rec := f.get(t, orphan+"/share"); rec.Code != http.StatusOK { |
| 50 | t.Fatalf("roster owner with a viewer share on an orphaned sheet's sharing = %d, want 200", rec.Code) |
| 51 | } |
| 52 | teamGuard = func(ctx context.Context, authorize func(roster.State) error, write func() error) error { |
| 53 | return teammode.Guard(ctx, authorize, func() error { |
| 54 | if err := write(); err != nil { |
| 55 | return err |
| 56 | } |
| 57 | f.clock.Step(roster.MaximumAge + time.Second) |
| 58 | return nil |
| 59 | }) |
| 60 | } |
| 61 | t.Cleanup(func() { teamGuard = teammode.Guard }) |
| 62 | if rec := f.request(t, "POST", orphan+"/share", url.Values{"address": {"new@example.com"}, "role": {"viewer"}}, false); rec.Code != http.StatusServiceUnavailable { |
| 63 | t.Errorf("an owner's share resting on standing, lease lapsing = %d, want 503", rec.Code) |
| 64 | } |
| 65 | teamGuard = teammode.Guard |
| 66 | var n int64 |
| 67 | 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 { |
| 68 | t.Errorf("a refused share committed %d grant(s)", n) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | // Fable's note 1 on #53: storage that cannot say who a session is answers |
| 73 | // a retryable 503, and keeps the session; it never signs the person out. |
| 74 | func TestTeamIdentityStorageFailureIsUnavailableNotSignOut(t *testing.T) { |
| 75 | f := newTeamFixture(t, "x@example.com") |
| 76 | f.local(t, "x@example.com") |
| 77 | session := f.jar[f.app.homeSessions.CookieName()] |
| 78 | closed, err := rdb.Open(filepath.Join(t.TempDir(), "closed.db"), quietLogger()) |
| 79 | if err != nil { |
| 80 | t.Fatal(err) |
| 81 | } |
| 82 | closed.Close() |
| 83 | f.app.team.db = closed.Writer() |
| 84 | if rec := f.get(t, "/sheets"); rec.Code != http.StatusServiceUnavailable { |
| 85 | t.Errorf("identity over unreadable storage = %d, want 503", rec.Code) |
| 86 | } |
| 87 | if f.jar[f.app.homeSessions.CookieName()] != session { |
| 88 | t.Error("a storage failure signed the person out") |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | func mustList(t *testing.T, f *teamFixture) []SheetSummary { |
| 93 | t.Helper() |
| 94 | _, me := f.teamWriter(t) |
| 95 | list, err := f.app.listSheets(me) |
| 96 | if err != nil { |
| 97 | t.Fatal(err) |
| 98 | } |
| 99 | return list |
| 100 | } |
| 101 | |
| 102 | // A Home-only lookup that cannot read storage is "cannot decide", never |
| 103 | // "nobody": connect would otherwise go on to allocate a member for a |
| 104 | // subject that may already have one. |
| 105 | func TestTeamHomeOnlyStorageFailureIsNotNobody(t *testing.T) { |
| 106 | f := newTeamFixture(t) |
| 107 | d, err := f.app.db.DB() |
| 108 | if err != nil { |
| 109 | t.Fatal(err) |
| 110 | } |
| 111 | closed, err := rdb.Open(filepath.Join(t.TempDir(), "closed.db"), quietLogger()) |
| 112 | if err != nil { |
| 113 | t.Fatal(err) |
| 114 | } |
| 115 | closed.Close() |
| 116 | if _, err := f.app.teamHomeOnly(closed.Writer())(t.Context(), homeTestIssuer, teamSubject('a')); err == nil || errors.Is(err, connect.ErrNoPerson) { |
| 117 | t.Errorf("HomeOnly over unreadable storage = %v, want unavailable", err) |
| 118 | } |
| 119 | if _, err := f.app.teamHomeOnly(d)(t.Context(), homeTestIssuer, teamSubject('a')); !errors.Is(err, connect.ErrNoPerson) { |
| 120 | t.Errorf("HomeOnly for nobody = %v, want ErrNoPerson", err) |
| 121 | } |
| 122 | } |
| 123 | |