M1 review round 2: the transport is no way round, nobody raises its own role, late staff collections

R2-1 the SMTP password is bound to server, port and user whatever the transport says; the transport alone is not bound.
R2-2 where the plugin's update rule is in force the roles field is closed for the account itself
     (the plugin's roles: only an admin; roles of the project's own: never one's own).
R2-3 auth collections a later plugin adds get the account rules, the password rule and the first-user endpoint when
     the config goes live; a staff slug that is not an auth collection stops the start.
R2-4 smtpRequireTls is taken out: the branch adds no field, column or collection.
Also: read (a staff account) and unlock (an admin) defaults on staff collections (accounts.staffOnlyRead);
doctor staff-own-roles and read / unlock in staff-accounts-open; r1 NULL against the default, also inside a row;
r2 bound values compared exactly.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-25 00:39:24 +01:00
co-authored by Claude Fable 5.2
parent a32329fbb4
commit 7002ebbfa6
20 changed files with 519 additions and 122 deletions
+44 -30
View File
@@ -33,7 +33,7 @@ let auth: Record<string, string>
const stored = async () => (await payload.findGlobal({ slug: 'site-integrations' as never })) as unknown as Record<string, unknown>
const api = (pathname: string, init: RequestInit = {}, headers: Record<string, string> = {}) => fetch(`${server.url}/api${pathname}`, { ...init, headers: { 'content-type': 'application/json', ...auth, ...headers } })
const save = (data: Record<string, unknown>, headers: Record<string, string> = {}) => api(G, { body: JSON.stringify(data), method: 'POST' }, headers)
const reset = () => payload.updateGlobal({ slug: 'site-integrations' as never, data: { emailTransport: 'smtp', smtpFromAddress: '[email protected]', smtpHost: 'smtp.example.com', smtpPassword: SMTP, smtpPort: 587, smtpRequireTls: false, smtpUser: 'mailer' } as never })
const reset = () => payload.updateGlobal({ slug: 'site-integrations' as never, data: { emailTransport: 'smtp', smtpFromAddress: '[email protected]', smtpHost: 'smtp.example.com', smtpPassword: SMTP, smtpPort: 587, smtpUser: 'mailer' } as never })
/** An SMTP server that offers no STARTTLS and writes down what a client logs in with. */
const fakeSmtp = async () => {
@@ -127,7 +127,10 @@ describe('the SMTP password stays with the server it was saved for (secrets.reen
['the server', { smtpHost: 'other.example.com' }],
['the port', { smtpPort: 2525 }],
['the user name', { smtpUser: 'someone-else' }],
['"require an encrypted connection"', { smtpRequireTls: true }],
['the server to another letter case', { smtpHost: 'SMTP.example.com' }],
['the server by a space', { smtpHost: 'smtp.example.com ' }],
['the server while naming Microsoft Graph as the transport', { emailTransport: 'graph', smtpHost: 'other.example.com' }],
['the port while naming Microsoft Graph as the transport', { emailTransport: 'graph', smtpPort: 2525 }],
['the server, sending the mask back as the panel does', { smtpHost: 'other.example.com', smtpPassword: SECRET_MASK }],
['the server, with an empty password', { smtpHost: 'other.example.com', smtpPassword: '' }],
['the server, in a draft', { _status: 'draft', smtpHost: 'other.example.com' }],
@@ -154,27 +157,55 @@ describe('the SMTP password stays with the server it was saved for (secrets.reen
expect((await save({ smtpHost: 'fourth.example.com' })).status).toBe(200)
})
test('a save that changes nothing the password belongs to keeps it: the same values sent back, other fields, another letter case', async () => {
test('a save that changes nothing the password belongs to keeps it: the same values sent back, other fields, the port as text', async () => {
const doc = (await (await api(G)).json()) as Record<string, unknown>
for (const key of ['id', 'createdAt', 'updatedAt', 'globalType']) {delete doc[key]}
expect((await save(doc)).status).toBe(200) // the whole form, as the panel sends it
expect((await save({ smtpFromName: 'The site', smtpHost: ' SMTP.example.com ', smtpPort: '587', smtpUser: 'mailer' })).status).toBe(200)
expect((await save({ smtpFromName: 'The site', smtpHost: 'smtp.example.com', smtpPort: '587', smtpUser: 'mailer' })).status).toBe(200)
expect((await save({ turnstileSiteKey: 'site-key' })).status).toBe(200)
expect(await stored()).toMatchObject({ smtpFromName: 'The site', smtpPassword: SMTP })
})
test('a document saved before the port and the checkbox had a value: their defaults are not a change', async () => {
await payload.db.updateGlobal({ slug: 'site-integrations', data: { ...(await stored()), smtpPort: null, smtpRequireTls: null } as never })
expect((await save({ smtpFromName: 'Again', smtpPort: 587, smtpRequireTls: false })).status).toBe(200)
test('a stored NULL where the field has a default (a SQL row, an older document) is not a change', async () => {
// the port sits inside a row; its default is 587, the transport's is 'smtp'
await payload.db.updateGlobal({ slug: 'site-integrations', data: { ...(await stored()), emailTransport: null, smtpPort: null } as never })
expect((await save({ smtpFromName: 'Again' })).status).toBe(200)
expect((await save({ emailTransport: 'smtp', smtpFromName: 'Again', smtpPort: 587 })).status).toBe(200)
await payload.db.updateGlobal({ slug: 'site-integrations', data: { ...(await stored()), emailTransport: null, smtpPort: null } as never })
expect((await save({ smtpFromName: 'Again', smtpPort: null })).status).toBe(200)
const doc = (await (await api(G)).json()) as Record<string, unknown>
for (const key of ['id', 'createdAt', 'updatedAt', 'globalType']) {delete doc[key]}
expect((await save(doc)).status).toBe(200)
expect((await save({ smtpPort: 2525 })).status).toBe(400)
expect((await stored()).smtpPassword).toBe(SMTP)
})
test('switching to Microsoft Graph asks for nothing (the password is not used there); switching back to another server does', async () => {
expect((await save({ emailTransport: 'graph' })).status).toBe(200)
expect((await stored()).smtpPassword).toBe(SMTP)
expect((await save({ emailTransport: 'smtp', smtpHost: 'other.example.com' })).status).toBe(400)
expect((await save({ emailTransport: 'smtp' })).status).toBe(400) // the transport itself is one of the settings
expect((await save({ emailTransport: 'smtp', smtpPassword: 'typed-again' })).status).toBe(200)
// Review round 2 (R2-1): "Microsoft Graph" in the panel does not mean Graph sends — the mail adapter falls back to
// SMTP when the GRAPH_* variables are missing. The transport decides nothing; the server, port and user do.
test('the transport switch is not a way round: Graph + my server in one save, or Graph first and my server after', async () => {
const mine = await fakeSmtp()
try {
const gql = async (data: string) => (await (await api('/graphql', { body: JSON.stringify({ query: `mutation { updateSiteIntegration(data: { ${data} }) { smtpHost } }` }), method: 'POST' })).json()) as { errors?: unknown[] }
// 1. in one save, over REST and over GraphQL
expect((await save({ emailTransport: 'graph', smtpHost: '127.0.0.1', smtpPort: mine.port })).status).toBe(400)
expect((await gql(`emailTransport: graph, smtpHost: "127.0.0.1", smtpPort: ${mine.port}`)).errors?.length).toBeGreaterThan(0)
await testEmail()
// 2. the switch alone passes (the password is not asked for, and stays)…
expect((await save({ emailTransport: 'graph' })).status).toBe(200)
// …and the server afterwards does not, with or without naming the transport again
expect((await save({ smtpHost: '127.0.0.1', smtpPort: mine.port })).status).toBe(400)
expect((await save({ emailTransport: 'graph', smtpHost: '127.0.0.1', smtpPort: mine.port })).status).toBe(400)
expect((await save({ emailTransport: 'smtp', smtpHost: '127.0.0.1', smtpPort: mine.port })).status).toBe(400)
expect((await gql(`smtpHost: "127.0.0.1", smtpPort: ${mine.port}`)).errors?.length).toBeGreaterThan(0)
await testEmail()
await api('/ipal/test-email', { body: JSON.stringify({ attachmentMB: 5, to: '[email protected]' }), method: 'POST' })
expect(mine.seen).toEqual({ auth: '', mails: 0 })
expect(await stored()).toMatchObject({ emailTransport: 'graph', smtpHost: 'smtp.example.com', smtpPassword: SMTP, smtpPort: 587 })
// back to SMTP alone: the same server as before, nothing to ask
expect((await save({ emailTransport: 'smtp' })).status).toBe(200)
} finally {
await mine.close()
}
})
test('server code (a seed, the Local API with access control off) moves the server and keeps the password', async () => {
@@ -205,23 +236,6 @@ describe('the SMTP password stays with the server it was saved for (secrets.reen
})
})
describe('"Require an encrypted connection" (smtpRequireTls)', () => {
test('a server that offers no encryption never sees the password; without the option it does, as before', async () => {
const plain = await fakeSmtp()
try {
expect((await save({ smtpHost: '127.0.0.1', smtpPassword: SMTP, smtpPort: plain.port, smtpRequireTls: true })).status).toBe(200)
await testEmail()
expect(plain.seen).toEqual({ auth: '', mails: 0 })
expect((await save({ smtpPassword: SMTP, smtpRequireTls: false })).status).toBe(200)
await testEmail()
expect(plain.seen.auth).toContain(SMTP)
expect(plain.seen.mails).toBe(1)
} finally {
await plain.close()
}
})
})
describe('versions on the global: a restore does not hand the value back', () => {
test('REST restore answers with the mask; the stored value is the restored one', async () => {
await save({ smtpFromName: 'One' })