oficina / base Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package browsertest
4
5import (
6 "fmt"
7 "slices"
8 "strings"
9 "sync"
10 "testing"
11
12 "github.com/chromedp/cdproto/network"
13 "github.com/chromedp/chromedp"
14
15 "amadan.net/rastrillo/rastrillo/harness"
16
17 "amadan.net/oficina/base/internal/consumer"
18)
19
20// headersSeen keeps the headers the browser received with responses whose URL
21// starts with prefix.
22type headersSeen struct {
23 mu sync.Mutex
24 seen []map[string]string
25}
26
27func watchHeaders(rig *harness.Rig, prefix string) *headersSeen {
28 h := &headersSeen{}
29 harness.Listen(rig, rig.Context(), network.ResponseReceived, func(e *network.EventResponseReceived) {
30 if !strings.HasPrefix(e.Response.URL, prefix) {
31 return
32 }
33 got := map[string]string{}
34 for k, v := range e.Response.Headers {
35 if s, ok := v.(string); ok {
36 got[strings.ToLower(k)] = s
37 }
38 }
39 h.mu.Lock()
40 h.seen = append(h.seen, got)
41 h.mu.Unlock()
42 })
43 return h
44}
45
46func (h *headersSeen) all() []map[string]string {
47 h.mu.Lock()
48 defer h.mu.Unlock()
49 return append([]map[string]string(nil), h.seen...)
50}
51
52// Signed in to Base, the owner opens Scratchpad from Home and lands on it
53// signed in. Base and Scratchpad are different sites, both served through
54// rastrillo's production stack, so this is the browser's own verdict on
55// Base's handoff page (its form-action override and Referrer-Policy: origin)
56// and on the app's cookies across the cross-site POST, 303 and GET.
57func TestHandoffInRealBrowser(t *testing.T) {
58 for _, scripts := range []bool{true, false} {
59 name := "scripts on"
60 if !scripts {
61 name = "scripts off"
62 }
63 t.Run(name, func(t *testing.T) {
64 w := newWorld(t)
65 if !scripts {
66 drive(t, w.rig, scriptsDisabled(true))
67 }
68 authorize := watchHeaders(w.rig, w.baseOrigin+"/authorize?")
69 signInByEmail(t, w.rig, w.baseOrigin, w.base.mail)
70
71 tile := chromedp.CSS(fmt.Sprintf(`a.app[href=%q]`, w.padOrigin+"/"))
72 note := chromedp.CSS(`textarea[name="note"]`)
73 if scripts {
74 // The handoff page's script posts its form; nobody presses anything.
75 drive(t, w.rig, chromedp.Click(tile), chromedp.WaitVisible(note))
76 } else {
77 button := chromedp.CSS(`form#handoff button[type="submit"]`)
78 drive(t, w.rig, chromedp.Click(tile), chromedp.WaitVisible(button))
79 w.rig.Screen(".card", "the handoff page, scripts off")
80 if here := harness.Read(w.rig, chromedp.Location()); !strings.HasPrefix(here, w.baseOrigin+"/authorize?") {
81 t.Fatalf("the Continue button is on %s, not Base's /authorize", here)
82 }
83 drive(t, w.rig, chromedp.Click(button), chromedp.WaitVisible(note))
84 }
85 w.rig.Screen("main", "Scratchpad")
86 if here := harness.Read(w.rig, chromedp.Location()); here != w.padOrigin+"/" {
87 t.Fatalf("the handoff ended at %s, want %s/", here, w.padOrigin)
88 }
89 want := "Signed in as " + ownerName + ", " + baseName
90 if got := harness.Evaluate[string](w.rig, `document.querySelector(".lead").textContent`); got != want {
91 t.Fatalf("Scratchpad says %q, want %q", got, want)
92 }
93
94 // What the browser was told on Base's handoff page.
95 pages := authorize.all()
96 if len(pages) != 1 {
97 t.Fatalf("the browser loaded Base's /authorize %d times, want once", len(pages))
98 }
99 if got := pages[0]["referrer-policy"]; got != "origin" {
100 t.Errorf("handoff page Referrer-Policy %q, want origin", got)
101 }
102 if got := pages[0]["content-security-policy"]; !strings.HasSuffix(got, "; form-action "+w.padOrigin) || strings.Contains(got, "form-action 'self'") {
103 t.Errorf("handoff page CSP %q, want form-action %s only", got, w.padOrigin)
104 }
105
106 // What Scratchpad received.
107 var posts, finishes []seen
108 for _, r := range w.pad.all() {
109 switch {
110 case r.method == "POST" && r.path == consumer.AssertionPath:
111 posts = append(posts, r)
112 case r.method == "GET" && r.path == consumer.FinishPath:
113 finishes = append(finishes, r)
114 }
115 }
116 if len(posts) != 1 || len(finishes) != 1 {
117 t.Fatalf("Scratchpad got %d assertion POSTs and %d finish GETs, want one each: %+v", len(posts), len(finishes), w.pad.all())
118 }
119 post, finish := posts[0], finishes[0]
120 t.Logf("handoff page: Referrer-Policy %q; CSP %q", pages[0]["referrer-policy"], pages[0]["content-security-policy"])
121 t.Logf("assertion POST: Origin %q, Sec-Fetch-Site %q, cookies %v", post.origin, post.fetchSite, post.cookies)
122 t.Logf("finish GET: Sec-Fetch-Site %q, cookies %v", finish.fetchSite, finish.cookies)
123 if post.origin != w.baseOrigin {
124 t.Errorf("the assertion POST carried Origin %q, want Base's %q", post.origin, w.baseOrigin)
125 }
126 // The control for everything about cookies below: the browser
127 // itself counts Base and Scratchpad as different sites.
128 if post.fetchSite != "cross-site" {
129 t.Errorf("the assertion POST is %q to the browser, want cross-site", post.fetchSite)
130 }
131 // SameSite=Lax keeps the app's cookies off a cross-site POST, which
132 // is why the assertion waits in an inbox for the same-site GET.
133 if slices.Contains(post.cookies, "__Host-base-pending") {
134 t.Errorf("the cross-site POST carried the pending cookie: %v", post.cookies)
135 }
136 for _, c := range []string{"__Host-base-pending", "__Host-base-inbox"} {
137 if !slices.Contains(finish.cookies, c) {
138 t.Errorf("GET %s after the 303 lacks %s: it carried %v", consumer.FinishPath, c, finish.cookies)
139 }
140 }
141 })
142 }
143}
144