| 1 | package slidestest |
| 2 | |
| 3 | // Pairing a phone from the presenter window's ⋯ menu (Task 13, spec §6 |
| 4 | // "Who may drive"): POST /p/{session}/pair, for the signed-in presenter |
| 5 | // alone, makes a new remote token, retires the old one, and answers |
| 6 | // with the code, the remote's address and its QR. Ending the talk ends |
| 7 | // the phone and the follow link with it. |
| 8 | |
| 9 | import ( |
| 10 | "net/http" |
| 11 | "net/http/httptest" |
| 12 | "net/url" |
| 13 | "regexp" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | ) |
| 17 | |
| 18 | // TestPairingReplacesTheOldToken: pairing answers with a fresh token, |
| 19 | // shown once and never cached, and the phone holding the old one |
| 20 | // drives and reads nothing from then on. |
| 21 | func TestPairingReplacesTheOldToken(t *testing.T) { |
| 22 | a := newApp(t) |
| 23 | cookies := signedIn(t, a, allowedAddress) |
| 24 | session, old := startPresenting(t, a, stepsDeck(t, a, cookies), cookies) |
| 25 | press(t, a, session, old, op(t, "next"), nil) |
| 26 | |
| 27 | rec := post(t, a, "/p/"+session+"/pair", nil, cookies...) |
| 28 | if rec.Code != http.StatusOK { |
| 29 | t.Fatalf("POST /p/%s/pair: status %d, want 200\n%s", session, rec.Code, rec.Body) |
| 30 | } |
| 31 | if cc := rec.Header().Get("Cache-Control"); !strings.Contains(cc, "no-store") { |
| 32 | t.Errorf("the pairing page is cacheable: Cache-Control %q", cc) |
| 33 | } |
| 34 | page := rec.Body.String() |
| 35 | fresh := tokenOn(t, page) |
| 36 | if fresh == old { |
| 37 | t.Fatalf("pairing kept the old token") |
| 38 | } |
| 39 | code := sessionCode(t, a, session) |
| 40 | remoteURL := testOrigin + "/r/" + code + "?t=" + fresh |
| 41 | if !strings.Contains(page, `value="`+remoteURL+`"`) { |
| 42 | t.Errorf("the pairing page does not show the remote address %q:\n%s", remoteURL, page) |
| 43 | } |
| 44 | if codeOn(t, page) != code { |
| 45 | t.Errorf("the pairing page shows code %q, want %q", codeOn(t, page), code) |
| 46 | } |
| 47 | if !strings.Contains(page, "slides-qr") || !strings.Contains(page, code+`"`) { |
| 48 | t.Errorf("the pairing page carries no QR naming the code:\n%s", page) |
| 49 | } |
| 50 | if !strings.Contains(page, "Pairing another phone from the menu retires this one.") { |
| 51 | t.Errorf("the pairing page does not say what pairing again does:\n%s", page) |
| 52 | } |
| 53 | |
| 54 | // The old phone is retired: it neither drives nor reads. |
| 55 | if rec := pressRequest(t, a, session, old, op(t, "next"), nil); rec.Code != http.StatusNotFound { |
| 56 | t.Errorf("a press with the retired token: status %d, want 404", rec.Code) |
| 57 | } |
| 58 | mustGet(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(old), http.StatusNotFound, nil) |
| 59 | if rec := get(t, a, "/r/"+code+"?t="+url.QueryEscape(old)); rec.Code == http.StatusSeeOther { |
| 60 | t.Errorf("the retired token still pairs through /r/%s", code) |
| 61 | } |
| 62 | |
| 63 | // The new one does both. |
| 64 | press(t, a, session, fresh, op(t, "next"), nil) |
| 65 | mustGet(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(fresh), http.StatusOK, nil) |
| 66 | mustGet(t, a, "/r/"+code+"?t="+url.QueryEscape(fresh), http.StatusSeeOther, nil) |
| 67 | if _, step, _ := rowPosition(t, a, session); step != 2 { |
| 68 | t.Errorf("the talk is at step %d, want 2", step) |
| 69 | } |
| 70 | |
| 71 | // And the presenter's own window never needed a token at all. |
| 72 | press(t, a, session, "", op(t, "next"), cookies) |
| 73 | } |
| 74 | |
| 75 | // TestOnlyThePresenterMayPair: nobody but the signed-in presenter may |
| 76 | // make a token: not a stranger, not the phone (a token is not a |
| 77 | // presenter), not someone else signed in, not another site through the |
| 78 | // presenter's browser. Each refusal leaves the paired phone paired. |
| 79 | func TestOnlyThePresenterMayPair(t *testing.T) { |
| 80 | a := newSharedApp(t) |
| 81 | mine := signedIn(t, a, allowedAddress) |
| 82 | theirs := signedIn(t, a, otherAddress) |
| 83 | session, token := startPresenting(t, a, stepsDeck(t, a, mine), mine) |
| 84 | target := "/p/" + session + "/pair" |
| 85 | |
| 86 | if rec := post(t, a, target, nil); rec.Code != http.StatusNotFound { |
| 87 | t.Errorf("pairing by nobody: status %d, want 404", rec.Code) |
| 88 | } |
| 89 | if rec := post(t, a, target, url.Values{"t": {token}}); rec.Code != http.StatusNotFound { |
| 90 | t.Errorf("pairing with the remote token: status %d, want 404", rec.Code) |
| 91 | } |
| 92 | if rec := post(t, a, target, nil, theirs...); rec.Code != http.StatusNotFound { |
| 93 | t.Errorf("pairing by somebody else signed in: status %d, want 404", rec.Code) |
| 94 | } |
| 95 | if rec := post(t, a, "/p/2222222222/pair", nil, mine...); rec.Code != http.StatusNotFound { |
| 96 | t.Errorf("pairing a session that never was: status %d, want 404", rec.Code) |
| 97 | } |
| 98 | |
| 99 | req := httptest.NewRequest(http.MethodPost, target, nil) |
| 100 | req.Header.Set("Origin", "https://elsewhere.example") |
| 101 | req.Header.Set("Sec-Fetch-Site", "cross-site") |
| 102 | for _, c := range mine { |
| 103 | req.AddCookie(c) |
| 104 | } |
| 105 | cross := httptest.NewRecorder() |
| 106 | a.ServeHTTP(cross, req) |
| 107 | if cross.Code != http.StatusForbidden { |
| 108 | t.Errorf("a cross-site pairing with the presenter's cookie: status %d, want 403", cross.Code) |
| 109 | } |
| 110 | |
| 111 | // Nothing refused retired the phone. |
| 112 | press(t, a, session, token, op(t, "next"), nil) |
| 113 | |
| 114 | // A talk that is over pairs nothing: it is gone. |
| 115 | command(t, a, session, "end", token, "", nil) |
| 116 | if rec := post(t, a, target, nil, mine...); rec.Code != http.StatusGone { |
| 117 | t.Errorf("pairing an ended talk: status %d, want 410", rec.Code) |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | // endForm is the window's End presenting form: its op and press id. |
| 122 | var endForm = regexp.MustCompile(`<input type="hidden" name="op" value="end">\s*<input type="hidden" name="press" value="([^"]+)">`) |
| 123 | |
| 124 | // TestEndingTheTalkEndsThePhone: End presenting in the window's menu, |
| 125 | // posted as the window posts it (by the presenter's cookie, no token), |
| 126 | // ends the talk, and the paired phone and the follow link end with it. |
| 127 | func TestEndingTheTalkEndsThePhone(t *testing.T) { |
| 128 | a := newApp(t) |
| 129 | cookies := signedIn(t, a, allowedAddress) |
| 130 | deck := stepsDeck(t, a, cookies) |
| 131 | share := "/s/" + mintShareLink(t, a, deck, cookies) |
| 132 | session, token := startPresenting(t, a, deck, cookies) |
| 133 | mustGet(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(token), http.StatusOK, nil) |
| 134 | mustGet(t, a, share+"?follow=1", http.StatusOK, nil) |
| 135 | |
| 136 | window := mustGet(t, a, "/p/"+session+"/notes", http.StatusOK, cookies).Body.String() |
| 137 | m := endForm.FindStringSubmatch(window) |
| 138 | if m == nil { |
| 139 | t.Fatalf("the window has no End presenting form:\n%s", window) |
| 140 | } |
| 141 | form := url.Values{"op": {"end"}, "press": {m[1]}} |
| 142 | req := httptest.NewRequest(http.MethodPost, "/p/"+session+"/command", strings.NewReader(form.Encode())) |
| 143 | req.Header.Set("Content-Type", "application/x-www-form-urlencoded") |
| 144 | req.Header.Set("Origin", testOrigin) |
| 145 | req.Header.Set("Sec-Fetch-Site", "same-origin") |
| 146 | req.Header.Set("Referer", testOrigin+"/p/"+session+"/notes") |
| 147 | for _, c := range cookies { |
| 148 | req.AddCookie(c) |
| 149 | } |
| 150 | rec := httptest.NewRecorder() |
| 151 | a.ServeHTTP(rec, req) |
| 152 | if rec.Code != http.StatusSeeOther { |
| 153 | t.Fatalf("End presenting: status %d, want 303\n%s", rec.Code, rec.Body) |
| 154 | } |
| 155 | |
| 156 | if rec := getHTML(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(token)); rec.Code != http.StatusGone { |
| 157 | t.Errorf("the phone after the talk ended: status %d, want 410", rec.Code) |
| 158 | } |
| 159 | if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusGone { |
| 160 | t.Errorf("a press from the phone after the talk ended: status %d, want 410", rec.Code) |
| 161 | } |
| 162 | if rec := get(t, a, share+"?follow=1"); rec.Code != http.StatusSeeOther { |
| 163 | t.Errorf("following an ended talk: status %d, want 303 back to the deck", rec.Code) |
| 164 | } |
| 165 | if strings.Contains(mustGet(t, a, share, http.StatusOK, nil).Body.String(), "Follow the presenter") { |
| 166 | t.Errorf("the follow offer outlives the talk") |
| 167 | } |
| 168 | } |
| 169 | |