|
| 1 | +import type { ProtectConfigJSON } from '@clerk/shared/types'; |
| 2 | +import type { Page } from '@playwright/test'; |
| 3 | +import { expect, test } from '@playwright/test'; |
| 4 | + |
| 5 | +import { appConfigs } from '../presets'; |
| 6 | +import { createTestUtils, testAgainstRunningApps } from '../testUtils'; |
| 7 | + |
| 8 | +const mockProtectSettings = async (page: Page, config?: ProtectConfigJSON) => { |
| 9 | + await page.route('*/**/v1/environment*', async route => { |
| 10 | + const response = await route.fetch(); |
| 11 | + const json = await response.json(); |
| 12 | + const newJson = { |
| 13 | + ...json, |
| 14 | + ...(config ? { protect_config: config } : {}), |
| 15 | + }; |
| 16 | + await route.fulfill({ response, json: newJson }); |
| 17 | + }); |
| 18 | +}; |
| 19 | + |
| 20 | +testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })('Clerk Protect checks @generic', ({ app }) => { |
| 21 | + test.describe.configure({ mode: 'parallel' }); |
| 22 | + |
| 23 | + test.afterAll(async () => { |
| 24 | + await app.teardown(); |
| 25 | + }); |
| 26 | + |
| 27 | + test('should add loader script when protect_config.loader is set', async ({ page, context }) => { |
| 28 | + const u = createTestUtils({ app, page, context }); |
| 29 | + await mockProtectSettings(page, { |
| 30 | + object: 'protect_config', |
| 31 | + id: 'n', |
| 32 | + loaders: [ |
| 33 | + { |
| 34 | + rollout: 1.0, |
| 35 | + type: 'script', |
| 36 | + target: 'body', |
| 37 | + attributes: { id: 'test-protect-loader-1', type: 'module', src: 'data:application/json;base64,Cgo=' }, |
| 38 | + }, |
| 39 | + ], |
| 40 | + }); |
| 41 | + await u.page.goToAppHome(); |
| 42 | + await u.page.waitForClerkJsLoaded(); |
| 43 | + |
| 44 | + await expect(page.locator('#test-protect-loader-1')).toHaveAttribute('type', 'module'); |
| 45 | + }); |
| 46 | + |
| 47 | + test('should not add loader script when protect_config.loader is set and rollout 0.00', async ({ page, context }) => { |
| 48 | + const u = createTestUtils({ app, page, context }); |
| 49 | + await mockProtectSettings(page, { |
| 50 | + object: 'protect_config', |
| 51 | + id: 'n', |
| 52 | + loaders: [ |
| 53 | + { |
| 54 | + rollout: 0, // force 0% rollout, should not materialize |
| 55 | + type: 'script', |
| 56 | + target: 'body', |
| 57 | + attributes: { id: 'test-protect-loader-2', type: 'module', src: 'data:application/json;base64,Cgo=' }, |
| 58 | + }, |
| 59 | + ], |
| 60 | + }); |
| 61 | + await u.page.goToAppHome(); |
| 62 | + await u.page.waitForClerkJsLoaded(); |
| 63 | + |
| 64 | + await expect(page.locator('#test-protect-loader-2')).toHaveCount(0); |
| 65 | + }); |
| 66 | + |
| 67 | + test('should not create loader element when protect_config.loader is not set', async ({ page, context }) => { |
| 68 | + const u = createTestUtils({ app, page, context }); |
| 69 | + await mockProtectSettings(page); |
| 70 | + await u.page.goToAppHome(); |
| 71 | + await u.page.waitForClerkJsLoaded(); |
| 72 | + |
| 73 | + // Playwright locators are always objects, never undefined |
| 74 | + await expect(page.locator('#test-protect-loader')).toHaveCount(0); |
| 75 | + }); |
| 76 | +}); |
0 commit comments