Files
ipal-kit/test/iawlIdeasBContent.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 ecf03c8dc0 IAWL ideas, group B: sizes before work, one link rule, no stand-in locale, strict switches
- Request bodies from strangers are read only up to a cap, declared length or not:
  mail-service confirm, CSP reports, page views, MCP (maxBytes, 413). readCappedBody moves
  to shared/ (the consent log keeps exporting it).
- searchContent / createSearchRoute / MCP search: queryLimit (default 200 characters,
  10 words) before the database; an unknown ?locale is the default locale throughout.
- Rich text: autolinks and links marked internal pass safeHref too; lexicalToMarkdown
  drops unsafe link targets.
- truncate() never returns more than its limit (summary / meta autofill after validation).
- resolveRoute answers null for a locale the site does not have (xx, all, *): no
  default-locale page under another code (page route past the proxy, /md/xx, MCP).
- A 404 page that is a draft is not shown (resolveNotFoundPage, getNotFoundPage).
- readPublished() access rule (exported); Site integrations readVersions: admin.
- Plugin switches, buildCsp, buildSecurityHeaders and the seed guard refuse nonsense
  values at start; an unknown role name is met by nobody, requireRole throws on one.
- Docs, AGENT.md, iapl-collection and iapl-audit skills.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-26 20:25:26 +01:00

100 lines
5.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* IAWL ideas 23–25 against a running Payload (SQLite): what a visitor's URL can make the content helpers serve.
* 23 — a draft is shown only where it is asked for: never as the site's 404 page.
* 24/25 — IAPL has no hidden locales; the stand-in it must not invent is a page in a locale the site does not
* have (`/md/xx`, `/xx/…` past the proxy, `all`): those are "not found", not the default locale's page.
*/
import type { Payload } from 'payload'
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 { createMarkdownRoute } from '../src/modules/content/createMarkdownRenderer.js'
import { resolveNotFoundPage } from '../src/modules/content/resolveNotFoundPage.js'
import { resolveRoute } from '../src/modules/content/resolveRoute.js'
import { iapl } from '../src/plugin.js'
import { buildTestConfig } from './helpers.js'
import { testDatabase } from './http/db.js'
vi.mock('server-only', () => ({}))
const i18n = { defaultLocale: 'pl', locales: [{ code: 'pl', label: 'Polski' }, { code: 'en', label: 'English' }] } as const
let payload: Payload
let draft404: number | string
let published404: number | string
beforeAll(async () => {
const config = buildTestConfig({
collections: [
{
slug: 'pages',
access: { read: () => true },
fields: [
{ name: 'title', type: 'text', localized: true },
{ name: 'slug', type: 'text', localized: true },
{ name: 'layout', type: 'blocks', blocks: [{ slug: 'text', fields: [{ name: 'body', type: 'textarea', localized: true }] }] },
],
versions: { drafts: true },
},
{ slug: 'media', fields: [], upload: { staticDir: path.join(os.tmpdir(), 'iawlb-content-media') } },
],
db: testDatabase('iawlb_content'),
editor: lexicalEditor(),
plugins: [iapl({ access: { authCollection: 'users' }, i18n, pages: { slug: 'pages' }, twoFactor: false })],
secret: 'iawl-ideas-b-content',
telemetry: false,
})
payload = await getPayload({ config })
const home = await payload.create({ collection: 'pages' as never, data: { slug: 'start', _status: 'published', layout: [{ blockType: 'text', body: 'Witamy' }], title: 'Start' } as never, locale: 'pl' })
await payload.update({ id: home.id, collection: 'pages' as never, data: { slug: 'home', _status: 'published', title: 'Home' } as never, locale: 'en' })
draft404 = (await payload.create({ collection: 'pages' as never, data: { slug: 'nie-znaleziono', _status: 'draft', title: 'Szkic: nowa cena 999 zł' } as never, draft: true, locale: 'pl' })).id
published404 = (await payload.create({ collection: 'pages' as never, data: { slug: 'brak', _status: 'published', title: 'Nie ma takiej strony' } as never, locale: 'pl' })).id
await payload.updateGlobal({ slug: 'site-settings' as never, data: { homepage: home.id, notFound: draft404, siteName: 'Test' } as never })
}, 60_000)
afterAll(async () => {
await payload?.destroy()
})
describe('idea 23: a never-published page is not the 404 page', () => {
test('resolveNotFoundPage gives the built-in view (null) while the assigned page is a draft', async () => {
expect(await resolveNotFoundPage({ locale: 'pl', payload })).toBeNull()
})
test('once a published page is assigned, it is the 404 page', async () => {
await payload.updateGlobal({ slug: 'site-settings' as never, data: { notFound: published404 } as never })
expect(await resolveNotFoundPage({ locale: 'pl', payload })).toMatchObject({ title: 'Nie ma takiej strony' })
await payload.updateGlobal({ slug: 'site-settings' as never, data: { notFound: draft404 } as never })
})
test('createContentHelpers().getNotFoundPage keeps the draft out too', async () => {
const { createContentHelpers } = await import('../src/modules/frontend/createContentHelpers.js')
const helpers = createContentHelpers({ config: payload.config, i18n })
expect(await helpers.getNotFoundPage('pl')).toBeNull()
})
})
describe('ideas 24/25: no stand-in locale', () => {
test('a configured locale resolves its own page', async () => {
expect(await resolveRoute({ locale: 'en', payload, segments: [] })).toMatchObject({ doc: { title: 'Home' } })
})
test.each(['xx', 'all', '*', 'de'])('locale %j resolves nothing (not the default locale\'s page)', async (locale) => {
expect(await resolveRoute({ locale, payload, segments: [] })).toBeNull()
})
test('the Markdown route answers 404 for /md/xx and /md/all', async () => {
const { GET } = createMarkdownRoute({ config: i18n, getCachedPayload: async () => payload })
for (const locale of ['xx', 'all']) {
const res = await GET(new Request(`http://site.test/md/${locale}`), { params: Promise.resolve({ locale }) })
expect(res.status).toBe(404)
}
const ok = await GET(new Request('http://site.test/md/pl'), { params: Promise.resolve({ locale: 'pl' }) })
expect(ok.status).toBe(200)
expect(await ok.text()).toContain('Witamy')
})
})