Mail relay: state block without an import-map entry, e2e on the reference site, sharper failure reports
- the state block is rendered by IntegrationStatusBanner (no new admin component: generate:importmap gives no diff) - reference site: e2e/mail-relay.spec.ts with a fixture and setup script; e2e:built runs it with both bundlers - a failed enrolment is reported as what it was; the service's own refusal after a fresh enrolment - a message keeps its key after any failure that is not final - a local site is shown as refused in the panel before any e-mail Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
dd3792cbc7
commit
2cceceffd0
@@ -0,0 +1,10 @@
|
||||
/**
|
||||
* Test fixture, on only with E2E_MAIL_RELAY_URL (e2e/mail-relay.spec.ts): points the mail service's client at
|
||||
* a stand-in the spec starts on this machine, so the spec can watch a built site enrol through its own confirm
|
||||
* route and send — without the real service. Never part of a real site: the starter drops `e2e/` and the lines
|
||||
* that use this file; a real site names no address (the plugin's own is used).
|
||||
*/
|
||||
export const E2E_MAIL_RELAY_URL = process.env.E2E_MAIL_RELAY_URL || undefined
|
||||
|
||||
/** `iapl({ mail })` for the test, `undefined` (the plugin's defaults) otherwise. */
|
||||
export const e2eMail = E2E_MAIL_RELAY_URL ? { relay: { retryBackoffMs: 50, url: E2E_MAIL_RELAY_URL } } : undefined
|
||||
@@ -0,0 +1,26 @@
|
||||
/**
|
||||
* For e2e/mail-relay.spec.ts — LOCAL / CI database only:
|
||||
* pnpm payload run e2e/mail-relay-setup.ts the site has no SMTP server and no token: its next e-mail
|
||||
* goes to the mail service, enrolling first
|
||||
* Afterwards the spec runs e2e/create-test-admin.ts again, which points e-mail back at the tests' SMTP sink.
|
||||
*/
|
||||
import config from '@payload-config'
|
||||
import { RELAY_KV_KEY } from '@intecion/ipal-kit'
|
||||
import { isLocalDatabase } from '@intecion/ipal-kit/seed'
|
||||
import { getPayload } from 'payload'
|
||||
|
||||
if (process.env.NODE_ENV === 'production' && !process.env.CI) {throw new Error('Refusing: NODE_ENV=production outside CI.')}
|
||||
if (!isLocalDatabase(process.env.DATABASE_URI ?? '')) {throw new Error('Refusing: DATABASE_URI is not a local database.')}
|
||||
|
||||
const payload = await getPayload({ config })
|
||||
await payload.updateGlobal({
|
||||
slug: 'site-integrations',
|
||||
context: { ipalIntegrationStatus: true },
|
||||
data: { emailStatus: { checkedAt: null, code: null, message: null, since: null, state: null }, emailTransport: 'smtp', smtpFromAddress: '[email protected]', smtpFromName: 'Example Studio', smtpHost: null, smtpPassword: null, smtpUser: null } as never,
|
||||
overrideAccess: true,
|
||||
})
|
||||
for (const key of await payload.kv.keys()) {
|
||||
if (key === RELAY_KV_KEY || key.startsWith(`${RELAY_KV_KEY}:`)) {await payload.kv.delete(key)}
|
||||
}
|
||||
console.log('[e2e] no SMTP server and no mail-service token: the next e-mail enrols and goes through the mail service')
|
||||
process.exit(0)
|
||||
@@ -0,0 +1,92 @@
|
||||
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 the built-in mail service|Testowy e-mail wysłano na adres anna@example\.org przez wbudowaną usługę pocztową/)).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 the built-in mail service|Połączono z wbudowaną usługą pocztową/)
|
||||
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_')}
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user