Real-service test: the connect flow against IWSN's code at IWDL de560ef (policy connect) — wait, link, confirm on the stand-in's page, active, send

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-12-01 21:44:19 +01:00
co-authored by Claude Opus 5.5
parent 2c0262367d
commit 25af43442e
+33 -8
View File
@@ -5,7 +5,7 @@
*
* Skipped unless the stand-in is named — nothing here can reach the deployed service:
* cd <IWDL>/services/mail-relay
* npm run dev:migrate && npm run ims -- allow localhost:3987 && npm run dev
* npm run dev:migrate && npm run dev (policy `connect`: the site waits; the test confirms it on the stand-in's page)
* npm run ims -- limits localhost:3987 --hour 2000 --day 20000 # after the first run: a run sends about ten messages,
* # and the default hour (24 for mail that is not account mail) lasts three runs
* IAPL_TEST_RELAY_URL=http://127.0.0.1:8787/mail pnpm vitest run test/mailRelayService.test.ts
@@ -27,7 +27,7 @@ import { RELAY_CONTRACT_VERSION } from '../src/modules/email/relay/constants.js'
import { withMailKind } from '../src/modules/email/relay/kinds.js'
import { resolveRelayOptions } from '../src/modules/email/relay/options.js'
import { sendThroughRelay } from '../src/modules/email/relay/send.js'
import { disconnectRelay, enrolRelay, getRelayStatus, rotateRelayToken } from '../src/modules/email/relay/session.js'
import { disconnectRelay, enrolRelay, getRelayStatus, requestRelayConnectLink, rotateRelayToken } from '../src/modules/email/relay/session.js'
import { readRelayStore } from '../src/modules/email/relay/store.js'
import { iapl } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
@@ -80,14 +80,39 @@ describe.skipIf(!SERVICE || !local)('against the real service code, run locally'
expect(await (await fetch(`${SERVICE}/health`)).json()).toEqual({ ok: true })
})
test('the first e-mail: the service calls the site\'s confirm route over HTTP, then takes the message', async () => {
test('the first e-mail: the service calls the site\'s confirm route, and the site waits for confirmation (contract 1.8.0, policy `connect`)', async () => {
const result = (await payload.sendEmail(withMailKind(MESSAGE, 'form-notification'))) as Record<string, unknown>
expect(result, JSON.stringify(result)).toMatchObject({ cause: 'site-pending', sent: false })
const stored = await readRelayStore(payload)
expect(stored).toMatchObject({ host: HOST, source: 'enrolment', state: 'pending' })
expect(stored.token).toMatch(/^ims_[\w-]{43}$/)
expect(await outbox()).toHaveLength(0)
expect(await getRelayStatus(payload, options, { refresh: true })).toMatchObject({ connectAvailable: true, state: 'pending' })
})
test('"Połącz z IWSN": the link, a person confirms it on the service\'s page, the site is active and sends', async () => {
const link = await requestRelayConnectLink(payload, options)
expect(link, JSON.stringify(link)).toMatchObject({ ok: true, state: 'pending' })
if (!link.ok || link.state !== 'pending') {throw new Error('no link')}
expect(link.connectUrl.startsWith(`${SERVICE}/connect?ref=`)).toBe(true)
// the person's browser: the page (on a developer's machine the stand-in takes the person as signed in), then
// its own form, sent from the page's own origin
const page = await fetch(link.connectUrl)
expect(page.status).toBe(200)
const html = await page.text()
const field = (name: string) => new RegExp(`name="${name}" value="([^"]+)"`).exec(html)?.[1] ?? ''
expect(field('form')).not.toBe('')
const origin = new URL(link.connectUrl).origin
const confirmed = await fetch(link.connectUrl, { body: new URLSearchParams({ form: field('form'), ref: field('ref') }).toString(), headers: { 'content-type': 'application/x-www-form-urlencoded', origin, 'sec-fetch-site': 'same-origin' }, method: 'POST', redirect: 'manual' })
expect(confirmed.status, await confirmed.clone().text()).toBeLessThan(400)
// the site learns it by asking
expect(await getRelayStatus(payload, options, { refresh: true })).toMatchObject({ connected: true, state: 'active' })
expect(await requestRelayConnectLink(payload, options)).toEqual({ ok: true, state: 'active' })
const result = (await payload.sendEmail(withMailKind(MESSAGE, 'form-notification'))) as Record<string, unknown>
expect(result, JSON.stringify(result)).toMatchObject({ replayed: false, senderName: 'Kancelaria Example', sent: true, transport: 'relay', warnings: [] })
const stored = await readRelayStore(payload)
expect(stored).toMatchObject({ host: HOST, source: 'enrolment', state: 'active' })
expect(stored.token).toMatch(/^ims_[\w-]{43}$/)
const mails = await outbox()
expect(mails).toHaveLength(1)
// the site's own mail (the service may also have sent its own notice of the confirmation to Intecion)
const mails = (await outbox()).filter((m) => JSON.stringify(m.to).includes('[email protected]'))
expect(mails, JSON.stringify((await outbox()).map((m) => [m.to, m.subject]))).toHaveLength(1)
expect(JSON.stringify(mails[0])).toContain('Kancelaria Example')
expect(JSON.stringify(mails[0])).toContain('[email protected]')
expect(JSON.stringify(mails[0])).toContain(HOST)