Files
ipal-kit/reference/e2e/closed-doors.spec.ts
T
Radosław SmolińskiandClaude Opus 5.5 2468390538 Outside-in tests (IAWL ideas 33, 34): closed doors on the built site as anonymous, customer and editor; every public route judged
- reference/e2e/closed-doors.ts + closed-doors.spec.ts: one table of doors, asked by pnpm e2e:built in both bundlers;
  doors still open while the owner decides are asserted open (test.fail) with their question
- listPublicRoutes / listNextRouteFiles / compareRouteSnapshot / updateRouteSnapshot (main entry), runRouteCheck (/doctor)
- reference/e2e/routes.ts + routes.snapshot.json (395 routes, one reason each); pnpm routes:check [--update], first step of e2e:built
- create-test-admin.ts makes an editor test account
- docs: security.md sections, AGENT.md 8a, iapl-audit templates, iapl-upgrade 1.17.1, changelog draft

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

87 lines
4.5 KiB
TypeScript

import { type APIRequestContext, expect, test } from '@playwright/test'
import { randomUUID } from 'node:crypto'
import { type Caller, DOORS, type DoorAnswer, refused } from './closed-doors'
import { editorTestAccount, totp } from './totp'
/**
* Closed doors from outside (IAWL idea 33): every door of e2e/closed-doors.ts asked on the BUILT site in production
* mode — `pnpm e2e:built` runs it after a webpack and a Turbopack build — as an anonymous visitor, a customer and an
* editor. Some behaviour differs outside production (Payload's GraphQL playground and introspection), so this is not
* a unit test. The customer needs the late-staff fixture (E2E_LATE_STAFF=1, a self-signing-up `e2e-visitors`
* collection); the editor the E2E_* test admin values (e2e/create-test-admin.ts makes the editor account).
*/
const { E2E_ADMIN_EMAIL: adminEmail, E2E_ADMIN_PASSWORD: password, E2E_LATE_STAFF: lateStaff, E2E_TOTP_SECRET: secret } = process.env
type Ctx = { api: APIRequestContext; email?: string }
async function signIn(caller: Caller, baseURL: string, playwright: { request: { newContext: (o: object) => Promise<APIRequestContext> } }): Promise<Ctx | null> {
const api = await playwright.request.newContext({ baseURL, extraHTTPHeaders: { origin: baseURL } })
if (caller === 'anonymous') {return { api }}
if (caller === 'customer') {
if (lateStaff !== '1') {return null}
const email = `door-${randomUUID().slice(0, 8)}@example.com`
const pass = `D-${randomUUID()}`
expect((await api.post('/api/e2e-visitors', { data: { email, password: pass } })).status()).toBe(201)
expect((await api.post('/api/e2e-visitors/login', { data: { email, password: pass } })).status()).toBe(200)
return { api, email }
}
if (!adminEmail || !password || !secret) {return null}
const email = editorTestAccount(adminEmail)
expect((await api.post('/api/users/login', { data: { email, password } })).status()).toBe(200)
expect((await (await api.post('/api/verify-totp', { data: { token: totp(secret) } })).json()).ok).toBe(true)
return { api, email }
}
async function ask(api: APIRequestContext, door: (typeof DOORS)[number]): Promise<DoorAnswer> {
const res = door.graphql
? await api.post('/api/graphql', { data: { query: door.graphql } })
: await api.fetch(door.path!, { data: door.body, maxRedirects: 0, method: door.method ?? 'GET' })
const text = await res.text()
let body: unknown = text
try {
body = JSON.parse(text)
} catch {
// not JSON
}
return { body, location: res.headers().location ?? null, status: res.status() }
}
for (const caller of ['anonymous', 'customer', 'editor'] as const) {
test.describe(`closed doors as ${caller === 'anonymous' ? 'an anonymous visitor' : `a ${caller}`}`, () => {
let ctx: Ctx | null = null
test.beforeAll(async ({ baseURL, playwright }) => {
ctx = await signIn(caller, baseURL!, playwright)
})
test.afterAll(async () => {
await ctx?.api.dispose()
})
for (const door of DOORS) {
const open = door.openFor?.callers.includes(caller)
if (!door.closedFor.includes(caller) && !open) {continue}
test(`${open ? '[open, waiting for the owner] ' : ''}${door.name}`, async () => {
test.skip(!ctx, caller === 'customer' ? 'E2E_LATE_STAFF=1 not set (the customer collection is the fixture\'s)' : 'E2E_* test admin not configured')
// still open while the owner decides: this asserts that it IS open; once closed, the test "passes unexpectedly"
if (open) {test.fail(true, door.openFor!.question)}
const answer = await ask(ctx!.api, door)
const closed = (door.closed ?? refused)(answer, { email: ctx!.email })
expect(closed, `${door.graphql ?? `${door.method ?? 'GET'} ${door.path}`} → ${answer.status} ${JSON.stringify(answer.body).slice(0, 300)}`).toBe(true)
})
}
})
}
test.describe('closed doors: the proxy skips /api, /admin and /_next as whole segments only (IAWL idea 36)', () => {
test('the admin and the API are not sent to a locale; a page that only starts with those letters is', async ({ request }) => {
for (const path of ['/admin', '/api/health']) {
const res = await request.get(path, { maxRedirects: 0 })
expect([path, res.headers().location ?? '']).not.toEqual([path, expect.stringMatching(/^(?:https?:\/\/[^/]+)?\/(?:pl|en)\//)])
}
for (const path of ['/administracja', '/apis']) {
const res = await request.get(path, { maxRedirects: 0 })
expect([path, res.status(), new URL(res.headers().location ?? '/', 'http://x').pathname]).toEqual([path, 307, `/pl${path}`])
}
})
})