| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "net/http/httptest" |
| 6 | "net/url" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | // talkSlide is one slide of a talk-marked deck: its id, how it is |
| 12 | // marked ("" is core), and the words that identify it wherever they |
| 13 | // leak. |
| 14 | type talkSlide struct { |
| 15 | id, talk, marker string |
| 16 | } |
| 17 | |
| 18 | // markedDeck makes a deck over the API whose slides carry a marker word |
| 19 | // in their heading and their notes. It returns the deck's path, its sqid |
| 20 | // and the agent token. |
| 21 | func markedDeck(t *testing.T, a *testApp, cookies []*http.Cookie, slides []talkSlide) (path, sqid, token string) { |
| 22 | t.Helper() |
| 23 | token = mintAgent(t, a, cookies, "marked-deck") |
| 24 | sqid = apiDeck(t, a, token, "Marked") |
| 25 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 26 | var ops []string |
| 27 | list, _ := view["slides"].([]any) |
| 28 | for _, s := range list { |
| 29 | id, _ := s.(map[string]any)["id"].(string) |
| 30 | ops = append(ops, `{"op":"remove_slide","id":`+quote(id)+`}`) |
| 31 | } |
| 32 | for _, s := range slides { |
| 33 | ops = append(ops, |
| 34 | `{"op":"add_slide","id":`+quote(s.id)+`,"layout":"flow","template":"title-body"}`, |
| 35 | `{"op":"add_block","slide":`+quote(s.id)+`,"id":`+quote("h"+s.id)+`,"type":"heading","region":"heading","markdown":`+quote(s.marker)+`,"level":1}`, |
| 36 | `{"op":"set_notes","id":`+quote(s.id)+`,"markdown":`+quote("notes "+s.marker)+`}`) |
| 37 | if s.talk != "" { |
| 38 | ops = append(ops, `{"op":"set_slide","id":`+quote(s.id)+`,"talk":`+quote(s.talk)+`}`) |
| 39 | } |
| 40 | } |
| 41 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) |
| 42 | return "/d/" + sqid, sqid, token |
| 43 | } |
| 44 | |
| 45 | var mixedTalk = []talkSlide{ |
| 46 | {"pone", "parked", "PARKEDONE"}, |
| 47 | {"stwo", "", "SHOWNTWO"}, |
| 48 | {"fthree", "full", "FULLTHREE"}, |
| 49 | {"pfour", "parked", "PARKEDFOUR"}, |
| 50 | {"sfive", "", "SHOWNFIVE"}, |
| 51 | } |
| 52 | |
| 53 | // pageCount is the pages in a PDF, either spelling of the entry. |
| 54 | func pageCount(pdf string) int { |
| 55 | n := func(entry string) int { |
| 56 | return strings.Count(pdf, entry) + strings.Count(pdf, strings.Replace(entry, " /", "/", 1)) |
| 57 | } |
| 58 | return n("/Type /Page") - n("/Type /Pages") |
| 59 | } |
| 60 | |
| 61 | // exportPDF runs the export flow and returns the PDF. |
| 62 | func exportPDF(t *testing.T, a *testApp, deck string, cookies []*http.Cookie) string { |
| 63 | t.Helper() |
| 64 | status := startExport(t, a, deck, cookies, url.Values{}) |
| 65 | fragment := waitForExport(t, a, status, cookies) |
| 66 | if strings.Contains(fragment, "rst-tone=\"negative\"") { |
| 67 | t.Fatalf("the export failed:\n%s", fragment) |
| 68 | } |
| 69 | return mustGet(t, a, status+".pdf", http.StatusOK, cookies).Body.String() |
| 70 | } |
| 71 | |
| 72 | // TestAParkedSlideReachesNoReader: every place a reader looks leaves |
| 73 | // the parked slides out, and the numbers they see count only what is |
| 74 | // there. |
| 75 | func TestAParkedSlideReachesNoReader(t *testing.T) { |
| 76 | a := newApp(t) |
| 77 | cookies := signedIn(t, a, allowedAddress) |
| 78 | path, _, _ := markedDeck(t, a, cookies, mixedTalk) |
| 79 | share := "/s/" + mintShareLink(t, a, path, cookies) |
| 80 | session, _ := startPresenting(t, a, path, cookies) |
| 81 | |
| 82 | cases := []struct { |
| 83 | name string |
| 84 | target string |
| 85 | want int |
| 86 | cookies []*http.Cookie |
| 87 | shows string // present when the page is 200 |
| 88 | }{ |
| 89 | {"viewer first slide", path + "?slide=1", 200, cookies, "SHOWNTWO"}, |
| 90 | {"viewer second slide", path + "?slide=2", 200, cookies, "FULLTHREE"}, |
| 91 | {"viewer past the end", path + "?slide=4", 404, cookies, ""}, |
| 92 | {"share link", share, 200, nil, "SHOWNTWO"}, |
| 93 | {"share link past the end", share + "?slide=4", 404, nil, ""}, |
| 94 | {"notes page", path + "/notes", 200, cookies, "notes SHOWNFIVE"}, |
| 95 | {"projector", "/p/" + session, 200, cookies, "SHOWNTWO"}, |
| 96 | {"projector past the end", "/p/" + session + "?slide=4", 404, cookies, ""}, |
| 97 | {"presenter notes", "/p/" + session + "/notes", 200, cookies, "SHOWNTWO"}, |
| 98 | {"follow link", share + "?follow=1", 200, nil, "SHOWNTWO"}, |
| 99 | {"follow past the end", share + "?follow=1&slide=4", 404, nil, ""}, |
| 100 | {"share thumbnail of a parked slide", share + "/slides/pone.svg", 404, nil, ""}, |
| 101 | {"share thumbnail of a shown slide", share + "/slides/stwo.svg", 200, nil, ""}, |
| 102 | {"session thumbnail of a parked slide", "/p/" + session + "/slides/pfour.svg", 404, nil, ""}, |
| 103 | {"session thumbnail of a shown slide", "/p/" + session + "/slides/sfive.svg", 200, nil, ""}, |
| 104 | } |
| 105 | for _, c := range cases { |
| 106 | t.Run(c.name, func(t *testing.T) { |
| 107 | rec := get(t, a, c.target, c.cookies...) |
| 108 | if rec.Code != c.want { |
| 109 | t.Fatalf("GET %s: status %d, want %d\n%s", c.target, rec.Code, c.want, rec.Body) |
| 110 | } |
| 111 | body := rec.Body.String() |
| 112 | if rec.Code == 200 && c.shows != "" && !strings.Contains(body, c.shows) { |
| 113 | t.Errorf("GET %s does not show %q:\n%s", c.target, c.shows, body) |
| 114 | } |
| 115 | if strings.Contains(body, "PARKED") || strings.Contains(body, "/pone.svg") || strings.Contains(body, "/pfour.svg") { |
| 116 | t.Errorf("GET %s carries a parked slide:\n%s", c.target, body) |
| 117 | } |
| 118 | }) |
| 119 | } |
| 120 | |
| 121 | t.Run("the counter counts what is there", func(t *testing.T) { |
| 122 | page := mustGet(t, a, share+"?slide=3", http.StatusOK, nil).Body.String() |
| 123 | if !strings.Contains(page, "SHOWNFIVE") || !strings.Contains(page, "3 of 3") { |
| 124 | t.Errorf("the third slide over a share link is not SHOWNFIVE of 3:\n%s", page) |
| 125 | } |
| 126 | }) |
| 127 | |
| 128 | t.Run("pdf", func(t *testing.T) { |
| 129 | if got := pageCount(exportPDF(t, a, path, cookies)); got != 3 { |
| 130 | t.Errorf("the PDF has %d pages, want 3 (the parked slides are left out)", got) |
| 131 | } |
| 132 | }) |
| 133 | } |
| 134 | |
| 135 | // TestAConditionalThumbnailOfAParkedSlideIsStill404: a wildcard or a |
| 136 | // matching tag must not turn "no such slide" into "not modified". |
| 137 | func TestAConditionalThumbnailOfAParkedSlideIsStill404(t *testing.T) { |
| 138 | a := newApp(t) |
| 139 | cookies := signedIn(t, a, allowedAddress) |
| 140 | path, _, _ := markedDeck(t, a, cookies, mixedTalk) |
| 141 | share := "/s/" + mintShareLink(t, a, path, cookies) |
| 142 | session, _ := startPresenting(t, a, path, cookies) |
| 143 | seq := "" |
| 144 | if row := a.db.G.Raw("SELECT seq FROM decks WHERE sqid = ?", strings.TrimPrefix(path, "/d/")).Row(); row != nil { |
| 145 | row.Scan(&seq) |
| 146 | } |
| 147 | for _, base := range []string{share, "/p/" + session} { |
| 148 | for _, tag := range []string{"*", `"` + seq + `-pone-` + `"`, `"` + seq + `-pone-480"`, `"` + seq + `-pone-1920"`} { |
| 149 | req := httptest.NewRequest(http.MethodGet, base+"/slides/pone.svg", nil) |
| 150 | req.Header.Set("If-None-Match", tag) |
| 151 | rec := httptest.NewRecorder() |
| 152 | a.ServeHTTP(rec, req) |
| 153 | if rec.Code != http.StatusNotFound { |
| 154 | t.Errorf("GET %s parked, If-None-Match %s: status %d, want 404", base, tag, rec.Code) |
| 155 | } |
| 156 | } |
| 157 | req := httptest.NewRequest(http.MethodGet, base+"/slides/stwo.svg", nil) |
| 158 | req.Header.Set("If-None-Match", "*") |
| 159 | rec := httptest.NewRecorder() |
| 160 | a.ServeHTTP(rec, req) |
| 161 | if rec.Code != http.StatusNotModified { |
| 162 | t.Errorf("GET %s shown slide, If-None-Match *: status %d, want 304", base, rec.Code) |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // TestAFullOnlySlideShowsToReaders: full only is a way of giving the |
| 168 | // talk, not a way of hiding a slide: readers get it. |
| 169 | func TestAFullOnlySlideShowsToReaders(t *testing.T) { |
| 170 | a := newApp(t) |
| 171 | cookies := signedIn(t, a, allowedAddress) |
| 172 | path, _, _ := markedDeck(t, a, cookies, mixedTalk) |
| 173 | share := "/s/" + mintShareLink(t, a, path, cookies) |
| 174 | if page := mustGet(t, a, share+"?slide=2", http.StatusOK, nil).Body.String(); !strings.Contains(page, "FULLTHREE") { |
| 175 | t.Errorf("a share link does not show the full-only slide:\n%s", page) |
| 176 | } |
| 177 | if rec := get(t, a, share+"/slides/fthree.svg"); rec.Code != http.StatusOK { |
| 178 | t.Errorf("the full-only slide's thumbnail over a share link: status %d, want 200", rec.Code) |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // TestTheEditorAndAPIStillDescribeAParkedSlide: people with access to |
| 183 | // the deck still see everything in it. |
| 184 | func TestTheEditorAndAPIStillDescribeAParkedSlide(t *testing.T) { |
| 185 | a := newApp(t) |
| 186 | cookies := signedIn(t, a, allowedAddress) |
| 187 | path, sqid, token := markedDeck(t, a, cookies, mixedTalk) |
| 188 | |
| 189 | if rec := get(t, a, path+"/slides/pone.svg", cookies...); rec.Code != http.StatusOK { |
| 190 | t.Errorf("the editor's thumbnail of a parked slide: status %d, want 200", rec.Code) |
| 191 | } |
| 192 | editor := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String() |
| 193 | if !strings.Contains(editor, "pone") { |
| 194 | t.Errorf("the editor does not describe the parked slide:\n%s", editor) |
| 195 | } |
| 196 | view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) |
| 197 | if got := mustJSON(t, view["slides"]); !strings.Contains(got, "pone") || !strings.Contains(got, "pfour") { |
| 198 | t.Errorf("the API deck leaves a parked slide out:\n%s", got) |
| 199 | } |
| 200 | if rec := send(t, a, bearerRequest(t, http.MethodGet, "/api/v1/decks/"+sqid+"/slides/pone.svg", token, "")); rec.Code != http.StatusOK { |
| 201 | t.Errorf("the API's thumbnail of a parked slide: status %d, want 200\n%s", rec.Code, rec.Body) |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | // TestEveryParkedDeckIsEmptyToReaders: a deck with nothing but parked |
| 206 | // slides is, for a reader, the deck with nothing in it. |
| 207 | func TestEveryParkedDeckIsEmptyToReaders(t *testing.T) { |
| 208 | a := newApp(t) |
| 209 | cookies := signedIn(t, a, allowedAddress) |
| 210 | path, _, _ := markedDeck(t, a, cookies, []talkSlide{ |
| 211 | {"pone", "parked", "PARKEDONE"}, |
| 212 | {"ptwo", "parked", "PARKEDTWO"}, |
| 213 | }) |
| 214 | share := "/s/" + mintShareLink(t, a, path, cookies) |
| 215 | session, remote := startPresenting(t, a, path, cookies) |
| 216 | |
| 217 | for _, target := range []string{share, path + "?slide=1", path + "/notes", |
| 218 | "/p/" + session, "/p/" + session + "/notes", "/p/" + session + "/remote?t=" + remote} { |
| 219 | rec := get(t, a, target, cookies...) |
| 220 | if target == share { |
| 221 | rec = get(t, a, target) |
| 222 | } |
| 223 | if strings.Contains(target, "/remote") { |
| 224 | rec = get(t, a, target) |
| 225 | } |
| 226 | body := rec.Body.String() |
| 227 | if rec.Code != http.StatusOK || strings.Contains(body, "PARKED") || strings.Contains(body, "<section slides-slide") { |
| 228 | t.Errorf("GET %s: status %d, want an empty page with no slide:\n%s", target, rec.Code, body) |
| 229 | } |
| 230 | } |
| 231 | if rec := get(t, a, share+"?follow=1"); rec.Code != http.StatusOK && rec.Code != http.StatusSeeOther || strings.Contains(rec.Body.String(), "PARKED") { |
| 232 | t.Errorf("the follow view of an all-parked deck: status %d, want the empty page or a redirect", rec.Code) |
| 233 | } |
| 234 | // Nothing is left to export, as for an empty deck: the job says so. |
| 235 | status := startExport(t, a, path, cookies, url.Values{}) |
| 236 | if fragment := waitForExport(t, a, status, cookies); !strings.Contains(fragment, "rst-tone=\"negative\"") { |
| 237 | t.Errorf("exporting an all-parked deck did not fail:\n%s", fragment) |
| 238 | } |
| 239 | // An empty deck answers any slide with its empty page, and so does this. |
| 240 | if rec := get(t, a, share+"?slide=1"); rec.Code != http.StatusOK || strings.Contains(rec.Body.String(), "<section slides-slide") { |
| 241 | t.Errorf("slide 1 of an all-parked deck over a share link: status %d, want the empty page", rec.Code) |
| 242 | } |
| 243 | } |
| 244 | |
| 245 | // TestACoverSkipsAParkedFirstSlide: the deck list's cover is the first |
| 246 | // slide that is not parked. |
| 247 | func TestACoverSkipsAParkedFirstSlide(t *testing.T) { |
| 248 | a := newApp(t) |
| 249 | cookies := signedIn(t, a, allowedAddress) |
| 250 | _, sqid, _ := markedDeck(t, a, cookies, mixedTalk) |
| 251 | list := mustGet(t, a, "/", http.StatusOK, cookies).Body.String() |
| 252 | if !strings.Contains(list, "/d/"+sqid+"/slides/stwo.svg") { |
| 253 | t.Errorf("the list's cover for the deck is not its first unparked slide:\n%s", list) |
| 254 | } |
| 255 | if strings.Contains(list, "/d/"+sqid+"/slides/pone.svg") { |
| 256 | t.Errorf("the list's cover is a parked slide:\n%s", list) |
| 257 | } |
| 258 | } |
| 259 | |