| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "crypto/rand" |
| 7 | "encoding/base64" |
| 8 | "encoding/json" |
| 9 | "net/http" |
| 10 | "net/http/httptest" |
| 11 | "net/url" |
| 12 | "regexp" |
| 13 | "strconv" |
| 14 | "strings" |
| 15 | "sync" |
| 16 | "testing" |
| 17 | "time" |
| 18 | ) |
| 19 | |
| 20 | // pressAnswer is the server's answer to a press asked for as JSON: the |
| 21 | // talk's absolute position after it, the session's own sequence and the |
| 22 | // deck's (presenting_position.go's positionAnswer). |
| 23 | type pressAnswer struct { |
| 24 | Slide string `json:"slide"` |
| 25 | Step int `json:"step"` |
| 26 | Mode string `json:"mode"` |
| 27 | Black bool `json:"black"` |
| 28 | Seq int64 `json:"seq"` |
| 29 | DeckSeq int64 `json:"deckSeq"` |
| 30 | } |
| 31 | |
| 32 | // newPressID is what a window sends with each press: 16 random bytes, |
| 33 | // base64url, so a press sent twice can be told from two presses. |
| 34 | func newPressID(t *testing.T) string { |
| 35 | t.Helper() |
| 36 | raw := make([]byte, 16) |
| 37 | if _, err := rand.Read(raw); err != nil { |
| 38 | t.Fatalf("press id: %v", err) |
| 39 | } |
| 40 | return base64.RawURLEncoding.EncodeToString(raw) |
| 41 | } |
| 42 | |
| 43 | // pressRequest is one press as a window posts it: a form, asking for |
| 44 | // JSON, with a browser's same-origin evidence. token is sent when it is |
| 45 | // not empty and cookies when there are some, because who may drive is |
| 46 | // half of what these tests are about. |
| 47 | func pressRequest(t *testing.T, a *testApp, session, token string, form url.Values, cookies []*http.Cookie) *httptest.ResponseRecorder { |
| 48 | t.Helper() |
| 49 | body := url.Values{} |
| 50 | for k, vs := range form { |
| 51 | body[k] = vs |
| 52 | } |
| 53 | if token != "" { |
| 54 | body.Set("t", token) |
| 55 | } |
| 56 | req := httptest.NewRequest(http.MethodPost, "/p/"+session+"/command", strings.NewReader(body.Encode())) |
| 57 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 58 | req.Header.Set("Accept", "application/json") |
| 59 | req.Header.Set("Origin", testOrigin) |
| 60 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 61 | for _, c := range cookies { |
| 62 | req.AddCookie(c) |
| 63 | } |
| 64 | rec := httptest.NewRecorder() |
| 65 | a.ServeHTTP(rec, req) |
| 66 | return rec |
| 67 | } |
| 68 | |
| 69 | // press sends one press and insists on a JSON position back. |
| 70 | func press(t *testing.T, a *testApp, session, token string, form url.Values, cookies []*http.Cookie) pressAnswer { |
| 71 | t.Helper() |
| 72 | rec := pressRequest(t, a, session, token, form, cookies) |
| 73 | if rec.Code != http.StatusOK { |
| 74 | t.Fatalf("press %v: status %d, want 200\n%s", form, rec.Code, rec.Body) |
| 75 | } |
| 76 | if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { |
| 77 | t.Fatalf("press %v: Content-Type %q, want JSON", form, ct) |
| 78 | } |
| 79 | var out pressAnswer |
| 80 | if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { |
| 81 | t.Fatalf("press %v: %v\n%s", form, err, rec.Body) |
| 82 | } |
| 83 | return out |
| 84 | } |
| 85 | |
| 86 | // op is a press's form: the op and a fresh press id. |
| 87 | func op(t *testing.T, name string) url.Values { |
| 88 | return url.Values{"op": {name}, "press": {newPressID(t)}} |
| 89 | } |
| 90 | |
| 91 | // stepsDeck is a talk to press through, made over the API: "steps" |
| 92 | // with four presses, "plain" with none, "extra" for the full talk only, |
| 93 | // "parked" in no talk, and "last". It returns the deck's path. |
| 94 | func stepsDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string { |
| 95 | t.Helper() |
| 96 | token := mintAgent(t, a, cookies, "steps-deck") |
| 97 | sqid := apiDeck(t, a, token, "Steps") |
| 98 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 99 | slides, _ := view["slides"].([]any) |
| 100 | var ops []string |
| 101 | for _, s := range slides { |
| 102 | id, _ := s.(map[string]any)["id"].(string) |
| 103 | ops = append(ops, `{"op":"remove_slide","id":`+quote(id)+`}`) |
| 104 | } |
| 105 | ops = append(ops, `{"op":"add_slide","id":"steps","layout":"flow","template":"title-body"}`) |
| 106 | for i, word := range []string{"one", "two", "three", "four"} { |
| 107 | id := quote("b" + string(rune('1'+i))) |
| 108 | ops = append(ops, |
| 109 | `{"op":"add_block","slide":"steps","id":`+id+`,"type":"text","region":"body","markdown":`+quote(word)+`}`, |
| 110 | `{"op":"set_block","slide":"steps","id":`+id+`,"step":`+string(rune('1'+i))+`}`) |
| 111 | } |
| 112 | for _, id := range []string{"plain", "extra", "parked", "last"} { |
| 113 | ops = append(ops, `{"op":"add_slide","id":`+quote(id)+`,"layout":"flow","template":"title-body"}`) |
| 114 | } |
| 115 | ops = append(ops, |
| 116 | `{"op":"set_slide","id":"extra","talk":"full"}`, |
| 117 | `{"op":"set_slide","id":"parked","talk":"parked"}`) |
| 118 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 119 | return "/d/" + sqid |
| 120 | } |
| 121 | |
| 122 | // rowPosition reads the session's position straight off its row. |
| 123 | func rowPosition(t *testing.T, a *testApp, session string) (slideID string, step int, seq int64) { |
| 124 | t.Helper() |
| 125 | row := a.db.G.Raw("SELECT slide_id, step, seq FROM presentations WHERE sqid = ?", session).Row() |
| 126 | if err := row.Scan(&slideID, &step, &seq); err != nil { |
| 127 | t.Fatalf("read the session's position: %v", err) |
| 128 | } |
| 129 | return slideID, step, seq |
| 130 | } |
| 131 | |
| 132 | // openStream opens the session's SSE stream on a real server. |
| 133 | func openStream(t *testing.T, a *testApp, path string) *bufio.Reader { |
| 134 | t.Helper() |
| 135 | srv := httptest.NewServer(a) |
| 136 | t.Cleanup(srv.Close) |
| 137 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 138 | t.Cleanup(cancel) |
| 139 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+path, nil) |
| 140 | if err != nil { |
| 141 | t.Fatalf("build the stream request: %v", err) |
| 142 | } |
| 143 | resp, err := http.DefaultClient.Do(req) |
| 144 | if err != nil { |
| 145 | t.Fatalf("open the stream: %v", err) |
| 146 | } |
| 147 | t.Cleanup(func() { resp.Body.Close() }) |
| 148 | return bufio.NewReader(resp.Body) |
| 149 | } |
| 150 | |
| 151 | // streamEvent is the stream's event, with the position fields. |
| 152 | type streamEvent struct { |
| 153 | Slide int `json:"slide"` |
| 154 | Blackout bool `json:"blackout"` |
| 155 | Ended bool `json:"ended"` |
| 156 | SlideID string `json:"slideId"` |
| 157 | Step int `json:"step"` |
| 158 | Mode string `json:"mode"` |
| 159 | Seq int64 `json:"seq"` |
| 160 | } |
| 161 | |
| 162 | func nextPositionEvent(t *testing.T, r *bufio.Reader) streamEvent { |
| 163 | t.Helper() |
| 164 | var e streamEvent |
| 165 | data := nextSlideEvent(t, r) |
| 166 | if err := json.Unmarshal([]byte(data), &e); err != nil { |
| 167 | t.Fatalf("the stream's event %q: %v", data, err) |
| 168 | } |
| 169 | return e |
| 170 | } |
| 171 | |
| 172 | // TestLaptopAndPhoneCrossingPressesBothCount: the laptop and the phone |
| 173 | // both press next from step 2 of a four-step slide at the same moment. |
| 174 | // The server orders them, so the talk reaches step 4 rather than one |
| 175 | // of the presses being lost (S18). |
| 176 | func TestLaptopAndPhoneCrossingPressesBothCount(t *testing.T) { |
| 177 | a := newApp(t) |
| 178 | cookies := signedIn(t, a, allowedAddress) |
| 179 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 180 | |
| 181 | press(t, a, session, token, op(t, "next"), nil) |
| 182 | at := press(t, a, session, token, op(t, "next"), nil) |
| 183 | if at.Slide != "steps" || at.Step != 2 { |
| 184 | t.Fatalf("two nexts reached %+v, want steps at step 2", at) |
| 185 | } |
| 186 | |
| 187 | var wg sync.WaitGroup |
| 188 | answers := make([]pressAnswer, 2) |
| 189 | codes := make([]int, 2) |
| 190 | start := make(chan struct{}) |
| 191 | for i := range 2 { |
| 192 | wg.Add(1) |
| 193 | go func() { |
| 194 | defer wg.Done() |
| 195 | <-start |
| 196 | var rec *httptest.ResponseRecorder |
| 197 | if i == 0 { |
| 198 | rec = pressRequest(t, a, session, "", op(t, "next"), cookies) // the laptop |
| 199 | } else { |
| 200 | rec = pressRequest(t, a, session, token, op(t, "next"), nil) // the phone |
| 201 | } |
| 202 | codes[i] = rec.Code |
| 203 | _ = json.Unmarshal(rec.Body.Bytes(), &answers[i]) |
| 204 | }() |
| 205 | } |
| 206 | close(start) |
| 207 | wg.Wait() |
| 208 | |
| 209 | for i, code := range codes { |
| 210 | if code != http.StatusOK { |
| 211 | t.Fatalf("press %d: status %d, want 200", i, code) |
| 212 | } |
| 213 | } |
| 214 | if answers[0].Seq == answers[1].Seq { |
| 215 | t.Errorf("both presses answered seq %d; each press must move the sequence", answers[0].Seq) |
| 216 | } |
| 217 | slide, step, seq := rowPosition(t, a, session) |
| 218 | if slide != "steps" || step != 4 { |
| 219 | t.Errorf("after two crossing presses the talk is at %s step %d, want steps step 4", slide, step) |
| 220 | } |
| 221 | if seq != at.Seq+2 { |
| 222 | t.Errorf("after two crossing presses seq is %d, want %d", seq, at.Seq+2) |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | // TestAPressSentTwiceCountsOnce: a window that did not hear its answer |
| 227 | // sends the same press again. The second answers where the talk is, |
| 228 | // moves nothing, publishes nothing and leaves the sequence alone. Only |
| 229 | // the newest 64 ids are kept per session. |
| 230 | func TestAPressSentTwiceCountsOnce(t *testing.T) { |
| 231 | a := newApp(t) |
| 232 | cookies := signedIn(t, a, allowedAddress) |
| 233 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 234 | events := openStream(t, a, "/p/"+session+"/stream") |
| 235 | nextPositionEvent(t, events) // the connect event |
| 236 | |
| 237 | form := op(t, "next") |
| 238 | first := press(t, a, session, token, form, nil) |
| 239 | again := press(t, a, session, token, form, nil) |
| 240 | if again != first { |
| 241 | t.Errorf("the press sent again answered %+v, want the same position %+v", again, first) |
| 242 | } |
| 243 | if first.Slide != "steps" || first.Step != 1 { |
| 244 | t.Errorf("one next answered %+v, want steps at step 1", first) |
| 245 | } |
| 246 | if _, step, seq := rowPosition(t, a, session); step != 1 || seq != first.Seq { |
| 247 | t.Errorf("after a repeated press the row is at step %d seq %d, want step 1 seq %d", step, seq, first.Seq) |
| 248 | } |
| 249 | |
| 250 | // The stream heard the first and nothing for the second: the event |
| 251 | // after the first press's is the next press's own. |
| 252 | if e := nextPositionEvent(t, events); e.Seq != first.Seq || e.Step != 1 || e.SlideID != "steps" { |
| 253 | t.Errorf("the first press's event is %+v, want steps step 1 seq %d", e, first.Seq) |
| 254 | } |
| 255 | second := press(t, a, session, token, op(t, "next"), nil) |
| 256 | if e := nextPositionEvent(t, events); e.Seq != second.Seq { |
| 257 | t.Errorf("the event after the repeat is seq %d, want the next press's seq %d", e.Seq, second.Seq) |
| 258 | } |
| 259 | |
| 260 | // A malformed press id is refused, and so is a press with none: |
| 261 | // without an id a press sent twice would count twice. |
| 262 | _, _, before := rowPosition(t, a, session) |
| 263 | bad := url.Values{"op": {"next"}, "press": {"short"}} |
| 264 | if rec := pressRequest(t, a, session, token, bad, nil); rec.Code != http.StatusUnprocessableEntity { |
| 265 | t.Errorf("a press id of %q: status %d, want 422", "short", rec.Code) |
| 266 | } |
| 267 | if rec := pressRequest(t, a, session, token, url.Values{"op": {"next"}}, nil); rec.Code != http.StatusUnprocessableEntity { |
| 268 | t.Errorf("a press with no id: status %d, want 422", rec.Code) |
| 269 | } |
| 270 | if _, _, seq := rowPosition(t, a, session); seq != before { |
| 271 | t.Errorf("refused presses moved the sequence from %d to %d", before, seq) |
| 272 | } |
| 273 | |
| 274 | // Sixty-four more presses push the first id out, and the table |
| 275 | // keeps sixty-four for the session. |
| 276 | for range 64 { |
| 277 | press(t, a, session, token, op(t, "blackout"), nil) |
| 278 | } |
| 279 | var kept int64 |
| 280 | if err := a.db.G.Raw(`SELECT COUNT(*) FROM presentation_presses |
| 281 | WHERE presentation_id = (SELECT id FROM presentations WHERE sqid = ?)`, session).Scan(&kept).Error; err != nil { |
| 282 | t.Fatalf("count press ids: %v", err) |
| 283 | } |
| 284 | if kept != 64 { |
| 285 | t.Errorf("the session keeps %d press ids, want 64", kept) |
| 286 | } |
| 287 | _, _, before = rowPosition(t, a, session) |
| 288 | if late := press(t, a, session, token, form, nil); late.Seq != before+1 { |
| 289 | t.Errorf("a press id older than the newest 64 answered seq %d, want it counted as new (%d)", late.Seq, before+1) |
| 290 | } |
| 291 | } |
| 292 | |
| 293 | // TestResumeTakesTheWindowsPosition: a window that pressed while the |
| 294 | // network was down sends resume with where it got to, and the server |
| 295 | // takes it, settled into the deck. |
| 296 | func TestResumeTakesTheWindowsPosition(t *testing.T) { |
| 297 | a := newApp(t) |
| 298 | cookies := signedIn(t, a, allowedAddress) |
| 299 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 300 | |
| 301 | press(t, a, session, token, op(t, "next"), nil) // the phone, meanwhile |
| 302 | |
| 303 | form := op(t, "resume") |
| 304 | form.Set("at_slide", "extra") |
| 305 | form.Set("at_step", "0") |
| 306 | form.Set("at_mode", "full") |
| 307 | got := press(t, a, session, "", form, cookies) |
| 308 | if got.Slide != "extra" || got.Step != 0 || got.Mode != "full" { |
| 309 | t.Errorf("resume answered %+v, want extra step 0 in full", got) |
| 310 | } |
| 311 | // The stored number is the slide's place in the full talk. |
| 312 | surface := mustGet(t, a, "/p/"+session, http.StatusOK, nil).Body.String() |
| 313 | if !strings.Contains(surface, `data-slide="3"`) { |
| 314 | t.Errorf("after resuming on the third slide of the full talk the surface does not say slide 3:\n%s", surface) |
| 315 | } |
| 316 | |
| 317 | // A step past the slide's presses settles onto its last. |
| 318 | form = op(t, "resume") |
| 319 | form.Set("at_slide", "steps") |
| 320 | form.Set("at_step", "9") |
| 321 | form.Set("at_mode", "core") |
| 322 | if got := press(t, a, session, "", form, cookies); got.Slide != "steps" || got.Step != 4 || got.Mode != "core" { |
| 323 | t.Errorf("resume past the last press answered %+v, want steps step 4 in core", got) |
| 324 | } |
| 325 | |
| 326 | // A mode that is not one is refused. |
| 327 | form = op(t, "resume") |
| 328 | form.Set("at_slide", "steps") |
| 329 | form.Set("at_mode", "sideways") |
| 330 | if rec := pressRequest(t, a, session, "", form, cookies); rec.Code != http.StatusUnprocessableEntity { |
| 331 | t.Errorf("resume in mode sideways: status %d, want 422", rec.Code) |
| 332 | } |
| 333 | } |
| 334 | |
| 335 | // TestPresentAgainLeavesThePhonePaired: pressing Present again rejoins |
| 336 | // the talk and leaves the phone's token working (S19). |
| 337 | func TestPresentAgainLeavesThePhonePaired(t *testing.T) { |
| 338 | a := newApp(t) |
| 339 | cookies := signedIn(t, a, allowedAddress) |
| 340 | path := sampleDeck(t, a, cookies) |
| 341 | session, token := startPresenting(t, a, path, cookies) |
| 342 | |
| 343 | rec := post(t, a, path+"/present", nil, cookies...) |
| 344 | if rec.Code != http.StatusSeeOther { |
| 345 | t.Fatalf("pressing Present again: status %d, want 303\n%s", rec.Code, rec.Body) |
| 346 | } |
| 347 | if loc := rec.Header().Get("Location"); loc != "/p/"+session { |
| 348 | t.Errorf("pressing Present again went to %q, want the same talk /p/%s", loc, session) |
| 349 | } |
| 350 | if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusNoContent { |
| 351 | t.Errorf("the phone's token after Present again: status %d, want 204", rec.Code) |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | // TestARevokedTokenDrivesNothing: once the phone's token is replaced, the |
| 356 | // old one moves nothing and reads nothing. |
| 357 | func TestARevokedTokenDrivesNothing(t *testing.T) { |
| 358 | a := newApp(t) |
| 359 | cookies := signedIn(t, a, allowedAddress) |
| 360 | session, old := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 361 | press(t, a, session, old, op(t, "next"), nil) |
| 362 | pairPhone(t, a, session, cookies) |
| 363 | |
| 364 | _, _, before := rowPosition(t, a, session) |
| 365 | if rec := pressRequest(t, a, session, old, op(t, "next"), nil); rec.Code != http.StatusNotFound { |
| 366 | t.Errorf("a press with a replaced token: status %d, want 404", rec.Code) |
| 367 | } |
| 368 | if rec := command(t, a, session, "next", old, "", nil); rec.Code != http.StatusNotFound { |
| 369 | t.Errorf("a form command with a replaced token: status %d, want 404", rec.Code) |
| 370 | } |
| 371 | mustGet(t, a, "/p/"+session+"/remote?t="+old, http.StatusNotFound, nil) |
| 372 | if slide, step, seq := rowPosition(t, a, session); slide != "steps" || step != 1 || seq != before { |
| 373 | t.Errorf("a replaced token moved the talk to %s step %d seq %d", slide, step, seq) |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // TestTheSignedInPresenterMayDriveWithoutAToken: the presenter's own |
| 378 | // session is enough to drive their own talk (S19). |
| 379 | func TestTheSignedInPresenterMayDriveWithoutAToken(t *testing.T) { |
| 380 | a := newApp(t) |
| 381 | cookies := signedIn(t, a, allowedAddress) |
| 382 | session, _ := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 383 | |
| 384 | got := press(t, a, session, "", op(t, "next"), cookies) |
| 385 | if got.Slide != "steps" || got.Step != 1 { |
| 386 | t.Errorf("the presenter's press answered %+v, want steps step 1", got) |
| 387 | } |
| 388 | // And a form with no JSON asked for keeps the old answers. |
| 389 | rec := post(t, a, "/p/"+session+"/command", op(t, "next"), cookies...) |
| 390 | if rec.Code != http.StatusSeeOther && rec.Code != http.StatusNoContent { |
| 391 | t.Errorf("the presenter's form press: status %d, want 303 or 204\n%s", rec.Code, rec.Body) |
| 392 | } |
| 393 | if _, step, _ := rowPosition(t, a, session); step != 2 { |
| 394 | t.Errorf("after the presenter's two presses the talk is at step %d, want 2", step) |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | // TestSomeoneElseSignedInMayNotDrive: being signed in is not being the |
| 399 | // presenter. |
| 400 | func TestSomeoneElseSignedInMayNotDrive(t *testing.T) { |
| 401 | a := newSharedApp(t) |
| 402 | mine := signedIn(t, a, allowedAddress) |
| 403 | theirs := signedIn(t, a, otherAddress) |
| 404 | session, _ := startPresenting(t, a, stepsDeck(t, a, mine), mine) |
| 405 | |
| 406 | if rec := pressRequest(t, a, session, "", op(t, "next"), theirs); rec.Code != http.StatusNotFound { |
| 407 | t.Errorf("somebody else's press: status %d, want 404", rec.Code) |
| 408 | } |
| 409 | if rec := pressRequest(t, a, session, "", op(t, "next"), nil); rec.Code != http.StatusNotFound { |
| 410 | t.Errorf("a press from nobody: status %d, want 404", rec.Code) |
| 411 | } |
| 412 | if _, step, _ := rowPosition(t, a, session); step != 0 { |
| 413 | t.Errorf("a refused press moved the talk to step %d", step) |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // TestACrossSitePressIsRefused: the presenter's cookie makes a press |
| 418 | // from their browser enough, so another site must not be able to make |
| 419 | // that browser press. The app-wide origin check refuses it. |
| 420 | func TestACrossSitePressIsRefused(t *testing.T) { |
| 421 | a := newApp(t) |
| 422 | cookies := signedIn(t, a, allowedAddress) |
| 423 | session, _ := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 424 | |
| 425 | form := op(t, "next") |
| 426 | req := httptest.NewRequest(http.MethodPost, "/p/"+session+"/command", strings.NewReader(form.Encode())) |
| 427 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 428 | req.Header.Set("Accept", "application/json") |
| 429 | req.Header.Set("Origin", "https://elsewhere.example") |
| 430 | req.Header.Set("Sec-Fetch-Site", "cross-site") |
| 431 | for _, c := range cookies { |
| 432 | req.AddCookie(c) |
| 433 | } |
| 434 | rec := httptest.NewRecorder() |
| 435 | a.ServeHTTP(rec, req) |
| 436 | if rec.Code != http.StatusForbidden { |
| 437 | t.Errorf("a cross-site press with the presenter's cookie: status %d, want 403", rec.Code) |
| 438 | } |
| 439 | if _, step, _ := rowPosition(t, a, session); step != 0 { |
| 440 | t.Errorf("a cross-site press moved the talk to step %d", step) |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // TestCommandsOnAnEmptyTalk: a deck with no slides, or with every slide |
| 445 | // parked, has nowhere to go, and every press still answers without an |
| 446 | // error. |
| 447 | func TestCommandsOnAnEmptyTalk(t *testing.T) { |
| 448 | a := newApp(t) |
| 449 | cookies := signedIn(t, a, allowedAddress) |
| 450 | |
| 451 | parked := stepsDeck(t, a, cookies) |
| 452 | token := mintAgent(t, a, cookies, "park-everything") |
| 453 | sqid := strings.TrimPrefix(parked, "/d/") |
| 454 | var ops []string |
| 455 | for _, id := range []string{"steps", "plain", "extra", "last"} { |
| 456 | ops = append(ops, `{"op":"set_slide","id":`+quote(id)+`,"talk":"parked"}`) |
| 457 | } |
| 458 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 459 | |
| 460 | for name, path := range map[string]string{ |
| 461 | "empty": strings.TrimSuffix(emptyDeck(t, a, cookies), "/edit"), |
| 462 | "parked": parked, |
| 463 | } { |
| 464 | t.Run(name, func(t *testing.T) { |
| 465 | session, token := startPresenting(t, a, path, cookies) |
| 466 | for _, name := range []string{"next", "prev", "first", "last", "mode", "blackout", "timer-reset", "resume", "goto"} { |
| 467 | form := op(t, name) |
| 468 | if name == "goto" { |
| 469 | form.Set("slide", "parked") |
| 470 | } |
| 471 | if name == "resume" { |
| 472 | form.Set("at_slide", "steps") |
| 473 | } |
| 474 | got := press(t, a, session, token, form, nil) |
| 475 | if got.Slide != "" || got.Step != 0 { |
| 476 | t.Errorf("%s on an empty talk answered %+v, want nowhere", name, got) |
| 477 | } |
| 478 | } |
| 479 | mustGet(t, a, "/p/"+session, http.StatusOK, nil) |
| 480 | }) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // TestASessionFromBeforeTheDeployKeepsItsIndex: a talk begun before |
| 485 | // positions existed carries on by its index for its remaining hours, |
| 486 | // and the new presses are not ones it knows. |
| 487 | func TestASessionFromBeforeTheDeployKeepsItsIndex(t *testing.T) { |
| 488 | a := newApp(t) |
| 489 | cookies := signedIn(t, a, allowedAddress) |
| 490 | path := sampleDeck(t, a, cookies) |
| 491 | session, token := startPresenting(t, a, path, cookies) |
| 492 | // What migration 0009 leaves on a row that was already there: the |
| 493 | // new columns, all NULL. |
| 494 | if err := a.db.G.Exec(`UPDATE presentations |
| 495 | SET positional = NULL, slide_id = NULL, step = NULL, mode = NULL, seq = NULL, timer_at = NULL, slide = 2 |
| 496 | WHERE sqid = ?`, session).Error; err != nil { |
| 497 | t.Fatalf("make the session a pre-deploy one: %v", err) |
| 498 | } |
| 499 | |
| 500 | if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusNoContent { |
| 501 | t.Fatalf("next on a pre-deploy session: status %d, want 204\n%s", rec.Code, rec.Body) |
| 502 | } |
| 503 | surface := mustGet(t, a, "/p/"+session, http.StatusOK, nil).Body.String() |
| 504 | if !strings.Contains(surface, "4 / 8") { |
| 505 | t.Errorf("next from index 2 does not show slide 4:\n%s", surface) |
| 506 | } |
| 507 | var untouched int64 |
| 508 | if err := a.db.G.Raw(`SELECT COUNT(*) FROM presentations WHERE sqid = ? |
| 509 | AND positional IS NULL AND slide_id IS NULL AND seq IS NULL AND timer_at IS NULL`, session).Scan(&untouched).Error; err != nil { |
| 510 | t.Fatalf("read the session: %v", err) |
| 511 | } |
| 512 | if untouched != 1 { |
| 513 | t.Error("a pre-deploy session gained a position, a sequence or a timer") |
| 514 | } |
| 515 | for _, name := range []string{"first", "last", "mode", "timer-reset", "resume"} { |
| 516 | if rec := command(t, a, session, name, token, "", nil); rec.Code != http.StatusUnprocessableEntity { |
| 517 | t.Errorf("%s on a pre-deploy session: status %d, want 422", name, rec.Code) |
| 518 | } |
| 519 | } |
| 520 | // The presenter's cookie drives it too, by its index: Present |
| 521 | // again rejoins it without a token (S19), and a talk the presenter |
| 522 | // can no longer drive would be no talk at all. |
| 523 | if rec := pressRequest(t, a, session, "", op(t, "prev"), cookies); rec.Code != http.StatusNoContent { |
| 524 | t.Errorf("the presenter's tokenless press on a pre-deploy session: status %d, want 204", rec.Code) |
| 525 | } |
| 526 | if surface := mustGet(t, a, "/p/"+session, http.StatusOK, nil).Body.String(); !strings.Contains(surface, "3 / 8") { |
| 527 | t.Errorf("the presenter's prev from index 3 does not show slide 3:\n%s", surface) |
| 528 | } |
| 529 | } |
| 530 | |
| 531 | // TestTheFirstPressStartsTheTimer: the timer starts at the first move, |
| 532 | // not when the session opens, and timer-reset clears it until the next. |
| 533 | func TestTheFirstPressStartsTheTimer(t *testing.T) { |
| 534 | a := newApp(t) |
| 535 | cookies := signedIn(t, a, allowedAddress) |
| 536 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 537 | |
| 538 | timerSet := func() bool { |
| 539 | t.Helper() |
| 540 | var n int64 |
| 541 | if err := a.db.G.Raw("SELECT COUNT(*) FROM presentations WHERE sqid = ? AND timer_at IS NOT NULL", session).Scan(&n).Error; err != nil { |
| 542 | t.Fatalf("read the timer: %v", err) |
| 543 | } |
| 544 | return n == 1 |
| 545 | } |
| 546 | timerAt := func() string { |
| 547 | t.Helper() |
| 548 | var at string |
| 549 | if err := a.db.G.Raw("SELECT timer_at FROM presentations WHERE sqid = ?", session).Row().Scan(&at); err != nil { |
| 550 | t.Fatalf("read the timer: %v", err) |
| 551 | } |
| 552 | return at |
| 553 | } |
| 554 | |
| 555 | if timerSet() { |
| 556 | t.Fatal("the timer runs before anybody pressed anything") |
| 557 | } |
| 558 | press(t, a, session, token, op(t, "next"), nil) |
| 559 | if !timerSet() { |
| 560 | t.Fatal("the first press did not start the timer") |
| 561 | } |
| 562 | started := timerAt() |
| 563 | time.Sleep(5 * time.Millisecond) |
| 564 | press(t, a, session, token, op(t, "next"), nil) |
| 565 | if again := timerAt(); again != started { |
| 566 | t.Errorf("a later press restarted the timer: %s then %s", started, again) |
| 567 | } |
| 568 | press(t, a, session, token, op(t, "timer-reset"), nil) |
| 569 | if timerSet() { |
| 570 | t.Error("timer-reset left the timer running") |
| 571 | } |
| 572 | press(t, a, session, token, op(t, "prev"), nil) |
| 573 | if !timerSet() { |
| 574 | t.Error("the first press after a reset did not start the timer") |
| 575 | } |
| 576 | } |
| 577 | |
| 578 | // pressField finds the press ids on a page's control forms. |
| 579 | var pressField = regexp.MustCompile(`name="press" value="([^"]*)"`) |
| 580 | |
| 581 | // TestEveryControlFormCarriesAFreshPressID: each control form carries a |
| 582 | // press id, fresh on every render, so a form resubmitted by a reload |
| 583 | // counts once and a page loaded again makes new presses. |
| 584 | func TestEveryControlFormCarriesAFreshPressID(t *testing.T) { |
| 585 | a := newApp(t) |
| 586 | cookies := signedIn(t, a, allowedAddress) |
| 587 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 588 | |
| 589 | seen := map[string]bool{} |
| 590 | for _, page := range []string{ |
| 591 | "/p/" + session + "/remote?t=" + token, |
| 592 | "/p/" + session + "/remote?t=" + token, |
| 593 | "/p/" + session + "/notes?t=" + token, |
| 594 | "/p/" + session + "?t=" + token, |
| 595 | } { |
| 596 | body := mustGet(t, a, page, http.StatusOK, nil).Body.String() |
| 597 | forms := strings.Count(body, `<form method="post"`) |
| 598 | ids := pressField.FindAllStringSubmatch(body, -1) |
| 599 | if len(ids) != forms || forms == 0 { |
| 600 | t.Errorf("%s: %d control forms and %d press ids, want one each", page, forms, len(ids)) |
| 601 | } |
| 602 | for _, m := range ids { |
| 603 | if !regexp.MustCompile(`^[A-Za-z0-9_-]{16,43}$`).MatchString(m[1]) { |
| 604 | t.Errorf("%s: press id %q is not one the server accepts", page, m[1]) |
| 605 | } |
| 606 | if seen[m[1]] { |
| 607 | t.Errorf("%s: press id %q was rendered before", page, m[1]) |
| 608 | } |
| 609 | seen[m[1]] = true |
| 610 | } |
| 611 | } |
| 612 | |
| 613 | // The form as rendered is a press, and the same form again is not. |
| 614 | body := mustGet(t, a, "/p/"+session+"/remote?t="+token, http.StatusOK, nil).Body.String() |
| 615 | id := regexp.MustCompile(`value="next">\s*<input type="hidden" name="t" value="[^"]*">\s*<input type="hidden" name="press" value="([^"]*)"`).FindStringSubmatch(body) |
| 616 | if id == nil { |
| 617 | t.Fatalf("the remote's next form carries no press id:\n%s", body) |
| 618 | } |
| 619 | form := url.Values{"op": {"next"}, "t": {token}, "press": {id[1]}} |
| 620 | for range 2 { |
| 621 | if rec := post(t, a, "/p/"+session+"/command", form); rec.Code != http.StatusNoContent && rec.Code != http.StatusSeeOther { |
| 622 | t.Fatalf("posting the remote's next form: status %d", rec.Code) |
| 623 | } |
| 624 | } |
| 625 | if _, step, _ := rowPosition(t, a, session); step != 1 { |
| 626 | t.Errorf("the next form posted twice left the talk at step %d, want 1", step) |
| 627 | } |
| 628 | } |
| 629 | |
| 630 | // linkPress is a press from a window's link: its link id and its |
| 631 | // number n, which rises with every command that link sends. |
| 632 | func linkPress(t *testing.T, name, link string, n int) url.Values { |
| 633 | form := op(t, name) |
| 634 | form.Set("link", link) |
| 635 | form.Set("n", strconv.Itoa(n)) |
| 636 | return form |
| 637 | } |
| 638 | |
| 639 | // TestAnAbandonedPressCannotApplyAfterTheResume: a window that gave up |
| 640 | // waiting for a press resumes the talk at its own position; when the |
| 641 | // press it gave up on reaches the server afterwards it is not applied, |
| 642 | // so one physical press never advances twice. Commands without a link |
| 643 | // (the phone's forms) are numbered by nobody and keep applying. |
| 644 | func TestAnAbandonedPressCannotApplyAfterTheResume(t *testing.T) { |
| 645 | a := newApp(t) |
| 646 | cookies := signedIn(t, a, allowedAddress) |
| 647 | session, token := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 648 | link := newPressID(t) |
| 649 | |
| 650 | press(t, a, session, "", linkPress(t, "next", link, 1), cookies) // steps 1 |
| 651 | abandoned := linkPress(t, "next", link, 2) // the press the window gave up on |
| 652 | resume := linkPress(t, "resume", link, 3) |
| 653 | resume.Set("at_slide", "steps") |
| 654 | resume.Set("at_step", "2") |
| 655 | resume.Set("at_mode", "core") |
| 656 | if got := press(t, a, session, "", resume, cookies); got.Slide != "steps" || got.Step != 2 { |
| 657 | t.Fatalf("resume answered %+v, want steps step 2", got) |
| 658 | } |
| 659 | _, _, seq := rowPosition(t, a, session) |
| 660 | late := press(t, a, session, "", abandoned, cookies) |
| 661 | if late.Slide != "steps" || late.Step != 2 || late.Seq != seq { |
| 662 | t.Errorf("the abandoned press, arriving after the resume, answered %+v (seq was %d); want it refused: steps step 2, nothing moved", late, seq) |
| 663 | } |
| 664 | if id, step, now := rowPosition(t, a, session); id != "steps" || step != 2 || now != seq { |
| 665 | t.Errorf("the abandoned press moved the talk to %s step %d (seq %d, was %d)", id, step, now, seq) |
| 666 | } |
| 667 | // The same number again is refused too; a higher one applies. |
| 668 | if got := press(t, a, session, "", linkPress(t, "next", link, 3), cookies); got.Step != 2 { |
| 669 | t.Errorf("a second command numbered 3 applied: %+v", got) |
| 670 | } |
| 671 | if got := press(t, a, session, "", linkPress(t, "next", link, 4), cookies); got.Step != 3 { |
| 672 | t.Errorf("the link's next command did not apply: %+v", got) |
| 673 | } |
| 674 | // Another window's link, and the phone with none, are their own. |
| 675 | if got := press(t, a, session, "", linkPress(t, "next", newPressID(t), 1), cookies); got.Step != 4 { |
| 676 | t.Errorf("another link's first command did not apply: %+v", got) |
| 677 | } |
| 678 | if got := press(t, a, session, token, op(t, "prev"), nil); got.Step != 3 { |
| 679 | t.Errorf("the phone's press did not apply: %+v", got) |
| 680 | } |
| 681 | // A link without its number, or a number that is not one, is refused. |
| 682 | for _, n := range []string{"", "0", "-1", "x"} { |
| 683 | form := op(t, "next") |
| 684 | form.Set("link", link) |
| 685 | if n != "" { |
| 686 | form.Set("n", n) |
| 687 | } |
| 688 | if rec := pressRequest(t, a, session, "", form, cookies); rec.Code != http.StatusUnprocessableEntity { |
| 689 | t.Errorf("a link press numbered %q: status %d, want 422", n, rec.Code) |
| 690 | } |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | // TestResumeTakesTheWindowsBlackScreen: the window's black screen is |
| 695 | // part of where it went offline, in both directions. |
| 696 | func TestResumeTakesTheWindowsBlackScreen(t *testing.T) { |
| 697 | a := newApp(t) |
| 698 | cookies := signedIn(t, a, allowedAddress) |
| 699 | session, _ := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 700 | for _, black := range []string{"true", "false"} { |
| 701 | form := op(t, "resume") |
| 702 | form.Set("at_slide", "plain") |
| 703 | form.Set("at_step", "0") |
| 704 | form.Set("at_mode", "core") |
| 705 | form.Set("at_black", black) |
| 706 | if got := press(t, a, session, "", form, cookies); got.Black != (black == "true") || got.Slide != "plain" { |
| 707 | t.Errorf("resume with at_black=%s answered %+v", black, got) |
| 708 | } |
| 709 | } |
| 710 | form := op(t, "resume") |
| 711 | form.Set("at_slide", "plain") |
| 712 | form.Set("at_black", "maybe") |
| 713 | if rec := pressRequest(t, a, session, "", form, cookies); rec.Code != http.StatusUnprocessableEntity { |
| 714 | t.Errorf("resume with at_black=maybe: status %d, want 422", rec.Code) |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | // TestALinkIsRememberedForTheWholeTalk: however many other windows |
| 719 | // press in between (each window load is a link of its own), a link's |
| 720 | // abandoned press arriving after its resume is still refused. |
| 721 | func TestALinkIsRememberedForTheWholeTalk(t *testing.T) { |
| 722 | a := newApp(t) |
| 723 | cookies := signedIn(t, a, allowedAddress) |
| 724 | session, _ := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 725 | link := newPressID(t) |
| 726 | |
| 727 | abandoned := linkPress(t, "next", link, 1) |
| 728 | resume := linkPress(t, "resume", link, 2) |
| 729 | resume.Set("at_slide", "plain") |
| 730 | resume.Set("at_step", "0") |
| 731 | resume.Set("at_mode", "core") |
| 732 | press(t, a, session, "", resume, cookies) |
| 733 | // 70 other window loads, each pressing once: blackout on and off, |
| 734 | // so the talk stays on the plain slide. |
| 735 | for range 70 { |
| 736 | press(t, a, session, "", linkPress(t, "blackout", newPressID(t), 1), cookies) |
| 737 | } |
| 738 | before, step, seq := rowPosition(t, a, session) |
| 739 | late := press(t, a, session, "", abandoned, cookies) |
| 740 | if id, k, now := rowPosition(t, a, session); id != before || k != step || now != seq || late.Seq != seq { |
| 741 | t.Errorf("after 70 other links, the abandoned press moved the talk from %s step %d to %s step %d (seq %d to %d)", |
| 742 | before, step, id, k, seq, now) |
| 743 | } |
| 744 | } |
| 745 | |