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