oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1//go:build browser
2
3package idtest
4
5import (
6 "context"
7 "fmt"
8 "net/http"
9 "net/http/httptest"
10 "net/url"
11 "os"
12 "path/filepath"
13 "testing"
14 "time"
15
16 "amadan.net/oficina/equipo/internal/browsertest"
17 "amadan.net/rastrillo/rastrillo/harness"
18 "github.com/chromedp/cdproto/cdp"
19 "github.com/chromedp/cdproto/emulation"
20 "github.com/chromedp/cdproto/network"
21 "github.com/chromedp/chromedp"
22)
23
24// Every state of the sign-in door in a real browser, forced light, at
25// 320px and 1280px, the widest included (a long remembered address, the
26// refusal, signed out after Oficina ID): no sideways scroll, every control
27// a 24px target, and the Oficina ID glyph drawn.
28func TestBrowserSmartDoor(t *testing.T) {
29 const origin = "https://equipo.example.test"
30 const long = "someone.with.a.rather.long.name@a-team-with-a-long-domain.example.com"
31 f := fixtureAt(t, true, origin, issuer)
32 f.member(t, long)
33 _, record := f.emailRecord(t, long)
34 f.link(t)
35 homeOut := f.request("POST", "/signout", origin, nil, cookie(t, f.oficinaSignin(t), sessionCookie))
36 logout := cookie(t, homeOut, "__Host-equipo-id-logout")
37 f.member(t, "removed@example.test")
38 removed := f.local(t, "removed@example.test")
39 f.remove(t, "removed@example.test")
40 server := httptest.NewTLSServer(f.h)
41 defer server.Close()
42 u, _ := url.Parse(server.URL)
43 opts := append(chromedp.DefaultExecAllocatorOptions[:], chromedp.ExecPath(harness.ChromePath(t)), chromedp.NoSandbox, chromedp.UserDataDir(t.TempDir()), chromedp.Flag("ignore-certificate-errors", true), chromedp.Flag("no-proxy-server", true), chromedp.Flag("host-resolver-rules", "MAP equipo.example.test "+u.Host))
44 alloc, stop := chromedp.NewExecAllocator(context.Background(), opts...)
45 defer stop()
46 ctx, cancel := chromedp.NewContext(alloc)
47 defer cancel()
48 ctx, deadline := context.WithTimeout(ctx, 3*time.Minute)
49 defer deadline()
50 run := func(actions ...chromedp.Action[chromedp.Void]) {
51 t.Helper()
52 if err := chromedp.Do(ctx, actions...); err != nil {
53 t.Fatal(err)
54 }
55 }
56 yes := true
57 setCookie := func(c *http.Cookie) chromedp.Action[chromedp.Void] {
58 return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
59 _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: c.Name, Value: c.Value, URL: origin + "/", Path: "/", Secure: &yes, HTTPOnly: &yes})
60 return err
61 })
62 }
63 clear := chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
64 _, err := cdp.Call(ctx, target, network.ClearBrowserCookies, cdp.Empty{})
65 return err
66 })
67 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
68 _, err := cdp.Call(ctx, target, emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{{Name: "prefers-color-scheme", Value: "light"}}})
69 return err
70 }))
71 for _, c := range []struct {
72 name, path string
73 cookies []*http.Cookie
74 glyph bool
75 }{
76 {"first-visit", "/signin", nil, true},
77 {"other-ways", "/signin?ways=1", nil, false},
78 {"email", "/signin?way=email", nil, false},
79 {"continue-as-long", "/signin", []*http.Cookie{record}, false},
80 {"other-ways-remembered", "/signin?ways=1&way=email", []*http.Cookie{record}, true},
81 {"problem", "/signin?err=expired", nil, false},
82 {"sent", "/signin?sent=1", nil, false},
83 {"step-up", "/signin/local?force=1&reauth=1", nil, false},
84 {"email-only", "/signin/local", nil, false},
85 {"refusal", "/signin", []*http.Cookie{removed}, false},
86 {"signed-out", "/signed-out", nil, false},
87 {"signed-out-after-oficina-id", "/signed-out", []*http.Cookie{logout}, false},
88 } {
89 actions := []chromedp.Action[chromedp.Void]{clear}
90 for _, k := range c.cookies {
91 actions = append(actions, setCookie(k))
92 }
93 run(append(actions, chromedp.Navigate(origin+c.path), browsertest.Wait(`!!document.querySelector('[estilo-signin]')`))...)
94 for _, width := range []int64{320, 1280} {
95 run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error {
96 if _, err := cdp.Call(ctx, target, emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1, Mobile: width == 320}); err != nil {
97 return err
98 }
99 problem, err := chromedp.Evaluate[string](`(() => {
100 if (document.documentElement.scrollWidth > innerWidth) return 'scrolls sideways';
101 for (const e of document.querySelectorAll('[estilo-signin] a, [estilo-signin] button, [estilo-signin] input:not([type=hidden]):not([tabindex="-1"])')) {
102 const r = e.getBoundingClientRect();
103 if (r.width && (r.width < 24 || r.height < 24)) return 'a target under 24px: ' + e.outerHTML.slice(0, 80);
104 }
105 if (`+fmt.Sprint(c.glyph)+`) {
106 const g = document.querySelector('[estilo-signin] a[href*="oficina"] svg, [estilo-signin] a[href*="oficina"] svg');
107 if (!g || g.getBoundingClientRect().width < 12) return 'no Oficina ID glyph';
108 }
109 if (getComputedStyle(document.body).colorScheme === 'dark') return 'dark';
110 return '';
111 })()`)(ctx, target)
112 if err != nil {
113 return err
114 }
115 if problem != "" {
116 return fmt.Errorf("%s at %d: %s", c.name, width, problem)
117 }
118 if dir := os.Getenv("EQUIPO_BROWSER_ARTIFACTS"); dir != "" {
119 shot, err := chromedp.CaptureScreenshot()(ctx, target)
120 if err != nil {
121 return err
122 }
123 if err = os.MkdirAll(dir, 0o700); err != nil {
124 return err
125 }
126 return os.WriteFile(filepath.Join(dir, fmt.Sprintf("door-%s-%d.png", c.name, width)), shot, 0o600)
127 }
128 return nil
129 }))
130 }
131 }
132}
133