|
| 1 | +import { test, expect } from '@playwright/test' |
| 2 | +import { authenticator } from 'otplib' |
| 3 | +import { |
| 4 | + randomEmail, |
| 5 | + randomPassword, |
| 6 | + createUser, |
| 7 | + createUserWithTwoFactor, |
| 8 | + loginAsUser, |
| 9 | + loginWithTwoFactor, |
| 10 | + logout, |
| 11 | +} from './helpers/auth' |
| 12 | + |
| 13 | +/** |
| 14 | + * Walk from the authenticated file browser to the change-password screen |
| 15 | + * via the in-app sidebar + "My account" link. A full `page.goto` would |
| 16 | + * reload the SPA and drop the in-memory keypair, bouncing the user back |
| 17 | + * to /auth/login. |
| 18 | + */ |
| 19 | +async function openChangePasswordViaSidebar(page: import('@playwright/test').Page) { |
| 20 | + await page.locator('aside').getByText('Account', { exact: true }).first().click() |
| 21 | + await page.waitForURL(/\/account(\/|$|\?)/) |
| 22 | + await page.getByRole('link', { name: 'Change', exact: true }).click() |
| 23 | + await page.waitForURL(/\/account\/change-password/) |
| 24 | +} |
| 25 | + |
| 26 | +test.describe('Change password', () => { |
| 27 | + test('with no TFA, change password via current password succeeds', async ({ page }) => { |
| 28 | + const email = randomEmail() |
| 29 | + const password = randomPassword() |
| 30 | + await createUser(page, email, password) |
| 31 | + |
| 32 | + await openChangePasswordViaSidebar(page) |
| 33 | + await page.locator('#current_password').fill(password) |
| 34 | + |
| 35 | + const newPassword = randomPassword() |
| 36 | + await page.locator('#password').fill(newPassword) |
| 37 | + |
| 38 | + const response = page.waitForResponse( |
| 39 | + (res) => |
| 40 | + res.url().includes('/api/auth/account/change-password') && |
| 41 | + res.request().method() === 'POST' |
| 42 | + ) |
| 43 | + await page.getByRole('button', { name: 'Change password' }).click() |
| 44 | + expect((await response).status()).toBe(204) |
| 45 | + |
| 46 | + await logout(page) |
| 47 | + await loginAsUser(page, email, newPassword) |
| 48 | + await expect(page).toHaveURL(/\/$/) |
| 49 | + }) |
| 50 | + |
| 51 | + // Regression test for https://github.com/hudikhq/hoodik/issues/164 |
| 52 | + test('with TFA enabled, change password via private key + OTP succeeds', async ({ page }) => { |
| 53 | + const email = randomEmail() |
| 54 | + const password = randomPassword() |
| 55 | + const { privateKey, secret } = await createUserWithTwoFactor(page, email, password) |
| 56 | + |
| 57 | + await openChangePasswordViaSidebar(page) |
| 58 | + await page.locator('#use_private_key').check() |
| 59 | + await page.locator('#private_key').fill(privateKey) |
| 60 | + await page.locator('#token').fill(authenticator.generate(secret)) |
| 61 | + |
| 62 | + const newPassword = randomPassword() |
| 63 | + await page.locator('#password').fill(newPassword) |
| 64 | + |
| 65 | + const response = page.waitForResponse( |
| 66 | + (res) => |
| 67 | + res.url().includes('/api/auth/account/change-password') && |
| 68 | + res.request().method() === 'POST' |
| 69 | + ) |
| 70 | + await page.getByRole('button', { name: 'Change password' }).click() |
| 71 | + |
| 72 | + // The bug in #164 manifests as a 401 with body `invalid_otp_token` |
| 73 | + // because the frontend silently dropped the token before sending. |
| 74 | + // After the fix the backend returns 204. |
| 75 | + expect((await response).status()).toBe(204) |
| 76 | + |
| 77 | + await logout(page) |
| 78 | + await loginWithTwoFactor(page, email, newPassword, secret) |
| 79 | + await expect(page).toHaveURL(/\/$/) |
| 80 | + }) |
| 81 | +}) |
0 commit comments