package slidestest import ( "encoding/json" "net/http" "net/http/httptest" "net/url" "regexp" "strings" "testing" ) // followURLPattern is the one address a follow-along code holds: this // instance's origin, a fresh token, ?follow=1. var followURLPattern = regexp.MustCompile(`^` + regexp.QuoteMeta(testOrigin) + `/s/([A-Za-z0-9_-]{43})\?follow=1$`) // qrLink posts the editor's request for an audience link and decodes // the answer. func qrLink(t *testing.T, a *testApp, deckPath string, cookies []*http.Cookie) (int, string) { t.Helper() rec := post(t, a, deckPath+"/qr-link", nil, cookies...) if rec.Code != http.StatusOK { return rec.Code, "" } if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") { t.Errorf("Content-Type %q, want JSON", ct) } if cc := rec.Header().Get("Cache-Control"); cc != "no-store" { t.Errorf("Cache-Control %q: an answer carrying a capability must not be cached", cc) } var body struct { URL string `json:"url"` } if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil { t.Fatalf("POST %s/qr-link: %v\n%s", deckPath, err, rec.Body) } return rec.Code, body.URL } // TestTheQRLinkRouteMintsAnAudienceLink: the follow-along code's link is // a share link made exactly as the share page makes one — only its hash // stored, revocable there — named Audience on that page, and answered // as its follow address. Like the share page it is the owner's alone: a // team member who can edit the deck still cannot mint a capability for // it, and nobody else learns the deck exists. func TestTheQRLinkRouteMintsAnAudienceLink(t *testing.T) { a := newSharedApp(t) mine := signedIn(t, a, allowedAddress) theirs := signedIn(t, a, otherAddress) path := createDeck(t, a, mine, "Talk") code, got := qrLink(t, a, path, mine) if code != http.StatusOK { t.Fatalf("POST %s/qr-link: status %d, want 200", path, code) } m := followURLPattern.FindStringSubmatch(got) if m == nil { t.Fatalf("answered %q, want %s/s/?follow=1", got, testOrigin) } token := m[1] // The address works with no session at all: nothing is being // presented, so it lands on the deck to read. rec := get(t, a, "/s/"+token+"?follow=1") if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/s/"+token { t.Errorf("following with no talk live: status %d to %q, want 303 to the deck", rec.Code, rec.Header().Get("Location")) } if page := mustGet(t, a, "/s/"+token, http.StatusOK, nil).Body.String(); !strings.Contains(page, "
]*>`).ReplaceAllString(body, "")), ""); !strings.Contains(words, aAddress) { t.Errorf("%s does not draw the reader's own address %s", target, aAddress) } } // Members see the address the code was made with. if page := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String(); !strings.Contains(page, bToken) { t.Error("the owner's viewer does not draw the code's own link") } } // codeDeckOn is codeDeck on a deck that already exists. func codeDeckOn(t *testing.T, a *testApp, cookies []*http.Cookie, path, url string) (string, string, string) { t.Helper() sqid := strings.TrimPrefix(path, "/d/") token := mintAgent(t, a, cookies, "code-on-"+sqid) view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) slide, _ := view["slides"].([]any)[0].(map[string]any)["id"].(string) apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[{"op":"add_block","slide":`+quote(slide)+`,"id":"qrcode000001","type":"qr","props":{"url":`+quote(url)+`}}]}`, http.StatusOK) return path, sqid, slide } // TestAFollowAlongCodeDrawsOnlyThisDecksLiveLink: the document takes // any follow-shaped address, and the page is the authority on which // ones are this talk's. A code is drawn only for a link of this // instance's origin that is a live share link of this very deck; // anything else is no code for a reader and an empty frame in the // editor — and revoking the link turns a drawn code off. func TestAFollowAlongCodeDrawsOnlyThisDecksLiveLink(t *testing.T) { a := newApp(t) mine := signedIn(t, a, allowedAddress) other := createDeck(t, a, mine, "Other") _, theirs := qrLink(t, a, other, mine) _, live := qrLink(t, a, other, mine) liveToken := followURLPattern.FindStringSubmatch(live)[1] for name, url := range map[string]string{ "another origin": "https://elsewhere.example/s/" + liveToken + "?follow=1", "another deck's": theirs, "a made-up token": testOrigin + "/s/" + strings.Repeat("a", 43) + "?follow=1", } { path, _, slide := codeDeck(t, a, mine, name, url) viewer := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String() if strings.Contains(viewer, `data-block="qr"`) { t.Errorf("%s: the viewer draws a code", name) } editor := mustGet(t, a, path+"/slides/"+slide+".html", http.StatusOK, mine).Body.String() if !strings.Contains(editor, `data-missing-qr="true"`) || strings.Contains(editor, "