Files
admin-frontend-domain/tests/e2e/fm-csrf.spec.ts
T
naibaandcloudcode 76b9eb6a54 test(e2e): send CSRF token on mutating requests and fix PAT UI selectors
The backend CSRF double-submit gate rejects unsafe methods unless
X-CSRF-Token mirrors the signed nz-csrf cookie. page.request bypasses the
SPA JS that does this, so every mutating E2E call got 403, failing the suite.

- Add csrfHeaders(page) helper that mirrors the nz-csrf cookie into the
  header, polling until the cookie is readable to avoid the post-login race.
- Apply it to all cookie-authenticated POST/PATCH/DELETE calls (the /mcp
  Bearer calls stay header-free since PAT requests are CSRF-exempt).
- loginAs waits for the nz-csrf cookie before returning.
- Fix the create-token dialog submit selector: the button is labelled
  'Create API token' (t('CreateApiToken')), not 'Create'.

Verified 8/8 passing across repeated CI-mode runs against a real backend.

Co-authored-by: cloudcode <cloudcode@users.noreply.github.com>
2026-05-31 08:17:15 +00:00

23 lines
859 B
TypeScript

import { expect } from "@playwright/test"
import { csrfHeaders, test } from "./fixtures"
test("file manager creation only accepts POST", async ({ adminPage: page }) => {
const getResp = await page.request.get("/api/v1/file?id=1", {
failOnStatusCode: false,
})
expect(
getResp.status() === 404 || getResp.status() === 405,
`GET /api/v1/file must no longer be routable (got ${getResp.status()})`,
).toBeTruthy()
const postResp = await page.request.post("/api/v1/file?id=1", {
failOnStatusCode: false,
headers: await csrfHeaders(page),
})
expect(postResp.status()).toBe(200)
const body = await postResp.json()
expect(body.success, "without a connected agent server the POST surfaces a Service error, but the route is reachable").not.toBe(true)
expect(body.error).toBeTruthy()
})