Files
picpeak/tests/e2e/optional-email-event-creation.spec.ts
T
Paul Nothaft 44adabca9e test(e2e): read the admin JWT from the cookie, not the login body (#1071)
Three specs acquire an admin token with `const body = await res.json();
return body.token`. The admin login has not returned a token in its body
for some time — establishAdminSession() sets the JWT as the httpOnly
`admin_token` cookie and responds with `res.json({ user })` — so the
token was undefined and every one of them failed at the first assertion,
before exercising anything they were written to cover.

Server-side the cookie and an Authorization: Bearer header are
interchangeable (see middleware/gallery.js, which reads the cookie first
and accepts an admin-typed Bearer second), so the fix is to read the
value back out of the context cookie jar and keep threading it as a
Bearer. Every downstream call in these specs stays exactly as it was.

Measured against a real stack, running only these three files:

  before   0 passed, 6 failed   — all six at the token assertion
  after    3 passed, 3 failed

The three that still fail no longer fail on auth: they get deep into the
flow and then miss UI that has since changed (a settings label, a
locator that no longer resolves). That is a separate and much larger
staleness problem across this directory — a full run is 12 passed
against roughly two dozen failures of that kind — and it is not
addressed here.

Worth knowing: no CI workflow runs tests/e2e at all, which is why this
rotted silently while `npm run test:e2e` stayed documented in CLAUDE.md.
Wiring it up is the obvious follow-up, but it has to wait until the
suite is actually green, or it would just pin main red.

Claude-Session: https://claude.ai/code/session_01Ra4hcsYiKuQLbbRsg6EjAc

Co-authored-by: Paul Nothaft <paul@MacStudio-von-Paul.local>
2026-08-18 22:14:00 +02:00

157 lines
5.3 KiB
TypeScript

import { test, expect, Page } from '@playwright/test';
const ADMIN_EMAIL = process.env.ADMIN_EMAIL || 'admin@example.com';
const ADMIN_PASSWORD = process.env.ADMIN_PASSWORD || 'Admin!234';
async function getAdminToken(page: Page): Promise<string> {
const res = await page.request.post('/api/auth/admin/login', {
data: { username: ADMIN_EMAIL, password: ADMIN_PASSWORD },
});
expect(res.ok()).toBeTruthy();
// The admin JWT is delivered as the httpOnly `admin_token` cookie, not in
// the response body. Server-side the cookie and an Authorization: Bearer
// header are interchangeable, so read it back out of the context jar and
// keep threading it as a Bearer — every downstream call stays as it was.
const cookies = await page.context().cookies();
const token = cookies.find((c) => c.name === 'admin_token')?.value;
expect(token, 'admin_token cookie missing from the login response').toBeTruthy();
return token as string;
}
async function updateEventSettings(
page: Page,
token: string,
settings: Record<string, boolean>
) {
const res = await page.request.put('/api/admin/settings/general', {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: settings,
});
expect(res.ok()).toBeTruthy();
}
test.describe('Optional email fields in event creation (#217)', () => {
test('event creation succeeds with empty emails when set to optional', async ({ page }) => {
const token = await getAdminToken(page);
// Disable email requirements
await updateEventSettings(page, token, {
event_require_customer_email: false,
event_require_admin_email: false,
});
try {
// Create event with empty email fields
const eventRes = await page.request.post('/api/admin/events', {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: {
event_type: 'wedding',
event_name: `E2E Optional Emails ${Date.now()}`,
event_date: new Date(Date.now() + 7 * 86400000).toISOString().slice(0, 10),
customer_name: 'Test Host',
customer_email: '',
admin_email: '',
password: 'TestPass123!',
expiration_days: 30,
},
});
const body = await eventRes.json();
expect(eventRes.ok(), `Expected 200 but got ${eventRes.status()}: ${JSON.stringify(body)}`).toBeTruthy();
expect(body.id).toBeTruthy();
// Cleanup: delete the created event
await page.request.delete(`/api/admin/events/${body.id}`, {
headers: { Authorization: `Bearer ${token}` },
});
} finally {
// Revert settings to required
await updateEventSettings(page, token, {
event_require_customer_email: true,
event_require_admin_email: true,
});
}
});
test('event creation still fails with empty emails when set to required', async ({ page }) => {
const token = await getAdminToken(page);
// Ensure email requirements are enabled
await updateEventSettings(page, token, {
event_require_customer_email: true,
event_require_admin_email: true,
});
const eventRes = await page.request.post('/api/admin/events', {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: {
event_type: 'wedding',
event_name: `E2E Required Emails ${Date.now()}`,
event_date: new Date(Date.now() + 7 * 86400000).toISOString().slice(0, 10),
customer_name: 'Test Host',
customer_email: '',
admin_email: '',
password: 'TestPass123!',
expiration_days: 30,
},
});
expect(eventRes.status()).toBe(400);
const body = await eventRes.json();
const paths = body.errors.map((e: { path: string }) => e.path);
expect(paths).toContain('customer_email');
expect(paths).toContain('admin_email');
});
test('event creation succeeds with missing email fields when optional', async ({ page }) => {
const token = await getAdminToken(page);
// Disable email requirements
await updateEventSettings(page, token, {
event_require_customer_email: false,
event_require_admin_email: false,
});
try {
// Create event without email fields at all (undefined, not empty string)
const eventRes = await page.request.post('/api/admin/events', {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
data: {
event_type: 'wedding',
event_name: `E2E Missing Emails ${Date.now()}`,
event_date: new Date(Date.now() + 7 * 86400000).toISOString().slice(0, 10),
customer_name: 'Test Host',
password: 'TestPass123!',
expiration_days: 30,
},
});
const body = await eventRes.json();
expect(eventRes.ok(), `Expected 200 but got ${eventRes.status()}: ${JSON.stringify(body)}`).toBeTruthy();
expect(body.id).toBeTruthy();
// Cleanup
await page.request.delete(`/api/admin/events/${body.id}`, {
headers: { Authorization: `Bearer ${token}` },
});
} finally {
await updateEventSettings(page, token, {
event_require_customer_email: true,
event_require_admin_email: true,
});
}
});
});