| 1 | package slidestest |
| 2 | |
| 3 | // Who may drive a talk by being signed in (spec §6 "Who may drive", |
| 4 | // S19), and the admission that comes first: the presenter's cookie is |
| 5 | // a capability on the /p/ routes, so it is put through the same check |
| 6 | // every signed-in route makes (auth.go's currentUser). An address taken |
| 7 | // off the list neither presses nor pairs, from the first request after |
| 8 | // the restart. And a talk begun before positions, rejoined through |
| 9 | // Present, is still the presenter's to drive. |
| 10 | |
| 11 | import ( |
| 12 | "net/http" |
| 13 | "net/url" |
| 14 | "strings" |
| 15 | "testing" |
| 16 | ) |
| 17 | |
| 18 | // removedPresenter is a talk under way on the old app, and the same |
| 19 | // database restarted with the presenter's address off the list. |
| 20 | func removedPresenter(t *testing.T) (after *testApp, session string, cookies []*http.Cookie) { |
| 21 | t.Helper() |
| 22 | cfg, mailer := newConfig(t) |
| 23 | d := openTestDB(t, cfg) |
| 24 | before := newAppOver(t, d, cfg, mailer) |
| 25 | cookies = signedIn(t, before, allowedAddress) |
| 26 | session, _ = startPresenting(t, before, stepsDeck(t, before, cookies), cookies) |
| 27 | // The cookie drives while the address is on the list. |
| 28 | press(t, before, session, "", op(t, "next"), cookies) |
| 29 | |
| 30 | gone, _ := newConfig(t) |
| 31 | gone.AllowedAddresses = []string{"someone.else@example.test"} |
| 32 | return newAppOver(t, d, gone, mailer), session, cookies |
| 33 | } |
| 34 | |
| 35 | // remoteHash is the stored hash of the session's remote token. |
| 36 | func remoteHash(t *testing.T, a *testApp, session string) string { |
| 37 | t.Helper() |
| 38 | var h string |
| 39 | if err := a.db.G.Raw("SELECT COALESCE(remote_token_hash, '') FROM presentations WHERE sqid = ?", session).Row().Scan(&h); err != nil { |
| 40 | t.Fatalf("read the remote token of %s: %v", session, err) |
| 41 | } |
| 42 | return h |
| 43 | } |
| 44 | |
| 45 | // TestARemovedPresenterNeitherPressesNorPairs: right after the address |
| 46 | // comes off the list, the presenter's cookie is refused as every |
| 47 | // signed-in route refuses it (403 for a POST, and the talk ended), and |
| 48 | // nothing moves: no press, no fresh token. |
| 49 | func TestARemovedPresenterNeitherPressesNorPairs(t *testing.T) { |
| 50 | t.Run("a press", func(t *testing.T) { |
| 51 | a, session, cookies := removedPresenter(t) |
| 52 | _, stepBefore, seqBefore := rowPosition(t, a, session) |
| 53 | rec := pressRequest(t, a, session, "", op(t, "next"), cookies) |
| 54 | if rec.Code != http.StatusForbidden { |
| 55 | t.Errorf("a press by a removed presenter: status %d, want 403\n%s", rec.Code, rec.Body) |
| 56 | } |
| 57 | if _, step, seq := rowPosition(t, a, session); step != stepBefore || seq != seqBefore { |
| 58 | t.Errorf("a removed presenter's press moved the talk: step %d seq %d, was step %d seq %d", step, seq, stepBefore, seqBefore) |
| 59 | } |
| 60 | if rec := get(t, a, "/p/"+session); rec.Code != http.StatusGone { |
| 61 | t.Errorf("the removed presenter's talk: status %d, want 410", rec.Code) |
| 62 | } |
| 63 | }) |
| 64 | t.Run("pairing", func(t *testing.T) { |
| 65 | a, session, cookies := removedPresenter(t) |
| 66 | before := remoteHash(t, a, session) |
| 67 | rec := post(t, a, "/p/"+session+"/pair", nil, cookies...) |
| 68 | if rec.Code != http.StatusForbidden { |
| 69 | t.Errorf("pairing by a removed presenter: status %d, want 403\n%s", rec.Code, rec.Body) |
| 70 | } |
| 71 | if strings.Contains(rec.Body.String(), "/r/") { |
| 72 | t.Errorf("a removed presenter was shown a remote address:\n%s", rec.Body) |
| 73 | } |
| 74 | if after := remoteHash(t, a, session); after != before { |
| 75 | t.Error("a removed presenter's pairing replaced the remote token") |
| 76 | } |
| 77 | }) |
| 78 | t.Run("the window", func(t *testing.T) { |
| 79 | a, session, cookies := removedPresenter(t) |
| 80 | rec := get(t, a, "/p/"+session+"/notes", cookies...) |
| 81 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/signin?err=removed" { |
| 82 | t.Errorf("a removed presenter's window: status %d to %q, want 303 to /signin?err=removed", rec.Code, rec.Header().Get("Location")) |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | |
| 87 | // TestRejoiningAPreDeployTalkKeepsItsControls: a talk begun before |
| 88 | // positions, rejoined through Present, is driven by the signed-in |
| 89 | // presenter from the slide window and the presenter view, by their |
| 90 | // cookie, by its index; the phone already paired still drives too, and |
| 91 | // the row stays an index row. Somebody else signed in only watches. |
| 92 | func TestRejoiningAPreDeployTalkKeepsItsControls(t *testing.T) { |
| 93 | a := newSharedApp(t) |
| 94 | cookies := signedIn(t, a, allowedAddress) |
| 95 | path := sampleDeck(t, a, cookies) |
| 96 | session, token := startPresenting(t, a, path, cookies) |
| 97 | preDeploy(t, a, session) |
| 98 | |
| 99 | // Present again, as the presenter does after a deploy. |
| 100 | rec := post(t, a, path+"/present", nil, cookies...) |
| 101 | if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/p/"+session { |
| 102 | t.Fatalf("Present on a pre-deploy talk: status %d to %q, want 303 to /p/%s", rec.Code, rec.Header().Get("Location"), session) |
| 103 | } |
| 104 | |
| 105 | surface := mustGet(t, a, "/p/"+session, http.StatusOK, cookies).Body.String() |
| 106 | if !strings.Contains(surface, `action="/p/`+session+`/command"`) || !strings.Contains(surface, `slides-op="next"`) { |
| 107 | t.Fatalf("the rejoined slide window has no controls for its presenter:\n%s", surface) |
| 108 | } |
| 109 | if !strings.Contains(surface, "data-drives") { |
| 110 | t.Error("the rejoined slide window does not tell its script that it drives") |
| 111 | } |
| 112 | notes := mustGet(t, a, "/p/"+session+"/notes", http.StatusOK, cookies).Body.String() |
| 113 | if !strings.Contains(notes, "slides-commands") || !strings.Contains(notes, `slides-op="next"`) { |
| 114 | t.Fatalf("the rejoined presenter view has no controls for its presenter:\n%s", notes) |
| 115 | } |
| 116 | |
| 117 | // The forms post as they are rendered: no token, the cookie. |
| 118 | form := url.Values{"op": {"next"}, "press": {newPressID(t)}} |
| 119 | if rec := post(t, a, "/p/"+session+"/command", form, cookies...); rec.Code != http.StatusNoContent { |
| 120 | t.Fatalf("the presenter's next on a pre-deploy talk: status %d, want 204\n%s", rec.Code, rec.Body) |
| 121 | } |
| 122 | if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusNoContent { |
| 123 | t.Fatalf("the paired phone's next: status %d, want 204", rec.Code) |
| 124 | } |
| 125 | var index int |
| 126 | var positional *bool |
| 127 | if err := a.db.G.Raw("SELECT slide, positional FROM presentations WHERE sqid = ?", session).Row().Scan(&index, &positional); err != nil { |
| 128 | t.Fatalf("read the session: %v", err) |
| 129 | } |
| 130 | if index != 2 || positional != nil { |
| 131 | t.Errorf("after two nexts the row is at index %d, positional %v; want index 2 and no position", index, positional) |
| 132 | } |
| 133 | |
| 134 | // Somebody else signed in watches and drives nothing. |
| 135 | theirs := signedIn(t, a, otherAddress) |
| 136 | if page := mustGet(t, a, "/p/"+session, http.StatusOK, theirs).Body.String(); strings.Contains(page, `slides-op="next"`) { |
| 137 | t.Error("somebody else signed in is given the controls") |
| 138 | } |
| 139 | if rec := post(t, a, "/p/"+session+"/command", url.Values{"op": {"next"}, "press": {newPressID(t)}}, theirs...); rec.Code != http.StatusNotFound { |
| 140 | t.Errorf("somebody else's next: status %d, want 404", rec.Code) |
| 141 | } |
| 142 | } |
| 143 | |