Files
ipal-kit/reference/e2e/mail-relay.spec.ts
T
Radosław SmolińskiandClaude Fable 5.2 ee8ea42956 E-mail: "Sending method" is an explicit choice - IWSN (built-in), Own SMTP server, Microsoft Graph (deprecated)
What is chosen is what is used. The choice is the stored field `emailMethod` of Site integrations (empty until
somebody chooses); `emailTransport` of 1.6 - 1.17.0 stays, hidden.

- no silent switch: a chosen method that cannot be used (SMTP incomplete, Graph without GRAPH_*) fails loudly
  - status smtp-incomplete / graph-not-set-up, state block, doctor mail-method - and nothing is sent another way
- upgrade writes nothing and loses no mail: until somebody chooses, the order of 1.17.0 applies and the field
  shows its result as selected; the first save of the panel's form stores the choice
- only the chosen method's fields are shown; the test e-mail names the method (Graph too)
- mail: { relay: false } does not offer "IWSN" (the stored values stay three: one SQL enum for every project)
- Postgres / SQLite: one migration (a column). Before it runs the plugin's readers read the global without the
  field, mail goes the old way, one loud log line and the doctor say what to do
- getMailMethod, setMailMethod, resolveMailMethod, readMailSettings, mailMethodColumnMissing exported
- tests on SQLite, Postgres and MongoDB, with and without the migration; docs, AGENT.md, skills, changelog

Co-Authored-By: Claude Fable 5.2 <[email protected]>
2026-11-26 22:39:03 +01:00

105 lines
6.2 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()
await method.locator('.rs__control').click()
await page.locator('.rs__option', { hasText: /IWSN/ }).click()
await expect(page.locator('#field-smtpHost')).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_')}
})
})