oficina / docs Public

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

Plain git — no account needed to clone.

Download

Download this file

1package docs
2
3import (
4 "net/http"
5 "net/http/httptest"
6 "strings"
7 "testing"
8 "time"
9)
10
11// Delete in team mode, through the real router and Home sign-in: the
12// owner's own document goes (303 to the list, and it is gone from the
13// store and announced on the suite feed); a document shared with her as
14// a writer, and another member's private one, answer 404 and stay. The
15// owner check is ownerDeletion's, around teammode.Guard's second
16// authorize, which only finds live documents (delete.go).
17func TestTeamOwnerDeletesOnlyHerOwnDocument(t *testing.T) {
18 f := newTeamRouterFixture(t)
19 b := f.browser()
20 b.localSignin(t, "alice@example.com")
21 b.linkProof(t, teamSubject('a'), "alice@example.com")
22 routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
23
24 for _, d := range []*Document{f.shared, f.hidden} {
25 if r := b.do("POST", "/d/"+d.Sqid+"/delete", ""); r.Code != http.StatusNotFound {
26 t.Errorf("delete %q (not hers): status %d, want 404", d.Title, r.Code)
27 }
28 if deleted(t, f, d) {
29 t.Errorf("%q is deleted after someone else's delete was refused", d.Title)
30 }
31 }
32
33 if r := b.do("POST", "/d/"+f.own.Sqid+"/delete", ""); r.Code != http.StatusSeeOther {
34 t.Fatalf("owner's delete: status %d, want 303", r.Code)
35 }
36 if !deleted(t, f, f.own) {
37 t.Error("the owner's document is still there after her delete")
38 }
39 var gone int64
40 if err := f.app.db.Writer().QueryRowContext(t.Context(), "SELECT COUNT(*) FROM suite_feed WHERE document_id = ? AND gone = 1", f.own.ID).Scan(&gone); err != nil {
41 t.Fatalf("read the suite feed: %v", err)
42 }
43 if gone == 0 {
44 t.Error("the delete was not announced on the suite feed: memoria would keep the document")
45 }
46 if r := b.do("POST", "/d/"+f.own.Sqid+"/delete", ""); r.Code != http.StatusNotFound {
47 t.Errorf("deleting it again: status %d, want 404", r.Code)
48 }
49}
50
51// The native Delete… in team mode (spec 2026-11-22 §1): the owner's JSON
52// POST answers {"ok":true} and the deletion is on the suite feed; another
53// member's team document, which she can read by its rung, and a document
54// shared with her as a writer answer 404 and stay; and under Unknown the
55// lookup fails with its status (503), never an ok and never a page, so
56// the client keeps the document and says it could not delete it.
57func TestTeamNativeDelete(t *testing.T) {
58 f := newTeamRouterFixture(t)
59 signIn := func(address string, subject byte) *routerBrowser {
60 b := f.browser()
61 b.localSignin(t, address)
62 b.linkProof(t, teamSubject(subject), address)
63 routerRedirect(t, b.do("POST", "/account/home/confirm", ""), connectedURL("/"))
64 return b
65 }
66 // A new document is on the team rung (Paul, 2026-11-01).
67 create := func(b *routerBrowser, title string) *Document {
68 sqid := strings.TrimPrefix(routerRedirect(t, b.do("POST", "/documents", "title="+title), ""), "/d/")
69 return &Document{Sqid: sqid, Title: title, ID: f.documentID(t, sqid)}
70 }
71 bob := signIn("bob@example.com", 'b')
72 bobsTeam := create(bob, "Bob+team")
73 alice := signIn("alice@example.com", 'a')
74 native := func(method, path string) *httptest.ResponseRecorder {
75 r := alice.request(method, path, "")
76 r.Header.Set("Accept", "application/json")
77 return alice.serve(r)
78 }
79
80 if r := native("GET", "/d/"+bobsTeam.Sqid); r.Code != http.StatusOK {
81 t.Fatalf("precondition: alice cannot read Bob's team document (%d)", r.Code)
82 }
83 for _, d := range []*Document{bobsTeam, f.shared} {
84 if r := native("POST", "/d/"+d.Sqid+"/delete"); r.Code != http.StatusNotFound || strings.Contains(r.Body.String(), `"ok"`) {
85 t.Errorf("native delete %q (not hers): %d %q, want 404", d.Title, r.Code, r.Body.String())
86 }
87 if deleted(t, f, d) {
88 t.Errorf("%q is deleted after someone else's delete was refused", d.Title)
89 }
90 }
91
92 r := native("POST", "/d/"+f.own.Sqid+"/delete")
93 if r.Code != http.StatusOK || strings.TrimSpace(r.Body.String()) != `{"ok":true}` {
94 t.Fatalf("owner's native delete: %d %q, want 200 {\"ok\":true}", r.Code, r.Body.String())
95 }
96 if !deleted(t, f, f.own) {
97 t.Error("the owner's document is still there after her native delete")
98 }
99 var gone int64
100 if err := f.app.db.Writer().QueryRowContext(t.Context(), "SELECT COUNT(*) FROM suite_feed WHERE document_id = ? AND gone = 1", f.own.ID).Scan(&gone); err != nil {
101 t.Fatalf("read the suite feed: %v", err)
102 }
103 if gone == 0 {
104 t.Error("the native delete was not announced on the suite feed")
105 }
106 if r := native("POST", "/d/"+f.own.Sqid+"/delete"); r.Code != http.StatusNotFound {
107 t.Errorf("deleting it again: %d, want 404", r.Code)
108 }
109
110 // Unknown: the roster cannot say who is on the team.
111 mine := create(alice, "Alice+second")
112 f.directory.change(nil, true)
113 f.clock.step(61 * time.Second)
114 r = native("POST", "/d/"+mine.Sqid+"/delete")
115 if r.Code != http.StatusServiceUnavailable {
116 t.Errorf("native delete under Unknown: %d %q, want 503", r.Code, r.Body.String())
117 }
118 if strings.Contains(r.Header().Get("Content-Type"), "text/html") || strings.Contains(r.Body.String(), `"ok"`) {
119 t.Errorf("native delete under Unknown answered a page or an ok: %q %q", r.Header().Get("Content-Type"), r.Body.String())
120 }
121 if deleted(t, f, mine) {
122 t.Error("a delete under Unknown deleted the document")
123 }
124}
125
126func (f *teamRouterFixture) documentID(t *testing.T, sqid string) int64 {
127 t.Helper()
128 var id int64
129 if err := f.app.db.Writer().QueryRowContext(t.Context(), "SELECT id FROM documents WHERE sqid = ?", sqid).Scan(&id); err != nil {
130 t.Fatalf("read %s: %v", sqid, err)
131 }
132 return id
133}
134
135func deleted(t *testing.T, f *teamRouterFixture, d *Document) bool {
136 t.Helper()
137 var n int64
138 if err := f.app.db.Writer().QueryRowContext(t.Context(), "SELECT COUNT(*) FROM documents WHERE id = ? AND deleted_at IS NOT NULL", d.ID).Scan(&n); err != nil {
139 t.Fatalf("read %q: %v", d.Title, err)
140 }
141 return n == 1
142}
143