package slidestest import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) // talkSlide is one slide of a talk-marked deck: its id, how it is // marked ("" is core), and the words that identify it wherever they // leak. type talkSlide struct { id, talk, marker string } // markedDeck makes a deck over the API whose slides carry a marker word // in their heading and their notes. It returns the deck's path, its sqid // and the agent token. func markedDeck(t *testing.T, a *testApp, cookies []*http.Cookie, slides []talkSlide) (path, sqid, token string) { t.Helper() token = mintAgent(t, a, cookies, "marked-deck") sqid = apiDeck(t, a, token, "Marked") view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) var ops []string list, _ := view["slides"].([]any) for _, s := range list { id, _ := s.(map[string]any)["id"].(string) ops = append(ops, `{"op":"remove_slide","id":`+quote(id)+`}`) } for _, s := range slides { ops = append(ops, `{"op":"add_slide","id":`+quote(s.id)+`,"layout":"flow","template":"title-body"}`, `{"op":"add_block","slide":`+quote(s.id)+`,"id":`+quote("h"+s.id)+`,"type":"heading","region":"heading","markdown":`+quote(s.marker)+`,"level":1}`, `{"op":"set_notes","id":`+quote(s.id)+`,"markdown":`+quote("notes "+s.marker)+`}`) if s.talk != "" { ops = append(ops, `{"op":"set_slide","id":`+quote(s.id)+`,"talk":`+quote(s.talk)+`}`) } } apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+sqid+"/ops", token, `{"ops":[`+strings.Join(ops, ",")+`]}`, http.StatusOK) return "/d/" + sqid, sqid, token } var mixedTalk = []talkSlide{ {"pone", "parked", "PARKEDONE"}, {"stwo", "", "SHOWNTWO"}, {"fthree", "full", "FULLTHREE"}, {"pfour", "parked", "PARKEDFOUR"}, {"sfive", "", "SHOWNFIVE"}, } // pageCount is the pages in a PDF, either spelling of the entry. func pageCount(pdf string) int { n := func(entry string) int { return strings.Count(pdf, entry) + strings.Count(pdf, strings.Replace(entry, " /", "/", 1)) } return n("/Type /Page") - n("/Type /Pages") } // exportPDF runs the export flow and returns the PDF. func exportPDF(t *testing.T, a *testApp, deck string, cookies []*http.Cookie) string { t.Helper() status := startExport(t, a, deck, cookies, url.Values{}) fragment := waitForExport(t, a, status, cookies) if strings.Contains(fragment, "rst-tone=\"negative\"") { t.Fatalf("the export failed:\n%s", fragment) } return mustGet(t, a, status+".pdf", http.StatusOK, cookies).Body.String() } // TestAParkedSlideReachesNoReader: every place a reader looks leaves // the parked slides out, and the numbers they see count only what is // there. func TestAParkedSlideReachesNoReader(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path, _, _ := markedDeck(t, a, cookies, mixedTalk) share := "/s/" + mintShareLink(t, a, path, cookies) session, _ := startPresenting(t, a, path, cookies) cases := []struct { name string target string want int cookies []*http.Cookie shows string // present when the page is 200 }{ {"viewer first slide", path + "?slide=1", 200, cookies, "SHOWNTWO"}, {"viewer second slide", path + "?slide=2", 200, cookies, "FULLTHREE"}, {"viewer past the end", path + "?slide=4", 404, cookies, ""}, {"share link", share, 200, nil, "SHOWNTWO"}, {"share link past the end", share + "?slide=4", 404, nil, ""}, {"notes page", path + "/notes", 200, cookies, "notes SHOWNFIVE"}, {"projector", "/p/" + session, 200, cookies, "SHOWNTWO"}, {"projector past the end", "/p/" + session + "?slide=4", 404, cookies, ""}, {"presenter notes", "/p/" + session + "/notes", 200, cookies, "SHOWNTWO"}, {"follow link", share + "?follow=1", 200, nil, "SHOWNTWO"}, {"follow past the end", share + "?follow=1&slide=4", 404, nil, ""}, {"share thumbnail of a parked slide", share + "/slides/pone.svg", 404, nil, ""}, {"share thumbnail of a shown slide", share + "/slides/stwo.svg", 200, nil, ""}, {"session thumbnail of a parked slide", "/p/" + session + "/slides/pfour.svg", 404, nil, ""}, {"session thumbnail of a shown slide", "/p/" + session + "/slides/sfive.svg", 200, nil, ""}, } for _, c := range cases { t.Run(c.name, func(t *testing.T) { rec := get(t, a, c.target, c.cookies...) if rec.Code != c.want { t.Fatalf("GET %s: status %d, want %d\n%s", c.target, rec.Code, c.want, rec.Body) } body := rec.Body.String() if rec.Code == 200 && c.shows != "" && !strings.Contains(body, c.shows) { t.Errorf("GET %s does not show %q:\n%s", c.target, c.shows, body) } if strings.Contains(body, "PARKED") || strings.Contains(body, "/pone.svg") || strings.Contains(body, "/pfour.svg") { t.Errorf("GET %s carries a parked slide:\n%s", c.target, body) } }) } t.Run("the counter counts what is there", func(t *testing.T) { page := mustGet(t, a, share+"?slide=3", http.StatusOK, nil).Body.String() if !strings.Contains(page, "SHOWNFIVE") || !strings.Contains(page, "3 of 3") { t.Errorf("the third slide over a share link is not SHOWNFIVE of 3:\n%s", page) } }) t.Run("pdf", func(t *testing.T) { if got := pageCount(exportPDF(t, a, path, cookies)); got != 3 { t.Errorf("the PDF has %d pages, want 3 (the parked slides are left out)", got) } }) } // TestAConditionalThumbnailOfAParkedSlideIsStill404: a wildcard or a // matching tag must not turn "no such slide" into "not modified". func TestAConditionalThumbnailOfAParkedSlideIsStill404(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path, _, _ := markedDeck(t, a, cookies, mixedTalk) share := "/s/" + mintShareLink(t, a, path, cookies) session, _ := startPresenting(t, a, path, cookies) seq := "" if row := a.db.G.Raw("SELECT seq FROM decks WHERE sqid = ?", strings.TrimPrefix(path, "/d/")).Row(); row != nil { row.Scan(&seq) } for _, base := range []string{share, "/p/" + session} { for _, tag := range []string{"*", `"` + seq + `-pone-` + `"`, `"` + seq + `-pone-480"`, `"` + seq + `-pone-1920"`} { req := httptest.NewRequest(http.MethodGet, base+"/slides/pone.svg", nil) req.Header.Set("If-None-Match", tag) rec := httptest.NewRecorder() a.ServeHTTP(rec, req) if rec.Code != http.StatusNotFound { t.Errorf("GET %s parked, If-None-Match %s: status %d, want 404", base, tag, rec.Code) } } req := httptest.NewRequest(http.MethodGet, base+"/slides/stwo.svg", nil) req.Header.Set("If-None-Match", "*") rec := httptest.NewRecorder() a.ServeHTTP(rec, req) if rec.Code != http.StatusNotModified { t.Errorf("GET %s shown slide, If-None-Match *: status %d, want 304", base, rec.Code) } } } // TestAFullOnlySlideShowsToReaders: full only is a way of giving the // talk, not a way of hiding a slide: readers get it. func TestAFullOnlySlideShowsToReaders(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path, _, _ := markedDeck(t, a, cookies, mixedTalk) share := "/s/" + mintShareLink(t, a, path, cookies) if page := mustGet(t, a, share+"?slide=2", http.StatusOK, nil).Body.String(); !strings.Contains(page, "FULLTHREE") { t.Errorf("a share link does not show the full-only slide:\n%s", page) } if rec := get(t, a, share+"/slides/fthree.svg"); rec.Code != http.StatusOK { t.Errorf("the full-only slide's thumbnail over a share link: status %d, want 200", rec.Code) } } // TestTheEditorAndAPIStillDescribeAParkedSlide: people with access to // the deck still see everything in it. func TestTheEditorAndAPIStillDescribeAParkedSlide(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path, sqid, token := markedDeck(t, a, cookies, mixedTalk) if rec := get(t, a, path+"/slides/pone.svg", cookies...); rec.Code != http.StatusOK { t.Errorf("the editor's thumbnail of a parked slide: status %d, want 200", rec.Code) } editor := mustGet(t, a, path+"/edit", http.StatusOK, cookies).Body.String() if !strings.Contains(editor, "pone") { t.Errorf("the editor does not describe the parked slide:\n%s", editor) } view := apiJSON(t, a, http.MethodGet, "/api/v1/decks/"+sqid, token, "", http.StatusOK) if got := mustJSON(t, view["slides"]); !strings.Contains(got, "pone") || !strings.Contains(got, "pfour") { t.Errorf("the API deck leaves a parked slide out:\n%s", got) } if rec := send(t, a, bearerRequest(t, http.MethodGet, "/api/v1/decks/"+sqid+"/slides/pone.svg", token, "")); rec.Code != http.StatusOK { t.Errorf("the API's thumbnail of a parked slide: status %d, want 200\n%s", rec.Code, rec.Body) } } // TestEveryParkedDeckIsEmptyToReaders: a deck with nothing but parked // slides is, for a reader, the deck with nothing in it. func TestEveryParkedDeckIsEmptyToReaders(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) path, _, _ := markedDeck(t, a, cookies, []talkSlide{ {"pone", "parked", "PARKEDONE"}, {"ptwo", "parked", "PARKEDTWO"}, }) share := "/s/" + mintShareLink(t, a, path, cookies) session, remote := startPresenting(t, a, path, cookies) for _, target := range []string{share, path + "?slide=1", path + "/notes", "/p/" + session, "/p/" + session + "/notes", "/p/" + session + "/remote?t=" + remote} { rec := get(t, a, target, cookies...) if target == share { rec = get(t, a, target) } if strings.Contains(target, "/remote") { rec = get(t, a, target) } body := rec.Body.String() if rec.Code != http.StatusOK || strings.Contains(body, "PARKED") || strings.Contains(body, "