| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "bufio" |
| 5 | "context" |
| 6 | "net/http" |
| 7 | "net/http/httptest" |
| 8 | "net/url" |
| 9 | "regexp" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | "time" |
| 13 | ) |
| 14 | |
| 15 | // presentLocation is what POST /d/{sqid}/present redirects to: the |
| 16 | // session's own slide (S12). It carries no capability: the presenter |
| 17 | // drives by being signed in, and a phone is paired separately. |
| 18 | var presentLocation = regexp.MustCompile( |
| 19 | `^/p/([23456789abcdefghjkmnpqrstuvwxyz]{10})$`) |
| 20 | |
| 21 | // pairingCode finds the six-character code on the presenter view. The |
| 22 | // alphabet is the unambiguous one (no O, no 0, no I, no 1), because a |
| 23 | // code is read aloud and typed on a phone. |
| 24 | // |
| 25 | // Anchored to the element that displays it rather than loose in the |
| 26 | // page: the remote token on the same page is base64url, and a run of |
| 27 | // six of ITS characters inside this alphabet is a coin flip away — |
| 28 | // an unanchored pattern would find the token about half the time. |
| 29 | var pairingCode = regexp.MustCompile( |
| 30 | `slides-code>([ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6})<`) |
| 31 | |
| 32 | // codeOn reads the pairing code off a presenter view. |
| 33 | func codeOn(t *testing.T, page string) string { |
| 34 | t.Helper() |
| 35 | m := pairingCode.FindStringSubmatch(page) |
| 36 | if m == nil { |
| 37 | t.Fatalf("no pairing code on the presenter view:\n%s", page) |
| 38 | } |
| 39 | return m[1] |
| 40 | } |
| 41 | |
| 42 | // sessionCode is a session's pairing code, read off its row: the |
| 43 | // presenter window shows no pairing block (pairing moves to its ⋯ menu, |
| 44 | // Task 13), so a test that needs the code takes it from where it is. |
| 45 | func sessionCode(t *testing.T, a *testApp, session string) string { |
| 46 | t.Helper() |
| 47 | var code string |
| 48 | if err := a.db.G.Raw("SELECT code FROM presentations WHERE sqid = ?", session).Row().Scan(&code); err != nil { |
| 49 | t.Fatalf("read the pairing code of %s: %v", session, err) |
| 50 | } |
| 51 | return code |
| 52 | } |
| 53 | |
| 54 | // preDeploy makes a session one begun before positions, as migration |
| 55 | // 0009 left such a row: it keeps the old presenter view, pairing block |
| 56 | // and all. |
| 57 | func preDeploy(t *testing.T, a *testApp, session string) { |
| 58 | t.Helper() |
| 59 | if err := a.db.G.Exec(`UPDATE presentations |
| 60 | SET positional = NULL, slide_id = NULL, step = NULL, mode = NULL, seq = NULL, timer_at = NULL |
| 61 | WHERE sqid = ?`, session).Error; err != nil { |
| 62 | t.Fatalf("make %s a pre-deploy session: %v", session, err) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | // startPresenting presses Present on a deck and returns the session's |
| 67 | // sqid and a freshly paired remote token: the presenter pairs a phone |
| 68 | // from the window's menu (pairPhone). |
| 69 | func startPresenting(t *testing.T, a *testApp, deckPath string, cookies []*http.Cookie) (string, string) { |
| 70 | t.Helper() |
| 71 | rec := post(t, a, deckPath+"/present", nil, cookies...) |
| 72 | if rec.Code != http.StatusSeeOther { |
| 73 | t.Fatalf("POST %s/present: status %d, want 303\n%s", deckPath, rec.Code, rec.Body) |
| 74 | } |
| 75 | loc := rec.Header().Get("Location") |
| 76 | m := presentLocation.FindStringSubmatch(loc) |
| 77 | if m == nil { |
| 78 | t.Fatalf("POST %s/present: Location = %q, want /p/{session}", deckPath, loc) |
| 79 | } |
| 80 | return m[1], pairPhone(t, a, m[1], cookies) |
| 81 | } |
| 82 | |
| 83 | // pairPhone pairs a phone as the presenter does, from the window's ⋯ |
| 84 | // menu: POST /p/{session}/pair, signed in, and the token read off the |
| 85 | // page it answers. Pairing again is also how a test retires the old |
| 86 | // token. |
| 87 | func pairPhone(t *testing.T, a *testApp, session string, cookies []*http.Cookie) string { |
| 88 | t.Helper() |
| 89 | rec := post(t, a, "/p/"+session+"/pair", nil, cookies...) |
| 90 | if rec.Code != http.StatusOK { |
| 91 | t.Fatalf("POST /p/%s/pair: status %d, want 200\n%s", session, rec.Code, rec.Body) |
| 92 | } |
| 93 | return tokenOn(t, rec.Body.String()) |
| 94 | } |
| 95 | |
| 96 | // remoteToken finds the token in the remote's address on a pairing |
| 97 | // page: /r/{code}?t={token}, the token base64url. |
| 98 | var remoteToken = regexp.MustCompile(`/r/[ABCDEFGHJKLMNPQRSTUVWXYZ23456789]{6}\?t=([A-Za-z0-9_-]{43})"`) |
| 99 | |
| 100 | // tokenOn reads the remote token off a pairing page. |
| 101 | func tokenOn(t *testing.T, page string) string { |
| 102 | t.Helper() |
| 103 | m := remoteToken.FindStringSubmatch(page) |
| 104 | if m == nil { |
| 105 | t.Fatalf("no remote address on the pairing page:\n%s", page) |
| 106 | } |
| 107 | return m[1] |
| 108 | } |
| 109 | |
| 110 | // command posts one remote command as the phone does: a form, with the |
| 111 | // capability as a field and a fresh press id, as every rendered control |
| 112 | // form carries one. referer is sent when it is not empty, because |
| 113 | // where the answer goes — 303 back, or 204 to a caller with no page to |
| 114 | // return to — is part of what these tests are about. |
| 115 | func command(t *testing.T, a *testApp, session, op, token, referer string, extra url.Values) *httptest.ResponseRecorder { |
| 116 | t.Helper() |
| 117 | form := url.Values{"op": {op}, "t": {token}, "press": {newPressID(t)}} |
| 118 | for k, vs := range extra { |
| 119 | form[k] = vs |
| 120 | } |
| 121 | req := httptest.NewRequest(http.MethodPost, "/p/"+session+"/command", |
| 122 | strings.NewReader(form.Encode())) |
| 123 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 124 | req.Header.Set("Origin", testOrigin) |
| 125 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 126 | if referer != "" { |
| 127 | req.Header.Set("Referer", referer) |
| 128 | } |
| 129 | rec := httptest.NewRecorder() |
| 130 | a.ServeHTTP(rec, req) |
| 131 | return rec |
| 132 | } |
| 133 | |
| 134 | // TestPresentCreatesSessionWithCodeAndRemoteUrl: the Present button is |
| 135 | // a form, pressing it opens a session, and the page it lands on shows |
| 136 | // the pairing code, the remote's address and its QR — once, to the |
| 137 | // presenter, and to nobody else. |
| 138 | func TestPresentCreatesSessionWithCodeAndRemoteUrl(t *testing.T) { |
| 139 | a := newApp(t) |
| 140 | cookies := signedIn(t, a, allowedAddress) |
| 141 | path := sampleDeck(t, a, cookies) |
| 142 | |
| 143 | viewer := mustGet(t, a, path+"?slide=1", http.StatusOK, cookies).Body.String() |
| 144 | if !strings.Contains(viewer, `action="`+path+`/present"`) { |
| 145 | t.Errorf("the viewer offers no Present form:\n%s", viewer) |
| 146 | } |
| 147 | |
| 148 | session, token := startPresenting(t, a, path, cookies) |
| 149 | // The pairing block is the old presenter view's, which a session |
| 150 | // begun before positions keeps; a new session pairs from the |
| 151 | // presenter window's menu (Task 13). |
| 152 | preDeploy(t, a, session) |
| 153 | |
| 154 | notes := mustGet(t, a, "/p/"+session+"/notes?t="+token, http.StatusOK, cookies).Body.String() |
| 155 | code := codeOn(t, notes) |
| 156 | remoteURL := testOrigin + "/r/" + code + "?t=" + token |
| 157 | if !strings.Contains(notes, remoteURL) { |
| 158 | t.Errorf("the presenter view does not show the remote address %q:\n%s", remoteURL, notes) |
| 159 | } |
| 160 | if !strings.Contains(notes, "slides-qr") || !strings.Contains(notes, code+`"`) { |
| 161 | t.Errorf("the presenter view carries no QR naming the code:\n%s", notes) |
| 162 | } |
| 163 | // The presenter view is the presenter's own screen, so it carries |
| 164 | // the notes the projector must not. |
| 165 | if !strings.Contains(notes, "Speaker notes") && !strings.Contains(notes, "Presenter view") { |
| 166 | t.Errorf("the presenter view does not name itself:\n%s", notes) |
| 167 | } |
| 168 | |
| 169 | // Without the capability in the URL there is nothing to copy: the |
| 170 | // pairing block is shown once, at the moment of pairing. |
| 171 | again := mustGet(t, a, "/p/"+session+"/notes", http.StatusOK, cookies).Body.String() |
| 172 | if strings.Contains(again, token) { |
| 173 | t.Errorf("coming back to the presenter view shows the remote token again:\n%s", again) |
| 174 | } |
| 175 | if strings.Contains(again, "slides-qr") { |
| 176 | t.Errorf("coming back to the presenter view shows the QR again:\n%s", again) |
| 177 | } |
| 178 | |
| 179 | // The phone holds the capability and not a session: it reads the |
| 180 | // notes and never the pairing block. |
| 181 | phone := mustGet(t, a, "/p/"+session+"/notes?t="+token, http.StatusOK, nil).Body.String() |
| 182 | if !strings.Contains(phone, "Speaker notes") && !strings.Contains(phone, "Presenter view") { |
| 183 | t.Errorf("the token holder cannot read the presenter view:\n%s", phone) |
| 184 | } |
| 185 | if strings.Contains(phone, remoteURL) { |
| 186 | t.Errorf("the token holder is shown the pairing block:\n%s", phone) |
| 187 | } |
| 188 | |
| 189 | // A stranger with neither gets nothing. |
| 190 | mustGet(t, a, "/p/"+session+"/notes", http.StatusNotFound, nil) |
| 191 | |
| 192 | // Pressing Present again reuses the same session rather than |
| 193 | // opening a second one on the same deck for the same person. |
| 194 | second, _ := startPresenting(t, a, path, cookies) |
| 195 | if second != session { |
| 196 | t.Errorf("pressing Present twice opened a second session: %q then %q", session, second) |
| 197 | } |
| 198 | |
| 199 | // The fullscreen surface is the sqid's own capability: anybody who |
| 200 | // has the address can watch, with no token and no session. |
| 201 | surface := mustGet(t, a, "/p/"+session, http.StatusOK, nil).Body.String() |
| 202 | if !strings.Contains(surface, `<main id="main" slides-present-stage>`) || strings.Contains(surface, `<main rst-page id="main">`) { |
| 203 | t.Errorf("the projector is not a full-screen stage outside the ordinary page wrapper:\n%s", surface) |
| 204 | } |
| 205 | if strings.Contains(surface, "<header estilo-bar") || strings.Contains(surface, "estilo-account") { |
| 206 | t.Errorf("the projector surface carries app chrome:\n%s", surface) |
| 207 | } |
| 208 | if !strings.Contains(surface, "<section slides-slide") { |
| 209 | t.Errorf("the fullscreen surface renders no slide:\n%s", surface) |
| 210 | } |
| 211 | if !strings.Contains(surface, "1 / 8") { |
| 212 | t.Errorf("the fullscreen surface does not say where in the deck it is:\n%s", surface) |
| 213 | } |
| 214 | if !strings.Contains(surface, "<progress") { |
| 215 | t.Errorf("the fullscreen surface has no progress element:\n%s", surface) |
| 216 | } |
| 217 | if strings.Contains(surface, "Speaker notes") { |
| 218 | t.Errorf("the projector surface carries the speaker notes:\n%s", surface) |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | // TestRemoteCommandAdvancesSlideAndPublishes: a phone posts "next" and |
| 223 | // every page watching the session hears about it — which is the whole |
| 224 | // mechanism, so it is asserted through the real stream rather than |
| 225 | // through the row. |
| 226 | func TestRemoteCommandAdvancesSlideAndPublishes(t *testing.T) { |
| 227 | a := newApp(t) |
| 228 | cookies := signedIn(t, a, allowedAddress) |
| 229 | path := sampleDeck(t, a, cookies) |
| 230 | session, token := startPresenting(t, a, path, cookies) |
| 231 | |
| 232 | srv := httptest.NewServer(a) |
| 233 | defer srv.Close() |
| 234 | |
| 235 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 236 | defer cancel() |
| 237 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/p/"+session+"/stream", nil) |
| 238 | if err != nil { |
| 239 | t.Fatalf("build the stream request: %v", err) |
| 240 | } |
| 241 | resp, err := http.DefaultClient.Do(req) |
| 242 | if err != nil { |
| 243 | t.Fatalf("open the stream: %v", err) |
| 244 | } |
| 245 | defer resp.Body.Close() |
| 246 | if got := resp.Header.Get("Content-Type"); got != "text/event-stream" { |
| 247 | t.Fatalf("the stream's Content-Type is %q, want text/event-stream", got) |
| 248 | } |
| 249 | events := bufio.NewReader(resp.Body) |
| 250 | |
| 251 | // One event on connect, so a page that opened the stream after the |
| 252 | // deck moved is right rather than merely hopeful. |
| 253 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"slide":1`) { |
| 254 | t.Errorf("the connect event is %q, want slide 1", data) |
| 255 | } |
| 256 | |
| 257 | rec := command(t, a, session, "next", token, "", nil) |
| 258 | if rec.Code != http.StatusNoContent { |
| 259 | t.Fatalf("POST command with no page to return to: status %d, want 204\n%s", rec.Code, rec.Body) |
| 260 | } |
| 261 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"slide":2`) { |
| 262 | t.Errorf("after next the event is %q, want slide 2", data) |
| 263 | } |
| 264 | |
| 265 | // A browser form submission goes back where it came from, saying |
| 266 | // the slide and step the press reached (Task 6). |
| 267 | back := testOrigin + "/p/" + session + "/remote?t=" + token |
| 268 | rec = command(t, a, session, "next", token, back, nil) |
| 269 | if rec.Code != http.StatusSeeOther { |
| 270 | t.Fatalf("POST command from a page: status %d, want 303\n%s", rec.Code, rec.Body) |
| 271 | } |
| 272 | want := testOrigin + "/p/" + session + "/remote?slide=3&step=0&t=" + token |
| 273 | if loc := rec.Header().Get("Location"); loc != want { |
| 274 | t.Errorf("the command redirected to %q, want %q", loc, want) |
| 275 | } |
| 276 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"slide":3`) { |
| 277 | t.Errorf("after a second next the event is %q, want slide 3", data) |
| 278 | } |
| 279 | |
| 280 | // Blackout is its own state, and it does not move the deck. |
| 281 | rec = command(t, a, session, "blackout", token, "", nil) |
| 282 | if rec.Code != http.StatusNoContent { |
| 283 | t.Fatalf("POST blackout: status %d, want 204\n%s", rec.Code, rec.Body) |
| 284 | } |
| 285 | data := nextSlideEvent(t, events) |
| 286 | if !strings.Contains(data, `"slide":3`) || !strings.Contains(data, `"blackout":true`) { |
| 287 | t.Errorf("after blackout the event is %q, want slide 3 blacked out", data) |
| 288 | } |
| 289 | |
| 290 | // goto names a slide; prev walks back; neither runs off an end. |
| 291 | command(t, a, session, "goto", token, "", url.Values{"slide": {"8"}}) |
| 292 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"slide":8`) { |
| 293 | t.Errorf("after goto 8 the event is %q, want slide 8", data) |
| 294 | } |
| 295 | command(t, a, session, "next", token, "", nil) |
| 296 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"slide":8`) { |
| 297 | t.Errorf("next past the last slide moved to %q, want to stay on slide 8", data) |
| 298 | } |
| 299 | |
| 300 | // And the surface everyone is watching really shows it. |
| 301 | surface := mustGet(t, a, "/p/"+session, http.StatusOK, nil).Body.String() |
| 302 | if !strings.Contains(surface, "8 / 8") { |
| 303 | t.Errorf("the surface does not show the session's own slide:\n%s", surface) |
| 304 | } |
| 305 | if !strings.Contains(surface, `data-blackout="true"`) { |
| 306 | t.Errorf("the surface does not carry the blackout state:\n%s", surface) |
| 307 | } |
| 308 | if !strings.Contains(surface, "blacked out") { |
| 309 | t.Errorf("the blackout is carried by colour alone, with no words:\n%s", surface) |
| 310 | } |
| 311 | |
| 312 | // The two URLs the script uses come down as attributes rather than |
| 313 | // being built in the browser (codex 5): on the projector they are |
| 314 | // the session's own, and static/present.js reads them instead of |
| 315 | // assembling a /p/ path out of a sqid it may not have. |
| 316 | if !strings.Contains(surface, `data-stream="/p/`+session+`/stream"`) { |
| 317 | t.Errorf("the surface does not name its own stream:\n%s", surface) |
| 318 | } |
| 319 | if !strings.Contains(surface, `data-fragment="/p/`+session+`?slide={n}&fragment=1"`) { |
| 320 | t.Errorf("the surface does not name its own fragment URL:\n%s", surface) |
| 321 | } |
| 322 | |
| 323 | // The fragment is the same slide with no page around it — what the |
| 324 | // script swaps in, and never a nested layout. |
| 325 | fragment := mustGet(t, a, "/p/"+session+"?slide=2&fragment=1", http.StatusOK, nil).Body.String() |
| 326 | if !strings.HasPrefix(strings.TrimSpace(fragment), "<section slides-slide") { |
| 327 | t.Errorf("the fragment is not a bare slide:\n%s", fragment) |
| 328 | } |
| 329 | if strings.Contains(fragment, "<html") { |
| 330 | t.Errorf("the fragment carries the whole page:\n%s", fragment) |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // nextSlideEvent reads events until a `slide` one arrives, returning |
| 335 | // its data line. Pings are skipped; the request's own context is the |
| 336 | // timeout, so a stream that never speaks fails the test rather than |
| 337 | // hanging it. |
| 338 | func nextSlideEvent(t *testing.T, r *bufio.Reader) string { |
| 339 | t.Helper() |
| 340 | var event string |
| 341 | for { |
| 342 | line, err := r.ReadString('\n') |
| 343 | if err != nil { |
| 344 | t.Fatalf("reading the stream: %v", err) |
| 345 | } |
| 346 | line = strings.TrimRight(line, "\r\n") |
| 347 | switch { |
| 348 | case strings.HasPrefix(line, "event: "): |
| 349 | event = strings.TrimPrefix(line, "event: ") |
| 350 | case strings.HasPrefix(line, "data: "): |
| 351 | if event == "slide" { |
| 352 | return strings.TrimPrefix(line, "data: ") |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | } |
| 357 | |
| 358 | // TestWrongRemoteTokenIs404: the token is the whole permission to move |
| 359 | // somebody else's deck, and a wrong one is not a 403 — a 403 would |
| 360 | // confirm the session is real to whoever is guessing. |
| 361 | func TestWrongRemoteTokenIs404(t *testing.T) { |
| 362 | a := newApp(t) |
| 363 | cookies := signedIn(t, a, allowedAddress) |
| 364 | path := sampleDeck(t, a, cookies) |
| 365 | session, token := startPresenting(t, a, path, cookies) |
| 366 | |
| 367 | wrong := strings.Repeat("a", len(token)) |
| 368 | if rec := command(t, a, session, "next", wrong, "", nil); rec.Code != http.StatusNotFound { |
| 369 | t.Errorf("a command with the wrong token: status %d, want 404", rec.Code) |
| 370 | } |
| 371 | if rec := command(t, a, session, "next", "", "", nil); rec.Code != http.StatusNotFound { |
| 372 | t.Errorf("a command with no token: status %d, want 404", rec.Code) |
| 373 | } |
| 374 | mustGet(t, a, "/p/"+session+"/remote?t="+wrong, http.StatusNotFound, nil) |
| 375 | mustGet(t, a, "/p/"+session+"/remote", http.StatusNotFound, nil) |
| 376 | // With nobody signed in, the token is the whole permission (the |
| 377 | // presenter's own session may drive too: TestTheSignedInPresenter- |
| 378 | // MayDriveWithoutAToken), so asking twice changes nothing. |
| 379 | if rec := command(t, a, session, "next", wrong, "", nil); rec.Code != http.StatusNotFound { |
| 380 | t.Errorf("a second command with the wrong token: status %d, want 404", rec.Code) |
| 381 | } |
| 382 | |
| 383 | // And nothing moved. |
| 384 | surface := mustGet(t, a, "/p/"+session, http.StatusOK, nil).Body.String() |
| 385 | if !strings.Contains(surface, "1 / 8") { |
| 386 | t.Errorf("a refused command moved the deck anyway:\n%s", surface) |
| 387 | } |
| 388 | |
| 389 | // An unknown session is a 404 too, with the right token or without. |
| 390 | mustGet(t, a, "/p/2222222222", http.StatusNotFound, nil) |
| 391 | if rec := command(t, a, "2222222222", "next", token, "", nil); rec.Code != http.StatusNotFound { |
| 392 | t.Errorf("a command against an unknown session: status %d, want 404", rec.Code) |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | // TestEndedSessionIs410: a talk that is over is gone, not missing. The |
| 397 | // distinction is the point — somebody following a link to last week's |
| 398 | // session should be told it finished, and a dead pairing code should |
| 399 | // tell a stranger nothing at all. |
| 400 | func TestEndedSessionIs410(t *testing.T) { |
| 401 | a := newApp(t) |
| 402 | cookies := signedIn(t, a, allowedAddress) |
| 403 | path := sampleDeck(t, a, cookies) |
| 404 | session, token := startPresenting(t, a, path, cookies) |
| 405 | |
| 406 | code := sessionCode(t, a, session) |
| 407 | mustGet(t, a, "/r/"+code+"?t="+token, http.StatusSeeOther, nil) |
| 408 | |
| 409 | if rec := command(t, a, session, "end", token, "", nil); rec.Code != http.StatusNoContent { |
| 410 | t.Fatalf("ending the session: status %d, want 204\n%s", rec.Code, rec.Body) |
| 411 | } |
| 412 | |
| 413 | for _, target := range []string{ |
| 414 | "/p/" + session, |
| 415 | "/p/" + session + "/notes?t=" + token, |
| 416 | "/p/" + session + "/remote?t=" + token, |
| 417 | } { |
| 418 | rec := getHTML(t, a, target, cookies...) |
| 419 | if rec.Code != http.StatusGone { |
| 420 | t.Errorf("GET %s after the session ended: status %d, want 410", target, rec.Code) |
| 421 | } |
| 422 | if !strings.Contains(rec.Body.String(), "over") { |
| 423 | t.Errorf("GET %s says nothing about the session being over:\n%s", target, rec.Body) |
| 424 | } |
| 425 | } |
| 426 | if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusGone { |
| 427 | t.Errorf("a command against an ended session: status %d, want 410", rec.Code) |
| 428 | } |
| 429 | // A dead code is a 404: a gone would tell whoever typed it that the |
| 430 | // code was once real. |
| 431 | mustGet(t, a, "/r/"+code+"?t="+token, http.StatusNotFound, nil) |
| 432 | mustGet(t, a, "/r/"+code, http.StatusNotFound, nil) |
| 433 | } |
| 434 | |
| 435 | // TestDeletingADeckEndsItsPresentations: deleting a deck takes its live |
| 436 | // talks with it. |
| 437 | // |
| 438 | // deleteDeck cascaded the update log, the grants and the share links |
| 439 | // and left the presentations behind, so a projector went on holding a |
| 440 | // session whose deck no longer existed, a pairing code went on being |
| 441 | // live, and every page watching sat there waiting for an event that |
| 442 | // was never coming. The row goes in the same transaction as the deck, |
| 443 | // and the hub is told the session is over, which is what a projector |
| 444 | // and a phone act on. |
| 445 | func TestDeletingADeckEndsItsPresentations(t *testing.T) { |
| 446 | a := newApp(t) |
| 447 | cookies := signedIn(t, a, allowedAddress) |
| 448 | path := sampleDeck(t, a, cookies) |
| 449 | session, token := startPresenting(t, a, path, cookies) |
| 450 | |
| 451 | code := sessionCode(t, a, session) |
| 452 | mustGet(t, a, "/p/"+session, http.StatusOK, nil) |
| 453 | |
| 454 | // A projector watching, so this asserts what a projector actually |
| 455 | // hears rather than only what the database holds. |
| 456 | srv := httptest.NewServer(a) |
| 457 | defer srv.Close() |
| 458 | ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
| 459 | defer cancel() |
| 460 | req, err := http.NewRequestWithContext(ctx, http.MethodGet, srv.URL+"/p/"+session+"/stream", nil) |
| 461 | if err != nil { |
| 462 | t.Fatalf("build the stream request: %v", err) |
| 463 | } |
| 464 | resp, err := http.DefaultClient.Do(req) |
| 465 | if err != nil { |
| 466 | t.Fatalf("open the stream: %v", err) |
| 467 | } |
| 468 | defer resp.Body.Close() |
| 469 | events := bufio.NewReader(resp.Body) |
| 470 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"ended":false`) { |
| 471 | t.Fatalf("the connect event is %q, want a live session", data) |
| 472 | } |
| 473 | |
| 474 | rec := post(t, a, path+"/delete", nil, cookies...) |
| 475 | if rec.Code != http.StatusSeeOther { |
| 476 | t.Fatalf("POST %s/delete: status %d, want 303\n%s", path, rec.Code, rec.Body) |
| 477 | } |
| 478 | |
| 479 | if data := nextSlideEvent(t, events); !strings.Contains(data, `"ended":true`) { |
| 480 | t.Errorf("the event after the deck was deleted is %q, want the session ended", data) |
| 481 | } |
| 482 | |
| 483 | // Gone or never-was are both honest here; still answering is not. |
| 484 | if rec := get(t, a, "/p/"+session); rec.Code != http.StatusNotFound && rec.Code != http.StatusGone { |
| 485 | t.Errorf("GET /p/%s after its deck was deleted: status %d, want 404 or 410\n%s", session, rec.Code, rec.Body) |
| 486 | } |
| 487 | // The pairing code is a 404 rather than a gone, for the reason a |
| 488 | // dead code always is: it tells whoever typed it nothing. |
| 489 | mustGet(t, a, "/r/"+code, http.StatusNotFound, nil) |
| 490 | mustGet(t, a, "/r/"+code+"?t="+token, http.StatusNotFound, nil) |
| 491 | |
| 492 | var left int64 |
| 493 | if err := a.db.G.Raw("SELECT COUNT(*) FROM presentations").Scan(&left).Error; err != nil { |
| 494 | t.Fatalf("count presentations: %v", err) |
| 495 | } |
| 496 | if left != 0 { |
| 497 | t.Errorf("%d presentation rows outlived their deck, want 0", left) |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | // TestScriptlessRemoteButtonsAreForms: the remote is four buttons, and |
| 502 | // with scripts off each one is a form that posts and comes back. This |
| 503 | // is §8's floor for the one screen most likely to be used on a phone |
| 504 | // with something blocking the script. |
| 505 | func TestScriptlessRemoteButtonsAreForms(t *testing.T) { |
| 506 | a := newApp(t) |
| 507 | cookies := signedIn(t, a, allowedAddress) |
| 508 | path := sampleDeck(t, a, cookies) |
| 509 | session, token := startPresenting(t, a, path, cookies) |
| 510 | |
| 511 | remote := mustGet(t, a, "/p/"+session+"/remote?t="+token, http.StatusOK, nil).Body.String() |
| 512 | if n := strings.Count(remote, `<form method="post"`); n != 4 { |
| 513 | t.Errorf("the remote has %d forms, want 4 (previous, next, blackout, end):\n%s", n, remote) |
| 514 | } |
| 515 | for _, op := range []string{"prev", "next", "blackout", "end"} { |
| 516 | if !strings.Contains(remote, `value="`+op+`"`) { |
| 517 | t.Errorf("the remote has no %s button:\n%s", op, remote) |
| 518 | } |
| 519 | } |
| 520 | if !strings.Contains(remote, `action="/p/`+session+`/command"`) { |
| 521 | t.Errorf("the remote's forms do not post commands:\n%s", remote) |
| 522 | } |
| 523 | if !strings.Contains(remote, `value="`+token+`"`) { |
| 524 | t.Errorf("the remote's forms carry no capability:\n%s", remote) |
| 525 | } |
| 526 | // The phone shows what is on screen now and what is coming, so the |
| 527 | // presenter is not reading the room off a projector behind them. |
| 528 | // A picture of each, not a live copy: two whole slides of DOM on a |
| 529 | // phone over a hotel wifi is a lot of markup for a preview |
| 530 | // (presentation.go's slideThumbImg). |
| 531 | if !strings.Contains(remote, `src="/p/`+session+`/slides/`) { |
| 532 | t.Errorf("the remote shows no picture of the slide:\n%s", remote) |
| 533 | } |
| 534 | if strings.Contains(remote, "<section slides-slide") { |
| 535 | t.Errorf("the remote still renders a whole slide:\n%s", remote) |
| 536 | } |
| 537 | |
| 538 | // Blacking out changes what the button says, not only how it looks. |
| 539 | if !strings.Contains(remote, "Black the screen") { |
| 540 | t.Errorf("the remote's blackout button is unlabelled:\n%s", remote) |
| 541 | } |
| 542 | command(t, a, session, "blackout", token, "", nil) |
| 543 | blacked := mustGet(t, a, "/p/"+session+"/remote?t="+token, http.StatusOK, nil).Body.String() |
| 544 | if !strings.Contains(blacked, "Show the screen") { |
| 545 | t.Errorf("the blackout button does not change its words when the screen is black:\n%s", blacked) |
| 546 | } |
| 547 | |
| 548 | // The pair page is the way in for a typed code, and it is a form |
| 549 | // too — the QR is the shortcut, never the only route. |
| 550 | code := sessionCode(t, a, session) |
| 551 | pair := mustGet(t, a, "/r/"+code, http.StatusOK, nil).Body.String() |
| 552 | if !strings.Contains(pair, "<form") { |
| 553 | t.Errorf("the pair page has no form:\n%s", pair) |
| 554 | } |
| 555 | if !strings.Contains(pair, code) { |
| 556 | t.Errorf("the pair page does not carry the code that reached it:\n%s", pair) |
| 557 | } |
| 558 | // The QR carries both halves, so scanning it lands on the remote. |
| 559 | rec := get(t, a, "/r/"+code+"?t="+token) |
| 560 | if rec.Code != http.StatusSeeOther { |
| 561 | t.Fatalf("GET /r/{code}?t=: status %d, want 303\n%s", rec.Code, rec.Body) |
| 562 | } |
| 563 | if loc := rec.Header().Get("Location"); loc != "/p/"+session+"/remote?t="+token { |
| 564 | t.Errorf("the pair redirect goes to %q, want the remote", loc) |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | // TestFollowOfferedOnlyWhileLive: a share link offers to follow the |
| 569 | // presenter while somebody is presenting, and goes back to being an |
| 570 | // ordinary read-only deck when nobody is. |
| 571 | func TestFollowOfferedOnlyWhileLive(t *testing.T) { |
| 572 | a := newApp(t) |
| 573 | cookies := signedIn(t, a, allowedAddress) |
| 574 | path := sampleDeck(t, a, cookies) |
| 575 | token := mintShareLink(t, a, path, cookies) |
| 576 | share := "/s/" + token |
| 577 | |
| 578 | quiet := mustGet(t, a, share, http.StatusOK, nil).Body.String() |
| 579 | if strings.Contains(quiet, "Follow the presenter") { |
| 580 | t.Errorf("a share link offers to follow with nobody presenting:\n%s", quiet) |
| 581 | } |
| 582 | // Nothing to follow: the flag is not a way to reach a session that |
| 583 | // does not exist. |
| 584 | rec := get(t, a, share+"?follow=1") |
| 585 | if rec.Code != http.StatusSeeOther { |
| 586 | t.Errorf("?follow=1 with nobody presenting: status %d, want 303", rec.Code) |
| 587 | } |
| 588 | |
| 589 | session, remoteToken := startPresenting(t, a, path, cookies) |
| 590 | command(t, a, session, "next", remoteToken, "", nil) |
| 591 | |
| 592 | offered := mustGet(t, a, share, http.StatusOK, nil).Body.String() |
| 593 | if !strings.Contains(offered, "Follow the presenter") { |
| 594 | t.Errorf("a live session is not offered on the share link:\n%s", offered) |
| 595 | } |
| 596 | if !strings.Contains(offered, share+"?follow=1") { |
| 597 | t.Errorf("the follow offer does not link to the follow surface:\n%s", offered) |
| 598 | } |
| 599 | |
| 600 | follow := mustGet(t, a, share+"?follow=1", http.StatusOK, nil).Body.String() |
| 601 | if !strings.Contains(follow, "2 / 8") { |
| 602 | t.Errorf("following does not show the presenter's own slide:\n%s", follow) |
| 603 | } |
| 604 | if !strings.Contains(follow, `http-equiv="refresh"`) { |
| 605 | t.Errorf("the follow surface does not refresh with scripts off:\n%s", follow) |
| 606 | } |
| 607 | if !strings.Contains(follow, share+`"`) { |
| 608 | t.Errorf("the follow surface offers no way back to browsing:\n%s", follow) |
| 609 | } |
| 610 | // Read-only means read-only: an audience cannot move the deck. |
| 611 | if strings.Contains(follow, "<form") { |
| 612 | t.Errorf("the follow surface carries a control:\n%s", follow) |
| 613 | } |
| 614 | if strings.Contains(follow, remoteToken) { |
| 615 | t.Errorf("the follow surface leaks the remote capability:\n%s", follow) |
| 616 | } |
| 617 | // And it never leaks the notes, which is what a share link is not. |
| 618 | if strings.Contains(follow, "Speaker notes") { |
| 619 | t.Errorf("the follow surface carries the speaker notes:\n%s", follow) |
| 620 | } |
| 621 | |
| 622 | command(t, a, session, "end", remoteToken, "", nil) |
| 623 | after := mustGet(t, a, share, http.StatusOK, nil).Body.String() |
| 624 | if strings.Contains(after, "Follow the presenter") { |
| 625 | t.Errorf("the follow offer outlives the session:\n%s", after) |
| 626 | } |
| 627 | if rec := get(t, a, share+"?follow=1"); rec.Code != http.StatusSeeOther { |
| 628 | t.Errorf("following an ended session: status %d, want 303 back to the deck", rec.Code) |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // TestEveryPresentingPageSaysWhichSlideItShows: every page that loads |
| 633 | // static/present.js puts the slide it was rendered for on the script's |
| 634 | // root as data-slide, the same 1-based number the stream's events |
| 635 | // carry. The presenter view and the remote once had none, so the |
| 636 | // script took them to be on slide 0 and reloaded on every event — |
| 637 | // the stream's first included — forever (polish audit B3). The |
| 638 | // browser half is TestBrowserPresentingScreensHoldStill; this one is |
| 639 | // in the plain suite so the gate holds it too. |
| 640 | func TestEveryPresentingPageSaysWhichSlideItShows(t *testing.T) { |
| 641 | a := newApp(t) |
| 642 | cookies := signedIn(t, a, allowedAddress) |
| 643 | path := sampleDeck(t, a, cookies) |
| 644 | session, token := startPresenting(t, a, path, cookies) |
| 645 | command(t, a, session, "next", token, "", nil) |
| 646 | |
| 647 | for _, page := range []string{ |
| 648 | "/p/" + session + "?t=" + token, |
| 649 | "/p/" + session + "/notes?t=" + token, |
| 650 | "/p/" + session + "/remote?t=" + token, |
| 651 | } { |
| 652 | body := mustGet(t, a, page, http.StatusOK, cookies).Body.String() |
| 653 | if !strings.Contains(body, `static/present`) { |
| 654 | t.Errorf("%s no longer loads present.js; this test is about the pages that do", page) |
| 655 | } |
| 656 | if !strings.Contains(body, `data-slide="2"`) { |
| 657 | t.Errorf("%s: the script root does not say data-slide=\"2\" after one next:\n%s", page, body) |
| 658 | } |
| 659 | } |
| 660 | } |
| 661 | |