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]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
7d766b6d58
commit
0ee74ab2a4
+102
-3
@@ -8,8 +8,11 @@ import type { PayloadEmailAdapter } from 'payload'
|
||||
|
||||
import { afterEach, beforeEach, describe, expect, test, vi } from 'vitest'
|
||||
|
||||
import { buildSmtpFields } from '../src/globals/SiteIntegrations/fields/smtp.js'
|
||||
import { adminTexts } from '../src/modules/admin-i18n/index.js'
|
||||
import { mailAdapter, resetGraphDeprecationWarning, resolveMailTransport } from '../src/modules/email/mailAdapter.js'
|
||||
import { relayAdapter } from '../src/modules/email/relay/adapter.js'
|
||||
import { RELAY_SHORT_NAME } from '../src/modules/email/relay/constants.js'
|
||||
import { withMailKind } from '../src/modules/email/relay/kinds.js'
|
||||
import { bareAddresses, buildRelayMessage, cleanSubject } from '../src/modules/email/relay/message.js'
|
||||
import { isPublicHost, relayHost, resolveRelayOptions } from '../src/modules/email/relay/options.js'
|
||||
@@ -370,12 +373,24 @@ describe('one message: one key, one encoding, three attempts', () => {
|
||||
expect(Date.now() - started).toBeLessThan(2000)
|
||||
})
|
||||
|
||||
test('an answer that is not JSON (an error page of the network in front) is "unavailable": retried, then not sent', async () => {
|
||||
test('an answer that is not the service\'s JSON: retried; "not confirmed" unless its status is 400 to 499 (contract 1.6.0)', async () => {
|
||||
const { fake, options, payload } = await ready()
|
||||
const page = () => new Response('<html>502 Bad Gateway</html>', { headers: { 'content-type': 'text/html' }, status: 502 })
|
||||
const page = (status = 502) => new Response(`<html>${status}</html>`, { headers: { 'content-type': 'text/html' }, status })
|
||||
// an error page of the network in front, after the service may have handled the request: it may be out
|
||||
const sends = capture(fake, [page(), page(), page()])
|
||||
expect(await sendThroughRelay(payload, options, MESSAGE)).toMatchObject({ cause: 'unavailable', code: 'relay-unreachable', confirmed: true, sent: false })
|
||||
expect(await sendThroughRelay(payload, options, MESSAGE)).toMatchObject({ cause: 'unavailable', code: 'relay-not-confirmed', confirmed: false, sent: false })
|
||||
expect(sends).toHaveLength(3)
|
||||
// turned away before the service handled it (a firewall's 403, a proxy's 404): not sent, and said so
|
||||
for (const status of [400, 403, 404, 499]) {
|
||||
const refusedInFront = capture(fake, [page(status), page(status), page(status)])
|
||||
expect(await sendThroughRelay(payload, options, { ...MESSAGE, subject: `Blocked ${status}` })).toMatchObject({ cause: 'unavailable', code: 'relay-unreachable', confirmed: true, sent: false })
|
||||
expect(refusedInFront).toHaveLength(3)
|
||||
}
|
||||
// a 200 whose body was cut off, a 500 page, a redirect: the same as no answer
|
||||
for (const answer of [new Response('{"ok":tr', { status: 200 }), page(500), new Response(null, { headers: { location: 'https://elsewhere.example' }, status: 302 })]) {
|
||||
capture(fake, [answer, answer.clone(), answer.clone()])
|
||||
expect(await sendThroughRelay(payload, options, { ...MESSAGE, subject: `Lost ${answer.status}` })).toMatchObject({ code: 'relay-not-confirmed', confirmed: false })
|
||||
}
|
||||
// an error page says nothing about what the service did: the same message sent again keeps its key
|
||||
const again = capture(fake, [])
|
||||
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
|
||||
@@ -604,6 +619,74 @@ describe('the token over time', () => {
|
||||
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
|
||||
})
|
||||
|
||||
test('a site with a token issued by hand does not enrol to heal: it stops and says what to ask for (contract 1.6.0)', async () => {
|
||||
// the site CAN be called back here (the fake would confirm it) — it still must not enrol by itself
|
||||
const { fake, options, payload } = relaySite({ fake: { allow: [HOST] } })
|
||||
await setRelayToken(payload, fake.issueToken(HOST), { host: HOST })
|
||||
expect((await sendThroughRelay(payload, options, MESSAGE)).sent).toBe(true)
|
||||
const token = (await readRelayStore(payload)).token
|
||||
|
||||
fake.clientAddress = '198.51.100.30' // the site moved, or a second server sends
|
||||
const moved = await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Moved' })
|
||||
expect(moved).toMatchObject({ cause: 'reverify', code: 'relay-refused', confirmed: true, sent: false })
|
||||
expect(moved.sent === false && moved.detail).toMatch(/ask Intecion to add this server's address/)
|
||||
expect((await getRelayStatus(payload, options)).refusal).toMatchObject({ code: 'reverify' })
|
||||
expect(await mailPanelState({ payload }, options)).toMatchObject({ relay: { connected: true, refusal: { code: 'reverify' }, tokenSource: 'manual' } })
|
||||
|
||||
fake.clientAddress = '203.0.113.10'
|
||||
await fake.handle(new Request(`${fake.url}/v1/revoke`, { headers: { authorization: `Bearer ${token}` }, method: 'POST' }))
|
||||
const gone = await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'Gone' })
|
||||
expect(gone).toMatchObject({ cause: 'invalid-token', code: 'relay-refused', sent: false })
|
||||
expect(gone.sent === false && gone.detail).toMatch(/ask Intecion for a new token/)
|
||||
// not one enrolment, and the token was kept
|
||||
expect(fake.requests.map((r) => r.path)).not.toContain('/v1/enrol')
|
||||
expect((await readRelayStore(payload)).token).toBe(token)
|
||||
// a new token from Intecion, or "Connect again" by an admin (when the site can be called back), heals it
|
||||
await setRelayToken(payload, fake.issueToken(HOST), { host: HOST })
|
||||
expect((await sendThroughRelay(payload, options, { ...MESSAGE, subject: 'New token' })).sent).toBe(true)
|
||||
expect((await getRelayStatus(payload, options)).refusal).toBeUndefined()
|
||||
// rotation does not make it a site that enrols: it is still hand-issued
|
||||
expect(await rotateRelayToken(payload, options, { force: true })).toBe('rotated')
|
||||
expect((await readRelayStore(payload)).manual).toBe(true)
|
||||
})
|
||||
|
||||
test('a database copied to another host does not use the first site\'s token: the copy starts unenrolled and enrols as itself', async () => {
|
||||
// one service, two sites: it calls each back at its own host
|
||||
const staging = relayPayload({ serverURL: 'https://staging.example.com' })
|
||||
const sites: Record<string, ReturnType<typeof callConfirmRoute>> = {}
|
||||
const production = relaySite({
|
||||
fake: { allow: [HOST, 'staging.example.com'], callSite: (url, init) => sites[new URL(url).host](url, init) },
|
||||
integrations: { smtpFromName: 'Example' },
|
||||
})
|
||||
sites[HOST] = callConfirmRoute(production.payload)
|
||||
sites['staging.example.com'] = callConfirmRoute(staging.payload)
|
||||
await sendThroughRelay(production.payload, production.options, MESSAGE)
|
||||
const token = (await readRelayStore(production.payload)).token!
|
||||
// the copy: the same key-value entries, another serverURL
|
||||
for (const [key, value] of production.store) {staging.store.set(key, structuredClone(value))}
|
||||
const options = resolveRelayOptions(staging.payload, { fetchImpl: production.fake.fetch, url: production.fake.url })!
|
||||
expect((await readRelayStore(staging.payload)).token).toBe(token) // it is there …
|
||||
const seen: string[] = []
|
||||
production.fake.intercept = (request) => void seen.push(request.headers.get('authorization') ?? '')
|
||||
|
||||
// … and is not shown as this site's, not used, and not revoked
|
||||
expect(await mailPanelState({ payload: staging.payload }, options)).toMatchObject({ relay: { connected: false, host: 'staging.example.com' } })
|
||||
expect((await readRelayStore(staging.payload)).token).toBeUndefined()
|
||||
expect(staging.logger.warn.mock.calls.map((c) => String(c[0])).join()).toMatch(/belongs to "example\.com", this site is "staging\.example\.com"/)
|
||||
await disconnectRelay(staging.payload, options) // even "Disconnect" on the copy
|
||||
expect(production.fake.requests.filter((r) => r.path === '/v1/revoke')).toEqual([])
|
||||
expect((await enrolRelay(staging.payload, options, { force: true })).ok).toBe(true)
|
||||
expect((await sendThroughRelay(staging.payload, options, { ...MESSAGE, subject: 'From staging' })).sent).toBe(true)
|
||||
expect(production.fake.outbox.at(-1)).toMatchObject({ host: 'staging.example.com', subject: 'From staging' })
|
||||
const own = (await readRelayStore(staging.payload)).token
|
||||
expect(own).not.toBe(token)
|
||||
expect(seen.every((header) => !header.includes(token))).toBe(true)
|
||||
expect(JSON.stringify(staging.logger.warn.mock.calls)).not.toContain('ims_')
|
||||
// production is untouched and still sends with its own token
|
||||
expect((await readRelayStore(production.payload)).token).toBe(token)
|
||||
expect((await sendThroughRelay(production.payload, production.options, { ...MESSAGE, subject: 'From production' })).sent).toBe(true)
|
||||
})
|
||||
|
||||
test('a token issued by hand is stored the same way; enrol: "manual" and enrol: false never enrol by themselves', async () => {
|
||||
const manual = relaySite({ options: { enrol: 'manual' } })
|
||||
expect(await sendThroughRelay(manual.payload, manual.options, MESSAGE)).toMatchObject({ code: 'relay-refused', sent: false })
|
||||
@@ -626,6 +709,22 @@ describe('the token over time', () => {
|
||||
})
|
||||
|
||||
describe('what the panel is told', () => {
|
||||
test('the panel calls the service by its short name, IWSN, in both languages — one constant, never another name', () => {
|
||||
expect(RELAY_SHORT_NAME).toBe('IWSN')
|
||||
for (const lang of ['en', 'pl'] as const) {
|
||||
const texts = Object.entries(adminTexts[lang] as Record<string, string>).filter(([key]) => /^(?:mailRelay|statusEmailRelay|testEmailSentRelay|testEmailNotConfirmed)/.test(key))
|
||||
expect(texts.length).toBeGreaterThan(40)
|
||||
const named = texts.filter(([, text]) => text.includes(RELAY_SHORT_NAME))
|
||||
expect(named.length).toBeGreaterThan(20)
|
||||
for (const [key, text] of texts) {expect(text, key).not.toMatch(/mail service|usług|Intecion|Webservice/i)}
|
||||
}
|
||||
const tab = JSON.stringify(buildSmtpFields())
|
||||
expect(tab).toContain('IWSN')
|
||||
expect(tab).not.toMatch(/mail service|usług/)
|
||||
expect(JSON.stringify(buildSmtpFields({ relay: false }))).not.toContain('IWSN')
|
||||
})
|
||||
|
||||
|
||||
test('a site that runs locally is shown as refused before any e-mail; a public one as "not connected yet"; SMTP and "off" say so', async () => {
|
||||
const local = relayPayload({ serverURL: 'http://localhost:3000' })
|
||||
expect(await mailPanelState(local, {})).toMatchObject({ relay: { connected: false, refusal: { code: 'host-not-allowed' } }, replyAddress: null, transport: 'relay' })
|
||||
|
||||
Reference in New Issue
Block a user