Files
ipal-kit/test/jobsAccess.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 9b83cc600a 1.18.0 upgrade notes
PROGRAMISTA §6 rows 83–87 now 1.18.0 (1.17.2 was never released alone) and new rows 88–94; iapl-upgrade blocks for
speed and growth and for drafts in related documents, the 1.17.2 blocks as "→ 1.18.0 (from 1.17.2)"; every "since
1.17.2" in docs, comments and tests reads 1.18.0; reference skills, AGENTS.md and the reference index on 1.18.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-12-02 09:38:00 +01:00

96 lines
4.5 KiB
TypeScript

/**
* Payload's job queue over HTTP (1.18.0, IAWL idea 35, owner's Q2): when the project writes no `jobs.access.run`,
* only an admin signed in with the second factor, or `Authorization: Bearer <CRON_SECRET>`, may run it. Payload's
* default let any signed-in account of any auth collection run the queue — a customer included.
*/
import type { Config, SanitizedConfig } from 'payload'
import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { afterEach, describe, expect, test, vi } from 'vitest'
import { jobsRunNotice, withJobsRunAccess } from '../src/modules/security/jobsAccess.js'
import { iapl } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
vi.mock('server-only', () => ({}))
const SECRET = 'a-cron-secret-of-32-characters!!'
const i18n = { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }
const project = (jobs?: Config['jobs']): Config =>
({
collections: [{ slug: 'customers', auth: true, fields: [] }, { slug: 'media', fields: [], upload: true }],
db: sqliteAdapter({ client: { url: 'file::memory:' } }),
jobs: { ...jobs, tasks: [{ slug: 'noop', handler: () => ({ output: {} }) }] },
secret: 'jobs-access-test',
telemetry: false,
}) as unknown as Config
const build = (jobs?: Config['jobs'], options: object = {}) =>
buildTestConfig({ ...project(jobs), plugins: [iapl({ access: { authCollection: 'users' }, i18n, twoFactor: false, ...options } as never)] })
/** Calls the sanitized run rule the way Payload's run endpoint does. */
const runAs = async (config: SanitizedConfig, user: unknown, authorization?: string) => {
const req = { headers: new Headers(authorization ? { authorization } : {}), payload: { config }, user }
return Boolean(await (config.jobs.access!.run as (a: { req: unknown }) => unknown)({ req }))
}
const admin = { id: 1, collection: 'users', roles: ['admin'] }
const editor = { id: 2, collection: 'users', roles: ['editor'] }
const customer = { id: 3, collection: 'customers' }
afterEach(() => {
vi.unstubAllEnvs()
})
describe('jobs.access.run when the project writes none', () => {
test('an admin, or the cron secret; not an editor, a customer or nobody', async () => {
vi.stubEnv('CRON_SECRET', SECRET)
const config = await build()
expect(await runAs(config, admin)).toBe(true)
expect(await runAs(config, null, `Bearer ${SECRET}`)).toBe(true)
expect(await runAs(config, null)).toBe(false)
expect(await runAs(config, customer)).toBe(false)
expect(await runAs(config, editor)).toBe(false)
expect(await runAs(config, null, 'Bearer wrong-secret-wrong-secret')).toBe(false)
expect(await runAs(config, customer, SECRET)).toBe(false) // without "Bearer "
}, 30_000)
test('a missing or short secret never matches', async () => {
vi.stubEnv('CRON_SECRET', 'short')
const config = await build()
expect(await runAs(config, null, 'Bearer short')).toBe(false)
vi.stubEnv('CRON_SECRET', '')
expect(await runAs(config, null, 'Bearer ')).toBe(false)
}, 30_000)
test('secretVar names another variable', async () => {
vi.stubEnv('MY_JOBS_SECRET', SECRET)
const config = await build(undefined, { jobsRunAccess: { secretVar: 'MY_JOBS_SECRET' } })
expect(await runAs(config, null, `Bearer ${SECRET}`)).toBe(true)
}, 30_000)
test('the project\'s own rule wins; jobsRunAccess: false keeps Payload\'s default', async () => {
const own = await build({ access: { run: () => true } })
expect(await runAs(own, null)).toBe(true)
const payloadDefault = await build(undefined, { jobsRunAccess: false })
expect(await runAs(payloadDefault, customer)).toBe(true) // Payload: any signed-in account
}, 30_000)
test('a config without jobs gets the rule too (Payload turns the queue on for scheduled publishing)', () => {
const config = withJobsRunAccess({ secret: 'x' } as Config)
expect(typeof config.jobs?.access?.run).toBe('function')
expect(withJobsRunAccess({ secret: 'x' } as Config, false).jobs).toBeUndefined()
})
test('at start: a note when the queue is on, the rule is iapl\'s and the secret is missing', () => {
const custom = { iapl: { jobsRunDefault: { secretVar: 'CRON_SECRET' } } }
vi.stubEnv('CRON_SECRET', '')
expect(jobsRunNotice({ custom, jobs: { enabled: true } })).toMatch(/Authorization: Bearer <CRON_SECRET>.*not set/)
expect(jobsRunNotice({ custom, jobs: { enabled: false } })).toBeNull()
expect(jobsRunNotice({ custom: {}, jobs: { enabled: true } })).toBeNull()
vi.stubEnv('CRON_SECRET', SECRET)
expect(jobsRunNotice({ custom, jobs: { enabled: true } })).toBeNull()
})
})