oficina / sheets Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package sheets
4
5// The smart door in a real Chromium, served through rastrillo's production
6// stack with Home configured, so the door offers Continue with Oficina ID.
7// Every state, the widest included (a long remembered address, Keymail, the
8// confirm step, the Keymail continuation, a refusal), reflows at 320px
9// (WCAG 1.4.10), every control on the card is a 24px target on its smaller
10// axis (2.5.8), and the Oficina ID button draws its glyph. The page is put
11// in the light scheme through the DevTools protocol, whatever the machine's.
12//
13// CGO_ENABLED=0 go test -tags browser -run SmartDoorInABrowser ./internal/sheets/
14//
15// SHEETS_SIGNIN_SHOTS=<dir> also writes a screenshot of each state there.
16
17import (
18 "context"
19 "net/http"
20 "net/http/httptest"
21 "net/url"
22 "os"
23 "path/filepath"
24 "strings"
25 "sync"
26 "testing"
27 "time"
28
29 "amadan.net/oficina/estilo.git/brand"
30 "amadan.net/oficina/estilo.git/chrome"
31 "amadan.net/oficina/estilo.git/sky"
32 "amadan.net/rastrillo/rastrillo/auth"
33 "amadan.net/rastrillo/rastrillo/harness"
34 "amadan.net/rastrillo/rastrillo/lastsignin"
35 "amadan.net/rastrillo/rastrillo/sessions"
36 "github.com/chromedp/cdproto/emulation"
37 "github.com/chromedp/chromedp"
38)
39
40// continuePreviewPath draws the Keymail continuation in preview (no meta
41// refresh, the link goes nowhere), so its layout can be measured without a
42// Keymail server.
43const continuePreviewPath = "/__test/continue"
44
45func TestSmartDoorInABrowser(t *testing.T) {
46 host := strings.TrimPrefix(testOrigin, "https://")
47 f := newHomeFixture(t)
48 served := productionHandler(t, f.app)
49 var (
50 mu sync.Mutex
51 inject []*http.Cookie
52 )
53 rig := harness.New(t, func(string) http.Handler {
54 // connect answers only its own origin's host; the browser is on
55 // the rig's. The case's cookies are added here, since the
56 // remembered and session cookies are __Host- and Secure.
57 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
58 r.Host = host
59 mu.Lock()
60 for _, c := range inject {
61 r.AddCookie(c)
62 }
63 mu.Unlock()
64 if r.URL.Path == continuePreviewPath {
65 st := auth.SigninState{Step: auth.StepContinue, ContinueURL: "https://keymail.example.com/oauth/authorize"}
66 screen := chrome.SignInScreen{Icon: brand.Path("sheets"), App: appName, Sky: sky.For(r.Host), State: st, Preview: true}
67 title, _ := signinTitle(screen)
68 f.app.render(w, r, "signin", frame{title: title, signinPage: true}, http.StatusOK, signinView{Screen: screen})
69 return
70 }
71 served.ServeHTTP(w, r)
72 })
73 })
74 remembered := func(rec lastsignin.Record) []*http.Cookie {
75 w := httptest.NewRecorder()
76 f.app.auth.RememberJar().Remember(w, rec)
77 return w.Result().Cookies()
78 }
79 long := "someone.with.a.rather.long.name@a-team-with-a-long-domain.example.com"
80 if _, err := f.app.memberFor("nobody-here@example.com"); err != nil {
81 t.Fatal(err)
82 }
83 refused, err := f.app.homeSessions.Mint(sessions.Session{Subject: "nobody-here@example.com", Method: "magiclink", AuthTime: time.Now()})
84 if err != nil {
85 t.Fatal(err)
86 }
87 f.request(t, "POST", "/signin", url.Values{"address": {signedInAddress}}, false)
88 link, err := url.Parse(f.app.testMailer(t).lastLink(t))
89 if err != nil {
90 t.Fatal(err)
91 }
92 confirm := link.RequestURI()
93
94 type measure struct {
95 Light, Door bool
96 Overflow int
97 Small []string
98 GlyphW, GlyphH float64
99 Heading string
100 }
101 const script = `(() => {
102 const card = document.querySelector('[estilo-signin]');
103 const small = [];
104 for (const el of card ? card.querySelectorAll('a, button, input:not([type=hidden])') : []) {
105 const r = el.getBoundingClientRect();
106 if (Math.min(r.width, r.height) < 24) small.push(el.outerHTML.slice(0, 80) + ' ' + r.width + 'x' + r.height);
107 }
108 const glyph = card && card.querySelector('a[href^="/signin/oficina"] svg');
109 const g = glyph ? glyph.getBoundingClientRect() : {width: 0, height: 0};
110 const h = card && card.querySelector('h1');
111 return {
112 Light: matchMedia('(prefers-color-scheme: light)').matches,
113 Door: !!card,
114 Overflow: document.documentElement.scrollWidth - innerWidth,
115 Small: small,
116 GlyphW: g.width, GlyphH: g.height,
117 Heading: h ? h.textContent : '',
118 };
119 })()`
120 light := command(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{
121 Features: []*emulation.MediaFeature{{Name: "prefers-color-scheme", Value: "light"}},
122 })
123 shots := os.Getenv("SHEETS_SIGNIN_SHOTS")
124 for _, c := range []struct {
125 name, path, heading string
126 cookies []*http.Cookie
127 glyph bool
128 }{
129 {name: "first visit", path: "/signin", heading: "Sign in to Sheets", glyph: true},
130 {name: "from a page", path: "/signin?return_to=" + url.QueryEscape("/sheets/7"), heading: "Sign in to Sheets", glyph: true},
131 {name: "other ways", path: "/signin?ways=1", heading: "Other ways to sign in"},
132 {name: "other ways remembered", path: "/signin?ways=1&way=oficina", heading: "Other ways to sign in",
133 cookies: remembered(lastsignin.Record{Method: lastsignin.MethodFederated, Provider: lastsignin.ProviderOficina, Address: long})},
134 {name: "email", path: "/signin?way=email", heading: "Sign in to Sheets"},
135 {name: "continue as", path: "/signin", heading: "Sign in to Sheets",
136 cookies: remembered(lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: long})},
137 {name: "keymail", path: "/signin", heading: "Sign in to Sheets",
138 cookies: remembered(lastsignin.Record{Method: lastsignin.MethodKeymail, Address: long})},
139 {name: "email only", path: "/signin/local", heading: "Sign in to Sheets"},
140 {name: "problem", path: "/signin?err=rate", heading: "Sign in to Sheets"},
141 {name: "refusal", path: "/signin", heading: "Sign in to Sheets",
142 cookies: append(remembered(lastsignin.Record{Method: lastsignin.MethodMagicLink, Address: long}),
143 &http.Cookie{Name: f.app.homeSessions.CookieName(), Value: refused})},
144 {name: "step-up", path: "/signin?reauth=1", heading: "Sign in to Sheets"},
145 {name: "sent", path: "/signin?sent=1", heading: "Check your email"},
146 {name: "confirm", path: confirm, heading: "Sign in to Sheets"},
147 {name: "continuation", path: continuePreviewPath, heading: "Signing you in"}, // rastrillo v0.45.0 never names Keymail
148 {name: "signed out", path: "/signed-out", heading: "You’re signed out"},
149 } {
150 mu.Lock()
151 inject = c.cookies
152 mu.Unlock()
153 for _, width := range []int64{320, 1280} {
154 ctx, cancel := context.WithTimeout(rig.Context(), 30*time.Second)
155 var m measure
156 err := chromedp.Do(ctx,
157 light,
158 chromedp.EmulateViewport(width, 900),
159 chromedp.Navigate(rig.Origin+c.path),
160 chromedp.WaitVisible(chromedp.CSS("[estilo-signin] h1")),
161 harness.Into(&m, chromedp.Evaluate[measure](script)),
162 )
163 if err == nil && shots != "" {
164 var shot []byte
165 shot, err = chromedp.Run(ctx, chromedp.CaptureScreenshot())
166 if err == nil {
167 name := strings.ReplaceAll(c.name, " ", "-") + "-" + map[int64]string{320: "phone", 1280: "desk"}[width] + ".png"
168 err = os.WriteFile(filepath.Join(shots, name), shot, 0o644)
169 }
170 }
171 cancel()
172 if err != nil {
173 t.Fatalf("%s at %dpx: %v", c.name, width, err)
174 }
175 if !m.Light {
176 t.Fatal("the page is not in the light scheme")
177 }
178 if !m.Door || m.Heading != c.heading {
179 t.Errorf("%s at %dpx: door=%v heading %q, want %q", c.name, width, m.Door, m.Heading, c.heading)
180 }
181 if m.Overflow > 0 {
182 t.Errorf("%s at %dpx scrolls sideways by %dpx", c.name, width, m.Overflow)
183 }
184 if len(m.Small) > 0 {
185 t.Errorf("%s at %dpx: targets under 24px: %q", c.name, width, m.Small)
186 }
187 if c.glyph && (m.GlyphW < 12 || m.GlyphH < 12) {
188 t.Errorf("%s at %dpx: the Oficina glyph is %vx%v", c.name, width, m.GlyphW, m.GlyphH)
189 }
190 }
191 }
192}
193