package slidestest
// Pairing a phone from the presenter window's ⋯ menu (Task 13, spec §6
// "Who may drive"): POST /p/{session}/pair, for the signed-in presenter
// alone, makes a new remote token, retires the old one, and answers
// with the code, the remote's address and its QR. Ending the talk ends
// the phone and the follow link with it.
import (
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"strings"
"testing"
)
// TestPairingReplacesTheOldToken: pairing answers with a fresh token,
// shown once and never cached, and the phone holding the old one
// drives and reads nothing from then on.
func TestPairingReplacesTheOldToken(t *testing.T) {
a := newApp(t)
cookies := signedIn(t, a, allowedAddress)
session, old := startPresenting(t, a, stepsDeck(t, a, cookies), cookies)
press(t, a, session, old, op(t, "next"), nil)
rec := post(t, a, "/p/"+session+"/pair", nil, cookies...)
if rec.Code != http.StatusOK {
t.Fatalf("POST /p/%s/pair: status %d, want 200\n%s", session, rec.Code, rec.Body)
}
if cc := rec.Header().Get("Cache-Control"); !strings.Contains(cc, "no-store") {
t.Errorf("the pairing page is cacheable: Cache-Control %q", cc)
}
page := rec.Body.String()
fresh := tokenOn(t, page)
if fresh == old {
t.Fatalf("pairing kept the old token")
}
code := sessionCode(t, a, session)
remoteURL := testOrigin + "/r/" + code + "?t=" + fresh
if !strings.Contains(page, `value="`+remoteURL+`"`) {
t.Errorf("the pairing page does not show the remote address %q:\n%s", remoteURL, page)
}
if codeOn(t, page) != code {
t.Errorf("the pairing page shows code %q, want %q", codeOn(t, page), code)
}
if !strings.Contains(page, "slides-qr") || !strings.Contains(page, code+`"`) {
t.Errorf("the pairing page carries no QR naming the code:\n%s", page)
}
if !strings.Contains(page, "Pairing another phone from the menu retires this one.") {
t.Errorf("the pairing page does not say what pairing again does:\n%s", page)
}
// The old phone is retired: it neither drives nor reads.
if rec := pressRequest(t, a, session, old, op(t, "next"), nil); rec.Code != http.StatusNotFound {
t.Errorf("a press with the retired token: status %d, want 404", rec.Code)
}
mustGet(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(old), http.StatusNotFound, nil)
if rec := get(t, a, "/r/"+code+"?t="+url.QueryEscape(old)); rec.Code == http.StatusSeeOther {
t.Errorf("the retired token still pairs through /r/%s", code)
}
// The new one does both.
press(t, a, session, fresh, op(t, "next"), nil)
mustGet(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(fresh), http.StatusOK, nil)
mustGet(t, a, "/r/"+code+"?t="+url.QueryEscape(fresh), http.StatusSeeOther, nil)
if _, step, _ := rowPosition(t, a, session); step != 2 {
t.Errorf("the talk is at step %d, want 2", step)
}
// And the presenter's own window never needed a token at all.
press(t, a, session, "", op(t, "next"), cookies)
}
// TestOnlyThePresenterMayPair: nobody but the signed-in presenter may
// make a token: not a stranger, not the phone (a token is not a
// presenter), not someone else signed in, not another site through the
// presenter's browser. Each refusal leaves the paired phone paired.
func TestOnlyThePresenterMayPair(t *testing.T) {
a := newSharedApp(t)
mine := signedIn(t, a, allowedAddress)
theirs := signedIn(t, a, otherAddress)
session, token := startPresenting(t, a, stepsDeck(t, a, mine), mine)
target := "/p/" + session + "/pair"
if rec := post(t, a, target, nil); rec.Code != http.StatusNotFound {
t.Errorf("pairing by nobody: status %d, want 404", rec.Code)
}
if rec := post(t, a, target, url.Values{"t": {token}}); rec.Code != http.StatusNotFound {
t.Errorf("pairing with the remote token: status %d, want 404", rec.Code)
}
if rec := post(t, a, target, nil, theirs...); rec.Code != http.StatusNotFound {
t.Errorf("pairing by somebody else signed in: status %d, want 404", rec.Code)
}
if rec := post(t, a, "/p/2222222222/pair", nil, mine...); rec.Code != http.StatusNotFound {
t.Errorf("pairing a session that never was: status %d, want 404", rec.Code)
}
req := httptest.NewRequest(http.MethodPost, target, nil)
req.Header.Set("Origin", "https://elsewhere.example")
req.Header.Set("Sec-Fetch-Site", "cross-site")
for _, c := range mine {
req.AddCookie(c)
}
cross := httptest.NewRecorder()
a.ServeHTTP(cross, req)
if cross.Code != http.StatusForbidden {
t.Errorf("a cross-site pairing with the presenter's cookie: status %d, want 403", cross.Code)
}
// Nothing refused retired the phone.
press(t, a, session, token, op(t, "next"), nil)
// A talk that is over pairs nothing: it is gone.
command(t, a, session, "end", token, "", nil)
if rec := post(t, a, target, nil, mine...); rec.Code != http.StatusGone {
t.Errorf("pairing an ended talk: status %d, want 410", rec.Code)
}
}
// endForm is the window's End presenting form: its op and press id.
var endForm = regexp.MustCompile(`\s*`)
// TestEndingTheTalkEndsThePhone: End presenting in the window's menu,
// posted as the window posts it (by the presenter's cookie, no token),
// ends the talk, and the paired phone and the follow link end with it.
func TestEndingTheTalkEndsThePhone(t *testing.T) {
a := newApp(t)
cookies := signedIn(t, a, allowedAddress)
deck := stepsDeck(t, a, cookies)
share := "/s/" + mintShareLink(t, a, deck, cookies)
session, token := startPresenting(t, a, deck, cookies)
mustGet(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(token), http.StatusOK, nil)
mustGet(t, a, share+"?follow=1", http.StatusOK, nil)
window := mustGet(t, a, "/p/"+session+"/notes", http.StatusOK, cookies).Body.String()
m := endForm.FindStringSubmatch(window)
if m == nil {
t.Fatalf("the window has no End presenting form:\n%s", window)
}
form := url.Values{"op": {"end"}, "press": {m[1]}}
req := httptest.NewRequest(http.MethodPost, "/p/"+session+"/command", strings.NewReader(form.Encode()))
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Origin", testOrigin)
req.Header.Set("Sec-Fetch-Site", "same-origin")
req.Header.Set("Referer", testOrigin+"/p/"+session+"/notes")
for _, c := range cookies {
req.AddCookie(c)
}
rec := httptest.NewRecorder()
a.ServeHTTP(rec, req)
if rec.Code != http.StatusSeeOther {
t.Fatalf("End presenting: status %d, want 303\n%s", rec.Code, rec.Body)
}
if rec := getHTML(t, a, "/p/"+session+"/remote?t="+url.QueryEscape(token)); rec.Code != http.StatusGone {
t.Errorf("the phone after the talk ended: status %d, want 410", rec.Code)
}
if rec := command(t, a, session, "next", token, "", nil); rec.Code != http.StatusGone {
t.Errorf("a press from the phone after the talk ended: status %d, want 410", rec.Code)
}
if rec := get(t, a, share+"?follow=1"); rec.Code != http.StatusSeeOther {
t.Errorf("following an ended talk: status %d, want 303 back to the deck", rec.Code)
}
if strings.Contains(mustGet(t, a, share, http.StatusOK, nil).Body.String(), "Follow the presenter") {
t.Errorf("the follow offer outlives the talk")
}
}