|
| 1 | +import { expect, Page, test } from '@playwright/test'; |
| 2 | + |
| 3 | +import { TEST_CONFIG } from '@/e2e-tests/config/test-config'; |
| 4 | +import { cleanupTestUserFromDB, createCredentialsTestUser } from '@/e2e-tests/helpers/helper-functions'; |
| 5 | +import { cleanupMailsacInbox, getEmailContent } from '@/e2e-tests/helpers/mailsac/mailsac'; |
| 6 | +import { fillLoginForm } from '@/e2e-tests/helpers/tests'; |
| 7 | + |
| 8 | +test.describe('2FA Authentication Flow', () => { |
| 9 | + const { MAILSAC_API_KEY, TEST_EMAIL, TEST_PASSWORD, TEST_NAME } = TEST_CONFIG; |
| 10 | + |
| 11 | + async function cleanupState() { |
| 12 | + await cleanupTestUserFromDB(TEST_EMAIL); |
| 13 | + const mailsacResponseStatus = await cleanupMailsacInbox(TEST_EMAIL, MAILSAC_API_KEY); |
| 14 | + expect(mailsacResponseStatus).toBe(204); |
| 15 | + } |
| 16 | + |
| 17 | + async function createTwoFactorUser() { |
| 18 | + await createCredentialsTestUser(TEST_NAME, TEST_EMAIL, TEST_PASSWORD, { |
| 19 | + isTwoFactorEnabled: true, |
| 20 | + emailVerified: true, |
| 21 | + }); |
| 22 | + } |
| 23 | + |
| 24 | + async function initiateLogin(page: Page) { |
| 25 | + await page.goto('/login'); |
| 26 | + await fillLoginForm(page, { |
| 27 | + email: TEST_EMAIL, |
| 28 | + password: TEST_PASSWORD, |
| 29 | + }); |
| 30 | + await page.locator('button[type="submit"]').click(); |
| 31 | + } |
| 32 | + |
| 33 | + async function getTwoFactorCode(): Promise<string> { |
| 34 | + const emailContent = await getEmailContent(TEST_EMAIL, MAILSAC_API_KEY, '2FA Code', { |
| 35 | + retries: 5, |
| 36 | + delay: 2000, |
| 37 | + exactMatch: false, |
| 38 | + }); |
| 39 | + |
| 40 | + const twoFactorCode = emailContent.match(/(\d{6})/)?.[1]; |
| 41 | + |
| 42 | + if (!twoFactorCode) { |
| 43 | + throw new Error('Could not extract 2FA code from email'); |
| 44 | + } |
| 45 | + |
| 46 | + return twoFactorCode; |
| 47 | + } |
| 48 | + |
| 49 | + async function submitTwoFactorCode(page: Page, code: string) { |
| 50 | + await page.locator('input[name="code"]').fill(code); |
| 51 | + await page.locator('button[type="submit"]').click(); |
| 52 | + } |
| 53 | + |
| 54 | + test('should successfully authenticate user with valid 2FA code', async ({ page }) => { |
| 55 | + await test.step('Setup test environment', async () => { |
| 56 | + await cleanupState(); |
| 57 | + await createTwoFactorUser(); |
| 58 | + }); |
| 59 | + |
| 60 | + await test.step('Initiate login process', async () => { |
| 61 | + await initiateLogin(page); |
| 62 | + }); |
| 63 | + |
| 64 | + await test.step('Process 2FA verification', async () => { |
| 65 | + const twoFactorCode = await getTwoFactorCode(); |
| 66 | + await submitTwoFactorCode(page, twoFactorCode); |
| 67 | + await page.waitForURL('**/settings'); |
| 68 | + await expect(page).toHaveURL('/settings'); |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + test('should reject login attempt with invalid 2FA code', async ({ page }) => { |
| 73 | + await test.step('Setup test environment', async () => { |
| 74 | + await cleanupState(); |
| 75 | + await createTwoFactorUser(); |
| 76 | + }); |
| 77 | + |
| 78 | + await test.step('Attempt login with invalid 2FA code', async () => { |
| 79 | + await initiateLogin(page); |
| 80 | + await submitTwoFactorCode(page, '000000'); |
| 81 | + await expect(page.getByText('Invalid code')).toBeVisible(); |
| 82 | + }); |
| 83 | + }); |
| 84 | +}); |
0 commit comments