//go:build browser package idtest import ( "context" "fmt" "net/http" "net/http/httptest" "net/url" "os" "path/filepath" "testing" "time" "amadan.net/oficina/equipo/internal/browsertest" "amadan.net/rastrillo/rastrillo/harness" "github.com/chromedp/cdproto/cdp" "github.com/chromedp/cdproto/emulation" "github.com/chromedp/cdproto/network" "github.com/chromedp/chromedp" ) // Every state of the sign-in door in a real browser, forced light, at // 320px and 1280px, the widest included (a long remembered address, the // refusal, signed out after Oficina ID): no sideways scroll, every control // a 24px target, and the Oficina ID glyph drawn. func TestBrowserSmartDoor(t *testing.T) { const origin = "https://equipo.example.test" const long = "someone.with.a.rather.long.name@a-team-with-a-long-domain.example.com" f := fixtureAt(t, true, origin, issuer) f.member(t, long) _, record := f.emailRecord(t, long) f.link(t) homeOut := f.request("POST", "/signout", origin, nil, cookie(t, f.oficinaSignin(t), sessionCookie)) logout := cookie(t, homeOut, "__Host-equipo-id-logout") f.member(t, "removed@example.test") removed := f.local(t, "removed@example.test") f.remove(t, "removed@example.test") server := httptest.NewTLSServer(f.h) defer server.Close() u, _ := url.Parse(server.URL) 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)) alloc, stop := chromedp.NewExecAllocator(context.Background(), opts...) defer stop() ctx, cancel := chromedp.NewContext(alloc) defer cancel() ctx, deadline := context.WithTimeout(ctx, 3*time.Minute) defer deadline() run := func(actions ...chromedp.Action[chromedp.Void]) { t.Helper() if err := chromedp.Do(ctx, actions...); err != nil { t.Fatal(err) } } yes := true setCookie := func(c *http.Cookie) chromedp.Action[chromedp.Void] { return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: c.Name, Value: c.Value, URL: origin + "/", Path: "/", Secure: &yes, HTTPOnly: &yes}) return err }) } clear := chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, network.ClearBrowserCookies, cdp.Empty{}) return err }) run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { _, err := cdp.Call(ctx, target, emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{{Name: "prefers-color-scheme", Value: "light"}}}) return err })) for _, c := range []struct { name, path string cookies []*http.Cookie glyph bool }{ {"first-visit", "/signin", nil, true}, {"other-ways", "/signin?ways=1", nil, false}, {"email", "/signin?way=email", nil, false}, {"continue-as-long", "/signin", []*http.Cookie{record}, false}, {"other-ways-remembered", "/signin?ways=1&way=email", []*http.Cookie{record}, true}, {"problem", "/signin?err=expired", nil, false}, {"sent", "/signin?sent=1", nil, false}, {"step-up", "/signin/local?force=1&reauth=1", nil, false}, {"email-only", "/signin/local", nil, false}, {"refusal", "/signin", []*http.Cookie{removed}, false}, {"signed-out", "/signed-out", nil, false}, {"signed-out-after-oficina-id", "/signed-out", []*http.Cookie{logout}, false}, } { actions := []chromedp.Action[chromedp.Void]{clear} for _, k := range c.cookies { actions = append(actions, setCookie(k)) } run(append(actions, chromedp.Navigate(origin+c.path), browsertest.Wait(`!!document.querySelector('[estilo-signin]')`))...) for _, width := range []int64{320, 1280} { run(chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { if _, err := cdp.Call(ctx, target, emulation.SetDeviceMetricsOverride, emulation.SetDeviceMetricsOverrideParams{Width: width, Height: 900, DeviceScaleFactor: 1, Mobile: width == 320}); err != nil { return err } problem, err := chromedp.Evaluate[string](`(() => { if (document.documentElement.scrollWidth > innerWidth) return 'scrolls sideways'; for (const e of document.querySelectorAll('[estilo-signin] a, [estilo-signin] button, [estilo-signin] input:not([type=hidden]):not([tabindex="-1"])')) { const r = e.getBoundingClientRect(); if (r.width && (r.width < 24 || r.height < 24)) return 'a target under 24px: ' + e.outerHTML.slice(0, 80); } if (`+fmt.Sprint(c.glyph)+`) { const g = document.querySelector('[estilo-signin] a[href*="oficina"] svg, [estilo-signin] a[href*="oficina"] svg'); if (!g || g.getBoundingClientRect().width < 12) return 'no Oficina ID glyph'; } if (getComputedStyle(document.body).colorScheme === 'dark') return 'dark'; return ''; })()`)(ctx, target) if err != nil { return err } if problem != "" { return fmt.Errorf("%s at %d: %s", c.name, width, problem) } if dir := os.Getenv("EQUIPO_BROWSER_ARTIFACTS"); dir != "" { shot, err := chromedp.CaptureScreenshot()(ctx, target) if err != nil { return err } if err = os.MkdirAll(dir, 0o700); err != nil { return err } return os.WriteFile(filepath.Join(dir, fmt.Sprintf("door-%s-%d.png", c.name, width)), shot, 0o600) } return nil })) } } }