|
| 1 | +import { test, expect, generateTestUser, createAndLoginUser } from '../../src/fixtures'; |
| 2 | + |
| 3 | +test.describe('MFA', () => { |
| 4 | + test.describe('Challenge Page', () => { |
| 5 | + test('should render the MFA WebAuthn challenge page structure', async ({ |
| 6 | + page, |
| 7 | + testApiClient, |
| 8 | + cleanupEmails, |
| 9 | + }) => { |
| 10 | + // Login first so we have a session (page requires auth when MFA is disabled) |
| 11 | + const user = generateTestUser('mfa-page'); |
| 12 | + cleanupEmails.push(user.email); |
| 13 | + |
| 14 | + await createAndLoginUser(page, testApiClient, user); |
| 15 | + |
| 16 | + // Navigate to the challenge page |
| 17 | + await page.goto('/user/mfa/webauthn-challenge.html'); |
| 18 | + await page.waitForLoadState('domcontentloaded'); |
| 19 | + |
| 20 | + // Verify page structure |
| 21 | + await expect(page.locator('.card-header')).toContainText('Additional Verification Required'); |
| 22 | + await expect(page.locator('#verifyPasskeyBtn')).toBeVisible(); |
| 23 | + }); |
| 24 | + |
| 25 | + test('should have a cancel/sign out option', async ({ |
| 26 | + page, |
| 27 | + testApiClient, |
| 28 | + cleanupEmails, |
| 29 | + }) => { |
| 30 | + const user = generateTestUser('mfa-cancel'); |
| 31 | + cleanupEmails.push(user.email); |
| 32 | + |
| 33 | + await createAndLoginUser(page, testApiClient, user); |
| 34 | + |
| 35 | + await page.goto('/user/mfa/webauthn-challenge.html'); |
| 36 | + await page.waitForLoadState('domcontentloaded'); |
| 37 | + |
| 38 | + // Verify cancel/sign out button is present (inside the logout form) |
| 39 | + await page.waitForLoadState('networkidle'); |
| 40 | + await expect( |
| 41 | + page.locator('form[action*="logout"] button[type="submit"]') |
| 42 | + ).toBeVisible(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + test.describe('MFA Status Endpoint', () => { |
| 47 | + test('should handle MFA status request for authenticated user', async ({ |
| 48 | + page, |
| 49 | + testApiClient, |
| 50 | + cleanupEmails, |
| 51 | + }) => { |
| 52 | + const user = generateTestUser('mfa-status'); |
| 53 | + cleanupEmails.push(user.email); |
| 54 | + |
| 55 | + await createAndLoginUser(page, testApiClient, user); |
| 56 | + |
| 57 | + // Call the MFA status endpoint |
| 58 | + const response = await page.request.get('/user/mfa/status'); |
| 59 | + |
| 60 | + // With MFA disabled in playwright-test profile, expect 404 |
| 61 | + // With MFA enabled, expect 200 with proper response shape |
| 62 | + expect([200, 404]).toContain(response.status()); |
| 63 | + |
| 64 | + if (response.status() === 200) { |
| 65 | + const body = await response.json(); |
| 66 | + expect(typeof body.mfaEnabled).toBe('boolean'); |
| 67 | + expect(typeof body.fullyAuthenticated).toBe('boolean'); |
| 68 | + } |
| 69 | + }); |
| 70 | + |
| 71 | + test('should require authentication for MFA status endpoint', async ({ page }) => { |
| 72 | + // Call without authentication |
| 73 | + const response = await page.request.get('/user/mfa/status', { |
| 74 | + maxRedirects: 0, |
| 75 | + }); |
| 76 | + |
| 77 | + // Should not return 200 for unauthenticated request |
| 78 | + // Expect redirect to login (302/303) or error (401/403) or 404 (MFA disabled) |
| 79 | + expect([302, 303, 401, 403, 404]).toContain(response.status()); |
| 80 | + }); |
| 81 | + }); |
| 82 | +}); |
0 commit comments