Files
penguin-harness/packages/web/e2e/auth.mjs
T
Yaowei Zheng 45bfae6e94 Initialize repository with harness code and assets
Initial import of all source code, config, and README assets: the
packages workspace (cli, core, server, web, docs, landing, skills),
build scripts, tooling config, and CI workflows.

Includes the data-layout revision made on this branch: the local data
root defaults to ~/.penguin/data (PENGUIN_HOME still overrides; the
installer keeps its binaries in ~/.penguin), and every Agent lives
under <project>/agents/<agent>/ — path helpers, the three
agent-enumeration scans, the system prompt, built-in Skills, tests
and docs all follow the new layout.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_018ihk8iQuo3kv2aPjAYEPuR
2026-07-19 14:06:53 +08:00

39 lines
1.6 KiB
JavaScript

/**
* e2e auth helper: with signup disabled, test users are always provisioned via
* the built-in admin account, then logged in. The server seeds an admin
* (admin / admin123) on startup; a single e2e run shares one data root, and
* provisioning is idempotent (reuses the user if it already exists) so a
* single spec can be rerun on its own.
*/
import { request } from "@playwright/test";
const BASE = process.env.BASE_URL;
export const ADMIN_ID = "admin";
export const ADMIN_PASSWORD = "admin123";
/** Log in: the cookie lands in the given request context (page.request is the browser context); returns user. */
export async function login(ctx, userId, password) {
const res = await ctx.post(`${BASE}/api/auth/login`, { data: { userId, password } });
if (!res.ok()) {
throw new Error(`login ${userId} failed: ${res.status()} ${await res.text()}`);
}
return (await res.json()).user;
}
/** Admin creates the user (409 is treated as already-exists, idempotent). */
export async function provisionUser(userId, password) {
const adminCtx = await request.newContext();
await login(adminCtx, ADMIN_ID, ADMIN_PASSWORD);
const created = await adminCtx.post(`${BASE}/api/admin/users`, { data: { userId, password } });
if (!created.ok() && created.status() !== 409) {
throw new Error(`create user ${userId} failed: ${created.status()} ${await created.text()}`);
}
await adminCtx.dispose();
}
/** Provision the user and log ctx in as them; returns user. */
export async function provisionAndLogin(ctx, userId, password) {
await provisionUser(userId, password);
return login(ctx, userId, password);
}