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 "strings"
7 "testing"
8
9 "github.com/carlosframework/docs/internal/docs"
10)
11
12// post sends a scriptless form the way a browser would: urlencoded body, a
13// real Origin, and the signed-in person's cookies. There is no CSRF token
14// field anywhere in this app — csrf.Protect reads Sec-Fetch-Site first and
15// falls back to Origin — so a test that forgets the header is testing the
16// CSRF layer rather than the handler.
17func post(t *testing.T, app *docs.App, cookies []*http.Cookie, path, body string) *httptest.ResponseRecorder {
18 t.Helper()
19 req := httptest.NewRequest(http.MethodPost, path, strings.NewReader(body))
20 req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
21 req.Header.Set("Origin", docs.DefaultOrigin)
22 for _, c := range cookies {
23 req.AddCookie(c)
24 }
25 rec := httptest.NewRecorder()
26 app.Handler().ServeHTTP(rec, req)
27 return rec
28}
29
30func get(t *testing.T, app *docs.App, cookies []*http.Cookie, path string) *httptest.ResponseRecorder {
31 t.Helper()
32 req := httptest.NewRequest(http.MethodGet, path, nil)
33 for _, c := range cookies {
34 req.AddCookie(c)
35 }
36 rec := httptest.NewRecorder()
37 app.Handler().ServeHTTP(rec, req)
38 return rec
39}
40
41// createDoc makes a document as the signed-in person and returns its sqid.
42func createDoc(t *testing.T, app *docs.App, cookies []*http.Cookie, title string) string {
43 t.Helper()
44 rec := post(t, app, cookies, "/documents", "title="+title)
45 if rec.Code != http.StatusSeeOther {
46 t.Fatalf("POST /documents: status %d, want 303", rec.Code)
47 }
48 return strings.TrimPrefix(rec.Header().Get("Location"), "/d/")
49}
50
51// createPrivateDoc makes a document and moves it off the team rung, for the
52// tests whose subject is who reaches a PRIVATE document — grants, revokes,
53// 404s for strangers. A new document is on the team rung (spec §8.2.1, Paul
54// 2026-11-01: "New docs Team, old private"), and in NewForTest's development
55// mode every signed-in address is a member, so createDoc alone would hand
56// every one of them the document by team standing and those tests would be
57// testing the rung instead of the grant. Moved through the share page's own
58// form, so it is the path an owner takes.
59func createPrivateDoc(t *testing.T, app *docs.App, cookies []*http.Cookie, title string) string {
60 t.Helper()
61 sqid := createDoc(t, app, cookies, title)
62 if rec := post(t, app, cookies, "/d/"+sqid+"/share/team", "visibility=private"); rec.Code != http.StatusSeeOther {
63 t.Fatalf("POST /d/%s/share/team: status %d, want 303", sqid, rec.Code)
64 }
65 return sqid
66}
67
68// The whole point of the feature, end to end and scriptless: Alice shares a
69// document with Bob by typing his address into a form, and Bob can read it.
70func TestShareThenTheGranteeCanRead(t *testing.T) {
71 app, _ := docs.NewForTest(t)
72 alice := signInCookies(t, app, "alice@example.com")
73 sqid := createPrivateDoc(t, app, alice, "Minutes")
74
75 // Bob cannot see it yet.
76 bob := signInCookies(t, app, "bob@example.com")
77 if rec := get(t, app, bob, "/d/"+sqid); rec.Code != http.StatusNotFound {
78 t.Fatalf("bob reading before the share: status %d, want 404", rec.Code)
79 }
80
81 rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader")
82 if rec.Code != http.StatusSeeOther {
83 t.Fatalf("POST share: status %d, want 303", rec.Code)
84 }
85
86 if rec := get(t, app, bob, "/d/"+sqid); rec.Code != http.StatusOK {
87 t.Errorf("bob reading after the share: status %d, want 200", rec.Code)
88 }
89}
90
91// The share page lists who currently holds a grant, or the owner cannot tell
92// what they have shared and with whom — which is the question the page exists
93// to answer.
94func TestSharePageListsGrantees(t *testing.T) {
95 app, _ := docs.NewForTest(t)
96 alice := signInCookies(t, app, "alice@example.com")
97 sqid := createDoc(t, app, alice, "Minutes")
98
99 if rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer"); rec.Code != http.StatusSeeOther {
100 t.Fatalf("POST share: status %d", rec.Code)
101 }
102
103 rec := get(t, app, alice, "/d/"+sqid+"/share")
104 if rec.Code != http.StatusOK {
105 t.Fatalf("GET share: status %d, want 200", rec.Code)
106 }
107 if !strings.Contains(rec.Body.String(), "bob@example.com") {
108 t.Error("the share page does not name the person the document is shared with")
109 }
110}
111
112// Only the owner shares. A grantee — even one who can write — reaches the
113// share page exactly as a stranger does, with a 404 rather than a 403: a
114// document you may not share does not announce itself as one you may not
115// share (spec §4.2).
116func TestOnlyTheOwnerReachesTheSharePage(t *testing.T) {
117 app, _ := docs.NewForTest(t)
118 alice := signInCookies(t, app, "alice@example.com")
119 sqid := createPrivateDoc(t, app, alice, "Minutes")
120
121 if rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer"); rec.Code != http.StatusSeeOther {
122 t.Fatalf("POST share: status %d", rec.Code)
123 }
124
125 bob := signInCookies(t, app, "bob@example.com")
126 if rec := get(t, app, bob, "/d/"+sqid+"/share"); rec.Code != http.StatusNotFound {
127 t.Errorf("a writer reaching the share page: status %d, want 404", rec.Code)
128 }
129 if rec := post(t, app, bob, "/d/"+sqid+"/share", "address=carol@example.com&role=reader"); rec.Code != http.StatusNotFound {
130 t.Errorf("a writer re-sharing: status %d, want 404", rec.Code)
131 }
132
133 carol := signInCookies(t, app, "carol@example.com")
134 if rec := get(t, app, carol, "/d/"+sqid); rec.Code != http.StatusNotFound {
135 t.Error("bob managed to re-share the document with carol")
136 }
137}
138
139// Revoking takes the access away again. Without this the share page is a
140// one-way door and a mistyped address is permanent.
141func TestRevokeRemovesAccess(t *testing.T) {
142 app, _ := docs.NewForTest(t)
143 alice := signInCookies(t, app, "alice@example.com")
144 sqid := createPrivateDoc(t, app, alice, "Minutes")
145 bob := signInCookies(t, app, "bob@example.com")
146
147 if rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader"); rec.Code != http.StatusSeeOther {
148 t.Fatalf("POST share: status %d", rec.Code)
149 }
150 if rec := get(t, app, bob, "/d/"+sqid); rec.Code != http.StatusOK {
151 t.Fatalf("bob cannot read after the share; the revoke below would prove nothing")
152 }
153
154 if rec := post(t, app, alice, "/d/"+sqid+"/share/revoke", "address=bob@example.com"); rec.Code != http.StatusSeeOther {
155 t.Fatalf("POST revoke: status %d, want 303", rec.Code)
156 }
157
158 if rec := get(t, app, bob, "/d/"+sqid); rec.Code != http.StatusNotFound {
159 t.Errorf("bob reading after revoke: status %d, want 404", rec.Code)
160 }
161}
162
163// A reader granted read access cannot write, through the real HTTP path
164// rather than only at the store. The 404 is the same one a stranger gets.
165func TestAReaderCannotPostUpdates(t *testing.T) {
166 app, _ := docs.NewForTest(t)
167 alice := signInCookies(t, app, "alice@example.com")
168 sqid := createDoc(t, app, alice, "Minutes")
169
170 if rec := post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader"); rec.Code != http.StatusSeeOther {
171 t.Fatalf("POST share: status %d", rec.Code)
172 }
173
174 bob := signInCookies(t, app, "bob@example.com")
175 rec := post(t, app, bob, "/d/"+sqid+"/updates", "")
176 if rec.Code != http.StatusNotFound {
177 t.Errorf("a reader posting an update: status %d, want 404", rec.Code)
178 }
179}
180
181// An empty address is refused rather than stored, so the page cannot mint a
182// grant that matches nobody and lists as a blank row forever.
183func TestShareRefusesAnEmptyAddress(t *testing.T) {
184 app, _ := docs.NewForTest(t)
185 alice := signInCookies(t, app, "alice@example.com")
186 sqid := createDoc(t, app, alice, "Minutes")
187
188 rec := post(t, app, alice, "/d/"+sqid+"/share", "address=&role=reader")
189 if rec.Code == http.StatusSeeOther {
190 t.Error("an empty address was accepted as a share")
191 }
192}
193