Files
punimtag/e2e/tests/viewer.write-gates.spec.ts
T
ilia 4273162d9e
CI / skip-ci-check (pull_request) Successful in 4s
CI / docker-ci (pull_request) Successful in 6s
CI / secret-scan (pull_request) Successful in 11s
CI / viewer-unit (pull_request) Failing after 1m3s
CI / admin-unit (pull_request) Failing after 1m1s
CI / e2e (pull_request) Failing after 59s
test: interceptNetworkCall + Zod widening, PROD smoke, NextAuth write gates
- upload.smoke.spec.ts / gallery.search-filters.spec.ts: replace
  waitForResponse/text-scrape with typed interceptNetworkCall spies (status
  + JSON shape) on the real upload POST and the filtered /api/search GET.
- api.fastapi-login.spec.ts: Zod-validate the FastAPI TokenResponse and
  /auth/me UserResponse instead of loose casts. gallery.search-filters.spec.ts
  gets the same treatment for the Prisma-backed SearchResponse.
- prod.smoke.spec.ts: opt-in (PROD_BASE_URL) health + login-page check on a
  real PROD host; skips as a no-op until the PROD LXC exists (none does yet
  per `pct list` — see ROADMAP).
- viewer.write-gates.spec.ts: NextAuth (browser-session) hasWriteAccess gate
  on POST /api/faces/{id}/identify — viewer 403s, admin passes through.
  Provisioned a third, independent auth-DB user (e2e-viewer@levkine.ca,
  hasWriteAccess=false) for this via ansible's provision-punimtag-e2e-user.py
  (see that repo for the vault/Infisical/Gitea/Vaultwarden side).
2026-07-14 22:07:30 -04:00

61 lines
2.4 KiB
TypeScript

import path from 'node:path';
import { test, expect } from '../fixtures';
/**
* NextAuth (browser-session) write gates — `session.user.hasWriteAccess`
* checks in viewer-frontend route handlers (see
* `app/api/faces/[id]/identify/route.ts`). Distinct from the FastAPI
* role-permission gates in `api.role-permissions.spec.ts` (separate user
* store, bearer auth instead of session cookies).
*
* Requires `E2E_VIEWER_EMAIL`/`PASSWORD` (auth-DB viewer, hasWriteAccess=false)
* and the admin storageState from `auth.setup.ts`.
*
* Uses a nonexistent face id so the *write-access* gate is what's being
* proven, not a real mutation: the route checks `hasWriteAccess` before
* loading the face, so viewer never reaches the 404 branch.
*/
const nonExistentFaceId = 999999999;
const viewerReady = Boolean(process.env.E2E_VIEWER_EMAIL && process.env.E2E_VIEWER_PASSWORD);
test.describe('viewer write gates (NextAuth, viewer) @smoke', () => {
test.use({ storageState: path.join(__dirname, '../.auth/viewer.json') });
test.skip(!viewerReady, 'E2E_VIEWER_EMAIL/PASSWORD required');
test('viewer without write access is denied on POST /api/faces/{id}/identify', async ({
page,
playkitConfig,
timings,
}) => {
const res = await timings.measure('viewer_identify', () =>
page.request.post(`${playkitConfig.baseUrl}/api/faces/${nonExistentFaceId}/identify`, {
data: { firstName: 'Test', lastName: 'Viewer' },
}),
);
expect(res.status()).toBe(403);
const body = await res.json();
expect(body).toMatchObject({ error: expect.stringMatching(/write access/i) });
});
});
test.describe('viewer write gates (NextAuth, admin) @smoke', () => {
test.use({ storageState: path.join(__dirname, '../.auth/admin.json') });
test('admin (write access) passes the gate on POST /api/faces/{id}/identify', async ({
page,
playkitConfig,
timings,
}) => {
const res = await timings.measure('admin_identify', () =>
page.request.post(`${playkitConfig.baseUrl}/api/faces/${nonExistentFaceId}/identify`, {
data: { firstName: 'Test', lastName: 'Admin' },
}),
);
// Admin clears the write-access gate; a nonexistent face id then 404s —
// proves the gate didn't block a legitimate write-access user.
expect(res.status()).toBe(404);
const body = await res.json();
expect(body).toMatchObject({ error: expect.stringMatching(/face not found/i) });
});
});