| 1 | package slidestest |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "net/http" |
| 6 | "net/http/httptest" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | ) |
| 10 | |
| 11 | type talkSlideJSON struct { |
| 12 | ID string `json:"id"` |
| 13 | Number int `json:"number"` |
| 14 | Title string `json:"title"` |
| 15 | Full bool `json:"full"` |
| 16 | Presses int `json:"presses"` |
| 17 | HTML string `json:"html"` |
| 18 | Assets []string `json:"assets"` |
| 19 | Thumb string `json:"thumb"` |
| 20 | Notes []string `json:"notes"` |
| 21 | } |
| 22 | |
| 23 | type talkJSON struct { |
| 24 | DeckSeq int64 `json:"deckSeq"` |
| 25 | Arrive string `json:"arrive"` |
| 26 | Slides []talkSlideJSON `json:"slides"` |
| 27 | } |
| 28 | |
| 29 | // talkDeck is stepsDeck with notes on its first slide: one before the |
| 30 | // press line, one after. |
| 31 | func talkDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string { |
| 32 | t.Helper() |
| 33 | path := stepsDeck(t, a, cookies) |
| 34 | token := mintAgent(t, a, cookies, "talk-notes") |
| 35 | apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(path, "/d/")+"/ops", token, |
| 36 | `{"ops":[{"op":"set_notes","id":"steps","markdown":"Open warmly\n\n(Press.)\n\nThen the point"}]}`, http.StatusOK) |
| 37 | return path |
| 38 | } |
| 39 | |
| 40 | func talkRequest(a *testApp, session, query, ifNoneMatch string, cookies []*http.Cookie) *httptest.ResponseRecorder { |
| 41 | req := httptest.NewRequest(http.MethodGet, "/p/"+session+"/talk"+query, nil) |
| 42 | if ifNoneMatch != "" { |
| 43 | req.Header.Set("If-None-Match", ifNoneMatch) |
| 44 | } |
| 45 | for _, c := range cookies { |
| 46 | req.AddCookie(c) |
| 47 | } |
| 48 | rec := httptest.NewRecorder() |
| 49 | a.ServeHTTP(rec, req) |
| 50 | return rec |
| 51 | } |
| 52 | |
| 53 | func decodeTalk(t *testing.T, rec *httptest.ResponseRecorder) talkJSON { |
| 54 | t.Helper() |
| 55 | if rec.Code != http.StatusOK { |
| 56 | t.Fatalf("talk: status %d, want 200\n%s", rec.Code, rec.Body) |
| 57 | } |
| 58 | var out talkJSON |
| 59 | if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { |
| 60 | t.Fatalf("talk: %v\n%s", err, rec.Body) |
| 61 | } |
| 62 | return out |
| 63 | } |
| 64 | |
| 65 | func TestTalkPayloadWithholdsNotesAndParked(t *testing.T) { |
| 66 | a := newApp(t) |
| 67 | cookies := signedIn(t, a, allowedAddress) |
| 68 | session, token := startPresenting(t, a, talkDeck(t, a, cookies), cookies) |
| 69 | |
| 70 | for name, tc := range map[string]struct { |
| 71 | query string |
| 72 | cookies []*http.Cookie |
| 73 | notes bool |
| 74 | }{ |
| 75 | "token": {"?t=" + token, nil, true}, |
| 76 | "presenter": {"", cookies, true}, |
| 77 | "slide": {"", nil, false}, |
| 78 | "bad token": {"?t=nope", nil, false}, |
| 79 | } { |
| 80 | rec := talkRequest(a, session, tc.query, "", tc.cookies) |
| 81 | got := decodeTalk(t, rec) |
| 82 | if cc := rec.Header().Get("Cache-Control"); cc != "no-store" { |
| 83 | t.Errorf("%s: Cache-Control %q, want no-store", name, cc) |
| 84 | } |
| 85 | var ids []string |
| 86 | for _, s := range got.Slides { |
| 87 | ids = append(ids, s.ID) |
| 88 | } |
| 89 | if want := "steps,plain,extra,last"; strings.Join(ids, ",") != want { |
| 90 | t.Errorf("%s: slides %v, want %s (parked left out, full-only in)", name, ids, want) |
| 91 | } |
| 92 | first, extra := got.Slides[0], got.Slides[2] |
| 93 | if first.Number != 1 || extra.Number != 3 || !extra.Full || first.Full { |
| 94 | t.Errorf("%s: numbers/full wrong: %+v / %+v", name, first, extra) |
| 95 | } |
| 96 | if first.Presses != 4 || got.Slides[1].Presses != 0 { |
| 97 | t.Errorf("%s: presses %d and %d, want 4 and 0", name, first.Presses, got.Slides[1].Presses) |
| 98 | } |
| 99 | if !strings.Contains(first.HTML, "data-press") || !strings.Contains(first.HTML, "data-waiting") { |
| 100 | t.Errorf("%s: html not rendered at press 0: %s", name, first.HTML) |
| 101 | } |
| 102 | if first.Thumb != "/p/"+session+"/slides/steps.svg" { |
| 103 | t.Errorf("%s: thumb %q", name, first.Thumb) |
| 104 | } |
| 105 | if got.Arrive != "appear" || got.DeckSeq == 0 { |
| 106 | t.Errorf("%s: arrive %q, deckSeq %d", name, got.Arrive, got.DeckSeq) |
| 107 | } |
| 108 | if tc.notes { |
| 109 | if len(first.Notes) != 2 || !strings.Contains(first.Notes[0], "Open warmly") || |
| 110 | !strings.Contains(first.Notes[1], "Then the point") || strings.Contains(strings.Join(first.Notes, ""), "Press.") { |
| 111 | t.Errorf("%s: notes %q, want two parts without the press line", name, first.Notes) |
| 112 | } |
| 113 | } else { |
| 114 | for _, s := range got.Slides { |
| 115 | if len(s.Notes) != 0 { |
| 116 | t.Errorf("%s: %s carries notes %q", name, s.ID, s.Notes) |
| 117 | } |
| 118 | } |
| 119 | if strings.Contains(rec.Body.String(), "Open warmly") { |
| 120 | t.Errorf("%s: the body holds the notes", name) |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | func TestTalkPayloadAnswersUnchanged(t *testing.T) { |
| 127 | a := newApp(t) |
| 128 | cookies := signedIn(t, a, allowedAddress) |
| 129 | session, token := startPresenting(t, a, talkDeck(t, a, cookies), cookies) |
| 130 | |
| 131 | rec := talkRequest(a, session, "?t="+token, "", nil) |
| 132 | etag := rec.Header().Get("ETag") |
| 133 | if etag == "" { |
| 134 | t.Fatal("no ETag") |
| 135 | } |
| 136 | again := talkRequest(a, session, "?t="+token, etag, nil) |
| 137 | if again.Code != http.StatusNotModified || again.Body.Len() != 0 { |
| 138 | t.Errorf("matching tag: status %d, body %d bytes, want 304 and none", again.Code, again.Body.Len()) |
| 139 | } |
| 140 | if cc := again.Header().Get("Cache-Control"); cc != "no-store" { |
| 141 | t.Errorf("304 Cache-Control %q, want no-store", cc) |
| 142 | } |
| 143 | // The notes-free body is another representation: the tag does not carry over. |
| 144 | if rec := talkRequest(a, session, "", etag, nil); rec.Code != http.StatusOK { |
| 145 | t.Errorf("the slide window with the presenter's tag: status %d, want 200", rec.Code) |
| 146 | } |
| 147 | if rec := talkRequest(a, session, "?t="+token, `"0"`, nil); rec.Code != http.StatusOK { |
| 148 | t.Errorf("a stale tag: status %d, want 200", rec.Code) |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | func TestTalkPayloadUnknownAndEnded(t *testing.T) { |
| 153 | a := newApp(t) |
| 154 | cookies := signedIn(t, a, allowedAddress) |
| 155 | session, _ := startPresenting(t, a, talkDeck(t, a, cookies), cookies) |
| 156 | if rec := talkRequest(a, session+"zzzz", "", "", nil); rec.Code != http.StatusNotFound { |
| 157 | t.Errorf("unknown session: status %d, want 404", rec.Code) |
| 158 | } |
| 159 | if res := a.db.G.Exec("UPDATE presentations SET ended_at = CURRENT_TIMESTAMP WHERE sqid = ?", session); res.Error != nil { |
| 160 | t.Fatal(res.Error) |
| 161 | } |
| 162 | if rec := talkRequest(a, session, "", "", nil); rec.Code != http.StatusGone { |
| 163 | t.Errorf("ended session: status %d, want 410", rec.Code) |
| 164 | } |
| 165 | } |
| 166 | |