Files
ipal-kit/test/drafts.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 4bb9c663fd Cookie scanner, phase 2: browser crawl through a shared scanner service
cookieScanner.crawl (opt-in) adds "Crawl site" to the Cookie review box. The
site asks the shared scanner service (services/cookie-crawler: Node +
Playwright, Dockerfile, deployed once on Coolify) to crawl its sitemap pages
twice, with no consent and after "Accept all". The service posts cookies,
storage keys and third-party hosts back to POST /api/ipal/cookie-crawl/results
(bearer token, running job only). No browser in the site.

Findings land as drafts through the same merge rules. Anything stored before
consent that is not necessary, and tracker requests before consent, are
violations, flagged on the review entries from the latest crawl only. The
cookie-crawl global keeps the latest result (no history). A dashboard warning
(CookieCrawlAlert) and an opt-in e-mail through the project's mail adapter
follow a crawl with violations or new unknown cookies. Scheduled crawls: a
Coolify scheduled task with the token. ipalEnvRules({ cookieCrawl: true }).

Also: the six older real-Payload tests no longer leave an orphaned
payload generate:types process (typescript.autoGenerate: false).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-27 15:14:01 +01:00

117 lines
6.1 KiB
TypeScript

/** Unpublished drafts must never be served by the public routing helpers. */
import type { Payload } from 'payload'
import { sqliteAdapter } from '@payloadcms/db-sqlite'
import { lexicalEditor } from '@payloadcms/richtext-lexical'
import os from 'os'
import path from 'path'
import { buildConfig, getPayload } from 'payload'
import { afterAll, beforeAll, describe, expect, test } from 'vitest'
import { resolveRoute } from '../src/modules/content/resolveRoute.js'
import { buildNoindexField } from '../src/modules/seo/buildNoindexField.js'
import { ipalKit } from '../src/plugin.js'
let payload: Payload
beforeAll(async () => {
const config = buildConfig({
collections: [
{
slug: 'pages',
fields: [
{ name: 'title', type: 'text' },
{ name: 'slug', type: 'text', localized: true },
buildNoindexField(),
],
versions: { drafts: true },
},
{ slug: 'posts', fields: [{ name: 'title', type: 'text' }, { name: 'slug', type: 'text', localized: true }], versions: { drafts: true } },
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'ipal-test-media') } },
],
db: sqliteAdapter({ client: { url: 'file::memory:' }, push: true }),
editor: lexicalEditor(),
plugins: [
ipalKit({
access: { authCollection: 'users' },
content: { collections: [{ slug: 'posts' }] },
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
pages: { slug: 'pages' },
twoFactor: false,
}),
],
secret: 'drafts-test',
telemetry: false,
typescript: { autoGenerate: false }, // no orphaned `payload generate:types` child
})
payload = await getPayload({ config })
}, 60_000)
afterAll(async () => {
await payload?.destroy()
})
describe('drafts', () => {
test('a never-published draft page is not resolvable', async () => {
await payload.create({ collection: 'pages' as never, data: { slug: 'tajne', _status: 'draft', title: 'Tajne' } as never, draft: true })
expect(await resolveRoute({ locale: 'pl', payload, segments: ['tajne'] })).toBeNull()
})
test('a published page is resolvable, and a later draft does not replace it', async () => {
const doc = await payload.create({ collection: 'pages' as never, data: { slug: 'oferta', _status: 'published', title: 'Oferta' } as never })
await payload.update({ id: doc.id, collection: 'pages' as never, data: { title: 'Oferta — szkic zmian' } as never, draft: true })
const route = await resolveRoute({ locale: 'pl', payload, segments: ['oferta'] })
expect((route?.doc as { title?: string })?.title).toBe('Oferta')
})
})
describe('archive listing', () => {
test('lists only published entries when the collection has drafts', async () => {
const { getArchiveEntries } = await import('../src/modules/content/getArchiveEntries.js')
await payload.create({ collection: 'posts' as never, data: { slug: 'jawny', _status: 'published', title: 'Jawny' } as never })
await payload.create({ collection: 'posts' as never, data: { slug: 'szkic', _status: 'draft', title: 'Szkic' } as never, draft: true })
const list = await getArchiveEntries({ collection: 'posts', locale: 'pl', payload })
expect(list.docs.map((d) => (d as { title: string }).title)).toEqual(['Jawny'])
expect(list.totalDocs).toBe(1)
const preview = await getArchiveEntries({ collection: 'posts', draft: true, locale: 'pl', payload })
expect(preview.totalDocs).toBe(2)
})
})
describe('noindex protection', () => {
test('the homepage cannot be set to noindex; an ordinary page can', async () => {
const home = await payload.create({ collection: 'pages' as never, data: { slug: 'start', _status: 'published', title: 'Start' } as never })
const other = await payload.create({ collection: 'pages' as never, data: { slug: 'dziekujemy', _status: 'published', title: 'Dziękujemy' } as never })
await payload.updateGlobal({ slug: 'site-settings' as never, data: { homepage: home.id, siteName: 'Acme' } as never })
await expect(payload.update({ id: home.id, collection: 'pages' as never, data: { noindex: true } as never })).rejects.toThrow(/Exclude from indexing/)
const updated = (await payload.update({ id: other.id, collection: 'pages' as never, data: { noindex: true } as never })) as { noindex?: boolean }
expect(updated.noindex).toBe(true)
})
})
describe('content entries: own JSON-LD and links', () => {
test('pageJsonLd.entries replaces the article node; link options know the archives', async () => {
const { createContentHelpers } = await import('../src/modules/frontend/createContentHelpers.js')
const { buildProductJsonLd } = await import('../src/modules/seo/buildProductJsonLd.js')
const archive = await payload.create({ collection: 'pages' as never, data: { slug: 'wyroby', _status: 'published', title: 'Wyroby' } as never })
await payload.updateGlobal({ slug: 'site-settings' as never, data: { postsArchive: archive.id, siteName: 'Acme' } as never })
await payload.create({ collection: 'posts' as never, data: { slug: 'pierscionek', _status: 'published', title: 'Pierścionek' } as never })
const h = createContentHelpers({
baseUrl: 'https://acme.pl',
config: payload.config,
content: { collections: [{ slug: 'posts' }] },
i18n: { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }] },
pageJsonLd: {
entries: { posts: ({ doc, siteName, url }) => buildProductJsonLd({ name: String(doc.title), brand: siteName, url }) },
},
})
const nodes = await h.getPageJsonLd('pl', ['wyroby', 'pierscionek'])
expect(nodes.map((n) => n['@type'])).toEqual(['Product', 'BreadcrumbList'])
expect(nodes[0]).toMatchObject({ name: 'Pierścionek', brand: { name: 'Acme' } })
expect(nodes[0]).not.toHaveProperty('offers')
const options = await h.getLinkOptions('pl')
expect(options.archives).toEqual({ posts: 'wyroby' })
expect(await h.resolveLink({ type: 'internal', label: 'x', page: { relationTo: 'posts', value: { slug: 'pierscionek' } } }, 'pl')).toMatchObject({ href: '/pl/wyroby/pierscionek' })
})
})