Files
static-pages/apps/survey.immich.app/e2e/helpers.ts
T
2026-08-19 14:53:56 +02:00

115 lines
3.3 KiB
TypeScript

const API = process.env.API_URL || 'http://localhost:8787';
const TEST_PASSWORD = process.env.TEST_PASSWORD || 'e2e-test-password-12345';
let sessionCookie: string | null = null;
export async function ensureAuth(): Promise<string> {
if (sessionCookie) {return sessionCookie;}
const meRes = await fetch(`${API}/api/auth/me`);
const me = (await meRes.json()) as { authenticated: boolean; needsSetup?: boolean };
if (me.needsSetup) {
const setupRes = await fetch(`${API}/api/auth/setup`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password: TEST_PASSWORD }),
});
const cookie = setupRes.headers.get('set-cookie');
if (cookie) {
sessionCookie = cookie.split(';', 1)[0];
return sessionCookie;
}
}
const loginRes = await fetch(`${API}/api/auth/password-login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ password: TEST_PASSWORD }),
});
const cookie = loginRes.headers.get('set-cookie');
if (cookie) {
sessionCookie = cookie.split(';', 1)[0];
}
if (!sessionCookie) {
throw new Error('Failed to authenticate for E2E tests');
}
return sessionCookie;
}
export function getAuthHeaders(): Record<string, string> {
const headers: Record<string, string> = { 'Content-Type': 'application/json' };
if (sessionCookie) {
headers['Cookie'] = sessionCookie;
}
return headers;
}
export async function apiPost(path: string, body?: unknown, retries = 3) {
await ensureAuth();
for (let attempt = 0; attempt < retries; attempt++) {
const res = await fetch(`${API}${path}`, {
method: 'POST',
headers: getAuthHeaders(),
...(body && { body: JSON.stringify(body) }),
});
if (res.status === 503 && attempt < retries - 1) {
// Retry on 503 (transient DO initialization failure)
await new Promise((r) => setTimeout(r, 1000));
continue;
}
if (!res.ok && res.status !== 201) {
const text = await res.text();
throw new Error(`POST ${path} failed (${res.status}): ${text}`);
}
if (res.status === 204) {return undefined;}
return res.json();
}
}
export async function apiPut(path: string, body?: unknown) {
await ensureAuth();
const res = await fetch(`${API}${path}`, {
method: 'PUT',
headers: getAuthHeaders(),
...(body && { body: JSON.stringify(body) }),
});
if (!res.ok) {
const text = await res.text();
throw new Error(`PUT ${path} failed (${res.status}): ${text}`);
}
if (res.status === 204) {return undefined;}
return res.json();
}
export async function apiGet(path: string) {
await ensureAuth();
const res = await fetch(`${API}${path}`, {
headers: getAuthHeaders(),
});
return res.json();
}
export async function apiRawGet(path: string) {
await ensureAuth();
return fetch(`${API}${path}`, {
headers: getAuthHeaders(),
});
}
export async function apiDelete(path: string) {
await ensureAuth();
return fetch(`${API}${path}`, {
method: 'DELETE',
headers: getAuthHeaders(),
});
}
export function parseCookie(cookie: string): { name: string; value: string } {
const idx = cookie.indexOf('=');
return { name: cookie.slice(0, idx), value: cookie.slice(idx + 1) };
}
export { API, TEST_PASSWORD };