|
| 1 | +import type { OrganizationMembershipRole } from '@clerk/backend'; |
| 2 | +import { expect, test } from '@playwright/test'; |
| 3 | + |
| 4 | +import { appConfigs } from '../presets'; |
| 5 | +import type { FakeOrganization, FakeUser } from '../testUtils'; |
| 6 | +import { createTestUtils, testAgainstRunningApps } from '../testUtils'; |
| 7 | + |
| 8 | +const utils = [ |
| 9 | + 'action', |
| 10 | + // , 'route' |
| 11 | +]; |
| 12 | +const capitalize = (type: string) => type[0].toUpperCase() + type.slice(1); |
| 13 | +testAgainstRunningApps({ withEnv: [appConfigs.envs.withReverification] })( |
| 14 | + '@nextjs require re-verification', |
| 15 | + ({ app }) => { |
| 16 | + test.describe.configure({ mode: 'parallel' }); |
| 17 | + |
| 18 | + let fakeAdmin: FakeUser; |
| 19 | + let fakeViewer: FakeUser; |
| 20 | + let fakeOrganization: FakeOrganization; |
| 21 | + |
| 22 | + test.beforeAll(async () => { |
| 23 | + const m = createTestUtils({ app }); |
| 24 | + fakeAdmin = m.services.users.createFakeUser(); |
| 25 | + const admin = await m.services.users.createBapiUser(fakeAdmin); |
| 26 | + fakeOrganization = await m.services.users.createFakeOrganization(admin.id); |
| 27 | + fakeViewer = m.services.users.createFakeUser(); |
| 28 | + const viewer = await m.services.users.createBapiUser(fakeViewer); |
| 29 | + await m.services.clerk.organizations.createOrganizationMembership({ |
| 30 | + organizationId: fakeOrganization.organization.id, |
| 31 | + role: 'org:viewer' as OrganizationMembershipRole, |
| 32 | + userId: viewer.id, |
| 33 | + }); |
| 34 | + }); |
| 35 | + |
| 36 | + test.afterAll(async () => { |
| 37 | + await fakeOrganization.delete(); |
| 38 | + await fakeViewer.deleteIfExists(); |
| 39 | + await fakeAdmin.deleteIfExists(); |
| 40 | + await app.teardown(); |
| 41 | + }); |
| 42 | + |
| 43 | + utils.forEach(type => { |
| 44 | + test(`reverification error from ${capitalize(type)}`, async ({ page, context }) => { |
| 45 | + test.setTimeout(270_000); |
| 46 | + const u = createTestUtils({ app, page, context }); |
| 47 | + |
| 48 | + await u.po.signIn.goTo(); |
| 49 | + await u.po.signIn.waitForMounted(); |
| 50 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password }); |
| 51 | + await u.po.expect.toBeSignedIn(); |
| 52 | + |
| 53 | + await u.po.organizationSwitcher.goTo(); |
| 54 | + await u.po.organizationSwitcher.waitForMounted(); |
| 55 | + await u.po.organizationSwitcher.waitForAnOrganizationToSelected(); |
| 56 | + |
| 57 | + await u.page.goToRelative(`/requires-re-verification`); |
| 58 | + await u.page.getByRole('button', { name: /LogUserId/i }).click(); |
| 59 | + await expect(u.page.getByText(/\{\s*"userId"\s*:\s*"user_[^"]+"\s*\}/i)).toBeVisible(); |
| 60 | + |
| 61 | + const total = 1000 * 120; |
| 62 | + await page.waitForTimeout(total / 3); |
| 63 | + await page.waitForTimeout(total / 3); |
| 64 | + await u.po.userProfile.goTo(); |
| 65 | + await page.waitForTimeout(total / 3); |
| 66 | + await u.page.goToRelative(`/requires-re-verification`); |
| 67 | + await u.page.getByRole('button', { name: /LogUserId/i }).click(); |
| 68 | + await expect( |
| 69 | + u.page.getByText( |
| 70 | + /\{\s*"clerk_error"\s*:\s*\{\s*"type"\s*:\s*"forbidden"\s*,\s*"reason"\s*:\s*"reverification-mismatch"\s*,\s*"metadata"\s*:\s*\{\s*"reverification"\s*:\s*\{\s*"level"\s*:\s*"secondFactor"\s*,\s*"afterMinutes"\s*:\s*1\s*\}\s*\}\s*\}\s*\}/i, |
| 71 | + ), |
| 72 | + ).toBeVisible(); |
| 73 | + }); |
| 74 | + |
| 75 | + test(`reverification recovery from ${capitalize(type)}`, async ({ page, context }) => { |
| 76 | + test.setTimeout(270_000); |
| 77 | + const u = createTestUtils({ app, page, context }); |
| 78 | + |
| 79 | + await u.po.signIn.goTo(); |
| 80 | + await u.po.signIn.waitForMounted(); |
| 81 | + await u.po.signIn.signInWithEmailAndInstantPassword({ email: fakeAdmin.email, password: fakeAdmin.password }); |
| 82 | + await u.po.expect.toBeSignedIn(); |
| 83 | + |
| 84 | + await u.po.organizationSwitcher.goTo(); |
| 85 | + await u.po.organizationSwitcher.waitForMounted(); |
| 86 | + await u.po.organizationSwitcher.waitForAnOrganizationToSelected(); |
| 87 | + |
| 88 | + await u.page.goToRelative(`/requires-re-verification`); |
| 89 | + await u.page.getByRole('button', { name: /LogUserId/i }).click(); |
| 90 | + await expect(u.page.getByText(/\{\s*"userId"\s*:\s*"user_[^"]+"\s*\}/i)).toBeVisible(); |
| 91 | + |
| 92 | + const total = 1000 * 120; |
| 93 | + await page.waitForTimeout(total / 3); |
| 94 | + await page.waitForTimeout(total / 3); |
| 95 | + await u.po.userProfile.goTo(); |
| 96 | + await page.waitForTimeout(total / 3); |
| 97 | + await u.page.goToRelative(`/action-with-use-reverification`); |
| 98 | + await u.po.expect.toBeSignedIn(); |
| 99 | + await u.page.getByRole('button', { name: /LogUserId/i }).click(); |
| 100 | + await u.po.userVerification.waitForMounted(); |
| 101 | + }); |
| 102 | + }); |
| 103 | + }, |
| 104 | +); |
0 commit comments