- R-1 a delivered token becomes the one in use only on 200 or a refusal given after authentication - R-2 a token of another site is refused by what /v1/status says (access-key field, scripts); docs for staging - R-3 the confirm route looks the nonce up first and counts only refused calls - R-4 one budget of time per message (10 s, 30 s with files), enrolment and retries included - R-5 'Disconnect' without the service's answer is not confirmed and keeps the token to try again - R-6 no enrolment without the confirm route in the config - R-7 half an SMTP set-up: one log line and a hint in the panel - R-8 to R-10, R-12 docs; R-11 the last failure stays in the state block - tests for redirect: manual, the candidate's nonce, the expectation taken back, one recipient only Co-Authored-By: Claude Fable 5.2 <[email protected]>
69 lines
3.8 KiB
TypeScript
69 lines
3.8 KiB
TypeScript
import type { BasePayload } from 'payload'
|
|
|
|
import { readdirSync, readFileSync } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { vi } from 'vitest'
|
|
|
|
import type { FakeRelayOptions } from '../../src/modules/email/relay/fake.js'
|
|
import type { RelayOptions, ResolvedRelayOptions } from '../../src/modules/email/relay/options.js'
|
|
|
|
import { relayConfirmEndpoint } from '../../src/modules/email/relay/confirm.js'
|
|
import { createFakeRelay } from '../../src/modules/email/relay/fake.js'
|
|
import { resolveRelayOptions } from '../../src/modules/email/relay/options.js'
|
|
|
|
export const CONTRACT_DIR = path.resolve(import.meta.dirname, '../../contracts/mail-relay')
|
|
|
|
export type ContractExample = {
|
|
client?: { reEnrol?: boolean; retry?: boolean; status?: null | string }
|
|
direction?: string
|
|
given?: Record<string, boolean | string>
|
|
id: string
|
|
request: { body?: unknown; headers?: Record<string, string>; method: string; path: string; schema?: string }
|
|
response: { body?: Record<string, unknown>; schema?: string; status: number }
|
|
then?: { mail?: Record<string, unknown>; mails?: number }
|
|
}
|
|
|
|
export const contractExamples = (): ContractExample[] =>
|
|
readdirSync(path.join(CONTRACT_DIR, 'examples'))
|
|
.filter((f) => f.endsWith('.json'))
|
|
.sort()
|
|
.map((f) => JSON.parse(readFileSync(path.join(CONTRACT_DIR, 'examples', f), 'utf8')) as ContractExample)
|
|
|
|
/** The slice of Payload the relay client touches: the key-value store, the config, the logger, the two globals. */
|
|
export function relayPayload({ custom, endpoints = [relayConfirmEndpoint()], integrations = {}, serverURL = 'https://example.com', siteName = '' }: { custom?: object; endpoints?: unknown[]; integrations?: Record<string, unknown>; serverURL?: string; siteName?: string } = {}) {
|
|
const store = new Map<string, unknown>()
|
|
const logger = { error: vi.fn(), info: vi.fn(), warn: vi.fn() }
|
|
const settings = { ...integrations }
|
|
const payload = {
|
|
config: { custom: custom ?? {}, endpoints, globals: [{ slug: 'site-integrations' }, { slug: 'site-settings' }], routes: { api: '/api' }, serverURL },
|
|
findGlobal: vi.fn(async ({ slug }: { slug: string }) => (slug === 'site-settings' ? { siteName } : settings)),
|
|
kv: {
|
|
delete: async (key: string) => void store.delete(key),
|
|
get: async (key: string) => (store.has(key) ? structuredClone(store.get(key)) : null),
|
|
set: async (key: string, value: object) => void store.set(key, structuredClone(value)),
|
|
},
|
|
logger,
|
|
updateGlobal: vi.fn(async ({ data }: { data: Record<string, unknown> }) => Object.assign(settings, data)),
|
|
}
|
|
return { logger, payload: payload as unknown as BasePayload, settings, store }
|
|
}
|
|
|
|
/** How the fake service reaches this site's confirm route: straight into the endpoint's handler. */
|
|
export function callConfirmRoute(payload: BasePayload) {
|
|
const endpoint = relayConfirmEndpoint()
|
|
return async (url: string, init: { body: string; headers: Record<string, string> }): Promise<Response> => {
|
|
const target = new URL(url)
|
|
if (target.pathname !== '/api/iapl/mail-relay/confirm') {return new Response(null, { status: 404 })}
|
|
const req = { headers: new Headers({ ...init.headers, 'x-forwarded-for': '198.51.100.7' }), payload, text: async () => init.body, url }
|
|
return (await endpoint.handler(req as never))
|
|
}
|
|
}
|
|
|
|
/** A site (the payload double) and a fake service that can call it back. */
|
|
export function relaySite({ fake: fakeOptions, options: own, ...site }: { fake?: FakeRelayOptions; options?: RelayOptions } & Parameters<typeof relayPayload>[0] = {}) {
|
|
const made = relayPayload(site)
|
|
const fake = createFakeRelay({ callSite: callConfirmRoute(made.payload), ...fakeOptions })
|
|
const options = resolveRelayOptions(made.payload, { fetchImpl: fake.fetch, retryBackoffMs: 1, url: fake.url, ...own }) as ResolvedRelayOptions
|
|
return { ...made, fake, options }
|
|
}
|