import { expect, test } from '@playwright/test' import { execFileSync } from 'node:child_process' import { createFakeRelay } from '@intecion/ipal-kit' import { totp } from './totp' /** * The mail service on a BUILT site (pnpm e2e:built runs this with webpack and with Turbopack): a stand-in for * the service runs inside this test and calls the site back over HTTP, as the real one would. * - the e-mail tab shows the state block (it rides on the status banner: no import-map entry of its own); * - the admin's test e-mail makes the site enrol through its own public confirm route and send; * - nothing the browser receives carries the token, and the confirm route tells a stranger nothing. * Needs the site started with E2E_MAIL_RELAY_URL (playwright.config.ts sets it) and the E2E_* test admin. */ const PORT = Number(process.env.E2E_RELAY_PORT ?? 2527) const { E2E_ADMIN_EMAIL: email, E2E_ADMIN_PASSWORD: password, E2E_TOTP_SECRET: secret } = process.env const run = (script: string) => execFileSync('pnpm', ['payload', 'run', script], { env: { ...process.env, NODE_ENV: 'development' }, stdio: 'inherit' }) test.describe('the mail service', () => { test.skip(!email || !password || !secret, 'E2E_* test admin not configured') let relay: ReturnType let server: Awaited['listen']>> let site = '' test.beforeAll(async ({ baseURL }) => { site = new URL(baseURL!).host // the site's host is listed, so it is active as soon as it enrols; `localhost` may be called back over http relay = createFakeRelay({ allow: [site], allowLocalHosts: true }) server = await relay.listen(PORT) run('e2e/mail-relay-setup.ts') }) test.afterAll(async () => { await server?.close() run('e2e/create-test-admin.ts') // e-mail back to the tests' SMTP sink, the test admins as they were }) test('the confirm route is public and tells a stranger nothing', async ({ request }) => { const res = await request.post('/api/iapl/mail-relay/confirm', { data: { nonce: 'n'.repeat(43), state: 'active', token: `ims_${'a'.repeat(43)}` } }) expect(res.status()).toBe(404) expect(await res.text()).toBe('') expect((await request.get('/api/iapl/mail-relay/status')).status()).toBe(401) expect(relay.requests).toEqual([]) }) test('the state block, the first e-mail through the confirm route, and no token in the browser', async ({ baseURL, page }) => { const seen: string[] = [] page.on('response', (response) => { if (response.url().includes('/api/')) {void response.text().then((body) => seen.push(body)).catch(() => undefined)} }) 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|\/admin(\?|$)/, { waitUntil: 'commit' }) if (page.url().includes('verify-totp')) { 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/globals/site-integrations') await page.getByRole('tab', { name: /E-mail sending|Wysyłka e-mail/ }).or(page.getByRole('button', { name: /E-mail sending|Wysyłka e-mail/ })).first().click() // the block is there in a production bundle, with the transport the settings give const block = page.locator('[data-mail-transport]') await expect(block).toHaveAttribute('data-mail-transport', 'relay', { timeout: 20_000 }) await expect(block).toContainText('notification@webservices.intecion.net') await expect(block).toContainText(/Not connected yet|Jeszcze nie połączono/) await page.fill('#ipal-test-email-to', 'anna@example.org') await page.getByRole('button', { name: /Send test e-mail|Wyślij testowy e-mail/ }).click() // the answer of the test e-mail names the transport and the sender name that was used await expect(page.getByText(/Test e-mail sent to anna@example\.org through IWSN|Testowy e-mail wysłano na adres anna@example\.org przez IWSN/)).toBeVisible({ timeout: 30_000 }) // the service was asked once, called the site's confirm route, and took one message expect(relay.requests.map((r) => r.path).slice(0, 2)).toEqual(['/v1/enrol', '/v1/send']) expect(relay.outbox).toHaveLength(1) expect(relay.outbox[0]).toMatchObject({ fromName: 'Example Studio', host: site, kind: 'test', replyTo: 'site@example-studio.test', to: ['anna@example.org'] }) // "Check now" asks the service; the block then says connected and what is left of the hour await block.getByRole('button', { name: /Check now|Sprawdź teraz/ }).click() await expect(block).toContainText(/Connected to IWSN|Połączono z IWSN/) await expect(block).toContainText(/Left this hour: 29|Pozostało w tej godzinie: 29/) // "Sending method": the method in effect is the one selected, and only its fields are on the page (not saved here) const method = page.locator('#field-emailMethod') await expect(method).toContainText(/IWSN \((built-in|wbudowany)\)/) await expect(page.locator('#field-smtpHost')).toHaveCount(0) await method.locator('.rs__control').click() await page.locator('.rs__option', { hasText: /Own SMTP server|Własny serwer SMTP/ }).click() await expect(page.locator('#field-smtpHost')).toBeVisible() await expect(page.locator('#field-smtpUser')).toBeVisible() // picked, not saved, and the server is empty: the block warns at once await expect(page.locator('[data-mail-method-warning="smtp"]')).toBeVisible() await method.locator('.rs__control').click() await page.locator('.rs__option', { hasText: /IWSN/ }).click() await expect(page.locator('#field-smtpHost')).toHaveCount(0) await expect(page.locator('[data-mail-method-warning]')).toHaveCount(0) // (Payload takes the login cookie only from the site's own origin, so the request says where it comes from) const status = await page.request.get('/api/iapl/mail-relay/status', { headers: { origin: baseURL! } }) expect(await status.json()).toMatchObject({ ok: true, relay: { connected: true, state: 'active', tokenSource: 'enrolment' }, transport: 'relay' }) // nothing the browser saw carries the token: not the page, not an API answer await page.waitForLoadState('networkidle') for (const body of [await page.content(), await status.text(), ...seen]) {expect(body).not.toContain('ims_')} }) })