Small fixes: optional R2_PUBLIC_URL, seo.uploadsCollection, staff features without roles
- ipalEnvRules({ storage: true }): R2_PUBLIC_URL optional (media served by
the app); storage: { requirePublicUrl: true } requires it again.
- seo.uploadsCollection: upload collection of the meta image (default media).
- userMeetsRole(req, minimum): the plugin's admin/editor features (Site
Integrations, settings globals, test e-mail, stats, consent records, form
submissions) stay reachable when `access` is omitted — without a roles
field every logged-in account passes, as in Payload's default.
- IndexNow skip for seed writes is covered by test/seed.test.ts.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
cff76c2a69
commit
4911da7112
+16
-2
@@ -12,10 +12,10 @@ const r2 = {
|
||||
R2_SECRET_ACCESS_KEY: 's',
|
||||
}
|
||||
|
||||
function check(env: Record<string, string>) {
|
||||
function check(env: Record<string, string>, storage: NonNullable<Parameters<typeof ipalEnvRules>[0]>['storage'] = true) {
|
||||
vi.unstubAllEnvs()
|
||||
for (const [k, v] of Object.entries(env)) {vi.stubEnv(k, v)}
|
||||
return () => assertEnv(ipalEnvRules({ storage: true }), { env })
|
||||
return () => assertEnv(ipalEnvRules({ storage }), { env })
|
||||
}
|
||||
|
||||
afterEach(() => vi.unstubAllEnvs())
|
||||
@@ -26,6 +26,20 @@ describe('ipalEnvRules({ storage })', () => {
|
||||
expect(check({ ...base, ...r2, NODE_ENV: 'production' })).not.toThrow()
|
||||
})
|
||||
|
||||
test('production: R2_PUBLIC_URL is optional by default (media served by the app)', () => {
|
||||
const { R2_PUBLIC_URL: _unused, ...credentials } = r2
|
||||
expect(check({ ...base, ...credentials, NODE_ENV: 'production' })).not.toThrow()
|
||||
// still validated when set
|
||||
expect(check({ ...base, ...r2, NODE_ENV: 'production', R2_PUBLIC_URL: '/media' })).toThrow(/absolute URL in production/)
|
||||
})
|
||||
|
||||
test('requirePublicUrl: true makes R2_PUBLIC_URL required with the credentials', () => {
|
||||
const { R2_PUBLIC_URL: _unused, ...credentials } = r2
|
||||
expect(check({ ...base, ...credentials, NODE_ENV: 'production' }, { requirePublicUrl: true })).toThrow(/R2_PUBLIC_URL is required/)
|
||||
expect(check({ ...base, ...r2, NODE_ENV: 'production' }, { requirePublicUrl: true })).not.toThrow()
|
||||
expect(check({ ...base, NODE_ENV: 'development' }, { requirePublicUrl: true })).not.toThrow()
|
||||
})
|
||||
|
||||
test('development: no R2 at all is fine (local disk)', () => {
|
||||
expect(check({ ...base, NODE_ENV: 'development' })).not.toThrow()
|
||||
})
|
||||
|
||||
@@ -86,3 +86,60 @@ describe('ipalKit — auth collection', () => {
|
||||
expect(users?.[0].fields.some((f) => 'name' in f && f.name === 'fullName')).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe('options that keep features reachable and overridable', () => {
|
||||
const findField = (fields: unknown[], name: string): Record<string, unknown> | undefined => {
|
||||
for (const f of fields as Record<string, unknown>[]) {
|
||||
if (f.name === name) {return f}
|
||||
const nested = [...((f.fields as unknown[]) ?? []), ...((f.tabs as { fields: unknown[] }[]) ?? []).flatMap((t) => t.fields)]
|
||||
const hit = nested.length ? findField(nested, name) : undefined
|
||||
if (hit) {return hit}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
const withPages = (): Config =>
|
||||
({
|
||||
collections: [
|
||||
{ slug: 'users', auth: true, fields: [] },
|
||||
{ slug: 'pages', fields: [{ name: 'title', type: 'text' }] },
|
||||
],
|
||||
secret: 'test',
|
||||
}) as unknown as Config
|
||||
|
||||
test('seo.uploadsCollection: the meta image relates to the given upload collection (default media)', async () => {
|
||||
const custom = await ipalKit({ i18n, seo: { collections: ['pages'], uploadsCollection: 'images' }, twoFactor: false })(withPages())
|
||||
const meta = findField(custom.collections!.find((c) => c.slug === 'pages')!.fields, 'meta')!
|
||||
expect(findField(meta.fields as unknown[], 'image')?.relationTo).toBe('images')
|
||||
|
||||
const plain = await ipalKit({ i18n, seo: { collections: ['pages'] }, twoFactor: false })(withPages())
|
||||
const metaDefault = findField(plain.collections!.find((c) => c.slug === 'pages')!.fields, 'meta')!
|
||||
expect(findField(metaDefault.fields as unknown[], 'image')?.relationTo).toBe('media')
|
||||
})
|
||||
|
||||
test('without `access` (no roles field) admin and editor features stay reachable for logged-in users', async () => {
|
||||
const config = await ipalKit({ consentLog: true, forms: {}, i18n, stats: true, twoFactor: false })(base())
|
||||
const users = config.collections!.find((c) => c.slug === 'users')!
|
||||
const req = (user: unknown) => ({ payload: { collections: { users: { config: users } } }, user }) as never
|
||||
const member = { id: 1, collection: 'users', email: '[email protected]' }
|
||||
const run = (access: unknown, user: unknown) => (access as (a: { req: unknown }) => unknown)({ req: req(user) })
|
||||
|
||||
const integrations = config.globals!.find((g) => g.slug === 'site-integrations')!
|
||||
expect(run(integrations.access!.read, member)).toBe(true)
|
||||
expect(run(integrations.access!.read, null)).toBe(false)
|
||||
expect(run(config.globals!.find((g) => g.slug === 'site-settings')!.access!.update, member)).toBe(true)
|
||||
expect(run(config.collections!.find((c) => c.slug === 'stats-visits')!.access!.read, member)).toBe(true)
|
||||
expect(run(config.collections!.find((c) => c.slug === 'consent-records')!.access!.read, member)).toBe(true)
|
||||
expect(run(config.collections!.find((c) => c.slug === 'form-submissions')!.access!.read, member)).toBe(true)
|
||||
})
|
||||
|
||||
test('with `access` the roles still decide', async () => {
|
||||
const config = await ipalKit({ access: { authCollection: 'users' }, i18n, stats: true, twoFactor: false })(base())
|
||||
const users = config.collections!.find((c) => c.slug === 'users')!
|
||||
const req = (user: unknown) => ({ payload: { collections: { users: { config: users } } }, user }) as never
|
||||
const read = config.globals!.find((g) => g.slug === 'site-integrations')!.access!.read as (a: { req: unknown }) => unknown
|
||||
expect(read({ req: req({ id: 1, collection: 'users', roles: ['editor'] }) })).toBe(false)
|
||||
expect(read({ req: req({ id: 1, collection: 'users', roles: ['admin'] }) })).toBe(true)
|
||||
// unknown collection = the strict side
|
||||
expect(read({ req: { payload: { collections: {} }, user: { id: 1, collection: 'other' } } })).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user