A stored consent counts only under the version it was given for, so a lower Cookies →
Consent version made consent given under an older policy valid again. A save through access
control (panel, REST, GraphQL) with a lower number is now refused with a message (English and
Polish); the same or a higher number and saves without it pass; the Local API with
overrideAccess may still lower it. iapl({ cookieSettings: { allowLowerPolicyVersion: true } })
keeps the old behaviour. Building blocks exported: policyVersionNeverDown,
buildCookieSettingsFields, buildCookieSettings. No schema change.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
107 lines
6.0 KiB
TypeScript
107 lines
6.0 KiB
TypeScript
/**
|
|
* Cookie settings → Consent version only goes up (owner's decision Q2, 1.17.2). A stored consent counts only under
|
|
* the version it was given for: a LOWER number made old consent cookies valid again. A save through access control
|
|
* with a lower number is refused with a message; the Local API with `overrideAccess` may still lower it;
|
|
* `cookieSettings: { allowLowerPolicyVersion: true }` keeps the old behaviour.
|
|
* Real HTTP; SQLite by default, Postgres / MongoDB with IPAL_TEST_POSTGRES_URI / IPAL_TEST_MONGO_URI.
|
|
*/
|
|
|
|
import { lexicalEditor } from '@payloadcms/richtext-lexical'
|
|
import os from 'os'
|
|
import path from 'path'
|
|
import { getPayload } from 'payload'
|
|
import { afterAll, beforeAll, describe, expect, test, vi } from 'vitest'
|
|
|
|
import { iapl } from '../src/plugin.js'
|
|
import { buildTestConfig } from './helpers.js'
|
|
import { testDatabase } from './http/db.js'
|
|
import { startPayloadServer } from './http/server.js'
|
|
|
|
vi.mock('server-only', () => ({}))
|
|
|
|
const PASSWORD = 'a-long-test-password-1'
|
|
|
|
async function site(name: string, cookieSettings?: { allowLowerPolicyVersion?: boolean }) {
|
|
const config = buildTestConfig({
|
|
admin: { user: 'users' },
|
|
collections: [{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), `iapl-${name}`) } }],
|
|
db: testDatabase(name),
|
|
editor: lexicalEditor(),
|
|
plugins: [iapl({ access: { authCollection: 'users' }, ...(cookieSettings ? { cookieSettings } : {}), i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, mail: { relay: false }, twoFactor: false })],
|
|
secret: `${name}-0123456789-0123456789`,
|
|
telemetry: false,
|
|
})
|
|
const payload = await getPayload({ config })
|
|
const server = await startPayloadServer(config)
|
|
await payload.create({ collection: 'users' as never, data: { email: '[email protected]', password: PASSWORD, roles: ['editor'] } as never })
|
|
const { token } = (await (await fetch(`${server.url}/api/users/login`, { body: JSON.stringify({ email: '[email protected]', password: PASSWORD }), headers: { 'content-type': 'application/json' }, method: 'POST' })).json()) as { token: string }
|
|
await payload.updateGlobal({ slug: 'cookie-settings' as never, data: { policyVersion: 3 } as never })
|
|
const save = (data: Record<string, unknown>) =>
|
|
fetch(`${server.url}/api/globals/cookie-settings`, { body: JSON.stringify(data), headers: { authorization: `JWT ${token}`, 'content-type': 'application/json' }, method: 'POST' })
|
|
const gql = (query: string) =>
|
|
fetch(`${server.url}/api/graphql`, { body: JSON.stringify({ query }), headers: { authorization: `JWT ${token}`, 'content-type': 'application/json' }, method: 'POST' }).then((r) => r.json() as Promise<{ errors?: { message: string }[] }>)
|
|
const stored = async () => ((await payload.findGlobal({ slug: 'cookie-settings' as never })) as { policyVersion?: number }).policyVersion
|
|
return { gql, payload, save, server, stored }
|
|
}
|
|
|
|
describe('by default the consent version only goes up', () => {
|
|
let s: Awaited<ReturnType<typeof site>>
|
|
beforeAll(async () => {
|
|
s = await site('policy_rising')
|
|
}, 120_000)
|
|
afterAll(async () => {
|
|
await s?.server.close()
|
|
await s?.payload.destroy?.()
|
|
})
|
|
|
|
test('a lower number is refused with the message, and the stored one stays', async () => {
|
|
const res = await s.save({ policyVersion: 1 })
|
|
expect(res.status).toBe(400)
|
|
expect(JSON.stringify(await res.json())).toContain('The consent version cannot go down: it is 3 now')
|
|
expect(await s.stored()).toBe(3)
|
|
})
|
|
|
|
test('over GraphQL too', async () => {
|
|
const res = await s.gql('mutation { updateCookieSetting(data: { policyVersion: 2 }) { policyVersion } }')
|
|
expect(res.errors?.[0]?.message ?? '').toMatch(/invalid|cannot go down/i)
|
|
expect(await s.stored()).toBe(3)
|
|
})
|
|
|
|
test('the same number, a higher one, and a save that does not send it all pass', async () => {
|
|
expect((await s.save({ policyVersion: 3 })).status).toBe(200)
|
|
expect((await s.save({ title: 'Cookies' })).status).toBe(200)
|
|
expect((await s.save({ policyVersion: 4 })).status).toBe(200)
|
|
expect(await s.stored()).toBe(4)
|
|
})
|
|
|
|
test('the Local API with access control off may lower it (a script an admin runs)', async () => {
|
|
await s.payload.updateGlobal({ slug: 'cookie-settings' as never, data: { policyVersion: 2 } as never })
|
|
expect(await s.stored()).toBe(2)
|
|
})
|
|
|
|
test('the Polish text exists', async () => {
|
|
const { adminTexts } = await import('../src/modules/admin-i18n/texts/index.js')
|
|
expect(JSON.stringify(adminTexts)).toContain('Wersji zgody nie można obniżyć')
|
|
})
|
|
})
|
|
|
|
describe('cookieSettings: { allowLowerPolicyVersion: true } keeps the old behaviour', () => {
|
|
test('the field has no consent-version rule then, and has it by default', async () => {
|
|
const { buildCookieSettings } = await import('../src/globals/CookieSettings/index.js')
|
|
const { policyVersionNeverDown } = await import('../src/globals/CookieSettings/fields.js')
|
|
const hooksOf = (g: { fields: unknown[] }) =>
|
|
((g.fields as { hooks?: { beforeValidate?: unknown[] }; name?: string }[]).find((f) => f.name === 'policyVersion')?.hooks?.beforeValidate ?? [])
|
|
expect(hooksOf(buildCookieSettings())).toContain(policyVersionNeverDown)
|
|
expect(hooksOf(buildCookieSettings({ allowLowerPolicyVersion: true }))).not.toContain(policyVersionNeverDown)
|
|
})
|
|
|
|
test('iapl() passes the option through', async () => {
|
|
const { buildConfig } = await import('payload')
|
|
const { policyVersionNeverDown } = await import('../src/globals/CookieSettings/fields.js')
|
|
const config = await iapl({ cookieSettings: { allowLowerPolicyVersion: true }, i18n: { defaultLocale: 'en', locales: [{ code: 'en', label: 'English' }] }, mail: { relay: false }, twoFactor: false })({ collections: [], secret: 'x' } as never)
|
|
void buildConfig
|
|
const g = config.globals!.find((x) => x.slug === 'cookie-settings') as { fields: { hooks?: { beforeValidate?: unknown[] }; name?: string }[] }
|
|
expect(g.fields.find((f) => f.name === 'policyVersion')?.hooks?.beforeValidate ?? []).not.toContain(policyVersionNeverDown)
|
|
})
|
|
})
|