- the state block on the e-mail tab (method IWSN): the button while the service offers the confirmation page
(connectAvailable) or before the site has enrolled; a click asks the site's server for the link
(POST /api/iapl/mail-relay/connect-link, admin only), which asks the service (POST /v1/connect, enrolling first),
checks that it begins with the service's own {base}/connect?, and hands it over without storing or logging it;
the browser opens it in a new tab with noopener noreferrer; the block asks /v1/status every 5 s for 10 min
- states: waiting for confirmation (button), waiting for approval (no page), connected, stopped; 503
connect-unavailable and 429 said in the admin's language; a load of the panel asks the service while the site waits
- dashboard alert for a waiting site (live); doctor mail-relay-connect (error) — replaces the pending warning and the
"not enrolled yet" note of mail-relay-state
- opt-out mail.relay.connectButton: false (no button, no route); building blocks requestRelayConnectLink,
isRelayConnectUrl, RELAY_CONNECT_LINK_PATH; the fake service implements /v1/connect, connectAvailable and
confirmConnect
- contract copy 1.8.0 (IWDL working tree, checksums match); docs, AGENT.md, setup / upgrade / debug skills, changelog
Co-Authored-By: Claude Opus 5.5 <[email protected]>
143 lines
8.5 KiB
TypeScript
143 lines
8.5 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/)
|
|
|
|
// "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_')}
|
|
})
|
|
|
|
test('"Connect to IWSN" (contract 1.8.0 §2.1): the link opens in a new tab, the person confirms, the block sees it by itself', async ({ page }) => {
|
|
// the site enrolled above; the service now holds it for confirmation by a person of Intecion
|
|
relay.site(site)!.state = 'pending'
|
|
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()
|
|
const block = page.locator('[data-mail-transport]')
|
|
await block.getByRole('button', { name: /Check now|Sprawdź teraz/ }).click()
|
|
const connect = block.locator('[data-mail-relay-connect]')
|
|
await expect(connect).toBeVisible({ timeout: 20_000 })
|
|
await expect(block).toContainText(/Waiting for confirmation|Oczekuje na potwierdzenie/)
|
|
// no link was asked for by the page itself
|
|
expect(relay.requests.filter((r) => r.path === '/v1/connect')).toEqual([])
|
|
|
|
const [tab] = await Promise.all([page.waitForEvent('popup'), connect.click()])
|
|
const url = tab.url()
|
|
expect(url.startsWith(`${server.url}/connect?ref=`)).toBe(true)
|
|
expect(await tab.evaluate(() => window.opener)).toBeNull()
|
|
// the person of Intecion signs in and presses "Potwierdź" on the service's page (the stand-in does it here)
|
|
expect(relay.confirmConnect(url)).toBe(true)
|
|
await tab.close()
|
|
// the block asks every 5 s and shows the result without a reload
|
|
await expect(block).toContainText(/Connected to IWSN|Połączono z IWSN/, { timeout: 20_000 })
|
|
await expect(connect).toHaveCount(0)
|
|
})
|
|
})
|