|
| 1 | +import { test, expect } from '@playwright/test'; |
| 2 | +import { TOTP } from 'otpauth'; |
| 3 | +import { newUser, registerViaUi, loginViaUi } from './helpers'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Builds a TOTP code from a base32 secret using the same defaults as the backend |
| 7 | + * (SHA1, 6 digits, 30s window). |
| 8 | + */ |
| 9 | +function totp(secret: string): string { |
| 10 | + return new TOTP({ |
| 11 | + secret: secret.replace(/\s+/g, ''), |
| 12 | + algorithm: 'SHA1', |
| 13 | + digits: 6, |
| 14 | + period: 30 |
| 15 | + }).generate(); |
| 16 | +} |
| 17 | + |
| 18 | +test('un caregiver può attivare la 2FA e poi accedere usando il codice TOTP', async ({ browser }) => { |
| 19 | + const setupCtx = await browser.newContext(); |
| 20 | + const setupPage = await setupCtx.newPage(); |
| 21 | + |
| 22 | + const user = newUser('twofa'); |
| 23 | + await registerViaUi(setupPage, user); |
| 24 | + |
| 25 | + await setupPage.goto('/account'); |
| 26 | + await expect(setupPage.getByRole('heading', { name: /autenticazione a due fattori/i })).toBeVisible(); |
| 27 | + |
| 28 | + // Start setup |
| 29 | + await setupPage.getByRole('button', { name: /attiva 2fa/i }).click(); |
| 30 | + |
| 31 | + // The secret is rendered inside a <code> element; read it back. |
| 32 | + const secretLocator = setupPage.locator('code').first(); |
| 33 | + await expect(secretLocator).toBeVisible(); |
| 34 | + const secret = (await secretLocator.textContent())?.trim() ?? ''; |
| 35 | + expect(secret.length).toBeGreaterThan(8); |
| 36 | + |
| 37 | + // Generate the current TOTP code and confirm. |
| 38 | + // The setup form lacks htmlFor on its label, so target the one-time-code input directly. |
| 39 | + const code = totp(secret); |
| 40 | + await setupPage.locator('input[autocomplete="one-time-code"]').fill(code); |
| 41 | + await setupPage.getByRole('button', { name: /conferma e attiva/i }).click(); |
| 42 | + |
| 43 | + // Recovery codes block appears + status flips to "enabled". |
| 44 | + await expect(setupPage.getByText(/salva i codici di recupero/i)).toBeVisible(); |
| 45 | + await expect(setupPage.getByText(/autenticazione a due fattori attiva/i)).toBeVisible(); |
| 46 | + |
| 47 | + await setupCtx.close(); |
| 48 | + |
| 49 | + // Now log in from a clean context to confirm the 2FA challenge. |
| 50 | + const loginCtx = await browser.newContext(); |
| 51 | + const loginPage = await loginCtx.newPage(); |
| 52 | + |
| 53 | + await loginViaUi(loginPage, user.email, user.password); |
| 54 | + |
| 55 | + // Should land on the second-factor screen, not on home. |
| 56 | + await expect(loginPage.getByRole('heading', { name: /verifica in due passaggi/i })).toBeVisible(); |
| 57 | + |
| 58 | + const challengeCode = totp(secret); |
| 59 | + await loginPage.locator('input.input').first().fill(challengeCode); |
| 60 | + await loginPage.getByRole('button', { name: /verifica|conferma|entra/i }).click(); |
| 61 | + |
| 62 | + // Successful sign-in leaves /login. |
| 63 | + await expect(loginPage).not.toHaveURL(/\/login/); |
| 64 | + |
| 65 | + await loginCtx.close(); |
| 66 | +}); |
0 commit comments