import { expect, test } from '@playwright/test' import { randomUUID } from 'node:crypto' import { waitForEnrolmentLink } from './enrolment' import { startSmtpSink } from './smtp-sink' import { totp } from './totp' /** * On the BUILT site (pnpm e2e:built, webpack and Turbopack), what 1.18.0 changed for one's own account and the * sign-in of staff: * - the account page has a "Current password" field; saving a new e-mail without it is refused on that field, * with it the e-mail changes (accounts.confirmOwnChanges); * - five failed sign-ins from one place for an address answer 429 there, the owner elsewhere still signs in * (accounts.signInLimit); a staff sign-in from another origin gets 403 (accounts.signInSameOrigin). * The accounts are fresh ones made here (the test admins are never touched). Needs the E2E_* test admin. */ const { E2E_ADMIN_EMAIL: adminEmail, E2E_ADMIN_PASSWORD: adminPassword, E2E_TOTP_SECRET: secret } = process.env const SECRET = 'JPURUZNWYTZX2L42MT4SS7736WWNQWBM' test('own account: a new e-mail needs the current password, in the panel', async ({ baseURL, browser, playwright }) => { test.skip(!adminEmail || !adminPassword || !secret, 'E2E_* test admin not configured') test.setTimeout(120_000) const email = `own-${randomUUID().slice(0, 8)}@example.com` const password = `O-${randomUUID()}` // made by the test admin; its TOTP set up over the API with a secret of our own const admin = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } }) expect((await admin.post('/api/users/login', { data: { email: adminEmail, password: adminPassword } })).status()).toBe(200) expect((await (await admin.post('/api/verify-totp', { data: { token: totp(secret!) } })).json()).ok).toBe(true) expect((await admin.post('/api/users', { data: { email, password, roles: ['editor'] } })).status()).toBe(201) await admin.dispose() const own = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } }) expect((await own.post('/api/users/login', { data: { email, password } })).status()).toBe(200) // the first set-up waits for its e-mail link (twoFactor.confirmByEmail): read it from the mail sink, confirm it here const sink = await startSmtpSink(Number(process.env.E2E_SMTP_PORT ?? 2526)) try { expect((await (await own.post('/api/setup-totp', { data: { secret: SECRET, token: totp(SECRET) } })).json()).pending).toBe(true) const link = await waitForEnrolmentLink(sink.messages, email) const done = await own.post('/api/iapl/two-factor/confirm', { form: { t: link.searchParams.get('t')!, u: link.searchParams.get('u')! } }) expect(done.status()).toBe(200) } finally { await sink.close() } await own.dispose() const context = await browser.newContext({ baseURL }) const page = await context.newPage() await page.goto('/admin/login') await page.fill('#field-email', email) await page.fill('#field-password', password) await page.click('.form-submit button') await page.waitForURL(/verify-totp/, { waitUntil: 'commit' }) await page.getByRole('textbox').first().click() await page.keyboard.type(totp(SECRET)) await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 }) await page.goto('/admin/account') const current = page.locator('#field-currentPassword') await expect(current).toBeVisible({ timeout: 20_000 }) await expect(current).toHaveAttribute('type', 'password') const renamed = `renamed-${randomUUID().slice(0, 8)}@example.com` await page.fill('#field-email', renamed) await page.click('#action-save') await expect(page.getByText(/Enter your current password|Wpisz obecne hasło/).first()).toBeVisible({ timeout: 20_000 }) await current.fill(password) await page.click('#action-save') await expect.poll(async () => { const check = await playwright.request.newContext({ baseURL }) const status = (await check.post('/api/users/login', { data: { email: renamed, password } })).status() await check.dispose() return status }, { timeout: 20_000 }).toBe(200) await context.close() }) test('staff sign-in: five failures from one place answer 429 there, not elsewhere; another origin gets 403', async ({ baseURL, playwright }) => { test.skip(!adminEmail || !adminPassword || !secret, 'E2E_* test admin not configured') const email = `limit-${randomUUID().slice(0, 8)}@example.com` const password = `S-${randomUUID()}` const admin = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } }) expect((await admin.post('/api/users/login', { data: { email: adminEmail, password: adminPassword } })).status()).toBe(200) expect((await (await admin.post('/api/verify-totp', { data: { token: totp(secret!) } })).json()).ok).toBe(true) expect((await admin.post('/api/users', { data: { email, password, roles: ['editor'] } })).status()).toBe(201) await admin.dispose() // the site reads the client address from IAPL_IP_HEADER (reference/.env: x-forwarded-for, one proxy) const from = async (ip: string) => playwright.request.newContext({ baseURL, extraHTTPHeaders: { 'x-forwarded-for': ip } }) const stranger = await from('198.51.100.123') for (let i = 0; i < 5; i++) {expect((await stranger.post('/api/users/login', { data: { email, password: 'a-wrong-password' } })).status()).toBe(401)} expect((await stranger.post('/api/users/login', { data: { email, password } })).status()).toBe(429) const owner = await from('203.0.113.123') expect((await owner.post('/api/users/login', { data: { email, password } })).status()).toBe(200) const foreign = await owner.post('/api/users/login', { data: { email, password }, headers: { origin: 'https://blog.example.test', 'sec-fetch-site': 'same-site' } }) expect(foreign.status()).toBe(403) await stranger.dispose() await owner.dispose() })