oficina / slides Public

Clone
git clone https://amadan.net/oficina/slides

Plain git — no account needed to clone.

Download

Download this file

1package slidestest
2
3import (
4 "encoding/json"
5 "net/http"
6 "net/http/httptest"
7 "net/url"
8 "regexp"
9 "strings"
10 "testing"
11)
12
13// followURLPattern is the one address a follow-along code holds: this
14// instance's origin, a fresh token, ?follow=1.
15var followURLPattern = regexp.MustCompile(`^` + regexp.QuoteMeta(testOrigin) + `/s/([A-Za-z0-9_-]{43})\?follow=1$`)
16
17// qrLink posts the editor's request for an audience link and decodes
18// the answer.
19func qrLink(t *testing.T, a *testApp, deckPath string, cookies []*http.Cookie) (int, string) {
20 t.Helper()
21 rec := post(t, a, deckPath+"/qr-link", nil, cookies...)
22 if rec.Code != http.StatusOK {
23 return rec.Code, ""
24 }
25 if ct := rec.Header().Get("Content-Type"); !strings.HasPrefix(ct, "application/json") {
26 t.Errorf("Content-Type %q, want JSON", ct)
27 }
28 if cc := rec.Header().Get("Cache-Control"); cc != "no-store" {
29 t.Errorf("Cache-Control %q: an answer carrying a capability must not be cached", cc)
30 }
31 var body struct {
32 URL string `json:"url"`
33 }
34 if err := json.Unmarshal(rec.Body.Bytes(), &body); err != nil {
35 t.Fatalf("POST %s/qr-link: %v\n%s", deckPath, err, rec.Body)
36 }
37 return rec.Code, body.URL
38}
39
40// TestTheQRLinkRouteMintsAnAudienceLink: the follow-along code's link is
41// a share link made exactly as the share page makes one — only its hash
42// stored, revocable there — named Audience on that page, and answered
43// as its follow address. Like the share page it is the owner's alone: a
44// team member who can edit the deck still cannot mint a capability for
45// it, and nobody else learns the deck exists.
46func TestTheQRLinkRouteMintsAnAudienceLink(t *testing.T) {
47 a := newSharedApp(t)
48 mine := signedIn(t, a, allowedAddress)
49 theirs := signedIn(t, a, otherAddress)
50 path := createDeck(t, a, mine, "Talk")
51
52 code, got := qrLink(t, a, path, mine)
53 if code != http.StatusOK {
54 t.Fatalf("POST %s/qr-link: status %d, want 200", path, code)
55 }
56 m := followURLPattern.FindStringSubmatch(got)
57 if m == nil {
58 t.Fatalf("answered %q, want %s/s/<token>?follow=1", got, testOrigin)
59 }
60 token := m[1]
61
62 // The address works with no session at all: nothing is being
63 // presented, so it lands on the deck to read.
64 rec := get(t, a, "/s/"+token+"?follow=1")
65 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != "/s/"+token {
66 t.Errorf("following with no talk live: status %d to %q, want 303 to the deck", rec.Code, rec.Header().Get("Location"))
67 }
68 if page := mustGet(t, a, "/s/"+token, http.StatusOK, nil).Body.String(); !strings.Contains(page, "<section slides-slide") {
69 t.Errorf("the link does not open the deck:\n%s", page)
70 }
71
72 // The share page lists it, by its name, and never shows the token.
73 share := mustGet(t, a, path+"/share", http.StatusOK, mine).Body.String()
74 if !strings.Contains(share, "Audience") {
75 t.Errorf("the share page does not name the audience link:\n%s", share)
76 }
77 if strings.Contains(share, token) {
78 t.Errorf("the share page shows the token:\n%s", share)
79 }
80 var stored int64
81 if err := a.db.G.Table("share_links").Where("audience = ?", true).Count(&stored).Error; err != nil || stored != 1 {
82 t.Errorf("audience links stored: %d (%v), want 1", stored, err)
83 }
84
85 // A second press is a second link; the first still works until it
86 // is revoked on the share page.
87 _, again := qrLink(t, a, path, mine)
88 if again == got || followURLPattern.FindStringSubmatch(again) == nil {
89 t.Errorf("a second link %q, after %q", again, got)
90 }
91 mustGet(t, a, "/s/"+token, http.StatusOK, nil)
92
93 // Somebody who cannot see the deck, and a team member who can edit
94 // it but not share it, get the share page's 404.
95 if code, _ := qrLink(t, a, path, theirs); code != http.StatusNotFound {
96 t.Errorf("a stranger: status %d, want 404", code)
97 }
98 if rec := post(t, a, path+"/settings", url.Values{"title": {"Talk"}, "theme": {"ink"}, "rung": {"team"}}, mine...); rec.Code != http.StatusSeeOther {
99 t.Fatalf("raising the rung: status %d", rec.Code)
100 }
101 mustGet(t, a, path+"/edit", http.StatusOK, theirs)
102 if code, _ := qrLink(t, a, path, theirs); code != http.StatusNotFound {
103 t.Errorf("a team member: status %d, want 404", code)
104 }
105 // Signed out, and from another site, nothing is made.
106 if rec := post(t, a, path+"/qr-link", nil); rec.Code == http.StatusOK {
107 t.Errorf("signed out: status %d", rec.Code)
108 }
109 req := httptest.NewRequest(http.MethodPost, path+"/qr-link", nil)
110 req.Header.Set("Origin", "https://evil.example")
111 req.Header.Set("Sec-Fetch-Site", "cross-site")
112 for _, c := range mine {
113 req.AddCookie(c)
114 }
115 cross := httptest.NewRecorder()
116 a.ServeHTTP(cross, req)
117 if cross.Code != http.StatusForbidden {
118 t.Errorf("a cross-site POST: status %d, want 403", cross.Code)
119 }
120 if err := a.db.G.Table("share_links").Where("audience = ?", true).Count(&stored).Error; err != nil || stored != 2 {
121 t.Errorf("audience links stored: %d (%v), want the owner's 2", stored, err)
122 }
123}
124
125// codeDeck makes a deck whose first slide holds a follow-along code
126// pointed at url (written over the API, which checks only its shape),
127// and returns the deck's path, its sqid and the slide's id.
128func codeDeck(t *testing.T, a *testApp, cookies []*http.Cookie, title, url string) (path, sqid, slide string) {
129 t.Helper()
130 return codeDeckOn(t, a, cookies, createDeck(t, a, cookies, title), url)
131}
132
133// TestAShareReaderIsShownTheirOwnLinkInTheCode: somebody reading over
134// share link A never learns the audience link B the code holds — on
135// every /s/ response (the page, its phone view, the follow page with a
136// talk live and its fragment, the thumbnail) the code and its address
137// are A's — so revoking A leaves them nothing.
138func TestAShareReaderIsShownTheirOwnLinkInTheCode(t *testing.T) {
139 a := newApp(t)
140 mine := signedIn(t, a, allowedAddress)
141 path := createDeck(t, a, mine, "Talk")
142 _, b := qrLink(t, a, path, mine)
143 bToken := followURLPattern.FindStringSubmatch(b)[1]
144 _, _, slide := codeDeckOn(t, a, mine, path, b)
145 aToken := mintShareLink(t, a, path, mine)
146 aAddress := strings.TrimPrefix(testOrigin, "http://") + "/s/" + aToken
147
148 startPresenting(t, a, path, mine)
149 for _, target := range []string{
150 "/s/" + aToken,
151 "/s/" + aToken + "?vp=phone",
152 "/s/" + aToken + "?follow=1",
153 "/s/" + aToken + "?follow=1&fragment=1",
154 "/s/" + aToken + "?present=1",
155 "/s/" + aToken + "/slides/" + slide + ".svg",
156 } {
157 body := mustGet(t, a, target, http.StatusOK, nil).Body.String()
158 if strings.Contains(body, bToken) {
159 t.Errorf("%s shows the audience link's token", target)
160 }
161 if words := strings.Join(strings.Fields(regexp.MustCompile(`<[^>]*>`).ReplaceAllString(body, "")), ""); !strings.Contains(words, aAddress) {
162 t.Errorf("%s does not draw the reader's own address %s", target, aAddress)
163 }
164 }
165 // Members see the address the code was made with.
166 if page := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String(); !strings.Contains(page, bToken) {
167 t.Error("the owner's viewer does not draw the code's own link")
168 }
169}
170
171// codeDeckOn is codeDeck on a deck that already exists.
172func codeDeckOn(t *testing.T, a *testApp, cookies []*http.Cookie, path, url string) (string, string, string) {
173 t.Helper()
174 sqid := strings.TrimPrefix(path, "/d/")
175 token := mintAgent(t, a, cookies, "code-on-"+sqid)
176 view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK)
177 slide, _ := view["slides"].([]any)[0].(map[string]any)["id"].(string)
178 apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token,
179 `{"ops":[{"op":"add_block","slide":`+quote(slide)+`,"id":"qrcode000001","type":"qr","props":{"url":`+quote(url)+`}}]}`, http.StatusOK)
180 return path, sqid, slide
181}
182
183// TestAFollowAlongCodeDrawsOnlyThisDecksLiveLink: the document takes
184// any follow-shaped address, and the page is the authority on which
185// ones are this talk's. A code is drawn only for a link of this
186// instance's origin that is a live share link of this very deck;
187// anything else is no code for a reader and an empty frame in the
188// editor — and revoking the link turns a drawn code off.
189func TestAFollowAlongCodeDrawsOnlyThisDecksLiveLink(t *testing.T) {
190 a := newApp(t)
191 mine := signedIn(t, a, allowedAddress)
192 other := createDeck(t, a, mine, "Other")
193 _, theirs := qrLink(t, a, other, mine)
194 _, live := qrLink(t, a, other, mine)
195 liveToken := followURLPattern.FindStringSubmatch(live)[1]
196
197 for name, url := range map[string]string{
198 "another origin": "https://elsewhere.example/s/" + liveToken + "?follow=1",
199 "another deck's": theirs,
200 "a made-up token": testOrigin + "/s/" + strings.Repeat("a", 43) + "?follow=1",
201 } {
202 path, _, slide := codeDeck(t, a, mine, name, url)
203 viewer := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String()
204 if strings.Contains(viewer, `data-block="qr"`) {
205 t.Errorf("%s: the viewer draws a code", name)
206 }
207 editor := mustGet(t, a, path+"/slides/"+slide+".html", http.StatusOK, mine).Body.String()
208 if !strings.Contains(editor, `data-missing-qr="true"`) || strings.Contains(editor, "<svg") {
209 t.Errorf("%s: the editor does not show an empty frame:\n%s", name, editor)
210 }
211 thumb := mustGet(t, a, path+"/slides/"+slide+".svg", http.StatusOK, mine).Body.String()
212 if strings.Contains(thumb, "/s/") {
213 t.Errorf("%s: the thumbnail prints an address", name)
214 }
215 }
216
217 // This deck's own live link draws; revoked, it does not, and the
218 // thumbnail's tag moves so no cache keeps the old picture.
219 path := createDeck(t, a, mine, "Mine")
220 _, own := qrLink(t, a, path, mine)
221 _, _, slide := codeDeckOn(t, a, mine, path, own)
222 if viewer := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String(); !strings.Contains(viewer, `data-block="qr"`) {
223 t.Fatal("the deck's own live link is not drawn")
224 }
225 before := mustGet(t, a, path+"/slides/"+slide+".svg", http.StatusOK, mine)
226 beforeHTML := mustGet(t, a, path+"/slides/"+slide+".html", http.StatusOK, mine)
227 share := mustGet(t, a, path+"/share", http.StatusOK, mine).Body.String()
228 for _, m := range revokeAction.FindAllStringSubmatch(share, -1) {
229 post(t, a, path+"/share/"+m[1]+"/revoke", nil, mine...)
230 }
231 if viewer := mustGet(t, a, path+"?slide=1", http.StatusOK, mine).Body.String(); strings.Contains(viewer, `data-block="qr"`) {
232 t.Error("a revoked link is still drawn")
233 }
234 after := mustGet(t, a, path+"/slides/"+slide+".svg", http.StatusOK, mine)
235 if before.Header().Get("ETag") == after.Header().Get("ETag") {
236 t.Error("revoking the link leaves the thumbnail's tag as it was")
237 }
238 afterHTML := mustGet(t, a, path+"/slides/"+slide+".html", http.StatusOK, mine)
239 if beforeHTML.Header().Get("ETag") == afterHTML.Header().Get("ETag") {
240 t.Error("revoking the link leaves the stage HTML's tag as it was")
241 }
242}
243
244// TestOnlyASharerIsOfferedTheFollowAlongCode: Insert's Follow-along
245// code and the card's Make an audience link are drawn for somebody who
246// may share the deck and for nobody else; a team member still sees a
247// code somebody else added, and its address, read-only.
248func TestOnlyASharerIsOfferedTheFollowAlongCode(t *testing.T) {
249 a := newSharedApp(t)
250 mine := signedIn(t, a, allowedAddress)
251 theirs := signedIn(t, a, otherAddress)
252 path := createDeck(t, a, mine, "Talk")
253 if rec := post(t, a, path+"/settings", url.Values{"title": {"Talk"}, "theme": {"ink"}, "rung": {"team"}}, mine...); rec.Code != http.StatusSeeOther {
254 t.Fatalf("raising the rung: status %d", rec.Code)
255 }
256 owner := mustGet(t, a, path+"/edit", http.StatusOK, mine).Body.String()
257 member := mustGet(t, a, path+"/edit", http.StatusOK, theirs).Body.String()
258 for _, s := range []string{`data-insert-block="qr"`, `data-command="qr-link"`, `data-qr-link=`} {
259 if !strings.Contains(owner, s) {
260 t.Errorf("the owner's editor has no %s", s)
261 }
262 if strings.Contains(member, s) {
263 t.Errorf("a member's editor offers %s", s)
264 }
265 }
266 for _, s := range []string{`name="rst-block-qr-url"`, `data-type-qr="Follow-along code"`} {
267 if !strings.Contains(member, s) {
268 t.Errorf("a member's editor has no %s for a code somebody else added", s)
269 }
270 }
271}
272