Sending method, review M-1 and M-2: the plugin's own status write never stores a choice; a non-method is refused
- M-1: the save rule of `emailMethod` ran after the early return for the plugin's status writes, so the first failed (or recovered) send stored the method the read showed as if an admin had chosen it. The rule is now a hook of its own that runs on every write: a write that does not name the method leaves what is stored, empty included. - M-2: a sent value that is not null or one of the three methods (a number, a list, another word) is refused with a ValidationError (400) before it reaches the database. Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
co-authored by
Claude Fable 5.2
parent
f2da28e7cb
commit
8c19391590
@@ -92,36 +92,46 @@ export function buildSiteIntegrations({
|
||||
...(writeOnly ? { afterRead: [maskSecretsOnRestore] } : {}),
|
||||
// What the caller sent for the sending method, before Payload merges the stored document into the data.
|
||||
beforeChange: [
|
||||
// Keys edited in the panel → clear that integration's old status, so a stale
|
||||
// "not working" banner doesn't linger until the next form submission.
|
||||
// "Sending method" (`emailMethod`) — on EVERY write, the plugin's own included (the status written after a
|
||||
// send): `data` is merged with the document as it was READ by now, and that read shows the method in effect
|
||||
// as selected — which must never be stored as a choice nobody made. So: what the caller really sent was
|
||||
// noted before the merge (beforeOperation below; without that note, a value that differs from the one read
|
||||
// was sent), and what is stored was noted by the field's own read.
|
||||
({ context, data, originalDoc, req }) => {
|
||||
if (context?.ipalIntegrationStatus || !data) {return data}
|
||||
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
|
||||
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
|
||||
// "Sending method" (`emailMethod`). `data` is merged with the document as it was READ by now, and that read
|
||||
// shows the method in effect as selected — which must not be stored as a choice nobody made. So: what the
|
||||
// caller really sent was noted before the merge (beforeOperation below; without that note, a value that
|
||||
// differs from the one read was sent), and what is stored was noted by the field's own read.
|
||||
const noted = context?.iaplMailSent as { method?: unknown; sent?: boolean; transport?: boolean } | undefined
|
||||
if (!data) {return data}
|
||||
const noted = context?.iaplMailSent as { sent?: boolean; transport?: boolean } | undefined
|
||||
const sent = noted ? noted.sent : data.emailMethod !== originalDoc?.emailMethod
|
||||
const stored = isMailMethod(context?.iaplMailMethodStored) ? context.iaplMailMethodStored : null
|
||||
if (sent && isMailMethod(data.emailMethod)) {
|
||||
if (data.emailMethod === 'relay' && !mailRelay) {
|
||||
throw new ValidationError({ errors: [{ message: 'This sending method is switched off for this site.', path: 'emailMethod' }], global: 'site-integrations' }, req.t)
|
||||
const refuse = (message: string): never => {
|
||||
throw new ValidationError({ errors: [{ message, path: 'emailMethod' }], global: 'site-integrations' }, req.t)
|
||||
}
|
||||
if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
|
||||
// anything but one of the three values (a number, a list, another word) is refused here: the database
|
||||
// would answer with an error of its own, or take it
|
||||
if (!isMailMethod(data.emailMethod)) {return refuse('This is not a sending method.')}
|
||||
if (data.emailMethod === 'relay' && !mailRelay) {return refuse('This sending method is switched off for this site.')}
|
||||
if (shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay) !== data.emailMethod) {
|
||||
data.emailStatus = { checkedAt: null, code: null, message: null, since: null, state: null }
|
||||
}
|
||||
if (shownMailMethod({ ...(originalDoc ?? {}), emailMethod: stored } as never, mailRelay) !== data.emailMethod) {data.emailStatus = cleared}
|
||||
// the stored field of 1.6 – 1.17.0 follows where it can say it
|
||||
data.emailTransport = data.emailMethod === 'graph' ? 'graph' : 'smtp'
|
||||
} else if (sent && data.emailMethod !== null && data.emailMethod !== undefined) {
|
||||
// not a method: left as it is — the field's own validation refuses it
|
||||
} else if (sent) {
|
||||
data.emailMethod = null // un-chosen on purpose (`setMailMethod(payload, null)`): the order of 1.17.0 again
|
||||
} else if ((noted ? noted.transport : true) && changed('emailTransport')) {
|
||||
} else if ((noted ? noted.transport : true) && 'emailTransport' in data && data.emailTransport !== originalDoc?.emailTransport) {
|
||||
// written the old way (a script from before 1.17.1): it means what it meant — the order of 1.17.0
|
||||
data.emailMethod = null
|
||||
} else {
|
||||
// a write that does not name the method leaves what is stored — empty included
|
||||
data.emailMethod = stored
|
||||
}
|
||||
return data
|
||||
},
|
||||
// Keys edited in the panel → clear that integration's old status, so a stale
|
||||
// "not working" banner doesn't linger until the next form submission.
|
||||
({ context, data, originalDoc }) => {
|
||||
if (context?.ipalIntegrationStatus || !data) {return data}
|
||||
const changed = (k: string) => k in data && data[k] !== originalDoc?.[k]
|
||||
const cleared = { checkedAt: null, code: null, message: null, since: null, state: null }
|
||||
if (changed('turnstileSecretKey') || changed('turnstileSiteKey')) {data.turnstileStatus = cleared}
|
||||
if (Object.keys(data).some((k) => (k === 'emailTransport' || k.startsWith('smtp')) && changed(k))) {
|
||||
data.emailStatus = cleared
|
||||
|
||||
@@ -251,6 +251,48 @@ describe('the choice: what is chosen is what is used', () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe('review M-1, M-2', () => {
|
||||
test('M-1: the plugin\'s own status write never stores a choice — a failed send, then a send that works again', async () => {
|
||||
// an upgraded site with an SMTP server nobody listens on: nothing chosen, SMTP shown
|
||||
await asStored(SMTP)
|
||||
expect(await getMailMethod(payload)).toBeNull()
|
||||
await send().catch(() => undefined)
|
||||
expect((await status())?.state).toBe('degraded') // the status WAS written
|
||||
expect(await getMailMethod(payload)).toBeNull()
|
||||
// the admin empties the SMTP server later: the site goes back to the service, as the old order says
|
||||
await payload.updateGlobal({ slug: 'site-integrations' as never, data: NO_SMTP as never })
|
||||
expect(await getMailMethod(payload)).toBeNull()
|
||||
// (emptying the server cleared the old status; a status written the plugin's way, then a send that works: degraded → ok)
|
||||
await payload.updateGlobal({ slug: 'site-integrations' as never, context: { ipalIntegrationStatus: true }, data: { emailStatus: { code: 'relay-error', state: 'degraded' } } as never })
|
||||
expect(await getMailMethod(payload)).toBeNull()
|
||||
expect(await send()).toMatchObject({ transport: 'relay' })
|
||||
expect((await status())?.state).toBe('ok')
|
||||
expect(await getMailMethod(payload)).toBeNull()
|
||||
expect((await read()).emailMethod).toBe('relay')
|
||||
// and a stored choice survives the same writes
|
||||
await setMailMethod(payload, 'smtp')
|
||||
await send()
|
||||
expect((await status())?.code).toBe('smtp-incomplete')
|
||||
expect(await getMailMethod(payload)).toBe('smtp')
|
||||
await setMailMethod(payload, 'relay')
|
||||
await send()
|
||||
expect(await getMailMethod(payload)).toBe('relay')
|
||||
})
|
||||
|
||||
test('M-2: anything but null or one of the three values is refused with 400, and nothing is stored', async () => {
|
||||
await asStored({})
|
||||
for (const bad of [0, 1, true, ['relay'], { method: 'relay' }, 'sendmail', '', 'RELAY']) {
|
||||
const res = await api('/globals/site-integrations', { body: JSON.stringify({ emailMethod: bad }), method: 'POST' })
|
||||
expect(res.status, JSON.stringify(bad)).toBe(400)
|
||||
await expect(payload.updateGlobal({ slug: 'site-integrations' as never, data: { emailMethod: bad } as never }), JSON.stringify(bad)).rejects.toThrow()
|
||||
}
|
||||
expect(await getMailMethod(payload)).toBeNull()
|
||||
expect((await api('/globals/site-integrations', { body: JSON.stringify({ emailMethod: null }), method: 'POST' })).status).toBe(200)
|
||||
expect((await api('/globals/site-integrations', { body: JSON.stringify({ emailMethod: 'smtp' }), method: 'POST' })).status).toBe(200)
|
||||
expect(await getMailMethod(payload)).toBe('smtp')
|
||||
})
|
||||
})
|
||||
|
||||
describe('resolveMailMethod', () => {
|
||||
const full = { smtpHost: 'smtp.example.com', smtpPassword: 'x', smtpUser: 'u' }
|
||||
const half = { smtpHost: 'smtp.example.com' }
|
||||
|
||||
Reference in New Issue
Block a user