buildR2Storage registered nothing without R2_* env, so `pnpm dev` / generate:importmap in dev
wrote an importMap.js without S3ClientUploadHandler — committed, the production admin (R2 on)
rendered a blank page with no console error (hit vanstev.pl). The injected s3Storage is now
always registered (enabled: false without R2 → local disk); storage-s3 still adds its client
handler, so the import map is identical in every environment. ipal-audit flags an import map
without the handler.
R2_PUBLIC_URL alone (absolute, outside production) = read-only media: URLs from the media
domain, upload/delete refused with 403 — local dev against a production database without
bucket keys. ipalEnvRules allows it in development.
Cookie Settings (MongoDB): the 1.11.1 rename buttons.save → savePreferences lost the texts on
the first save; onInit copies the old value (all locales) when the new field is missing.
getConsentTexts read the old field as a string, Mongo returns the raw { pl, en } map.
Also: context.ipalSkipAutoFill (SKIP_AUTO_FILL) — save without meta / aiSummary auto-fill;
createPageRoute redirects the homepage under its own slug (homeAlias, default 'redirect');
ipal-audit: multi-line tsconfig exclude, numeronyms (a11y) not "numbered fields",
`${prefix}/${slug}` links and slugs typed into links detected, clearer panel-language message.
Docs: fallback-on-save in i18n.md, `pnpm up --latest` and the stale-token 401 in PROGRAMISTA §1.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
102 lines
4.5 KiB
TypeScript
102 lines
4.5 KiB
TypeScript
import type { Config } from 'payload'
|
|
|
|
import { s3Storage } from '@payloadcms/storage-s3'
|
|
import { afterEach, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { buildR2Storage } from '../src/modules/storage/buildR2Storage.js'
|
|
|
|
const r2 = {
|
|
R2_ACCESS_KEY_ID: 'k',
|
|
R2_BUCKET: 'b',
|
|
R2_ENDPOINT: 'https://acc.r2.cloudflarestorage.com',
|
|
R2_PUBLIC_URL: 'https://media.acme.pl',
|
|
R2_SECRET_ACCESS_KEY: 's',
|
|
}
|
|
const HANDLER = '@payloadcms/storage-s3/client#S3ClientUploadHandler'
|
|
|
|
function stub(env: Record<string, string>) {
|
|
vi.unstubAllEnvs()
|
|
for (const key of Object.keys(r2)) {vi.stubEnv(key, '')}
|
|
for (const [k, v] of Object.entries(env)) {vi.stubEnv(k, v)}
|
|
}
|
|
|
|
const baseConfig = (): Config =>
|
|
({ collections: [{ slug: 'media', fields: [], upload: true }] }) as unknown as Config
|
|
|
|
/** Admin component paths the import map is generated from. */
|
|
function adminDependencies(config: Config): string[] {
|
|
const deps = (config.admin?.dependencies ?? {}) as Record<string, { path?: string }>
|
|
return Object.values(deps).map((d) => d.path ?? '')
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.unstubAllEnvs()
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
describe('buildR2Storage — import map independent of env (1.12.2)', () => {
|
|
test('no R2 env: storage-s3 registered disabled, its client handler still in admin dependencies', async () => {
|
|
stub({ NODE_ENV: 'development' })
|
|
const config = await buildR2Storage(['media'], { s3Storage })(baseConfig())
|
|
expect(adminDependencies(config)).toContain(HANDLER)
|
|
// still local disk: no adapter, no disableLocalStorage
|
|
const media = config.collections?.find((c) => c.slug === 'media')
|
|
expect((media?.upload as { disableLocalStorage?: boolean }).disableLocalStorage).toBeUndefined()
|
|
})
|
|
|
|
test('full R2 env: same handler (the import maps match)', async () => {
|
|
stub({ ...r2, NODE_ENV: 'production' })
|
|
const config = await buildR2Storage(['media'], { s3Storage })(baseConfig())
|
|
expect(adminDependencies(config)).toContain(HANDLER)
|
|
})
|
|
|
|
test('no adapter injected and no R2 env: plain passthrough (local-disk projects)', async () => {
|
|
stub({ NODE_ENV: 'development' })
|
|
const config = await buildR2Storage(['media'])(baseConfig())
|
|
expect(adminDependencies(config)).not.toContain(HANDLER)
|
|
})
|
|
})
|
|
|
|
describe('buildR2Storage — read-only media (R2_PUBLIC_URL only, outside production)', () => {
|
|
type Hooks = {
|
|
afterRead: ((a: { doc: Record<string, unknown> }) => Record<string, unknown>)[]
|
|
beforeOperation: ((a: { args: { req?: { file?: unknown } }; operation: string }) => unknown)[]
|
|
}
|
|
async function mediaHooks(env: Record<string, string>): Promise<Hooks | undefined> {
|
|
vi.spyOn(console, 'warn').mockImplementation(() => {})
|
|
stub(env)
|
|
const config = await buildR2Storage(['media'], { s3Storage })(baseConfig())
|
|
return config.collections?.find((c) => c.slug === 'media')?.hooks as unknown as Hooks | undefined
|
|
}
|
|
|
|
test('URLs point at the public domain, sizes included', async () => {
|
|
const hooks = await mediaHooks({ NODE_ENV: 'development', R2_PUBLIC_URL: 'https://media.acme.pl/' })
|
|
const doc = hooks!.afterRead[0]({
|
|
doc: {
|
|
filename: 'logo plik.png',
|
|
sizes: { thumbnail: { filename: 'logo-400x300.png', url: '/api/media/file/logo-400x300.png' } },
|
|
thumbnailURL: '/api/media/file/logo-400x300.png',
|
|
url: '/api/media/file/logo plik.png',
|
|
},
|
|
})
|
|
expect(doc.url).toBe('https://media.acme.pl/logo%20plik.png')
|
|
expect(doc.thumbnailURL).toBe('https://media.acme.pl/logo-400x300.png')
|
|
expect((doc.sizes as Record<string, { url: string }>).thumbnail.url).toBe('https://media.acme.pl/logo-400x300.png')
|
|
})
|
|
|
|
test('uploads and deletes are refused, metadata edits pass', async () => {
|
|
const hooks = await mediaHooks({ NODE_ENV: 'development', R2_PUBLIC_URL: 'https://media.acme.pl' })
|
|
const guard = hooks!.beforeOperation[0]
|
|
expect(() => guard({ args: { req: { file: {} } }, operation: 'create' })).toThrow(/read-only/)
|
|
expect(() => guard({ args: { req: {} }, operation: 'delete' })).toThrow(/read-only/)
|
|
expect(() => guard({ args: { req: {} }, operation: 'update' })).not.toThrow()
|
|
})
|
|
|
|
test('not in production, not with credentials, not with a relative URL', async () => {
|
|
expect(await mediaHooks({ NODE_ENV: 'production', R2_PUBLIC_URL: 'https://media.acme.pl' })).toBeUndefined()
|
|
expect(await mediaHooks({ NODE_ENV: 'development', R2_PUBLIC_URL: '/media' })).toBeUndefined()
|
|
const partial = await mediaHooks({ NODE_ENV: 'development', R2_BUCKET: 'b', R2_PUBLIC_URL: 'https://media.acme.pl' })
|
|
expect(partial).toBeUndefined()
|
|
})
|
|
})
|