| 1 | package sheets |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/home/roster" |
| 13 | |
| 14 | "amadan.net/oficina/sheets/internal/sheets/store" |
| 15 | ) |
| 16 | |
| 17 | func openTeamStream(t *testing.T, f *teamFixture, base string, s SheetSummary) *http.Response { |
| 18 | t.Helper() |
| 19 | ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) |
| 20 | t.Cleanup(cancel) |
| 21 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/api/sheets/%d/tabs/%d/stream", base, s.ID, s.TabID), nil) |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | req.Host = strings.TrimPrefix(testOrigin, "https://") |
| 26 | for _, c := range f.jar { |
| 27 | req.AddCookie(c) |
| 28 | } |
| 29 | resp, err := http.DefaultClient.Do(req) |
| 30 | if err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | t.Cleanup(func() { resp.Body.Close() }) |
| 34 | if resp.StatusCode != http.StatusOK { |
| 35 | t.Fatalf("stream = %d", resp.StatusCode) |
| 36 | } |
| 37 | return resp |
| 38 | } |
| 39 | |
| 40 | // A silent stream must not outlive the lease its standing rests on. The |
| 41 | // stream is opened a second before the lease ends with the directory |
| 42 | // down; nothing is written to the tab, and the stream still says bye. |
| 43 | func TestTeamStreamClosesAtLeaseExpiry(t *testing.T) { |
| 44 | f := newTeamFixture(t) |
| 45 | plan := teamSheet(t, f, "Team plan") |
| 46 | a := teamSubject('a') |
| 47 | f.dir.Set(a, "member") |
| 48 | f.signInHome(t, a) |
| 49 | f.dir.SetDown(true) |
| 50 | f.clock.Step(59 * time.Second) |
| 51 | |
| 52 | srv := liveServer(t, f.app) |
| 53 | resp := openTeamStream(t, f, srv.URL, plan) |
| 54 | waitForSubscriber(t, f.app, plan.TabID) |
| 55 | f.clock.Step(2 * time.Second) |
| 56 | if got := readEventWithin(t, resp.Body, 5*time.Second); !strings.Contains(got, "access-changed") { |
| 57 | t.Fatalf("a stream past its lease sent %q, want bye", got) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // Renewal: at the lease deadline the stream asks again, and while the |
| 62 | // roster still says Member it re-arms and carries on delivering. |
| 63 | // |
| 64 | // Set up so no refresh is in flight when the stream opens (one had just |
| 65 | // failed, and the cache backs off), because a publish moves the cache's |
| 66 | // version and ends every older lease: the stream opens on a lease with a |
| 67 | // second left, the directory is back, and the deadline's re-check must |
| 68 | // fetch a new roster to keep it open. |
| 69 | func TestTeamStreamRenewsWhileStillAMember(t *testing.T) { |
| 70 | f := newTeamFixture(t) |
| 71 | plan := teamSheet(t, f, "Team plan") |
| 72 | a := teamSubject('a') |
| 73 | f.dir.Set(a, "member") |
| 74 | f.signInHome(t, a) |
| 75 | f.clock.Step(59 * time.Second) |
| 76 | f.dir.SetDown(true) |
| 77 | if err := f.app.team.Refresh(t.Context()); err == nil { |
| 78 | t.Fatal("refresh succeeded with the directory down") |
| 79 | } |
| 80 | f.dir.SetDown(false) |
| 81 | |
| 82 | srv := liveServer(t, f.app) |
| 83 | resp := openTeamStream(t, f, srv.URL, plan) |
| 84 | waitForSubscriber(t, f.app, plan.TabID) |
| 85 | f.clock.Step(6 * time.Second) // past the lease and the backoff |
| 86 | time.Sleep(1500 * time.Millisecond) // past the stream's lease timer |
| 87 | cells := fmt.Sprintf("/api/sheets/%d/tabs/%d/cells", plan.ID, plan.TabID) |
| 88 | if post := postCellAs(t, f, cells, `{"r":0,"c":0,"raw":"still here"}`); post.Code != http.StatusOK { |
| 89 | t.Fatalf("member write = %d %s", post.Code, post.Body.String()) |
| 90 | } |
| 91 | if got := readEventWithin(t, resp.Body, 5*time.Second); !strings.Contains(got, "still here") { |
| 92 | t.Fatalf("renewed stream sent %q", got) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func postCellAs(t *testing.T, f *teamFixture, path, body string) *httptest.ResponseRecorder { |
| 97 | t.Helper() |
| 98 | req := httptest.NewRequest(http.MethodPost, testOrigin+path, strings.NewReader(body)) |
| 99 | req.Header.Set("Content-Type", "application/json") |
| 100 | req.Header.Set("Origin", testOrigin) |
| 101 | for _, c := range f.jar { |
| 102 | req.AddCookie(c) |
| 103 | } |
| 104 | return do(t, f.app, req) |
| 105 | } |
| 106 | |
| 107 | // A decision held past its lease is not standing, even mid-request: the |
| 108 | // lease is checked each time roleFor asks, not once at the door. |
| 109 | func TestTeamLeaseEndsAHeldDecision(t *testing.T) { |
| 110 | f := newTeamFixture(t) |
| 111 | plan := teamSheet(t, f, "Team plan") |
| 112 | a := teamSubject('a') |
| 113 | f.dir.Set(a, "member") |
| 114 | f.signInHome(t, a) |
| 115 | m := f.viewer(t) |
| 116 | if !f.app.mayWrite(plan.ID, m) { |
| 117 | t.Fatal("member cannot write a team sheet") |
| 118 | } |
| 119 | f.dir.SetDown(true) |
| 120 | f.clock.Step(59 * time.Second) |
| 121 | if !f.app.mayWrite(plan.ID, m) { |
| 122 | t.Fatal("standing ended inside the lease") |
| 123 | } |
| 124 | f.clock.Step(2 * time.Second) |
| 125 | if f.app.mayWrite(plan.ID, m) || f.app.mayRead(plan.ID, m) { |
| 126 | t.Fatal("a held decision outlived its lease") |
| 127 | } |
| 128 | if m.team.state() != roster.Unknown { |
| 129 | t.Fatal("an expired lease was not Unknown") |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | // A reach through the team rung never stands without a lease. The roster |
| 134 | // clock ticks a millisecond on every reading, and the lease's end is |
| 135 | // swept across each reading streamAccess makes in turn, so for some step |
| 136 | // it lapses after the reach has been decided and before standing is read |
| 137 | // (oficina/slides #33 found that window in Slides; Sheets had the same |
| 138 | // order). Whatever the step, the stream either closes or re-decides on a |
| 139 | // timer, never "reaches, nothing to renew". A share is the one thing |
| 140 | // that holds without a lease, and it holds at every step. |
| 141 | func TestTeamTeamRungReachNeverStandsWithoutALease(t *testing.T) { |
| 142 | for _, shared := range []bool{false, true} { |
| 143 | var opened, closed int |
| 144 | for k := 1; k <= 40; k++ { |
| 145 | f := newTeamFixture(t) |
| 146 | plan := teamSheet(t, f, "Team plan") |
| 147 | a := teamSubject('a') |
| 148 | f.dir.Set(a, "member") |
| 149 | f.signInHome(t, a) |
| 150 | if shared { |
| 151 | colleague, err := f.app.memberFor("colleague@example.com") |
| 152 | if err != nil { |
| 153 | t.Fatal(err) |
| 154 | } |
| 155 | me := f.viewer(t) |
| 156 | if err := f.app.db.Create(&Grant{SheetID: plan.ID, MemberID: me.ID, Role: string(SheetViewer), GrantedBy: colleague.ID}).Error; err != nil { |
| 157 | t.Fatal(err) |
| 158 | } |
| 159 | } |
| 160 | r := httptest.NewRequest("GET", testOrigin+"/api/sheets", nil) |
| 161 | for _, c := range f.jar { |
| 162 | r.AddCookie(c) |
| 163 | } |
| 164 | |
| 165 | f.dir.SetDown(true) |
| 166 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 167 | f.clock.SetTick(time.Millisecond) |
| 168 | ok, renew := f.app.streamAccess(r, plan.ID) |
| 169 | f.clock.SetTick(0) |
| 170 | |
| 171 | switch { |
| 172 | case shared && !ok: |
| 173 | t.Fatalf("k=%d: a share did not hold through a lapse", k) |
| 174 | case !shared && ok && renew <= 0: |
| 175 | t.Fatalf("k=%d: a stream on the team rung stands with nothing to renew", k) |
| 176 | case ok: |
| 177 | opened++ |
| 178 | default: |
| 179 | closed++ |
| 180 | } |
| 181 | } |
| 182 | // The sweep must cross the lease's end: some steps decide before |
| 183 | // it (open, on a timer) and some after (closed, or a share). |
| 184 | if !shared && (opened == 0 || closed == 0) { |
| 185 | t.Fatalf("the sweep did not cross the lease's end: %d open, %d closed", opened, closed) |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Standing is not reach: a Member's stream on a sheet deleted since it |
| 191 | // opened ends at the next decision, lease or no lease. |
| 192 | func TestTeamMemberStreamEndsWithItsSheet(t *testing.T) { |
| 193 | f := newTeamFixture(t) |
| 194 | plan := teamSheet(t, f, "Team plan") |
| 195 | a := teamSubject('a') |
| 196 | f.dir.Set(a, "member") |
| 197 | f.signInHome(t, a) |
| 198 | r := httptest.NewRequest("GET", testOrigin+"/api/sheets", nil) |
| 199 | for _, c := range f.jar { |
| 200 | r.AddCookie(c) |
| 201 | } |
| 202 | if ok, renew := f.app.streamAccess(r, plan.ID); !ok || renew <= 0 { |
| 203 | t.Fatalf("a member's stream on a team sheet = %v, %v; want open on a lease", ok, renew) |
| 204 | } |
| 205 | if err := f.app.db.Model(&store.Sheet{}).Where("id = ?", plan.ID).Update("deleted_at", time.Now()).Error; err != nil { |
| 206 | t.Fatal(err) |
| 207 | } |
| 208 | if ok, _ := f.app.streamAccess(r, plan.ID); ok { |
| 209 | t.Fatal("a member's stream outlived its deleted sheet") |
| 210 | } |
| 211 | } |
| 212 | |