|
| 1 | +import type { ClerkClient } from '@clerk/backend'; |
| 2 | +import { createPageObjects } from '@clerk/testing/playwright/unstable'; |
| 3 | + |
| 4 | +import { appConfigs } from '../../presets'; |
| 5 | +import { createTestUtils } from '../../testUtils'; |
| 6 | +import { createEmailService } from '../../testUtils/emailService'; |
| 7 | +import type { FakeUser, UserService } from '../../testUtils/usersService'; |
| 8 | +import { expect, test as extensionTest } from './fixtures'; |
| 9 | + |
| 10 | +for (const environmentType of ['development', 'production'] as const) { |
| 11 | + const test = extensionTest.extend({ |
| 12 | + extensionEnv: [ |
| 13 | + environmentType === 'production' ? appConfigs.envs.sessionsProd1 : appConfigs.envs.withEmailCodes, |
| 14 | + { scope: 'worker' }, |
| 15 | + ], |
| 16 | + extensionTestingToken: [false, { scope: 'worker' }], |
| 17 | + }); |
| 18 | + |
| 19 | + test.describe(`chrome extension ${environmentType} auth @chrome-extension`, () => { |
| 20 | + test.describe.configure({ mode: 'serial' }); |
| 21 | + |
| 22 | + let services: { clerk: ClerkClient; users: UserService }; |
| 23 | + let user: FakeUser; |
| 24 | + let addedOrigin: string | undefined; |
| 25 | + |
| 26 | + test.beforeAll(async ({ extensionApp }) => { |
| 27 | + const publishableKey = extensionApp.env.publicVariables.get('CLERK_PUBLISHABLE_KEY'); |
| 28 | + expect(publishableKey?.startsWith(environmentType === 'production' ? 'pk_live_' : 'pk_test_')).toBe(true); |
| 29 | + ({ services } = await createTestUtils({ app: extensionApp })); |
| 30 | + }); |
| 31 | + |
| 32 | + test.beforeEach(async ({ extensionId }) => { |
| 33 | + user = services.users.createFakeUser(test, { |
| 34 | + fictionalEmail: environmentType === 'development', |
| 35 | + }); |
| 36 | + |
| 37 | + if (environmentType === 'production') { |
| 38 | + const origin = `chrome-extension://${extensionId}`; |
| 39 | + const instance = await services.clerk.instance.get(); |
| 40 | + if (!instance.allowedOrigins?.includes(origin)) { |
| 41 | + await services.clerk.instance.update({ allowedOrigins: [...(instance.allowedOrigins || []), origin] }); |
| 42 | + addedOrigin = origin; |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + test.afterEach(async () => { |
| 48 | + try { |
| 49 | + await user?.deleteIfExists(); |
| 50 | + } finally { |
| 51 | + if (addedOrigin) { |
| 52 | + const instance = await services.clerk.instance.get(); |
| 53 | + await services.clerk.instance.update({ |
| 54 | + allowedOrigins: (instance.allowedOrigins || []).filter(origin => origin !== addedOrigin), |
| 55 | + }); |
| 56 | + addedOrigin = undefined; |
| 57 | + } |
| 58 | + } |
| 59 | + }); |
| 60 | + |
| 61 | + test('signs in with email and password without a Testing Token', async ({ extensionPage }) => { |
| 62 | + await services.users.createBapiUser(user); |
| 63 | + const { signIn } = createPageObjects({ page: extensionPage, useTestingToken: false }); |
| 64 | + await signIn.waitForMounted(); |
| 65 | + await signIn.setIdentifier(user.email!); |
| 66 | + await signIn.continue(); |
| 67 | + await signIn.getPasswordInput().fill(user.password); |
| 68 | + await signIn.continue(); |
| 69 | + await expect(extensionPage.getByTestId('user-id')).toHaveText(/^user_/); |
| 70 | + await expect(extensionPage.getByTestId('session-id')).toHaveText(/^sess_/); |
| 71 | + }); |
| 72 | + |
| 73 | + test('signs up with email and password without a Testing Token', async ({ extensionPage }) => { |
| 74 | + await extensionPage.getByRole('button', { name: 'Create account', exact: true }).click(); |
| 75 | + const { signUp } = createPageObjects({ page: extensionPage, useTestingToken: false }); |
| 76 | + await signUp.waitForMounted(); |
| 77 | + await signUp.signUpWithEmailAndPassword({ email: user.email, password: user.password }); |
| 78 | + |
| 79 | + if (environmentType === 'development') { |
| 80 | + await signUp.enterTestOtpCode(); |
| 81 | + } else { |
| 82 | + const code = await createEmailService().getCodeForEmailAddress(user.email!); |
| 83 | + await signUp.enterOtpCode(code); |
| 84 | + } |
| 85 | + |
| 86 | + await expect(extensionPage.getByTestId('user-id')).toHaveText(/^user_/); |
| 87 | + await expect(extensionPage.getByTestId('session-id')).toHaveText(/^sess_/); |
| 88 | + }); |
| 89 | + }); |
| 90 | +} |
0 commit comments