|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { addVirtualAuthenticator } from './webauthn'; |
| 3 | + |
| 4 | +test('register a passkey in settings, then log in with it as the primary factor', async ({ |
| 5 | + page, |
| 6 | +}) => { |
| 7 | + await addVirtualAuthenticator(page); |
| 8 | + |
| 9 | + const email = `pw-passkey-${Date.now()}@example.com`; |
| 10 | + const password = 'TestPass123!'; |
| 11 | + |
| 12 | + await page.goto('/'); |
| 13 | + await page.getByText('Sign Up', { exact: true }).click(); |
| 14 | + await page.locator('#authorizer-sign-up-given_name').fill('Playwright'); |
| 15 | + await page.locator('#authorizer-sign-up-family_name').fill('Test'); |
| 16 | + await page.locator('#authorizer-sign-up-email_or_phone_number').fill(email); |
| 17 | + await page.locator('#authorizer-sign-up-password').fill(password); |
| 18 | + await page.locator('#authorizer-sign-up-confirmPassword').fill(password); |
| 19 | + await page.getByRole('button', { name: 'Sign Up' }).click(); |
| 20 | + |
| 21 | + // Passkey is never offered on the login-time offer screen (it needs a |
| 22 | + // bearer token webauthn_registration_options/_verify require, which |
| 23 | + // doesn't exist in the withheld-token state) - skip to reach the |
| 24 | + // dashboard, then register the passkey from settings instead. |
| 25 | + await page.getByRole('button', { name: 'Skip for now' }).click(); |
| 26 | + await expect(page.getByText('Hey 👋,')).toBeVisible({ timeout: 10_000 }); |
| 27 | + |
| 28 | + await page.getByRole('link', { name: 'Manage sign-in methods' }).click(); |
| 29 | + await page.getByRole('button', { name: 'Set up' }).click(); |
| 30 | + await page.getByRole('button', { name: 'Add a passkey' }).click(); |
| 31 | + // AuthorizerMFASetup wires onSuccess={backToList}, so a successful |
| 32 | + // registration immediately returns to the method list - there's no |
| 33 | + // stable success message to assert on, only the absence of an error and |
| 34 | + // the navigation back. |
| 35 | + await expect( |
| 36 | + page.getByRole('heading', { name: 'Manage sign-in methods' }), |
| 37 | + ).toBeVisible({ timeout: 10_000 }); |
| 38 | + await expect(page.getByText(/failed to verify passkey/i)).toHaveCount(0); |
| 39 | + |
| 40 | + await page.getByRole('link', { name: 'Back to dashboard' }).click(); |
| 41 | + await page.getByText('Logout', { exact: true }).click(); |
| 42 | + |
| 43 | + await page.getByRole('button', { name: 'Sign in with a passkey' }).click(); |
| 44 | + // The just-registered credential satisfies the account's MFA requirement |
| 45 | + // (webauthn credentials count as a verified factor) and setup was |
| 46 | + // skipped earlier, so this must log straight in - no MFA screen. |
| 47 | + await expect(page.getByText('Hey 👋,')).toBeVisible({ timeout: 10_000 }); |
| 48 | +}); |
| 49 | + |
| 50 | +test('passkey login button is hidden when MFA is enforced', async ({ |
| 51 | + page, |
| 52 | +}) => { |
| 53 | + const patchMeta = (json: any) => { |
| 54 | + const meta = json?.data?.meta ?? json; |
| 55 | + if (meta && typeof meta === 'object' && 'is_mfa_enforced' in meta) { |
| 56 | + meta.is_mfa_enforced = true; |
| 57 | + } |
| 58 | + return json; |
| 59 | + }; |
| 60 | + |
| 61 | + await page.route('**/v1/meta', async (route) => { |
| 62 | + const response = await route.fetch(); |
| 63 | + const json = patchMeta(await response.json()); |
| 64 | + await route.fulfill({ response, json }); |
| 65 | + }); |
| 66 | + await page.route('**/graphql', async (route) => { |
| 67 | + const postData = route.request().postDataJSON?.(); |
| 68 | + if (postData?.operationName !== 'meta') { |
| 69 | + await route.continue(); |
| 70 | + return; |
| 71 | + } |
| 72 | + const response = await route.fetch(); |
| 73 | + const json = patchMeta(await response.json()); |
| 74 | + await route.fulfill({ response, json }); |
| 75 | + }); |
| 76 | + |
| 77 | + await page.goto('/'); |
| 78 | + await page |
| 79 | + .locator('#authorizer-login-email-or-phone-number') |
| 80 | + .waitFor({ state: 'visible', timeout: 10_000 }); |
| 81 | + await expect( |
| 82 | + page.getByRole('button', { name: 'Sign in with a passkey' }), |
| 83 | + ).toHaveCount(0); |
| 84 | +}); |
0 commit comments