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 "strings"
6 "testing"
7
8 "github.com/carlosframework/docs/internal/docs"
9)
10
11// members builds an app whose front door is a real allowlist, so the
12// difference between a configured member and somebody admitted only by a
13// grant actually exists. NewForTest's empty allowlist means "admit
14// everyone" in development, which is why none of this was reachable before.
15func members(t *testing.T, addresses string) *docs.App {
16 t.Helper()
17 app, err := docs.NewForTestWith(t, docs.Config{AllowedAddresses: addresses})
18 if err != nil {
19 t.Fatalf("NewForTestWith: %v", err)
20 }
21 return app
22}
23
24// A grant admits its holder as a GUEST, not as a member.
25//
26// Suite §1 ruling 1: "Sharing is membership; an outside guest is a guest
27// member." Docs §8.2: "Outside guests are granted a specific document and
28// become guest members." The branch had no guest kind at all, so a single
29// reader grant was an unbounded, transitive key to the instance's front
30// door: Bob is admitted by Alice's grant, creates his own documents, shares
31// them to Mallory, who is now admitted and can repeat it. Alice cannot
32// undo any of it, and $DOCS_ALLOWED_ADDRESSES silently stopped being the
33// membership list the operator configured. (Branch review, finding 1.)
34//
35// A guest reads and writes what they were granted, and nothing else.
36func TestAGuestCannotCreateOrShare(t *testing.T) {
37 app := members(t, "alice@example.com")
38 alice := signInCookies(t, app, "alice@example.com")
39 sqid := createDoc(t, app, alice, "Minutes")
40 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer")
41
42 // Bob gets in — the invitation is redeemable, which is the whole
43 // reason grant-admission exists.
44 bob := signInCookies(t, app, "bob@example.com")
45 if rec := get(t, app, bob, "/d/"+sqid); rec.Code != http.StatusOK {
46 t.Fatalf("the guest cannot read the document he was granted: %d", rec.Code)
47 }
48
49 // But he is a guest. He cannot mint documents of his own — 403
50 // exactly, so a 500 cannot pass for a refusal.
51 if rec := post(t, app, bob, "/documents", "title=Bob%27s+own"); rec.Code != http.StatusForbidden {
52 t.Errorf("guest create: status %d, want 403", rec.Code)
53 }
54 // ...and he cannot admit anybody else, which is the transitive half
55 // and the one that makes the first unbounded.
56 //
57 // Asserting 403 specifically, not merely "not 303". The first version
58 // of this test accepted any non-303, which the OWNER check already
59 // produces for Bob on Alice's document — so deleting the member guard
60 // from sharePost left it green. The re-review caught that by mutation:
61 // three of the four guarded routes had never been exercised at all.
62 //
63 // 404 since 2026-11-01 (Astra): a route about one document answers a
64 // guest as one about a document they cannot reach, never 403, as team
65 // mode always has. The owner check answers Bob 404 on Alice's document
66 // too, so the guard is only visible on a document the guest OWNS: Carol
67 // below owns one (made before she lost her place on the list, say) and
68 // is admitted only by a grant. Without the guard her share would 303.
69 //
70 // MUTATION-TESTED 2026-11-01, each reverted: shareTeam without the
71 // member guard → "carol on her own, team rung" 303; the lone-shape guard
72 // answering 403 whatever item says (the old code) → every case 403.
73 carolsID := app.TestPerson(t, "carol@example.com")
74 carols := app.TestCreatePrivate(t, carolsID, "Carol's own").Sqid
75 post(t, app, alice, "/d/"+sqid+"/share", "address=carol@example.com&role=reader")
76 carol := signInCookies(t, app, "carol@example.com")
77 for _, who := range []struct {
78 name string
79 cookies []*http.Cookie
80 sqid string
81 }{{"bob on alice's", bob, sqid}, {"carol on her own", carol, carols}, {"bob on none", bob, "zzzzzzzzzz"}} {
82 for _, c := range []struct{ what, path, body string }{
83 {"share", "/share", "address=mallory@example.com&role=writer"},
84 {"revoke", "/share/revoke", "address=alice@example.com"},
85 {"publish", "/share/link", "on=1"},
86 {"team rung", "/share/team", "visibility=team"},
87 {"delete", "/delete", ""},
88 } {
89 if rec := post(t, app, who.cookies, "/d/"+who.sqid+c.path, c.body); rec.Code != http.StatusNotFound {
90 t.Errorf("guest %s, %s: status %d, want 404", who.name, c.what, rec.Code)
91 }
92 }
93 }
94}
95
96// A guest is not shown a control they cannot use. Two of them: the create
97// form in the list header, and File → New document in the editor's menu
98// bar. Both post /documents, which answers a guest 403 with a bare text
99// page. (Re-review, finding 3.)
100func TestAGuestIsNotShownCreateControls(t *testing.T) {
101 app := members(t, "alice@example.com")
102 alice := signInCookies(t, app, "alice@example.com")
103 sqid := createDoc(t, app, alice, "Minutes")
104 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer")
105 bob := signInCookies(t, app, "bob@example.com")
106
107 for _, page := range []string{"/", "/d/" + sqid} {
108 rec := get(t, app, bob, page)
109 // The status first: an absent control and an unrendered page look
110 // identical to a substring check, so without this the test passes
111 // on a 404 — which is the opposite of what it claims to show.
112 if rec.Code != http.StatusOK {
113 t.Fatalf("the guest's %s answered %d, want 200", page, rec.Code)
114 }
115 if strings.Contains(rec.Body.String(), `action="/documents"`) {
116 t.Errorf("the guest's %s offers a create control they are refused", page)
117 }
118 }
119
120 // Alice keeps both, or the check above passes on an app that offers
121 // nobody a way to create a document.
122 for _, page := range []string{"/", "/d/" + sqid} {
123 if body := get(t, app, alice, page).Body.String(); !strings.Contains(body, `action="/documents"`) {
124 t.Errorf("the member's %s lost its create control", page)
125 }
126 }
127}
128
129// A configured member keeps everything. Without this the rule above passes
130// on an app where nobody can create anything.
131func TestAMemberStillCreatesAndShares(t *testing.T) {
132 app := members(t, "alice@example.com,carol@example.com")
133 carol := signInCookies(t, app, "carol@example.com")
134
135 sqid := createDoc(t, app, carol, "Carol's")
136 if rec := post(t, app, carol, "/d/"+sqid+"/share", "address=dave@example.com&role=reader"); rec.Code != http.StatusSeeOther {
137 t.Errorf("a configured member could not share: status %d", rec.Code)
138 }
139}
140
141// Revoking the last grant ends admission, not just the document.
142//
143// Before this, `allows()` ran only at sign-in, so a revoked guest kept a
144// valid session for up to 30 days — and, per the finding above, kept
145// creating and sharing with it. "Stop sharing" was complete for the
146// document and said nothing about the instance. (Branch review, finding 3.)
147func TestRevokingTheLastGrantEndsAdmission(t *testing.T) {
148 app := members(t, "alice@example.com")
149 alice := signInCookies(t, app, "alice@example.com")
150 sqid := createDoc(t, app, alice, "Minutes")
151 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader")
152
153 bob := signInCookies(t, app, "bob@example.com")
154 if rec := get(t, app, bob, "/d/"+sqid); rec.Code != http.StatusOK {
155 t.Fatalf("precondition: the guest cannot read (%d)", rec.Code)
156 }
157
158 post(t, app, alice, "/d/"+sqid+"/share/revoke", "address=bob@example.com")
159
160 // The same cookies, still cryptographically valid, now reach nothing.
161 // 303 exactly: "not 200" would let a 500 pass for a refusal.
162 for _, path := range []string{"/", "/d/" + sqid} {
163 if rec := get(t, app, bob, path); rec.Code != http.StatusSeeOther {
164 t.Errorf("a revoked guest's GET %s answered %d, want 303", path, rec.Code)
165 }
166 }
167
168 // A member is not touched by any of this: the recheck must key on
169 // admission, not on holding a grant.
170 if rec := get(t, app, alice, "/"); rec.Code != http.StatusOK {
171 t.Errorf("the owner lost access to her own list: %d", rec.Code)
172 }
173}
174
175// A revoked guest's writes are REFUSED, not redirected.
176//
177// The worst defect on this branch, and I introduced it while fixing the
178// last one. requireAdmission answered every method with 303 → /signin.
179// fetch() follows redirects by default, so the editor's POST became a GET
180// of the sign-in page, came back 200 with HTML, and save-queue.mjs read
181// res.ok and reported the batch saved. Alice revokes Bob mid-sentence and
182// Bob types for an hour into a page that says everything is fine.
183//
184// That is strictly worse than the reader-editor bug this branch already
185// fixed: that one at least lit the "save failed" indicator. rastrillo's
186// own refuseSession redirects GET/HEAD and 403s everything else, for
187// exactly this reason, and requireAdmission now does the same.
188//
189// A redirect is a fine answer to "show me a page". It is never an answer
190// to "store this". (Re-review, finding 1.)
191func TestARevokedGuestsWritesAreRefusedNotRedirected(t *testing.T) {
192 app := members(t, "alice@example.com")
193 alice := signInCookies(t, app, "alice@example.com")
194 sqid := createDoc(t, app, alice, "Minutes")
195 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=writer")
196 bob := signInCookies(t, app, "bob@example.com")
197
198 // He really can write, first — or the assertion below passes on an
199 // app where nobody can.
200 if rec := postBytes(t, app, bob, "/d/"+sqid+"/updates", docs.ReadFragmentFixture(t, "hello")); rec.Code != http.StatusNoContent {
201 t.Fatalf("precondition: the guest writer cannot write (%d)", rec.Code)
202 }
203
204 post(t, app, alice, "/d/"+sqid+"/share/revoke", "address=bob@example.com")
205
206 // Now every write must be refused with a status the browser reports
207 // as a failure. A 3xx here is the bug: fetch follows it and the save
208 // queue reads the sign-in page's 200 as success.
209 rec := postBytes(t, app, bob, "/d/"+sqid+"/updates", docs.ReadFragmentFixture(t, "hello"))
210 if rec.Code >= 300 && rec.Code < 400 {
211 t.Errorf("a revoked guest's write was redirected (%d → %q); fetch follows it and the save reports ok",
212 rec.Code, rec.Header().Get("Location"))
213 }
214 if rec.Code != http.StatusForbidden {
215 t.Errorf("a revoked guest's write answered %d; want 403", rec.Code)
216 }
217
218 // GETs still redirect — that is what a redirect is for, and a person
219 // following a link deserves the sign-in page rather than a bare 403.
220 if rec := get(t, app, bob, "/"); rec.Code != http.StatusSeeOther {
221 t.Errorf("a revoked guest's GET / answered %d, want 303 to sign-in", rec.Code)
222 }
223}
224
225// The wiring, end to end through the real sign-in, rather than allows()
226// and HasGrant each tested against a stand-in for the other.
227func TestAGrantAdmitsThroughTheRealSignIn(t *testing.T) {
228 app := members(t, "alice@example.com")
229 alice := signInCookies(t, app, "alice@example.com")
230 sqid := createDoc(t, app, alice, "Minutes")
231
232 // Before any grant, Bob is refused — and refused invisibly, since
233 // gateBegin answers a refused address exactly as it answers a real
234 // one so the endpoint is not a membership oracle.
235 if _, err := app.TestSignIn(t, "bob@example.com"); err == nil {
236 t.Fatal("an address on no list and holding no grant was mailed a link")
237 }
238
239 post(t, app, alice, "/d/"+sqid+"/share", "address=bob@example.com&role=reader")
240
241 link, err := app.TestSignIn(t, "bob@example.com")
242 if err != nil {
243 t.Fatalf("a granted address was not mailed a link: %v", err)
244 }
245 if !strings.Contains(link, "/auth/verify") {
246 t.Fatalf("not a verify link: %q", link)
247 }
248}
249