|
| 1 | +import { createClerkClient } from '@clerk/backend'; |
| 2 | +import { expect, test } from '@playwright/test'; |
| 3 | + |
| 4 | +import type { Application } from '../../models/application'; |
| 5 | +import { appConfigs } from '../../presets'; |
| 6 | +import { instanceKeys } from '../../presets/envs'; |
| 7 | +import type { FakeUser } from '../../testUtils'; |
| 8 | +import { createTestUtils } from '../../testUtils'; |
| 9 | +import { createUserService } from '../../testUtils/usersService'; |
| 10 | + |
| 11 | +test.describe('Custom Flows OAuth @custom', () => { |
| 12 | + test.describe.configure({ mode: 'serial' }); |
| 13 | + |
| 14 | + let app: Application; |
| 15 | + let fakeUser: FakeUser; |
| 16 | + |
| 17 | + test.beforeAll(async () => { |
| 18 | + test.setTimeout(150_000); |
| 19 | + app = await appConfigs.customFlows.reactVite.clone().commit(); |
| 20 | + await app.setup(); |
| 21 | + await app.withEnv(appConfigs.envs.withEmailCodes); |
| 22 | + await app.dev(); |
| 23 | + |
| 24 | + const client = createClerkClient({ |
| 25 | + secretKey: instanceKeys.get('oauth-provider').sk, |
| 26 | + publishableKey: instanceKeys.get('oauth-provider').pk, |
| 27 | + }); |
| 28 | + const users = createUserService(client); |
| 29 | + fakeUser = users.createFakeUser({ withUsername: true }); |
| 30 | + await users.createBapiUser(fakeUser); |
| 31 | + }); |
| 32 | + |
| 33 | + test.afterAll(async () => { |
| 34 | + const u = createTestUtils({ app }); |
| 35 | + await fakeUser.deleteIfExists(); |
| 36 | + await u.services.users.deleteIfExists({ email: fakeUser.email }); |
| 37 | + await app.teardown(); |
| 38 | + }); |
| 39 | + |
| 40 | + test('SDK-75: retrying OAuth after an abandoned redirect creates a fresh sign-in', async ({ page, context }) => { |
| 41 | + const u = createTestUtils({ app, page, context }); |
| 42 | + |
| 43 | + // Block the OAuth provider redirect on the first attempt only. clerk-js sets |
| 44 | + // `firstFactorVerification.status='unverified'` and `externalVerificationRedirectURL` |
| 45 | + // the moment the POST resolves — before the navigation runs — so aborting the |
| 46 | + // navigation deterministically reproduces the SDK-75 abandoned-redirect state |
| 47 | + // without depending on browser back/BFCache semantics. |
| 48 | + let blockOnce = true; |
| 49 | + await page.route('**/oauth/authorize**', async route => { |
| 50 | + if (blockOnce && route.request().isNavigationRequest()) { |
| 51 | + blockOnce = false; |
| 52 | + await route.abort('aborted'); |
| 53 | + return; |
| 54 | + } |
| 55 | + await route.continue(); |
| 56 | + }); |
| 57 | + |
| 58 | + await u.page.goToRelative('/sign-in'); |
| 59 | + await u.page.waitForClerkJsLoaded(); |
| 60 | + |
| 61 | + const oauthButton = u.page.getByRole('button', { name: /^Sign in with / }); |
| 62 | + await oauthButton.first().waitFor(); |
| 63 | + |
| 64 | + // First attempt: capture the POST, then let the redirect get aborted. |
| 65 | + const firstPostPromise = page.waitForRequest( |
| 66 | + req => req.method() === 'POST' && /\/v1\/client\/sign_ins(\?|$)/.test(req.url()), |
| 67 | + ); |
| 68 | + await oauthButton.first().click(); |
| 69 | + await firstPostPromise; |
| 70 | + |
| 71 | + // The redirect was aborted, so we stay on the app's sign-in page with stale |
| 72 | + // OAuth state lingering in the SignIn resource. Wait for the OAuth button to |
| 73 | + // be re-enabled (fetchStatus settles back to 'idle' once the navigation aborts). |
| 74 | + await u.page.waitForURL(url => url.toString().startsWith(app.serverUrl) && url.pathname.includes('/sign-in')); |
| 75 | + await oauthButton.first().waitFor(); |
| 76 | + |
| 77 | + // Second attempt: must POST to /client/sign_ins again. If the previous reuse |
| 78 | + // logic kicked in (pre-fix), SignInFuture.sso would skip create and silently |
| 79 | + // no-op — so the second POST not happening is exactly the regression. |
| 80 | + const secondPostPromise = page.waitForRequest( |
| 81 | + req => req.method() === 'POST' && /\/v1\/client\/sign_ins(\?|$)/.test(req.url()), |
| 82 | + ); |
| 83 | + await oauthButton.first().click(); |
| 84 | + const secondPost = await secondPostPromise; |
| 85 | + expect(secondPost.method()).toBe('POST'); |
| 86 | + |
| 87 | + // Complete the OAuth flow end-to-end and assert we're signed in on the app instance. |
| 88 | + await u.page.getByText('Sign in to oauth-provider').waitFor(); |
| 89 | + await u.po.signIn.setIdentifier(fakeUser.email); |
| 90 | + await u.po.signIn.continue(); |
| 91 | + await u.po.signIn.enterTestOtpCode(); |
| 92 | + |
| 93 | + await u.page.waitForAppUrl('/protected'); |
| 94 | + await u.po.expect.toBeSignedIn(); |
| 95 | + }); |
| 96 | +}); |
0 commit comments