| 1 | package calls |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "amadan.net/oficina/home/roster" |
| 12 | "amadan.net/oficina/home/teammode" |
| 13 | ) |
| 14 | |
| 15 | // A held decision ends at the lease under a failing refresh, existing |
| 16 | // sessions included: at 59 s the list is whole and the meeting opens; at |
| 17 | // 61 s the list is the partial notice with nothing in it, every item page |
| 18 | // and form is a retryable 503 (a missing meeting too: not "not found"), |
| 19 | // and the member is still signed in. |
| 20 | func TestTeamStandingEndsAtTheLeaseUnderAFailingRefresh(t *testing.T) { |
| 21 | f := newTeamFixture(t) |
| 22 | f.member(teamSubject('l'), "member") |
| 23 | sqid, _ := f.meeting("Quarterly review") |
| 24 | f.refreshed() |
| 25 | f.dir.SetDown(true) |
| 26 | f.clock.Step(roster.MaximumAge - time.Second) |
| 27 | if w := f.page("/"); w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "Quarterly review") || strings.Contains(w.Body.String(), "data-partial") { |
| 28 | t.Fatalf("59 s list = %d, want the whole list", w.Code) |
| 29 | } |
| 30 | if w := f.page("/m/" + sqid + "/join"); w.Code != http.StatusOK { |
| 31 | t.Fatalf("59 s holding screen = %d", w.Code) |
| 32 | } |
| 33 | f.clock.Step(2 * time.Second) |
| 34 | w := f.page("/") |
| 35 | if w.Code != http.StatusOK || strings.Contains(w.Body.String(), "Quarterly review") || !strings.Contains(w.Body.String(), "data-partial") { |
| 36 | t.Fatalf("61 s list = %d, want partial and without the meeting:\n%s", w.Code, w.Body.String()) |
| 37 | } |
| 38 | for _, path := range []string{ |
| 39 | "/m/" + sqid + "/join", "/m/" + sqid, "/m/" + sqid + "/info", "/m/" + sqid + "/app", |
| 40 | "/meetings/" + sqid + "/settings", "/meetings/" + sqid + "/end", "/meetings/" + sqid + "/links/replace", |
| 41 | "/settings", "/m/2222222222/join", "/meetings/2222222222/settings", |
| 42 | } { |
| 43 | retryable(t, "61 s "+path, f.page(path)) |
| 44 | } |
| 45 | for path, body := range map[string]string{ |
| 46 | "/meetings": "title=Later", "/meetings/start": "", "/m/" + sqid + "/share": "", |
| 47 | "/meetings/" + sqid + "/links": "", "/meetings/" + sqid + "/end": "", "/settings/defaults": "", |
| 48 | "/meetings/" + sqid + "/settings": "", "/settings/agents": "name=bot", |
| 49 | } { |
| 50 | retryable(t, "61 s POST "+path, f.post(path, body)) |
| 51 | } |
| 52 | retryable(t, "61 s stream for a missing meeting", f.doWith("GET", "/m/2222222222/stream", "", http.Header{"Accept": {"text/event-stream"}})) |
| 53 | retryable(t, "61 s native start", f.json("/native/meetings/start", "{}")) |
| 54 | retryable(t, "61 s native device", f.json("/native/devices", `{"token":"aabb","platform":"ios"}`)) |
| 55 | if f.cookies[f.app.home.sessions.CookieName()] == nil { |
| 56 | t.Fatal("Unknown signed the member out") |
| 57 | } |
| 58 | // /signin under Unknown is the ordinary shortcut, never a sign-out. |
| 59 | if w := f.get("/signin"); w.Code != http.StatusSeeOther || f.cookies[f.app.home.sessions.CookieName()] == nil { |
| 60 | t.Fatalf("/signin under Unknown = %d %q", w.Code, w.Header().Get("Location")) |
| 61 | } |
| 62 | // The meeting was never ended by a roster that could not be read. |
| 63 | if f.meetingRow(sqid).EndedAt != nil { |
| 64 | t.Fatal("Unknown ended the meeting") |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | // Paul's ruling, condition 2 of 4 on #98 and #53's ask 3: under Unknown |
| 69 | // the list says partial even when there is nothing in it, and withholds |
| 70 | // the empty state, which would read as "no meetings". |
| 71 | func TestTeamEmptyPartialListSaysPartialNotEmpty(t *testing.T) { |
| 72 | f := newTeamFixture(t) |
| 73 | f.member(teamSubject('e'), "member") |
| 74 | if w := f.page("/"); !strings.Contains(w.Body.String(), "No meetings yet") { |
| 75 | t.Fatal("the empty state is not where this test expects it") |
| 76 | } |
| 77 | f.expired() |
| 78 | w := f.page("/") |
| 79 | body := w.Body.String() |
| 80 | if w.Code != http.StatusOK || !strings.Contains(body, "data-partial") || !strings.Contains(body, partialTitle) || strings.Contains(body, "No meetings yet") || strings.Contains(body, "rst-empty") { |
| 81 | t.Fatalf("empty partial list = %d:\n%s", w.Code, body) |
| 82 | } |
| 83 | // A guest is never partial: they never reach the list. |
| 84 | f.cookies = map[string]*http.Cookie{} |
| 85 | if w := f.page("/"); strings.Contains(w.Body.String(), "data-partial") { |
| 86 | t.Fatal("a signed-out visitor sees the partial notice") |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // The list asks again after it is built, of the same Access: swept across |
| 91 | // the lease's end with a clock that ticks on every reading, the list is |
| 92 | // always either whole or the partial notice with nothing in it — never a |
| 93 | // short list without the notice. The drive surface's JSON list, which |
| 94 | // cannot carry the notice, is whole or a retryable 503. |
| 95 | func TestTeamListIsWholeOrPartialAcrossTheLeaseEdge(t *testing.T) { |
| 96 | sawWhole, sawPartial := false, false |
| 97 | apiWhole, api503 := false, false |
| 98 | for k := 1; k <= 40; k++ { |
| 99 | f := newTeamFixture(t) |
| 100 | // Meetings of several hosts, so the list asks the roster about |
| 101 | // each of them between building and asking again: the window a |
| 102 | // lapse can fall in. |
| 103 | for i, c := range []byte("vxyz") { |
| 104 | f.member(teamSubject(c), "member") |
| 105 | f.meeting("Other " + string("0123"[i])) |
| 106 | } |
| 107 | f.member(teamSubject('w'), "member") |
| 108 | f.meeting("Alpha") |
| 109 | f.meeting("Beta") |
| 110 | token := f.agentToken("lister") |
| 111 | f.refreshed() |
| 112 | f.dir.SetDown(true) |
| 113 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 114 | f.clock.SetTick(time.Millisecond) |
| 115 | w := f.page("/") |
| 116 | body := w.Body.String() |
| 117 | whole := strings.Contains(body, "Alpha") && strings.Contains(body, "Beta") && !strings.Contains(body, "data-partial") |
| 118 | partial := !strings.Contains(body, "Alpha") && !strings.Contains(body, "Beta") && strings.Contains(body, "data-partial") |
| 119 | switch { |
| 120 | case w.Code == http.StatusOK && whole: |
| 121 | sawWhole = true |
| 122 | case w.Code == http.StatusOK && partial: |
| 123 | sawPartial = true |
| 124 | default: |
| 125 | t.Fatalf("k=%d: the list is neither whole nor partial (%d):\n%s", k, w.Code, body) |
| 126 | } |
| 127 | f.clock.SetTick(0) |
| 128 | // The JSON list from where the page started, in its own request. |
| 129 | f.clock.Step(-time.Duration(0)) |
| 130 | a := apiAt(f, token, k) |
| 131 | switch { |
| 132 | case a.Code == http.StatusOK && strings.Contains(a.Body.String(), "Alpha") && strings.Contains(a.Body.String(), "Beta"): |
| 133 | apiWhole = true |
| 134 | case a.Code == http.StatusServiceUnavailable && a.Header().Get("Retry-After") != "": |
| 135 | api503 = true |
| 136 | default: |
| 137 | t.Fatalf("k=%d: the API list = %d %s", k, a.Code, a.Body.String()) |
| 138 | } |
| 139 | } |
| 140 | if !sawWhole || !sawPartial || !apiWhole || !api503 { |
| 141 | t.Fatalf("the sweep did not cross the lease's end: whole %v partial %v api whole %v api 503 %v", sawWhole, sawPartial, apiWhole, api503) |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | // agentToken mints an agent for whoever is signed in, through the page, |
| 146 | // and returns its bearer. |
| 147 | func (f *teamFixture) agentToken(name string) string { |
| 148 | f.t.Helper() |
| 149 | w := f.post("/settings/agents", "name="+name) |
| 150 | if w.Code != http.StatusOK { |
| 151 | f.t.Fatalf("mint agent = %d %s", w.Code, w.Body.String()) |
| 152 | } |
| 153 | i := strings.Index(w.Body.String(), "mt_") |
| 154 | if i < 0 { |
| 155 | f.t.Fatal("no token on the page") |
| 156 | } |
| 157 | end := i |
| 158 | for end < len(w.Body.String()) && strings.IndexByte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-", w.Body.String()[end]) >= 0 { |
| 159 | end++ |
| 160 | } |
| 161 | return w.Body.String()[i:end] |
| 162 | } |
| 163 | |
| 164 | // api is one drive-surface request with bearer. |
| 165 | func (f *teamFixture) api(method, path, bearer, body string) *httptest.ResponseRecorder { |
| 166 | r := httptest.NewRequest(method, homeTestOrigin+path, strings.NewReader(body)) |
| 167 | r.Header.Set("Authorization", "Bearer "+bearer) |
| 168 | if body != "" { |
| 169 | r.Header.Set("Content-Type", "application/json") |
| 170 | } |
| 171 | w := httptest.NewRecorder() |
| 172 | f.handler.ServeHTTP(w, r.WithContext(context.Background())) |
| 173 | return w |
| 174 | } |
| 175 | |
| 176 | // The list asks again after it is built, and a list built on standing that |
| 177 | // has run out since is withheld, not shown beside the notice. Swept one |
| 178 | // reading at a time across the lease's end, with the request's own Access |
| 179 | // made just before the page is drawn, so the readings between building and |
| 180 | // asking again (the hosts the list settles) can straddle the end. |
| 181 | func TestTeamListBuiltThenLapsedIsWithheld(t *testing.T) { |
| 182 | sawWhole, sawPartial := false, false |
| 183 | for k := 1; k <= 12; k++ { |
| 184 | f := newTeamFixture(t) |
| 185 | subject := teamSubject('a') |
| 186 | p := f.member(subject, "member") |
| 187 | f.meeting("Alpha") |
| 188 | gamma, _ := f.meeting("Gamma") |
| 189 | if w := f.post("/meetings/"+gamma+"/end", ""); w.Code != http.StatusSeeOther { |
| 190 | t.Fatalf("end = %d", w.Code) |
| 191 | } |
| 192 | f.refreshed() |
| 193 | f.dir.SetDown(true) |
| 194 | // The refresh this request would start has just been tried, so |
| 195 | // none is in flight to contend with the readings. |
| 196 | f.app.team.Refresh(context.Background()) |
| 197 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 198 | f.clock.SetTick(time.Millisecond) |
| 199 | ctx := context.Background() |
| 200 | tr := &teamReq{person: p, subject: subject, method: "home", access: f.app.team.Access(ctx, subject)} |
| 201 | r := httptest.NewRequest("GET", homeTestOrigin+"/", nil) |
| 202 | r.Header.Set("Accept", "text/html") |
| 203 | r = r.WithContext(context.WithValue(withTeamReq(ctx, tr), personCtxKey{}, p)) |
| 204 | w := httptest.NewRecorder() |
| 205 | f.app.renderIndex(w, r, "", "") |
| 206 | // Alpha is upcoming and Gamma ended: the list's two sections. |
| 207 | listed := strings.Contains(w.Body.String(), "Alpha") || strings.Contains(w.Body.String(), "Gamma") |
| 208 | partial := strings.Contains(w.Body.String(), "data-partial") |
| 209 | switch { |
| 210 | case listed && partial: |
| 211 | t.Fatalf("k=%d: a list built before the lease ran out is shown beside the partial notice", k) |
| 212 | case listed: |
| 213 | sawWhole = true |
| 214 | case partial: |
| 215 | sawPartial = true |
| 216 | } |
| 217 | } |
| 218 | if !sawWhole || !sawPartial { |
| 219 | t.Fatalf("the sweep did not cross the lease's end: whole %v partial %v", sawWhole, sawPartial) |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // apiAt is the drive surface's list from k ms before the lease's end, a |
| 224 | // reading at a time. |
| 225 | func apiAt(f *teamFixture, token string, k int) *httptest.ResponseRecorder { |
| 226 | f.refreshed() |
| 227 | f.dir.SetDown(true) |
| 228 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 229 | f.clock.SetTick(time.Millisecond) |
| 230 | defer f.clock.SetTick(0) |
| 231 | return f.api("GET", "/api/v1/meetings", token, "") |
| 232 | } |
| 233 | |
| 234 | // The drive surface's list asks again after it is built, of the same |
| 235 | // Access its gate put on the request: swept a reading at a time across |
| 236 | // the lease's end from just before the handler runs, it is whole or a |
| 237 | // retryable 503, and both happen. |
| 238 | func TestTeamAPIListAsksAgainAfterBuilding(t *testing.T) { |
| 239 | sawWhole, saw503 := false, false |
| 240 | for k := 1; k <= 12; k++ { |
| 241 | f := newTeamFixture(t) |
| 242 | subject := teamSubject('a') |
| 243 | p := f.member(subject, "member") |
| 244 | f.meeting("Alpha") |
| 245 | f.refreshed() |
| 246 | f.dir.SetDown(true) |
| 247 | f.app.team.Refresh(context.Background()) |
| 248 | f.clock.Step(roster.MaximumAge - time.Duration(k)*time.Millisecond) |
| 249 | f.clock.SetTick(time.Millisecond) |
| 250 | ctx := teammode.WithAccess(context.Background(), f.app.team.Access(context.Background(), subject)) |
| 251 | r := httptest.NewRequest("GET", homeTestOrigin+"/api/v1/meetings", nil) |
| 252 | r = r.WithContext(withAgent(ctx, Agent{ID: 1}, p)) |
| 253 | if acc, _ := teammode.FromContext(r.Context()); acc.State() != roster.Member { |
| 254 | continue // the gate would have refused it |
| 255 | } |
| 256 | w := httptest.NewRecorder() |
| 257 | f.app.apiListMeetings(w, r) |
| 258 | switch { |
| 259 | case w.Code == http.StatusOK && strings.Contains(w.Body.String(), "Alpha"): |
| 260 | sawWhole = true |
| 261 | case w.Code == http.StatusServiceUnavailable && w.Header().Get("Retry-After") != "": |
| 262 | saw503 = true |
| 263 | default: |
| 264 | t.Fatalf("k=%d: %d %s", k, w.Code, w.Body.String()) |
| 265 | } |
| 266 | } |
| 267 | if !sawWhole || !saw503 { |
| 268 | t.Fatalf("the sweep did not cross the lease's end: whole %v 503 %v", sawWhole, saw503) |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | // Settling the list never ends a meeting on a host the roster cannot |
| 273 | // decide: only a definite NotMember does. |
| 274 | func TestTeamSettlingLeavesAnUnknownHostsMeeting(t *testing.T) { |
| 275 | f := newTeamFixture(t) |
| 276 | f.member(teamSubject('h'), "member") |
| 277 | sqid, _ := f.meeting("Undecided") |
| 278 | views, err := f.app.store.listMeetings(context.Background()) |
| 279 | if err != nil { |
| 280 | t.Fatal(err) |
| 281 | } |
| 282 | f.expired() |
| 283 | f.app.settleHosts(context.Background(), views) |
| 284 | if f.meetingRow(sqid).EndedAt != nil { |
| 285 | t.Fatal("an Unknown host's meeting was ended") |
| 286 | } |
| 287 | } |
| 288 | |