oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1package webtest
2
3import (
4 "database/sql"
5 "time"
6
7 "amadan.net/oficina/base/internal/config"
8 "amadan.net/oficina/base/internal/web"
9 "net/url"
10 "strings"
11 "testing"
12
13 "amadan.net/oficina/base/internal/equipo"
14 "amadan.net/oficina/base/internal/equipo/equipotest"
15)
16
17// Fake tokens are built at run time (R12), never written as literals.
18func fakeCode() string { return strings.Repeat("c", 3) + "-" + strings.Repeat("9", 8) }
19func fakeState() string { return strings.Repeat("s", 3) + "_" + strings.Repeat("7", 8) }
20
21func doneURL(state, code string) string {
22 return "/ask/oficina/done?" + url.Values{"oficina_state": {state}, "oficina_code": {code}}.Encode()
23}
24
25func TestOficinaConnectStartsAndRedirects(t *testing.T) {
26 a, f := newAskApp(t)
27 token := a.connectEquipo(f)
28 f.OficinaConsent("https://oficina.example/consent?x=1")
29 r := a.get("/ask/oficina/connect")
30 if r.Code != 303 || r.Location() != "https://oficina.example/consent?x=1" {
31 t.Fatalf("connect: %d %q", r.Code, r.Location())
32 }
33 if r.Header.Get("Cache-Control") != "no-store" || r.Header.Get("Referrer-Policy") != "no-referrer" {
34 t.Errorf("headers: %v", r.Header)
35 }
36 calls := f.Calls("/api/v1/assistant/oficina/connect")
37 if len(calls) != 1 || calls[0].Header.Get("Authorization") != "Bearer "+token ||
38 !strings.Contains(calls[0].Body, `"return_to":"`+testOrigin+`/ask/oficina/done"`) {
39 t.Fatalf("calls: %+v", calls)
40 }
41}
42
43func TestOficinaConnectNeverRedirectsAnywhereOdd(t *testing.T) {
44 a, f := newAskApp(t)
45 a.connectEquipo(f)
46 for _, bad := range []string{"http://evil.example/x", "javascript:alert(1)", "//evil.example/x", "/relative", "https://user:pw@evil.example/", "https:///nohost"} {
47 f.OficinaConsent(bad)
48 r := a.get("/ask/oficina/connect")
49 if r.Code == 303 && r.Location() == bad || r.Code != 303 || r.Location() != "/" {
50 t.Errorf("%q: %d %q, want 303 to /", bad, r.Code, r.Location())
51 }
52 if bad != "" && !strings.Contains(r.Header.Get("Set-Cookie"), "oficina_note=unavailable") {
53 t.Errorf("%q: no unavailable note: %v", bad, r.Header)
54 }
55 }
56}
57
58func TestOficinaConnectAllowsEquipoOrigin(t *testing.T) {
59 a, f := newAskApp(t)
60 a.connectEquipo(f)
61 f.OficinaConsent(f.URL + "/consent")
62 if r := a.get("/ask/oficina/connect"); r.Code != 303 || r.Location() != f.URL+"/consent" {
63 t.Fatalf("%d %q", r.Code, r.Location())
64 }
65}
66
67func TestOficinaConnectNeedsSignIn(t *testing.T) {
68 f := equipotest.New(t)
69 a := newAppConfigured(t, func(*sql.DB, config.Config) web.Installer { return noInstaller{} }, time.UTC,
70 func(c *config.Config) { c.EquipoOrigin = f.URL })
71 if r := a.get("/ask/oficina/connect"); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") {
72 t.Fatalf("signed out: %d %q", r.Code, r.Location())
73 }
74 if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || !strings.HasPrefix(r.Location(), "/signin") {
75 t.Fatalf("done, signed out: %d %q", r.Code, r.Location())
76 }
77}
78
79func TestOficinaConnectNeedsTheChatConnected(t *testing.T) {
80 a, f := newAskApp(t) // signed in, Equipo not connected
81 if r := a.get("/ask/oficina/connect"); r.Code != 303 || r.Location() != "/ask" {
82 t.Fatalf("connect: %d %q", r.Code, r.Location())
83 }
84 if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/ask" {
85 t.Fatalf("done: %d %q", r.Code, r.Location())
86 }
87 if n := len(f.Calls("/api/v1/assistant/oficina/connect")) + len(f.Calls("/api/v1/assistant/oficina/connect/finish")); n != 0 {
88 t.Errorf("%d calls without a bearer", n)
89 }
90}
91
92func TestOficinaConnectEquipoErrors(t *testing.T) {
93 for _, c := range []struct {
94 status int
95 code string
96 }{{422, "invalid_input"}, {503, "unavailable"}, {500, "internal"}} {
97 a, f := newAskApp(t)
98 a.connectEquipo(f)
99 f.FailOficina(c.status, c.code)
100 r := a.get("/ask/oficina/connect")
101 if r.Code != 303 || r.Location() != "/" || !strings.Contains(r.Header.Get("Set-Cookie"), "oficina_note=unavailable") {
102 t.Errorf("%d: %d %q %v", c.status, r.Code, r.Location(), r.Header)
103 }
104 }
105 a, f := newAskApp(t)
106 a.connectEquipo(f)
107 f.FailOficina(401, "unauthenticated")
108 if r := a.get("/ask/oficina/connect"); r.Code != 303 || r.Location() != "/ask" {
109 t.Errorf("401: %d %q", r.Code, r.Location())
110 }
111}
112
113func TestOficinaDoneFinishesWithTheBearer(t *testing.T) {
114 a, f := newAskApp(t)
115 token := a.connectEquipo(f)
116 st, code := fakeState(), fakeCode()
117 r := a.get(doneURL(st, code))
118 if r.Code != 303 || r.Location() != "/" {
119 t.Fatalf("done: %d %q", r.Code, r.Location())
120 }
121 if r.Header.Get("Cache-Control") != "no-store" || r.Header.Get("Referrer-Policy") != "no-referrer" {
122 t.Errorf("headers: %v", r.Header)
123 }
124 calls := f.Calls("/api/v1/assistant/oficina/connect/finish")
125 if len(calls) != 1 || calls[0].Header.Get("Authorization") != "Bearer "+token ||
126 !strings.Contains(calls[0].Body, `"state":"`+st+`"`) || !strings.Contains(calls[0].Body, `"code":"`+code+`"`) {
127 t.Fatalf("calls: %+v", calls)
128 }
129 // The outcome reaches the chat once, and nothing secret is logged.
130 home := a.get("/")
131 if !strings.Contains(home.Body, "Oficina connected.") {
132 t.Fatalf("home lacks the note:\n%s", home.Body)
133 }
134 if again := a.get("/"); strings.Contains(again.Body, "Oficina connected.") {
135 t.Errorf("the note showed twice")
136 }
137 for _, secret := range []string{st, code} {
138 if strings.Contains(a.logs.String(), secret) {
139 t.Errorf("a log carried the state or code")
140 }
141 }
142}
143
144func TestOficinaDoneOutcomes(t *testing.T) {
145 a, f := newAskApp(t)
146 a.connectEquipo(f)
147 for _, c := range []struct{ finish, want string }{
148 {"connected", "Oficina connected."},
149 {"denied", "Oficina wasn't connected."},
150 {"expired", "That link expired. Try again."},
151 {"unavailable", "Couldn't reach Oficina. Try again later."},
152 {"surprise", "Couldn't reach Oficina. Try again later."},
153 } {
154 f.FinishWith(c.finish)
155 if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/" {
156 t.Fatalf("%s: %d %q", c.finish, r.Code, r.Location())
157 }
158 if home := a.get("/"); !strings.Contains(strings.ReplaceAll(home.Body, "'", "'"), c.want) {
159 t.Errorf("%s: home lacks %q", c.finish, c.want)
160 }
161 }
162}
163
164func TestOficinaDoneReturnedOutcomeWithoutCode(t *testing.T) {
165 a, f := newAskApp(t)
166 a.connectEquipo(f)
167 for param, want := range map[string]string{"denied": "Oficina wasn't connected.", "expired": "That link expired. Try again.", "unavailable": "Couldn't reach Oficina. Try again later."} {
168 if r := a.get("/ask/oficina/done?oficina=" + param); r.Code != 303 || r.Location() != "/" {
169 t.Fatalf("%s: %d %q", param, r.Code, r.Location())
170 }
171 if home := a.get("/"); !strings.Contains(strings.ReplaceAll(home.Body, "'", "'"), want) {
172 t.Errorf("%s: home lacks %q", param, want)
173 }
174 }
175 if n := len(f.Calls("/api/v1/assistant/oficina/connect/finish")); n != 0 {
176 t.Errorf("finished with no code")
177 }
178 // Only finishing connects: a bare "connected" says nothing.
179 a, f = newAskApp(t)
180 a.connectEquipo(f)
181 if r := a.get("/ask/oficina/done?oficina=connected"); r.Code != 303 || r.Header.Get("Set-Cookie") != "" {
182 t.Errorf("bare connected: %d %v", r.Code, r.Header)
183 }
184 // Anything else says nothing.
185 if r := a.get("/ask/oficina/done?oficina=<b>x</b>"); r.Code != 303 || r.Header.Get("Set-Cookie") != "" {
186 t.Errorf("odd outcome: %d %v", r.Code, r.Header)
187 }
188}
189
190func TestOficinaDoneAnotherPersonsState(t *testing.T) {
191 a, f := newAskApp(t)
192 a.connectEquipo(f)
193 r := a.get(doneURL(equipotest.OtherPersonsState, fakeCode()))
194 if r.Code != 303 || r.Location() != "/" {
195 t.Fatalf("%d %q", r.Code, r.Location())
196 }
197 if home := a.get("/"); strings.Contains(home.Body, "Oficina connected.") {
198 t.Error("someone else's state read as connected")
199 }
200}
201
202func TestOficinaDoneLostBearer(t *testing.T) {
203 a, f := newAskApp(t)
204 a.connectEquipo(f)
205 f.FailOficina(401, "unauthenticated")
206 if r := a.get(doneURL(fakeState(), fakeCode())); r.Code != 303 || r.Location() != "/ask" {
207 t.Errorf("%d %q", r.Code, r.Location())
208 }
209}
210
211func TestOficinaConnectCardLinkKeepsItsRel(t *testing.T) {
212 a, f := newAskApp(t)
213 a.connectEquipo(f)
214 good := equipo.Link{Label: "Connect", URL: testOrigin + "/ask/oficina/connect", Rel: "oficina_connect"}
215 elsewhere := equipo.Link{Label: "Other", URL: "https://evil.example/x", Rel: "oficina_connect"}
216 // The same path on another host gets no same-tab treatment either.
217 lookalike := equipo.Link{Label: "Lookalike", URL: "https://evil.example/ask/oficina/connect", Rel: "oficina_connect"}
218 f.AskOutcome("Connect Oficina to go on.", nil, []equipo.Link{good, elsewhere, lookalike})
219 body := a.get("/ask/state").Body
220 if !strings.Contains(body, `"rel":"oficina_connect"`) || strings.Count(body, `"rel":"oficina_connect"`) != 1 {
221 t.Fatalf("state: %s", body)
222 }
223 page := a.get("/ask").Body
224 if !strings.Contains(page, `<a class="card-link" href="`+testOrigin+`/ask/oficina/connect">Connect</a>`) {
225 t.Errorf("same-tab link missing:\n%s", page)
226 }
227 if !strings.Contains(page, `href="https://evil.example/ask/oficina/connect" target="_blank" rel="noopener">Lookalike`) {
228 t.Errorf("lookalike link got same-tab treatment:\n%s", page)
229 }
230 if !strings.Contains(page, `href="https://evil.example/x" target="_blank" rel="noopener"`) {
231 t.Errorf("other link not opened in a new tab:\n%s", page)
232 }
233}
234
235// sends are the person's messages Base sent to Equipo, oldest first.
236func sends(f *equipotest.Fake) []string {
237 var out []string
238 for _, c := range f.Calls("/api/v1/assistant/messages") {
239 if c.Method == "POST" {
240 out = append(out, c.Body)
241 }
242 }
243 return out
244}
245
246// Base never sends anything for the person on returning from connecting
247// Oficina, however it went: Equipo goes on with the conversation itself
248// once the connect card resolves.
249func TestOficinaDoneSendsNothing(t *testing.T) {
250 for _, finish := range []string{"connected", "denied", "expired", "unavailable", "surprise"} {
251 a, f := newAskApp(t)
252 a.connectEquipo(f)
253 f.FinishWith(finish)
254 a.get(doneURL(fakeState(), fakeCode()))
255 a.get("/")
256 a.get("/")
257 if got := sends(f); len(got) != 0 {
258 t.Errorf("%s: sent %v", finish, got)
259 }
260 }
261 a, f := newAskApp(t)
262 a.connectEquipo(f)
263 for _, target := range []string{"/ask/oficina/done?oficina=denied", "/ask/oficina/done?oficina=connected", "/ask/oficina/done?oficina=expired",
264 doneURL(equipotest.OtherPersonsState, fakeCode())} {
265 a.get(target)
266 a.get("/")
267 }
268 if got := sends(f); len(got) != 0 {
269 t.Errorf("sent %v", got)
270 }
271}
272
273// The note sits in the conversation, after the line it follows (the reply
274// that asked to connect), not under it: what comes later goes below it.
275func TestOficinaNoteSitsInTheConversation(t *testing.T) {
276 a, f := newAskApp(t)
277 a.connectEquipo(f)
278 f.Say("Connect Oficina so I can add it to your calendar.")
279 a.get("/ask/oficina/done?oficina=denied")
280 home := strings.ReplaceAll(a.get("/").Body, "&#39;", "'")
281 thread := home[strings.Index(home, `<ol class="thread"`):]
282 thread = thread[:strings.Index(thread, "</ol>")]
283 reply := strings.Index(thread, "Connect Oficina so I can add it to your calendar.")
284 note := strings.Index(thread, `<li class="ask-notice" data-after="m_`)
285 if reply < 0 || note < reply || !strings.Contains(thread[note:], ">Oficina wasn't connected.</li>") {
286 t.Fatalf("the note is not in the conversation after the reply:\n%s", home)
287 }
288 if strings.Contains(home, `<p class="ask-notice"`) {
289 t.Errorf("the note is under the conversation too")
290 }
291}
292
293// Password managers leave the composer alone (1Password put its icon over
294// it): it is a message, not a login. Spell-check and a phone's keyboard
295// keep working: nothing turns them off.
296func TestComposerIgnoredByPasswordManagers(t *testing.T) {
297 a, f := newAskApp(t)
298 a.connectEquipo(f)
299 for _, path := range []string{"/", "/ask"} {
300 page := a.get(path).Body
301 i := strings.Index(page, `<textarea class="input" id="ask-text"`)
302 if i < 0 {
303 t.Fatalf("%s: no composer", path)
304 }
305 field := page[i : i+strings.Index(page[i:], ">")]
306 for _, want := range []string{`autocomplete="off"`, `data-1p-ignore`, `data-lpignore="true"`, `data-form-type="other"`} {
307 if !strings.Contains(field, want) {
308 t.Errorf("%s: composer lacks %s: %s", path, want, field)
309 }
310 }
311 for _, off := range []string{"spellcheck", "autocorrect", "autocapitalize", "inputmode"} {
312 if strings.Contains(field, off) {
313 t.Errorf("%s: composer sets %s: %s", path, off, field)
314 }
315 }
316 }
317}
318