import { readFileSync } from 'node:fs' import { fr } from '@/i18n/fr' import type { Page } from '@playwright/test' import { totpCode } from '../../../../packages/supabase/tests/sandbox/totp' import { expect, test } from '../support/browser' import { shownRevision } from '../support/saveLineText' // Owner trial through its LAN origin (C47): sign-in, TOTP enrolment and challenge, the // catalogue, a non-equine sheet, an SVG import, save and reload. The request guard of // support/browser.ts fails on any request outside the origin; the listener below also // names 127.0.0.1 and localhost. TRIAL_EMAIL and TRIAL_PW_FILE (first line, mode 600). const dict = fr const email = process.env.TRIAL_EMAIL ?? '' const password = (process.env.TRIAL_PW_FILE ?? '') ? readFileSync(process.env.TRIAL_PW_FILE ?? '', 'utf8').split('\n')[0] : '' const SVG = '' + '' + '' const saveLine = (page: Page) => page.getByTestId('save-line') // Read while the line says « Révision n »: a pending change shows no revision. async function revisionShown(page: Page): Promise { return shownRevision((await saveLine(page).textContent()) ?? '') } async function nextCode(secret: string): Promise { const left = 30_000 - (Date.now() % 30_000) if (left < 3000) await new Promise((r) => setTimeout(r, left + 200)) return totpCode(secret) } async function signIn(page: Page) { await page.goto('/login') await page.getByLabel(dict['auth.email']).fill(email) await page.getByLabel(dict['auth.password'], { exact: true }).fill(password) await page.getByRole('button', { name: dict['auth.login.submit'] }).click() await expect(page).toHaveURL(/\/app\/?$/) } async function enrol(page: Page): Promise { await page.goto('/app/account') await page.getByRole('button', { name: dict['mfa.enrol.start'] }).click() await page.getByLabel(dict['mfa.enrol.name']).fill('Essai LAN') await page.getByRole('button', { name: dict['mfa.enrol.continue'] }).click() const key = page.getByText(/^[A-Z2-7]{16,}$/) await expect(key).toBeVisible() const secret = ((await key.textContent()) ?? '').trim() await page.getByLabel(dict['mfa.code.label']).fill(await nextCode(secret)) await page.getByRole('button', { name: dict['mfa.code.verify'] }).click() await expect(page.getByText(dict['mfa.enrol.done'])).toBeVisible() return secret } async function challenge(page: Page, secret: string) { await page.goto('/app/admin') const field = page.getByRole('main').getByLabel(dict['mfa.code.label']) await expect(field).toBeVisible() await page.waitForLoadState('networkidle') await field.fill(await nextCode(secret)) await page .getByRole('main') .getByRole('button', { name: dict['mfa.code.verify'] }) .click() await expect(field).toBeHidden() } async function createNonEquineSheet(page: Page, tag: string) { await page.goto('/app/admin/sheets') await expect( page.getByRole('heading', { name: dict['admin.sheets.title'] }), ).toBeVisible() await page.getByRole('link', { name: dict['admin.sheets.new'] }).click() const species = page.getByLabel(dict['admin.sheet_form.species'], { exact: true, }) const nonEquine = species.locator( 'option:not([value=""]):not([value="equine"])', ) await expect(nonEquine.first()).toBeAttached() await page.waitForLoadState('networkidle') const chosen = (await nonEquine.first().getAttribute('value')) ?? '' await species.selectOption(chosen) await expect(species).toHaveValue(chosen) await page .getByLabel(dict['admin.sheet_form.name_fr']) .fill(`Fiche LAN ${tag}`) await page .getByLabel(dict['admin.sheet_form.name_en']) .fill(`LAN sheet ${tag}`) await page .getByRole('button', { name: dict['admin.sheet_form.submit'] }) .click() await expect(page).toHaveURL(/\/app\/admin\/sheets\/[0-9a-f-]{36}\/draft$/) } /** The views section: the Annotations tab, then the overview entry (U1 moved the views). */ async function openViews(page: Page) { await page .getByRole('tab', { name: dict['editor.tab.annotations'], exact: true }) .click() await page.locator('[data-section-entry="views"]').click() } test('trial through the LAN origin: MFA, new sheet, SVG import, save, reload', async ({ page, }) => { test.skip(!email || !password, 'TRIAL_EMAIL and TRIAL_PW_FILE are required') // floor-guard: allow C47 run by hand on a live trial test.setTimeout(120_000) const local: string[] = [] page.on('request', (request) => { if (/\/\/(127\.0\.0\.1|localhost)/.test(request.url())) local.push(request.url()) }) const tag = Date.now().toString(36) await signIn(page) const secret = await enrol(page) // Enrolment leaves an aal2 session: a new sign-in brings the real challenge. await page .getByRole('button', { name: dict['shell.sign_out'] }) .first() .click() await expect(page).not.toHaveURL(/\/app/) await signIn(page) await challenge(page, secret) await createNonEquineSheet(page, tag) await openViews(page) await expect(saveLine(page)).toHaveText(/^Révision \d+$/) const before = await revisionShown(page) await page .locator('[data-testid$="-file"]') .first() .setInputFiles({ name: 'lan.svg', mimeType: 'image/svg+xml', buffer: Buffer.from(SVG), }) const dialog = page.getByRole('dialog', { name: dict['editor.cleaning.title'], }) await expect(dialog).toBeVisible() await dialog .getByRole('button', { name: dict['editor.cleaning.confirm'] }) .click() await expect(page.getByText(dict['editor.upload.done'])).toBeVisible() await page.getByRole('button', { name: dict['common.save'] }).click() await expect(saveLine(page)).toHaveText( new RegExp(`^Enregistré( à \\d\\d:\\d\\d)? · révision ${before + 1}$`), ) await page.reload() await openViews(page) await expect( page.getByRole('tabpanel').getByText(dict['editor.views.drawing.ok']), ).toBeVisible() expect(local, 'requests to 127.0.0.1 or localhost').toEqual([]) })