| 1 | //go:build browser |
| 2 | |
| 3 | package browsertest |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "net/http" |
| 8 | "testing" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/chromedp/cdproto/cdp" |
| 12 | "github.com/chromedp/cdproto/emulation" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | |
| 15 | "amadan.net/rastrillo/rastrillo/harness" |
| 16 | ) |
| 17 | |
| 18 | // passkeyDoor is the sign-in page's passkey button; addPasskey is Settings'. |
| 19 | const ( |
| 20 | passkeyDoor = `[data-rst-passkey]` |
| 21 | addPasskey = `[data-passkey-add]` |
| 22 | ) |
| 23 | |
| 24 | // newPasskeyBase serves Base on the rig's own origin, http://localhost:PORT: |
| 25 | // the one origin a WebAuthn ceremony accepts without a certificate (an IP is |
| 26 | // not a relying party ID, and Chromium refuses WebAuthn on a page with a |
| 27 | // certificate error). |
| 28 | func newPasskeyBase(t *testing.T) (*harness.Rig, *baseApp) { |
| 29 | t.Helper() |
| 30 | var b *baseApp |
| 31 | rig := newRig(t, func(origin string) http.Handler { |
| 32 | b = newBase(t, baseConfig(origin)) |
| 33 | return b.handler |
| 34 | }) |
| 35 | return rig, b |
| 36 | } |
| 37 | |
| 38 | // The owner signs in by email, adds a passkey from Settings, signs out, and |
| 39 | // signs back in with the passkey alone: no second email is sent. |
| 40 | func TestPasskeyRegisterThenSignIn(t *testing.T) { |
| 41 | rig, b := newPasskeyBase(t) |
| 42 | signInByEmail(t, rig, rig.Origin, b.mail) |
| 43 | |
| 44 | drive(t, rig, chromedp.Navigate(rig.Origin+"/settings"), chromedp.WaitVisible(chromedp.CSS(addPasskey))) |
| 45 | rig.Screen(".sheet", "Settings") |
| 46 | if got := harness.Evaluate[string](rig, `document.querySelector("#passkeys").textContent`); got != "None yet" { |
| 47 | t.Fatalf("before adding one, Settings lists passkeys as %q", got) |
| 48 | } |
| 49 | drive(t, rig, chromedp.Click(chromedp.CSS(addPasskey)), chromedp.WaitVisible(chromedp.CSS(`#passkey-added`))) |
| 50 | rig.Screen(".sheet", "Settings, passkey added") |
| 51 | if got := harness.Evaluate[string](rig, `document.querySelector("#passkeys").textContent`); got != "1 added" { |
| 52 | t.Fatalf("after adding one, Settings lists passkeys as %q", got) |
| 53 | } |
| 54 | |
| 55 | drive(t, rig, chromedp.Click(chromedp.CSS(`form[action="/auth/signout"] button`)), chromedp.WaitVisible(chromedp.CSS(passkeyDoor))) |
| 56 | rig.Screen(".card", "the sign-in page after signing out") |
| 57 | if here := harness.Read(rig, chromedp.Location()); here != rig.Origin+"/signin" { |
| 58 | t.Fatalf("signing out ended at %s", here) |
| 59 | } |
| 60 | mails := b.mail.count() |
| 61 | |
| 62 | drive(t, rig, chromedp.Click(chromedp.CSS(passkeyDoor)), chromedp.WaitVisible(chromedp.CSS(".hello"))) |
| 63 | rig.Screen("main", "Home, signed in with a passkey") |
| 64 | if here := harness.Read(rig, chromedp.Location()); here != rig.Origin+"/" { |
| 65 | t.Fatalf("the passkey sign-in ended at %s", here) |
| 66 | } |
| 67 | if n := b.mail.count(); n != mails { |
| 68 | t.Fatalf("the passkey sign-in sent %d emails", n-mails) |
| 69 | } |
| 70 | var method string |
| 71 | if err := b.db.QueryRow(`SELECT method FROM sessions ORDER BY created_at DESC, rowid DESC LIMIT 1`).Scan(&method); err != nil { |
| 72 | t.Fatal(err) |
| 73 | } |
| 74 | if method != "passkey" { |
| 75 | t.Fatalf("the newest session was signed in by %q, want passkey", method) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // With scripts off there is no passkey button anywhere, and email sign-in |
| 80 | // still works. |
| 81 | func TestPasskeyButtonsHiddenWithoutScripts(t *testing.T) { |
| 82 | rig, b := newPasskeyBase(t) |
| 83 | drive(t, rig, scriptsDisabled(true)) |
| 84 | |
| 85 | // Half a second is what scripts would need to reveal the button, so a |
| 86 | // hidden one is the scripts' absence, not a read taken too soon. |
| 87 | drive(t, rig, chromedp.Navigate(rig.Origin+"/signin"), chromedp.WaitVisible(chromedp.CSS(`input[name="address"]`)), |
| 88 | chromedp.Sleep(500*time.Millisecond)) |
| 89 | if harness.Evaluate[bool](rig, `document.querySelector("`+passkeyDoor+`").checkVisibility()`) { |
| 90 | t.Fatal("with scripts off the sign-in page shows the passkey button") |
| 91 | } |
| 92 | |
| 93 | signInByEmail(t, rig, rig.Origin, b.mail) |
| 94 | |
| 95 | drive(t, rig, chromedp.Navigate(rig.Origin+"/settings"), chromedp.WaitVisible(chromedp.CSS("#passkeys")), |
| 96 | chromedp.Sleep(500*time.Millisecond)) |
| 97 | rig.Screen(".sheet", "Settings, scripts off") |
| 98 | if harness.Evaluate[bool](rig, `document.querySelector("`+addPasskey+`").checkVisibility()`) { |
| 99 | t.Fatal("with scripts off Settings shows the add-a-passkey button") |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | // scriptsDisabled switches the page's own scripts off or on. DevTools' |
| 104 | // evaluations still run. |
| 105 | func scriptsDisabled(off bool) chromedp.Action[chromedp.Void] { |
| 106 | return chromedp.Func(func(ctx context.Context, target *chromedp.Target) error { |
| 107 | _, err := cdp.Call(ctx, target, emulation.SetScriptExecutionDisabled, |
| 108 | emulation.SetScriptExecutionDisabledParams{Value: off}) |
| 109 | return err |
| 110 | }) |
| 111 | } |
| 112 | |