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 "bytes"
5 "crypto/sha256"
6 "encoding/base64"
7 "encoding/json"
8 "net/http"
9 "net/http/httptest"
10 "net/url"
11 "strings"
12 "testing"
13
14 "github.com/carlosframework/docs/internal/docs"
15)
16
17func TestNativeProofAndSessionRevocation(t *testing.T) {
18 app := members(t, "alice@example.com")
19 browser := signInCookies(t, app, "alice@example.com")
20 verifier := base64.RawURLEncoding.EncodeToString(bytes.Repeat([]byte{42}, 32))
21 hash := sha256.Sum256([]byte(verifier))
22 begin := nativeRequest(app, nil, "POST", "/native/request", `{"challenge":"`+base64.RawURLEncoding.EncodeToString(hash[:])+`"}`)
23 var pending struct{ ID string }
24 if begin.Code != 200 || json.Unmarshal(begin.Body.Bytes(), &pending) != nil || pending.ID == "" {
25 t.Fatalf("native begin: %d", begin.Code)
26 }
27 exchange := `{"id":"` + pending.ID + `","verifier":"` + verifier + `"}`
28 if got := nativeRequest(app, nil, "POST", "/native/exchange", exchange); got.Code != 202 || strings.Contains(got.Body.String(), `"value"`) {
29 t.Fatalf("pending leaks credential: %d", got.Code)
30 }
31 wrong := nativeRequest(app, nil, "POST", "/native/exchange", `{"id":"`+pending.ID+`","verifier":"wrong"}`)
32 if wrong.Code == 200 {
33 t.Fatal("wrong proof accepted")
34 }
35 if got := nativeRequest(app, browser, "POST", "/native/authorize?id="+pending.ID, ""); got.Code != 200 {
36 t.Fatalf("approve: %d", got.Code)
37 }
38 done := nativeRequest(app, nil, "POST", "/native/exchange", exchange)
39 var credential struct{ Name, Value string }
40 if done.Code != 200 || json.Unmarshal(done.Body.Bytes(), &credential) != nil || credential.Value == "" {
41 t.Fatalf("exchange: %d", done.Code)
42 }
43 native := []*http.Cookie{{Name: credential.Name, Value: credential.Value}}
44 if got := nativeRequest(app, native, "GET", "/", ""); got.Code != 200 {
45 t.Fatalf("native session rejected: %d", got.Code)
46 }
47 nativeRequest(app, native, "POST", "/signout", "")
48 if got := nativeRequest(app, native, "GET", "/", ""); got.Code != 303 {
49 t.Fatalf("revoked native session accepted: %d", got.Code)
50 }
51 if got := nativeRequest(app, browser, "GET", "/", ""); got.Code != 200 {
52 t.Fatalf("native signout incorrectly revoked browser: %d", got.Code)
53 }
54 request := httptest.NewRequest("POST", "/native/request", strings.NewReader(`{"challenge":"`+base64.RawURLEncoding.EncodeToString(hash[:])+`"}`))
55 request.Header.Set("Origin", "https://foreign.invalid")
56 request.Header.Set("Sec-Fetch-Site", "cross-site")
57 recorder := httptest.NewRecorder()
58 app.Handler().ServeHTTP(recorder, request)
59 if recorder.Code != 403 {
60 t.Fatalf("cross-origin native begin: %d", recorder.Code)
61 }
62}
63
64func nativeRequest(app *docs.App, cookies []*http.Cookie, method, path, form string) *httptest.ResponseRecorder {
65 r := httptest.NewRequest(method, path, strings.NewReader(form))
66 r.Header.Set("Accept", "application/json")
67 r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
68 if strings.HasPrefix(form, "{") {
69 r.Header.Set("Content-Type", "application/json")
70 }
71 r.Header.Set("Sec-Fetch-Site", "same-origin")
72 for _, cookie := range cookies {
73 r.AddCookie(cookie)
74 }
75 w := httptest.NewRecorder()
76 app.Handler().ServeHTTP(w, r)
77 return w
78}
79
80func TestNativeMetadataPreservesPermissions(t *testing.T) {
81 app := members(t, "alice@example.com,bob@example.com")
82 alice := signInCookies(t, app, "alice@example.com")
83 bob := signInCookies(t, app, "bob@example.com")
84 created := nativeRequest(app, alice, "POST", "/documents", "title=Native+minutes")
85 if created.Code != 201 {
86 t.Fatalf("create: %d %s", created.Code, created.Body.String())
87 }
88 var document struct{ Sqid, Title, GUID string }
89 if err := json.Unmarshal(created.Body.Bytes(), &document); err != nil || document.Sqid == "" || document.GUID == "" {
90 t.Fatalf("missing native identity: %s", created.Body.String())
91 }
92 path := "/d/" + document.Sqid
93 // New documents are on the team rung (spec §8.2.1); this test is about
94 // what a grant carries, so the document goes private first.
95 if got := nativeRequest(app, alice, "POST", path+"/share/team", "visibility=private"); got.Code != 200 {
96 t.Fatalf("make private: %d", got.Code)
97 }
98 if got := nativeRequest(app, bob, "GET", path, ""); got.Code != 404 {
99 t.Fatalf("foreign document: %d", got.Code)
100 }
101 grant := nativeRequest(app, alice, "POST", path+"/share", "address=bob%40example.com&role=reader")
102 if grant.Code != 200 {
103 t.Fatalf("grant: %d", grant.Code)
104 }
105 got := nativeRequest(app, bob, "GET", path, "")
106 var detail struct {
107 CanWrite bool `json:"can_write"`
108 CanShare bool `json:"can_share"`
109 GUID string `json:"guid"`
110 }
111 if got.Code != 200 || json.Unmarshal(got.Body.Bytes(), &detail) != nil || detail.GUID == "" || detail.CanWrite || detail.CanShare {
112 t.Fatalf("reader metadata: %d %s", got.Code, got.Body.String())
113 }
114 if got.Header().Get("Cache-Control") != "no-store" {
115 t.Fatal("native content is cacheable")
116 }
117 if got := nativeRequest(app, bob, "GET", path+"/share", ""); got.Code != 404 {
118 t.Fatalf("reader sees sharing: %d", got.Code)
119 }
120 listing := nativeRequest(app, bob, "GET", "/?q=Native", "")
121 if listing.Code != 200 || !strings.Contains(listing.Body.String(), document.Sqid) {
122 t.Fatalf("shared search missing: %s", listing.Body.String())
123 }
124 revoked := nativeRequest(app, alice, "POST", path+"/share/revoke", "address=bob%40example.com")
125 if revoked.Code != 200 {
126 t.Fatalf("revoke: %d", revoked.Code)
127 }
128 if got := nativeRequest(app, bob, "GET", path, ""); got.Code != 404 {
129 t.Fatalf("revoked reader: %d", got.Code)
130 }
131 listing = nativeRequest(app, bob, "GET", "/", "")
132 if strings.Contains(listing.Body.String(), document.Sqid) {
133 t.Fatal("revoked document remains in native list")
134 }
135}
136
137func TestNativePublicLinkShownOnceAndAnonymousRefused(t *testing.T) {
138 app := members(t, "alice@example.com")
139 alice := signInCookies(t, app, "alice@example.com")
140 sqid := createDoc(t, app, alice, "Published native")
141 path := "/d/" + sqid + "/share"
142 pub := nativeRequest(app, alice, "POST", path+"/link", "on=1")
143 var result struct {
144 Published bool `json:"published"`
145 URL string `json:"public_url"`
146 }
147 if pub.Code != 200 || json.Unmarshal(pub.Body.Bytes(), &result) != nil || !result.Published || result.URL == "" {
148 t.Fatalf("publish: %d %s", pub.Code, pub.Body.String())
149 }
150 if pub.Header().Get("Cache-Control") != "no-store" {
151 t.Fatal("public token response cacheable")
152 }
153 url, err := url.Parse(result.URL)
154 if err != nil || !strings.HasPrefix(url.Path, "/p/") {
155 t.Fatal("invalid public link")
156 }
157 next := nativeRequest(app, alice, "GET", path, "")
158 if next.Code != 200 || strings.Contains(next.Body.String(), result.URL) {
159 t.Fatal("public token returned twice")
160 }
161 if got := nativeRequest(app, nil, "GET", "/", ""); got.Code != 303 {
162 t.Fatalf("anonymous native listing: %d", got.Code)
163 }
164 if got := nativeRequest(app, nil, "POST", "/documents", "title=No"); got.Code != 403 {
165 t.Fatalf("anonymous native creation: %d", got.Code)
166 }
167}
168
169func TestNativeDraftCreationRetryAndRename(t *testing.T) {
170 app := members(t, "alice@example.com,bob@example.com")
171 alice := signInCookies(t, app, "alice@example.com")
172 bob := signInCookies(t, app, "bob@example.com")
173 const form = "creation_token=dd1f5245-2d96-4f11-af6b-350de8c32896"
174 // Concurrent/lost-response retries must all resolve the same owner-scoped token.
175 results := make(chan *httptest.ResponseRecorder, 8)
176 for i := 0; i < 8; i++ {
177 go func() { results <- nativeRequest(app, alice, "POST", "/documents", form) }()
178 }
179 var sqid string
180 for i := 0; i < 8; i++ {
181 got := <-results
182 var doc struct{ Sqid string }
183 if got.Code != 201 || json.Unmarshal(got.Body.Bytes(), &doc) != nil {
184 t.Fatalf("create: %d %s", got.Code, got.Body.String())
185 }
186 if sqid != "" && sqid != doc.Sqid {
187 t.Fatal("retry created another document")
188 }
189 sqid = doc.Sqid
190 }
191 path := "/d/" + sqid
192 if got := nativeRequest(app, alice, "POST", path+"/title", "title=Renamed"); got.Code != 204 {
193 t.Fatalf("rename: %d %s", got.Code, got.Body.String())
194 }
195 var replay struct{ Sqid, Title string }
196 got := nativeRequest(app, alice, "POST", "/documents", form+"&title=Old")
197 if json.Unmarshal(got.Body.Bytes(), &replay) != nil || replay.Sqid != sqid || replay.Title != "Renamed" {
198 t.Fatalf("replay overwrote title: %s", got.Body.String())
199 }
200 if got := nativeRequest(app, bob, "POST", path+"/title", "title=Stolen"); got.Code != 404 {
201 t.Fatalf("stranger rename: %d", got.Code)
202 }
203 nativeRequest(app, alice, "POST", path+"/share", "address=bob%40example.com&role=reader")
204 if got := nativeRequest(app, bob, "POST", path+"/title", "title=Reader"); got.Code != 404 {
205 t.Fatalf("reader rename: %d", got.Code)
206 }
207 nativeRequest(app, alice, "POST", path+"/share", "address=bob%40example.com&role=writer")
208 if got := nativeRequest(app, bob, "POST", path+"/title", "title=Writer"); got.Code != 204 {
209 t.Fatalf("writer rename: %d", got.Code)
210 }
211 if got := nativeRequest(app, alice, "POST", path+"/title", "title="+strings.Repeat("a", 1025)); got.Code != 400 {
212 t.Fatalf("oversized title: %d", got.Code)
213 }
214 other := nativeRequest(app, bob, "POST", "/documents", form)
215 if json.Unmarshal(other.Body.Bytes(), &replay) != nil || replay.Sqid == sqid {
216 t.Fatal("creation token escaped owner scope")
217 }
218}
219