Files
admin-frontend-domain/tests/e2e/auth.spec.ts
T
naibaandcloudcode 7057aa3098 test(e2e): fix revoke empty-list crash and password-restore CSRF cascade
Two CI-only failures surfaced against a fresh backend DB:

- The revoke test read after.data.find(), but the list endpoint omits data
  entirely when the admin has zero tokens, throwing on undefined. Default to [].
- The password-change test's restore POST hit a 403: changing the password
  triggers a refresh-token that re-mints the nz-csrf cookie, so the X-CSRF-Token
  read just before the request can be stale. A failed restore left the admin on
  the rotated password and cascaded into cron/fm/visibility login failures.
  Add csrfRequest(), which retries once on 403 after re-reading the cookie, and
  use it for both profile mutations.

Verified 8/8 passing across repeated fresh-DB CI-mode runs.

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

58 lines
2.2 KiB
TypeScript

import { expect } from "@playwright/test"
import { csrfRequest, defaultAdmin, expectAuthenticated, expectUnauthenticated, loginAs, test } from "./fixtures"
test("login persists session via cookie and getProfile succeeds", async ({ page }) => {
await loginAs(page, defaultAdmin)
await expectAuthenticated(page)
})
test("password change rotates TokenVersion and revokes existing session", async ({ page }) => {
await loginAs(page, defaultAdmin)
const originalCookies = await page.context().cookies()
const originalJWT = originalCookies.find((c) => c.name === "nz-jwt")?.value
expect(originalJWT, "login must set nz-jwt cookie").toBeTruthy()
const newPassword = `e2e-${Date.now().toString(36)}`
let isOnNewPassword = false
try {
const resp = await csrfRequest(page, "post", "/api/v1/profile", {
data: {
original_password: defaultAdmin.password,
new_password: newPassword,
new_username: defaultAdmin.username,
reject_password: false,
},
})
expect(resp.ok(), "profile update must succeed").toBeTruthy()
isOnNewPassword = true
await page.context().clearCookies()
if (originalJWT) {
await page.context().addCookies([
{
name: "nz-jwt",
value: originalJWT,
url: page.url() || "http://localhost:5173",
},
])
}
await expectUnauthenticated(page)
} finally {
if (isOnNewPassword) {
await page.context().clearCookies()
await loginAs(page, { username: defaultAdmin.username, password: newPassword })
const restoreResp = await csrfRequest(page, "post", "/api/v1/profile", {
data: {
original_password: newPassword,
new_password: defaultAdmin.password,
new_username: defaultAdmin.username,
reject_password: false,
},
})
expect(restoreResp.ok(), "password restore must succeed so other suites can still log in").toBeTruthy()
}
}
})