|
| 1 | +import type { Page } from '@playwright/test'; |
| 2 | +import { expect, test } from '@playwright/test'; |
| 3 | + |
| 4 | +import type { LastAuthenticationStrategy } from '../../packages/types'; |
| 5 | +import { appConfigs } from '../presets'; |
| 6 | +import { createTestUtils, testAgainstRunningApps } from '../testUtils'; |
| 7 | + |
| 8 | +const mockLastAuthenticationStrategyResponse = async ( |
| 9 | + page: Page, |
| 10 | + lastAuthenticationStrategy: LastAuthenticationStrategy | null | undefined, |
| 11 | +) => { |
| 12 | + await page.route('**/v1/client?**', async route => { |
| 13 | + const response = await route.fetch(); |
| 14 | + const json = await response.json(); |
| 15 | + let modifiedJson = json; |
| 16 | + |
| 17 | + if (lastAuthenticationStrategy === undefined && json.response.last_authentication_strategy) { |
| 18 | + delete json.response['last_authentication_strategy']; |
| 19 | + modifiedJson = json; |
| 20 | + } else { |
| 21 | + modifiedJson = { |
| 22 | + ...json, |
| 23 | + response: { |
| 24 | + ...json.response, |
| 25 | + last_authentication_strategy: lastAuthenticationStrategy, |
| 26 | + }, |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + await route.fulfill({ response, json: modifiedJson }); |
| 31 | + }); |
| 32 | +}; |
| 33 | + |
| 34 | +testAgainstRunningApps({ withEnv: [appConfigs.envs.withEmailCodes] })( |
| 35 | + 'lastAuthenticationStrategy @generic', |
| 36 | + ({ app }) => { |
| 37 | + test.afterAll(async () => { |
| 38 | + await app.teardown(); |
| 39 | + }); |
| 40 | + |
| 41 | + // TODO(Tom): Remove once the API-side of this feature is fully released. [2025-09-07] |
| 42 | + test('should not show "Last used" badge when lastAuthenticationStrategy is not present', async ({ |
| 43 | + page, |
| 44 | + context, |
| 45 | + }) => { |
| 46 | + const u = createTestUtils({ app, page, context }); |
| 47 | + await mockLastAuthenticationStrategyResponse(page, undefined); |
| 48 | + |
| 49 | + await u.po.signIn.goTo(); |
| 50 | + await u.po.signIn.waitForMounted(); |
| 51 | + |
| 52 | + // Ensure no "Last used" badge is present. |
| 53 | + await expect(page.locator('.cl-lastAuthenticationStrategyBadge')).toHaveCount(0); |
| 54 | + |
| 55 | + // Ensure none of the social buttons have been pulled to the first row. |
| 56 | + const socialButtonContainers = u.page.locator('.cl-socialButtons'); |
| 57 | + await expect(socialButtonContainers).toHaveCount(1); |
| 58 | + await expect(socialButtonContainers.first().locator('.cl-button')).toHaveCount(3); |
| 59 | + }); |
| 60 | + |
| 61 | + test('should not show "Last used" badge when lastAuthenticationStrategy is null', async ({ page, context }) => { |
| 62 | + const u = createTestUtils({ app, page, context }); |
| 63 | + await mockLastAuthenticationStrategyResponse(page, null); |
| 64 | + |
| 65 | + await u.po.signIn.goTo(); |
| 66 | + await u.po.signIn.waitForMounted(); |
| 67 | + |
| 68 | + // Ensure no "Last used" badge is present. |
| 69 | + await expect(page.locator('.cl-lastAuthenticationStrategyBadge')).toHaveCount(0); |
| 70 | + |
| 71 | + // Ensure none of the social buttons have been pulled to the first row. |
| 72 | + const socialButtonContainers = u.page.locator('.cl-socialButtons'); |
| 73 | + await expect(socialButtonContainers).toHaveCount(1); |
| 74 | + await expect(socialButtonContainers.first().locator('.cl-button')).toHaveCount(3); |
| 75 | + }); |
| 76 | + |
| 77 | + test('should show "Last used" badge when lastAuthenticationStrategy is oauth_google', async ({ page, context }) => { |
| 78 | + const u = createTestUtils({ app, page, context }); |
| 79 | + await mockLastAuthenticationStrategyResponse(page, 'oauth_google'); |
| 80 | + |
| 81 | + await u.po.signIn.goTo(); |
| 82 | + await u.po.signIn.waitForMounted(); |
| 83 | + |
| 84 | + // Ensure "Last used" badge is present. |
| 85 | + const lastUsedBadge = page.locator('.cl-lastAuthenticationStrategyBadge'); |
| 86 | + await expect(lastUsedBadge).toBeVisible(); |
| 87 | + await expect(lastUsedBadge).toHaveCount(1); |
| 88 | + |
| 89 | + const btn = page.getByRole('button', { name: 'Last used Sign in with Google' }); |
| 90 | + await expect(btn).toBeVisible(); |
| 91 | + |
| 92 | + // Ensure the last used social button has been pulled to the first row. |
| 93 | + const socialButtonContainers = u.page.locator('.cl-socialButtons'); |
| 94 | + await expect(socialButtonContainers).toHaveCount(2); |
| 95 | + await expect(socialButtonContainers.first().locator('.cl-button__google')).toHaveCount(1); |
| 96 | + await expect(socialButtonContainers.last().locator('.cl-button')).toHaveCount(2); |
| 97 | + }); |
| 98 | + |
| 99 | + test('should show "Last used" badge when lastAuthenticationStrategy is email_address and identifier is toggleable', async ({ |
| 100 | + page, |
| 101 | + context, |
| 102 | + }) => { |
| 103 | + const u = createTestUtils({ app, page, context }); |
| 104 | + await mockLastAuthenticationStrategyResponse(page, 'email_address'); |
| 105 | + |
| 106 | + await u.po.signIn.goTo(); |
| 107 | + await u.po.signIn.waitForMounted(); |
| 108 | + |
| 109 | + // Ensure "Last used" badge is not present. |
| 110 | + const lastUsedBadge = page.locator('.cl-lastAuthenticationStrategyBadge'); |
| 111 | + await expect(lastUsedBadge).toHaveCount(0); |
| 112 | + |
| 113 | + // Ensure none of the social buttons have been pulled to the first row. |
| 114 | + const socialButtonContainers = u.page.locator('.cl-socialButtons'); |
| 115 | + await expect(socialButtonContainers).toHaveCount(1); |
| 116 | + await expect(socialButtonContainers.first().locator('.cl-button')).toHaveCount(3); |
| 117 | + }); |
| 118 | + }, |
| 119 | +); |
0 commit comments