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:
Radosław Smoliński
2026-11-26 23:08:34 +01:00
co-authored by Claude Fable 5.2
parent f2da28e7cb
commit 8c19391590
2 changed files with 69 additions and 17 deletions
+42
View File
@@ -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' }