Files
ipal-kit/reference/e2e/mail-relay.spec.ts
T
Radosław SmolińskiandClaude Fable 5.2 0ee74ab2a4 Mail relay: contract 1.6.0, the token tied to its host, IWSN in the panel
- contracts/mail-relay refreshed from IWDL 83af8c6 (1.6.0)
- an answer that is not the service's JSON is 'not confirmed' unless its status is 400 to 499
- a site with a token issued by hand does not enrol to heal: it stops and says what to ask Intecion for
- a store made on another host (a database copied to staging) is not used: the copy enrols as itself, nothing is revoked
- the panel calls the service IWSN (RELAY_SHORT_NAME); docs, logs and the doctor keep the full name

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-25 21:51:47 +01:00

93 lines
5.4 KiB
TypeScript

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<typeof createFakeRelay>
let server: Awaited<ReturnType<ReturnType<typeof createFakeRelay>['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('[email protected]')
await expect(block).toContainText(/Not connected yet|Jeszcze nie połączono/)
await page.fill('#ipal-test-email-to', '[email protected]')
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: '[email protected]', to: ['[email protected]'] })
// "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/)
// (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_')}
})
})