The verify count now survives a restart of the site, so the specs can no longer share one test admin across server runs: create-test-admin.ts makes two test admins and clears their stored counts; the setup spec signs in as the second. The whole suite passes in one run (20 passed, 1 skipped). Co-Authored-By: Claude Fable 5.2 <[email protected]>
129 lines
7.3 KiB
TypeScript
129 lines
7.3 KiB
TypeScript
import { type APIRequestContext, expect, type Page, test } from '@playwright/test'
|
|
|
|
import { secondTestAdmin, totp } from './totp'
|
|
|
|
/**
|
|
* "2FA setup required" in a real browser: an account that has not set two-step verification up sees only the setup
|
|
* screen and gets nothing from the API; after the code it is in. Covers the first admin of a fresh site (only when
|
|
* the database has no users yet — run this file before e2e/create-test-admin.ts for that), a new editor, and the
|
|
* administrator's reset on the account page.
|
|
* E2E_BASE_URL=http://localhost:3217 pnpm exec playwright test e2e/two-factor-setup.spec.ts
|
|
*/
|
|
test.describe.configure({ mode: 'serial' })
|
|
|
|
const stamp = Date.now()
|
|
const OWNER = { email: `e2e-owner-${stamp}@example.com`, password: `owner-${stamp}-Hk29dLq` }
|
|
const EDITOR = { email: `e2e-editor-${stamp}@example.com`, password: `editor-${stamp}-Pz71mWx` }
|
|
/** The admin the later tests act as: the owner made in the first test, else the smoke-test admin. */
|
|
let admin: { email: string; password: string; secret: string } | null =
|
|
process.env.E2E_ADMIN_EMAIL && process.env.E2E_ADMIN_PASSWORD && process.env.E2E_TOTP_SECRET
|
|
? // the second test admin: the other specs spend the first one's verify attempts (5 per account in 10 minutes)
|
|
{ email: secondTestAdmin(process.env.E2E_ADMIN_EMAIL), password: process.env.E2E_ADMIN_PASSWORD, secret: process.env.E2E_TOTP_SECRET }
|
|
: null
|
|
let editorId: string | undefined
|
|
|
|
async function typeCode(page: Page, secret: string) {
|
|
// six one-digit inputs: type the code into the first, focus moves on by itself
|
|
await page.getByRole('textbox').first().click()
|
|
await page.keyboard.type(totp(secret))
|
|
}
|
|
|
|
async function signIn(page: Page, who: { email: string; password: string }) {
|
|
await page.goto('/admin/login')
|
|
await page.fill('#field-email', who.email)
|
|
await page.fill('#field-password', who.password)
|
|
await page.click('.form-submit button')
|
|
}
|
|
|
|
/** A fully logged-in API session (password, then the code). Payload takes the cookie only with the site's Origin. */
|
|
async function adminApi(playwright: { request: { newContext: (o: object) => Promise<APIRequestContext> } }, baseURL: string) {
|
|
const api = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL } })
|
|
expect((await api.post('/api/users/login', { data: { email: admin!.email, password: admin!.password } })).status()).toBe(200)
|
|
expect((await (await api.post('/api/verify-totp', { data: { token: totp(admin!.secret) } })).json()).ok).toBe(true)
|
|
return api
|
|
}
|
|
|
|
test('first admin of a fresh site: created, sent to 2FA setup, in after the code', async ({ baseURL, page }) => {
|
|
// With users in the database this address leads to the login screen instead.
|
|
await page.goto('/admin/create-first-user')
|
|
const firstUserForm = await page.locator('#field-confirm-password').waitFor({ timeout: 10_000 }).then(() => true, () => false)
|
|
test.skip(!firstUserForm, 'the database already has users')
|
|
// Type like a person, once the form is live: values filled in before hydration are not in the form's state.
|
|
await page.waitForLoadState('networkidle')
|
|
await page.locator('#field-email').pressSequentially(OWNER.email)
|
|
await page.locator('#field-password').pressSequentially(OWNER.password)
|
|
await page.locator('#field-confirm-password').pressSequentially(OWNER.password)
|
|
const registered = page.waitForResponse((r) => r.url().includes('/first-register') && r.request().method() === 'POST')
|
|
await page.click('.form-submit button')
|
|
expect((await registered).status()).toBe(200)
|
|
|
|
await page.waitForURL(/setup-totp/)
|
|
await expect(page.locator('#ipal-two-factor-notice')).toContainText(/Ustaw weryfikację dwuetapową do|Set up two-step verification by/)
|
|
// nothing but setup, over the API too
|
|
for (const path of ['/api/globals/site-integrations', '/api/users', '/api/pages']) {
|
|
expect((await page.request.get(path, { headers: { origin: baseURL! } })).status(), path).toBe(403)
|
|
}
|
|
const secret = await page.locator('input[name="secret"]').inputValue()
|
|
await typeCode(page, secret)
|
|
await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 })
|
|
expect((await page.request.get('/api/globals/site-integrations', { headers: { origin: baseURL! } })).status()).toBe(200)
|
|
admin = { ...OWNER, secret }
|
|
})
|
|
|
|
test('a new editor: setup screen with the deadline, nothing else, in after the code', async ({ baseURL, page, playwright }) => {
|
|
test.skip(!admin, 'no admin to create the editor with (set E2E_* or run on a fresh database)')
|
|
const api = await adminApi(playwright, baseURL!)
|
|
const created = await api.post('/api/users', { data: { ...EDITOR, roles: ['editor'] } })
|
|
expect(created.status()).toBe(201)
|
|
editorId = String((await created.json()).doc.id)
|
|
await api.dispose()
|
|
|
|
// the brand images must load on the setup screen too (the session is refused `media` itself)
|
|
const refusedImages: string[] = []
|
|
page.on('response', (r) => {
|
|
if (r.status() === 403 && /\/api\/(media\/|ipal\/admin-icon)/.test(r.url())) {refusedImages.push(r.url())}
|
|
})
|
|
await signIn(page, EDITOR)
|
|
await page.waitForURL(/setup-totp/)
|
|
await expect(page.locator('#ipal-two-factor-notice')).toContainText(/Ustaw weryfikację dwuetapową do|Set up two-step verification by/)
|
|
for (const path of ['/api/pages', '/api/form-submissions', '/api/globals/site-settings', `/api/users/${editorId}`]) {
|
|
expect((await page.request.get(path, { headers: { origin: baseURL! } })).status(), path).toBe(403)
|
|
}
|
|
expect(refusedImages).toEqual([])
|
|
// going elsewhere in the panel leads back to setup
|
|
await page.goto('/admin/collections/pages')
|
|
await page.waitForURL(/setup-totp/)
|
|
|
|
const secret = await page.locator('input[name="secret"]').inputValue()
|
|
await typeCode(page, secret)
|
|
// back where it wanted to go, and the data opens
|
|
await page.waitForURL(/\/admin\/collections\/pages/, { timeout: 20_000 })
|
|
expect((await page.request.get('/api/pages', { headers: { origin: baseURL! } })).status()).toBe(200)
|
|
})
|
|
|
|
test('the administrator sees the status on the account and resets it; the editor must set it up again', async ({ baseURL, page, playwright }) => {
|
|
test.skip(!admin || !editorId, 'needs the editor from the previous test')
|
|
await signIn(page, admin!)
|
|
await page.waitForURL(/verify-totp|\/admin(\?|$)/, { waitUntil: 'commit' })
|
|
if (page.url().includes('verify-totp')) {await typeCode(page, admin!.secret)}
|
|
await expect(page).toHaveTitle(/Dashboard|Pulpit/, { timeout: 20_000 })
|
|
|
|
await page.goto(`/admin/collections/users/${editorId}`)
|
|
const status = page.locator('#ipal-two-factor-status')
|
|
await expect(status).toContainText(/Ustawiona\.|Set up\./)
|
|
page.once('dialog', (dialog) => dialog.accept())
|
|
await status.getByRole('button').click()
|
|
await expect(status).toContainText(/Nieustawiona\. Konto może ją ustawić do|Not set up\. The account can set it up until/, { timeout: 15_000 })
|
|
|
|
// the editor's password alone opens nothing again
|
|
const editor = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL! } })
|
|
expect((await editor.post('/api/users/login', { data: EDITOR })).status()).toBe(200)
|
|
expect((await editor.get('/api/pages')).status()).toBe(403)
|
|
await editor.dispose()
|
|
|
|
// clean up the account this file made
|
|
const api = await adminApi(playwright, baseURL!)
|
|
expect((await api.delete(`/api/users/${editorId}`)).status()).toBe(200)
|
|
await api.dispose()
|
|
})
|