| 1 | //go:build browser |
| 2 | |
| 3 | package fichastest |
| 4 | |
| 5 | // The smart door in a real Chromium, forced to light: every state at 320 |
| 6 | // and 1280px with no sideways scroll, every control a 24px target at |
| 7 | // least, nothing serious or critical to axe, and the refusal drawn for a |
| 8 | // session the owners list no longer holds. |
| 9 | |
| 10 | import ( |
| 11 | "context" |
| 12 | "encoding/base64" |
| 13 | "io" |
| 14 | "log/slog" |
| 15 | "net/http" |
| 16 | "net/http/httptest" |
| 17 | "path/filepath" |
| 18 | "testing" |
| 19 | "time" |
| 20 | |
| 21 | "amadan.net/rastrillo/rastrillo/db" |
| 22 | "amadan.net/rastrillo/rastrillo/harness" |
| 23 | "amadan.net/rastrillo/rastrillo/sessions" |
| 24 | "github.com/chromedp/cdproto/cdp" |
| 25 | "github.com/chromedp/cdproto/emulation" |
| 26 | "github.com/chromedp/cdproto/network" |
| 27 | "github.com/chromedp/chromedp" |
| 28 | |
| 29 | fichas "amadan.net/oficina/fichas/internal/fichas" |
| 30 | ) |
| 31 | |
| 32 | // doorRig is the app as main.go wires it (real sign-in, no test identity), |
| 33 | // and a session for TestOther, who is not an owner: the cookie value the |
| 34 | // refusal state presents. |
| 35 | func doorRig(t *testing.T) (*harness.Rig, string) { |
| 36 | var refused string |
| 37 | rig := harness.New(t, func(origin string) http.Handler { |
| 38 | logger := slog.New(slog.NewTextHandler(io.Discard, nil)) |
| 39 | d, err := db.Open(filepath.Join(t.TempDir(), "app.db"), logger) |
| 40 | if err != nil { |
| 41 | t.Fatalf("db.Open: %v", err) |
| 42 | } |
| 43 | t.Cleanup(func() { d.Close() }) |
| 44 | cfg, err := fichas.LoadConfig(func(k string) string { |
| 45 | return map[string]string{ |
| 46 | "FICHAS_DEK": base64.StdEncoding.EncodeToString(testKey()), |
| 47 | "FICHAS_INSTANCE_KEY": "browser", |
| 48 | "FICHAS_OWNERS": TestOwner, |
| 49 | "FICHAS_ORIGIN": origin, |
| 50 | "FICHAS_ALLOW_LOGGED_MAIL": "1", |
| 51 | }[k] |
| 52 | }) |
| 53 | if err != nil { |
| 54 | t.Fatalf("LoadConfig: %v", err) |
| 55 | } |
| 56 | mux, app, err := fichas.App(d, cfg, logger) |
| 57 | if err != nil { |
| 58 | t.Fatalf("App: %v", err) |
| 59 | } |
| 60 | au, err := fichas.NewAuth(d.Writer(), fichas.AuthEnv{Origin: cfg.Origin, InstanceKey: cfg.InstanceKey, Mailer: &capturingMailer{}}, logger, app) |
| 61 | if err != nil { |
| 62 | t.Fatalf("NewAuth: %v", err) |
| 63 | } |
| 64 | app.UseAuth(au) |
| 65 | core, err := sessions.New(sessions.Config{DB: d.Writer(), Origin: origin}) |
| 66 | if err != nil { |
| 67 | t.Fatal(err) |
| 68 | } |
| 69 | w := httptest.NewRecorder() |
| 70 | if err := core.SignIn(w, httptest.NewRequest(http.MethodGet, "/", nil), sessions.Session{Subject: TestOther, Method: "magiclink", AuthTime: time.Now()}); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | for _, c := range w.Result().Cookies() { |
| 74 | if c.Name == core.CookieName() { |
| 75 | refused = c.Value |
| 76 | } |
| 77 | } |
| 78 | return mux |
| 79 | }) |
| 80 | return rig, refused |
| 81 | } |
| 82 | |
| 83 | func TestSmartDoorInABrowser(t *testing.T) { |
| 84 | states := []struct { |
| 85 | name, path string |
| 86 | refused bool |
| 87 | }{ |
| 88 | {"field", "/signin", false}, |
| 89 | {"rate", "/signin?err=rate", false}, |
| 90 | {"address", "/signin?err=address", false}, |
| 91 | {"keymail", "/signin?err=keymail&force=1", false}, |
| 92 | {"expired", "/signin?err=expired", false}, |
| 93 | {"generic", "/signin?err=1", false}, |
| 94 | {"reauth", "/signin?reauth=1", false}, |
| 95 | {"sent", "/signin?sent=1", false}, |
| 96 | {"confirm", "/auth/verify?token=" + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA", false}, |
| 97 | {"signed-out", "/signed-out", false}, |
| 98 | {"refusal", "/signin", true}, |
| 99 | } |
| 100 | for _, width := range []int64{320, 1280} { |
| 101 | for _, st := range states { |
| 102 | rig, refused := doorRig(t) |
| 103 | var facts struct { |
| 104 | Scroll, Inner float64 |
| 105 | Small []string |
| 106 | Refusal, Door bool |
| 107 | } |
| 108 | steps := []chromedp.Action[chromedp.Void]{ |
| 109 | send(emulation.SetEmulatedMedia, emulation.SetEmulatedMediaParams{Features: []*emulation.MediaFeature{{Name: "prefers-color-scheme", Value: "light"}}}), |
| 110 | viewport(width), |
| 111 | } |
| 112 | if st.refused { |
| 113 | steps = append(steps, chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 114 | _, err := cdp.Call(ctx, target, network.SetCookie, network.SetCookieParams{Name: "rastrillo_session", Value: refused, URL: rig.Origin + "/"}) |
| 115 | return err |
| 116 | })) |
| 117 | } |
| 118 | steps = append(steps, |
| 119 | chromedp.Navigate(rig.Origin+st.path), |
| 120 | chromedp.WaitVisible(`[estilo-signin]`), |
| 121 | harness.Into(&facts, chromedp.Evaluate[struct { |
| 122 | Scroll, Inner float64 |
| 123 | Small []string |
| 124 | Refusal, Door bool |
| 125 | }](`({ |
| 126 | Scroll: document.documentElement.scrollWidth, |
| 127 | Inner: innerWidth, |
| 128 | Small: [...document.querySelectorAll('[estilo-signin] :is(a, button, input:not([type=hidden]))')] |
| 129 | .filter(e => e.offsetParent !== null) |
| 130 | .filter(e => { const r = e.getBoundingClientRect(); return r.height < 24 || r.width < 24 }) |
| 131 | .map(e => e.outerHTML.slice(0, 80)), |
| 132 | Refusal: document.body.textContent.includes('can’t sign in to Fichas'), |
| 133 | Door: !!document.querySelector('[estilo-door]'), |
| 134 | })`)), |
| 135 | ) |
| 136 | rig.Run(steps...) |
| 137 | if !facts.Door { |
| 138 | t.Errorf("%s at %d: not the door", st.name, width) |
| 139 | } |
| 140 | if facts.Scroll > facts.Inner { |
| 141 | t.Errorf("%s at %d: scrolls sideways (%.0f > %.0f)", st.name, width, facts.Scroll, facts.Inner) |
| 142 | } |
| 143 | for _, s := range facts.Small { |
| 144 | t.Errorf("%s at %d: a target under 24px: %s", st.name, width, s) |
| 145 | } |
| 146 | if facts.Refusal != st.refused { |
| 147 | t.Errorf("%s at %d: refusal drawn %v, want %v", st.name, width, facts.Refusal, st.refused) |
| 148 | } |
| 149 | axeDoor(t, rig, width) |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | // axeDoor runs axe on the page the rig is on, as axeCheckWith does for |
| 155 | // the app's own pages. |
| 156 | func axeDoor(t *testing.T, rig *harness.Rig, width int64) { |
| 157 | t.Helper() |
| 158 | found := axeHere(t, rig) |
| 159 | for _, v := range found { |
| 160 | t.Errorf("door at %dpx: axe: %s", width, v) |
| 161 | } |
| 162 | } |
| 163 | |