package slidestest import ( "encoding/json" "net/http" "net/http/httptest" "strings" "testing" ) type talkSlideJSON struct { ID string `json:"id"` Number int `json:"number"` Title string `json:"title"` Full bool `json:"full"` Presses int `json:"presses"` HTML string `json:"html"` Assets []string `json:"assets"` Thumb string `json:"thumb"` Notes []string `json:"notes"` } type talkJSON struct { DeckSeq int64 `json:"deckSeq"` Arrive string `json:"arrive"` Slides []talkSlideJSON `json:"slides"` } // talkDeck is stepsDeck with notes on its first slide: one before the // press line, one after. func talkDeck(t *testing.T, a *testApp, cookies []*http.Cookie) string { t.Helper() path := stepsDeck(t, a, cookies) token := mintAgent(t, a, cookies, "talk-notes") apiJSON(t, a, http.MethodPost, "/api/v1/decks/"+strings.TrimPrefix(path, "/d/")+"/ops", token, `{"ops":[{"op":"set_notes","id":"steps","markdown":"Open warmly\n\n(Press.)\n\nThen the point"}]}`, http.StatusOK) return path } func talkRequest(a *testApp, session, query, ifNoneMatch string, cookies []*http.Cookie) *httptest.ResponseRecorder { req := httptest.NewRequest(http.MethodGet, "/p/"+session+"/talk"+query, nil) if ifNoneMatch != "" { req.Header.Set("If-None-Match", ifNoneMatch) } for _, c := range cookies { req.AddCookie(c) } rec := httptest.NewRecorder() a.ServeHTTP(rec, req) return rec } func decodeTalk(t *testing.T, rec *httptest.ResponseRecorder) talkJSON { t.Helper() if rec.Code != http.StatusOK { t.Fatalf("talk: status %d, want 200\n%s", rec.Code, rec.Body) } var out talkJSON if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil { t.Fatalf("talk: %v\n%s", err, rec.Body) } return out } func TestTalkPayloadWithholdsNotesAndParked(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, token := startPresenting(t, a, talkDeck(t, a, cookies), cookies) for name, tc := range map[string]struct { query string cookies []*http.Cookie notes bool }{ "token": {"?t=" + token, nil, true}, "presenter": {"", cookies, true}, "slide": {"", nil, false}, "bad token": {"?t=nope", nil, false}, } { rec := talkRequest(a, session, tc.query, "", tc.cookies) got := decodeTalk(t, rec) if cc := rec.Header().Get("Cache-Control"); cc != "no-store" { t.Errorf("%s: Cache-Control %q, want no-store", name, cc) } var ids []string for _, s := range got.Slides { ids = append(ids, s.ID) } if want := "steps,plain,extra,last"; strings.Join(ids, ",") != want { t.Errorf("%s: slides %v, want %s (parked left out, full-only in)", name, ids, want) } first, extra := got.Slides[0], got.Slides[2] if first.Number != 1 || extra.Number != 3 || !extra.Full || first.Full { t.Errorf("%s: numbers/full wrong: %+v / %+v", name, first, extra) } if first.Presses != 4 || got.Slides[1].Presses != 0 { t.Errorf("%s: presses %d and %d, want 4 and 0", name, first.Presses, got.Slides[1].Presses) } if !strings.Contains(first.HTML, "data-press") || !strings.Contains(first.HTML, "data-waiting") { t.Errorf("%s: html not rendered at press 0: %s", name, first.HTML) } if first.Thumb != "/p/"+session+"/slides/steps.svg" { t.Errorf("%s: thumb %q", name, first.Thumb) } if got.Arrive != "appear" || got.DeckSeq == 0 { t.Errorf("%s: arrive %q, deckSeq %d", name, got.Arrive, got.DeckSeq) } if tc.notes { if len(first.Notes) != 2 || !strings.Contains(first.Notes[0], "Open warmly") || !strings.Contains(first.Notes[1], "Then the point") || strings.Contains(strings.Join(first.Notes, ""), "Press.") { t.Errorf("%s: notes %q, want two parts without the press line", name, first.Notes) } } else { for _, s := range got.Slides { if len(s.Notes) != 0 { t.Errorf("%s: %s carries notes %q", name, s.ID, s.Notes) } } if strings.Contains(rec.Body.String(), "Open warmly") { t.Errorf("%s: the body holds the notes", name) } } } } func TestTalkPayloadAnswersUnchanged(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, token := startPresenting(t, a, talkDeck(t, a, cookies), cookies) rec := talkRequest(a, session, "?t="+token, "", nil) etag := rec.Header().Get("ETag") if etag == "" { t.Fatal("no ETag") } again := talkRequest(a, session, "?t="+token, etag, nil) if again.Code != http.StatusNotModified || again.Body.Len() != 0 { t.Errorf("matching tag: status %d, body %d bytes, want 304 and none", again.Code, again.Body.Len()) } if cc := again.Header().Get("Cache-Control"); cc != "no-store" { t.Errorf("304 Cache-Control %q, want no-store", cc) } // The notes-free body is another representation: the tag does not carry over. if rec := talkRequest(a, session, "", etag, nil); rec.Code != http.StatusOK { t.Errorf("the slide window with the presenter's tag: status %d, want 200", rec.Code) } if rec := talkRequest(a, session, "?t="+token, `"0"`, nil); rec.Code != http.StatusOK { t.Errorf("a stale tag: status %d, want 200", rec.Code) } } func TestTalkPayloadUnknownAndEnded(t *testing.T) { a := newApp(t) cookies := signedIn(t, a, allowedAddress) session, _ := startPresenting(t, a, talkDeck(t, a, cookies), cookies) if rec := talkRequest(a, session+"zzzz", "", "", nil); rec.Code != http.StatusNotFound { t.Errorf("unknown session: status %d, want 404", rec.Code) } if res := a.db.G.Exec("UPDATE presentations SET ended_at = CURRENT_TIMESTAMP WHERE sqid = ?", session); res.Error != nil { t.Fatal(res.Error) } if rec := talkRequest(a, session, "", "", nil); rec.Code != http.StatusGone { t.Errorf("ended session: status %d, want 410", rec.Code) } }