package slidestest // Who may drive a talk by being signed in (spec ยง6 "Who may drive", // S19), and the admission that comes first: the presenter's cookie is // a capability on the /p/ routes, so it is put through the same check // every signed-in route makes (auth.go's currentUser). An address taken // off the list neither presses nor pairs, from the first request after // the restart. And a talk begun before positions, rejoined through // Present, is still the presenter's to drive. import ( "net/http" "net/url" "strings" "testing" ) // removedPresenter is a talk under way on the old app, and the same // database restarted with the presenter's address off the list. func removedPresenter(t *testing.T) (after *testApp, session string, cookies []*http.Cookie) { t.Helper() cfg, mailer := newConfig(t) d := openTestDB(t, cfg) before := newAppOver(t, d, cfg, mailer) cookies = signedIn(t, before, allowedAddress) session, _ = startPresenting(t, before, stepsDeck(t, before, cookies), cookies) // The cookie drives while the address is on the list. press(t, before, session, "", op(t, "next"), cookies) gone, _ := newConfig(t) gone.AllowedAddresses = []string{"someone.else@example.test"} return newAppOver(t, d, gone, mailer), session, cookies } // remoteHash is the stored hash of the session's remote token. func remoteHash(t *testing.T, a *testApp, session string) string { t.Helper() var h string if err := a.db.G.Raw("SELECT COALESCE(remote_token_hash, '') FROM presentations WHERE sqid = ?", session).Row().Scan(&h); err != nil { t.Fatalf("read the remote token of %s: %v", session, err) } return h } // TestARemovedPresenterNeitherPressesNorPairs: right after the address // comes off the list, the presenter's cookie is refused as every // signed-in route refuses it (403 for a POST, and the talk ended), and // nothing moves: no press, no fresh token. func TestARemovedPresenterNeitherPressesNorPairs(t *testing.T) { t.Run("a press", func(t *testing.T) { a, session, cookies := removedPresenter(t) _, stepBefore, seqBefore := rowPosition(t, a, session) rec := pressRequest(t, a, session, "", op(t, "next"), cookies) if rec.Code != http.StatusForbidden { t.Errorf("a press by a removed presenter: status %d, want 403\n%s", rec.Code, rec.Body) } if _, step, seq := rowPosition(t, a, session); step != stepBefore || seq != seqBefore { t.Errorf("a removed presenter's press moved the talk: step %d seq %d, was step %d seq %d", step, seq, stepBefore, seqBefore) } if rec := get(t, a, "/p/"+session); rec.Code != http.StatusGone { t.Errorf("the removed presenter's talk: status %d, want 410", rec.Code) } }) t.Run("pairing", func(t *testing.T) { a, session, cookies := removedPresenter(t) before := remoteHash(t, a, session) rec := post(t, a, "/p/"+session+"/pair", nil, cookies...) if rec.Code != http.StatusForbidden { t.Errorf("pairing by a removed presenter: status %d, want 403\n%s", rec.Code, rec.Body) } if strings.Contains(rec.Body.String(), "/r/") { t.Errorf("a removed presenter was shown a remote address:\n%s", rec.Body) } if after := remoteHash(t, a, session); after != before { t.Error("a removed presenter's pairing replaced the remote token") } }) t.Run("the window", func(t *testing.T) { a, session, cookies := removedPresenter(t) rec := get(t, a, "/p/"+session+"/notes", cookies...) if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/signin?err=removed" { t.Errorf("a removed presenter's window: status %d to %q, want 303 to /signin?err=removed", rec.Code, rec.Header().Get("Location")) } }) } // TestRejoiningAPreDeployTalkKeepsItsControls: a talk begun before // positions, rejoined through Present, is driven by the signed-in // presenter from the slide window and the presenter view, by their // cookie, by its index; the phone already paired still drives too, and // the row stays an index row. Somebody else signed in only watches. func TestRejoiningAPreDeployTalkKeepsItsControls(t *testing.T) { a := newSharedApp(t) cookies := signedIn(t, a, allowedAddress) path := sampleDeck(t, a, cookies) session, token := startPresenting(t, a, path, cookies) preDeploy(t, a, session) // Present again, as the presenter does after a deploy. rec := post(t, a, path+"/present", nil, cookies...) if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/p/"+session { t.Fatalf("Present on a pre-deploy talk: status %d to %q, want 303 to /p/%s", rec.Code, rec.Header().Get("Location"), session) } surface := mustGet(t, a, "/p/"+session, http.StatusOK, cookies).Body.String() if !strings.Contains(surface, `action="/p/`+session+`/command"`) || !strings.Contains(surface, `slides-op="next"`) { t.Fatalf("the rejoined slide window has no controls for its presenter:\n%s", surface) } if !strings.Contains(surface, "data-drives") { t.Error("the rejoined slide window does not tell its script that it drives") } notes := mustGet(t, a, "/p/"+session+"/notes", http.StatusOK, cookies).Body.String() if !strings.Contains(notes, "slides-commands") || !strings.Contains(notes, `slides-op="next"`) { t.Fatalf("the rejoined presenter view has no controls for its presenter:\n%s", notes) } // The forms post as they are rendered: no token, the cookie. form := url.Values{"op": {"next"}, "press": {newPressID(t)}} if rec := post(t, a, "/p/"+session+"/command", form, cookies...); rec.Code != http.StatusNoContent { t.Fatalf("the presenter's next on a pre-deploy talk: status %d, want 204\n%s", rec.Code, rec.Body) } if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusNoContent { t.Fatalf("the paired phone's next: status %d, want 204", rec.Code) } var index int var positional *bool if err := a.db.G.Raw("SELECT slide, positional FROM presentations WHERE sqid = ?", session).Row().Scan(&index, &positional); err != nil { t.Fatalf("read the session: %v", err) } if index != 2 || positional != nil { t.Errorf("after two nexts the row is at index %d, positional %v; want index 2 and no position", index, positional) } // Somebody else signed in watches and drives nothing. theirs := signedIn(t, a, otherAddress) if page := mustGet(t, a, "/p/"+session, http.StatusOK, theirs).Body.String(); strings.Contains(page, `slides-op="next"`) { t.Error("somebody else signed in is given the controls") } if rec := post(t, a, "/p/"+session+"/command", url.Values{"op": {"next"}, "press": {newPressID(t)}}, theirs...); rec.Code != http.StatusNotFound { t.Errorf("somebody else's next: status %d, want 404", rec.Code) } }