oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs_test
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "regexp"
7 "strings"
8 "testing"
9
10 "github.com/carlosframework/docs/internal/docs"
11)
12
13// fetchAs sends what the share popover's script sends: a fetch, so
14// Sec-Fetch-Dest is "empty", with the person's cookies. method POST
15// carries body as a form.
16func fetchAs(t *testing.T, app *docs.App, cookies []*http.Cookie, method, path, body, dest string) *httptest.ResponseRecorder {
17 t.Helper()
18 var req *http.Request
19 if method == http.MethodPost {
20 req = httptest.NewRequest(method, path, strings.NewReader(body))
21 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
22 req.Header.Set("Origin", docs.DefaultOrigin)
23 req.Header.Set("Sec-Fetch-Site", "same-origin")
24 } else {
25 req = httptest.NewRequest(method, path, nil)
26 }
27 if dest != "" {
28 req.Header.Set("Sec-Fetch-Dest", dest)
29 }
30 for _, c := range cookies {
31 req.AddCookie(c)
32 }
33 rec := httptest.NewRecorder()
34 app.Handler().ServeHTTP(rec, req)
35 return rec
36}
37
38func isPanel(body string) bool {
39 return strings.Contains(body, "docs-share-panel") && !strings.Contains(body, "<html")
40}
41
42// ?panel=1 is the popover's panel only for a request a script made. A
43// navigation, or a client that sends no Sec-Fetch-Dest, gets the whole
44// page, so a reloaded or typed URL is never a bare fragment (spec P1.1).
45func TestSharePanelOnlyForAScriptsRequest(t *testing.T) {
46 app, _ := docs.NewForTest(t)
47 alice := signInCookies(t, app, "alice@example.com")
48 sqid := createDoc(t, app, alice, "Minutes")
49 for _, tc := range []struct {
50 dest string
51 panel bool
52 }{
53 {"empty", true}, {"document", false}, {"", false},
54 // Anything that frames or embeds the URL is a page, not a fetch:
55 // the check is an allow-list of "empty", not a deny-list of
56 // "document" (review on #106).
57 {"iframe", false}, {"frame", false}, {"embed", false}, {"object", false},
58 } {
59 rec := fetchAs(t, app, alice, http.MethodGet, "/d/"+sqid+"/share?panel=1", "", tc.dest)
60 if rec.Code != http.StatusOK {
61 t.Fatalf("dest %q: status %d", tc.dest, rec.Code)
62 }
63 if got := isPanel(rec.Body.String()); got != tc.panel {
64 t.Errorf("Sec-Fetch-Dest %q: panel = %v, want %v", tc.dest, got, tc.panel)
65 }
66 if !strings.Contains(rec.Header().Get("Vary"), "Sec-Fetch-Dest") {
67 t.Errorf("dest %q: no Vary: Sec-Fetch-Dest on a URL that answers two ways", tc.dest)
68 }
69 }
70 // Without panel=1, a fetch still gets the page.
71 if rec := fetchAs(t, app, alice, http.MethodGet, "/d/"+sqid+"/share", "", "empty"); isPanel(rec.Body.String()) {
72 t.Error("a fetch of the share page without panel=1 got the panel")
73 }
74}
75
76// The panel is the owner's, as the page is: a writer and a stranger get
77// the 404 a document they cannot share answers.
78func TestSharePanelIsOwnerOnly(t *testing.T) {
79 app, _ := docs.NewForTest(t)
80 alice := signInCookies(t, app, "alice@example.com")
81 sqid := createPrivateDoc(t, app, alice, "Minutes")
82 if rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer"); rec.Code != http.StatusSeeOther {
83 t.Fatalf("share: %d", rec.Code)
84 }
85 for _, who := range []string{"bob@example.com", "carol@example.com"} {
86 c := signInCookies(t, app, who)
87 rec := fetchAs(t, app, c, http.MethodGet, "/d/"+sqid+"/share?panel=1", "", "empty")
88 if rec.Code != http.StatusNotFound || isPanel(rec.Body.String()) {
89 t.Errorf("%s: status %d (panel %v), want 404", who, rec.Code, isPanel(rec.Body.String()))
90 }
91 }
92}
93
94// Each panel form's 303 comes back to the panel, and a page form's to the
95// page; the invite lands and the panel names the person.
96func TestSharePanelFormsRedirectToThePanel(t *testing.T) {
97 app, _ := docs.NewForTest(t)
98 alice := signInCookies(t, app, "alice@example.com")
99 sqid := createDoc(t, app, alice, "Minutes")
100 base := "/d/" + sqid + "/share"
101 for _, tc := range []struct{ path, body string }{
102 {base, "panel=1&address=bob@example.com&role=writer"},
103 {base + "/team", "panel=1&visibility=private"},
104 {base + "/link", "panel=1&on=1"},
105 {base + "/link", "panel=1&on=0"},
106 {base + "/revoke", "panel=1&address=bob@example.com"},
107 } {
108 rec := fetchAs(t, app, alice, http.MethodPost, tc.path, tc.body, "empty")
109 if rec.Code != http.StatusSeeOther || rec.Header().Get("Location") != base+"?panel=1" {
110 t.Errorf("POST %s %s: %d to %q, want 303 to the panel", tc.path, tc.body, rec.Code, rec.Header().Get("Location"))
111 }
112 page := strings.Replace(tc.body, "panel=1&", "", 1)
113 if rec := post(t, app, alice, tc.path, page); rec.Header().Get("Location") != base {
114 t.Errorf("POST %s without panel: to %q, want the page", tc.path, rec.Header().Get("Location"))
115 }
116 }
117 if rec := fetchAs(t, app, alice, http.MethodPost, base, "panel=1&address=dana@example.com&role=reader", "empty"); rec.Code != http.StatusSeeOther {
118 t.Fatalf("invite: %d", rec.Code)
119 }
120 body := fetchAs(t, app, alice, http.MethodGet, base+"?panel=1", "", "empty").Body.String()
121 if !strings.Contains(body, "dana@example.com") || !strings.Contains(body, "alice@example.com") {
122 t.Error("the panel does not list the owner and the person just invited")
123 }
124}
125
126// The one-time link reaches the person who published it from the popover:
127// it is in the panel that follows, with no-store, and in no later answer,
128// page or panel (spec P1.1).
129func TestSharePanelShowsTheOneTimeLinkOnce(t *testing.T) {
130 app, _ := docs.NewForTest(t)
131 alice := signInCookies(t, app, "alice@example.com")
132 sqid := createDoc(t, app, alice, "Minutes")
133 base := "/d/" + sqid + "/share"
134 link := regexp.MustCompile(`/p/[A-Za-z0-9_-]{16,}`)
135
136 if rec := fetchAs(t, app, alice, http.MethodPost, base+"/link", "panel=1&on=1", "empty"); rec.Code != http.StatusSeeOther {
137 t.Fatalf("publish: %d", rec.Code)
138 }
139 first := fetchAs(t, app, alice, http.MethodGet, base+"?panel=1", "", "empty")
140 if !isPanel(first.Body.String()) || !link.MatchString(first.Body.String()) {
141 t.Fatal("the panel after a publish does not carry the link")
142 }
143 if cc := first.Header().Get("Cache-Control"); !strings.Contains(cc, "no-store") {
144 t.Errorf("the panel carrying the link is cacheable: Cache-Control %q", cc)
145 }
146 for _, path := range []string{base + "?panel=1", base} {
147 if rec := fetchAs(t, app, alice, http.MethodGet, path, "", "empty"); link.MatchString(rec.Body.String()) {
148 t.Errorf("GET %s after the panel showed it: the link is shown again", path)
149 }
150 }
151}
152
153// An invite with no address is answered with the panel and its message,
154// 400, when it came from the panel, as the page answers the page's.
155func TestSharePanelEmptyAddressAnswersThePanel(t *testing.T) {
156 app, _ := docs.NewForTest(t)
157 alice := signInCookies(t, app, "alice@example.com")
158 sqid := createDoc(t, app, alice, "Minutes")
159 rec := fetchAs(t, app, alice, http.MethodPost, "/d/"+sqid+"/share", "panel=1&address=&role=reader", "empty")
160 if rec.Code != http.StatusBadRequest || !isPanel(rec.Body.String()) || !strings.Contains(rec.Body.String(), "docs-share-error") {
161 t.Errorf("empty address from the panel: status %d, panel %v, error shown %v; want 400 with the panel and its error",
162 rec.Code, isPanel(rec.Body.String()), strings.Contains(rec.Body.String(), "docs-share-error"))
163 }
164}
165