Files
2026-11-20 04:07:02 +00:00

178 lines
7.4 KiB
TypeScript

import { test, expect } from "@playwright/test";
import { resetDb, signInAs, BASE } from "./dev";
const PASSWORD = "first-password-123";
const NEW_PASSWORD = "second-password-456";
test.describe("username and password accounts", () => {
test.beforeAll(async ({ browser }) => {
const context = await browser.newContext();
const page = await context.newPage();
await resetDb(page);
await context.close();
});
test("first magic link sets credentials; the user can change their password", async ({
page,
}) => {
const email = `e2e-password-${Date.now()}@example.com`;
const username = `pwuser${Date.now()}`;
const linkRes = await page.request.post(`${BASE}/api/dev/magic-link`, {
data: { email },
});
expect(linkRes.ok()).toBe(true);
const link = (await linkRes.json()) as { url?: string };
await page.goto(link.url!);
await expect(page).toHaveURL(`${BASE}/welcome`);
await expect(
page.getByRole("heading", { name: "Set your login" }),
).toBeVisible();
await expect(page.getByLabel("Username")).toHaveValue(email);
await page.getByLabel("Username").fill(username);
await page.getByLabel("Password", { exact: true }).fill(PASSWORD);
await page.getByLabel("Confirm password").fill(PASSWORD);
await page.getByRole("button", { name: "Save and continue" }).click();
await expect(page.getByRole("heading", { name: "Schedule", level: 1 })).toBeVisible();
await page.context().clearCookies();
await page.goto(`${BASE}/login`);
await page.getByLabel("Username or email").fill(username);
await page.getByLabel("Password", { exact: true }).fill(PASSWORD);
await page.getByRole("button", { name: "Sign in", exact: true }).click();
await expect(page.getByRole("heading", { name: "Schedule", level: 1 })).toBeVisible();
await page.goto(`${BASE}/settings`);
await page.getByLabel("Current password", { exact: true }).fill(PASSWORD);
await page.getByLabel("New password", { exact: true }).fill(NEW_PASSWORD);
await page.getByLabel("Confirm new password", { exact: true }).fill(NEW_PASSWORD);
await page.getByRole("button", { name: "Change password" }).click();
await expect(page.getByText("Your login has been updated.")).toBeVisible();
await page.context().clearCookies();
await page.goto(`${BASE}/login`);
await page.getByLabel("Username or email").fill(username);
await page.getByLabel("Password", { exact: true }).fill(PASSWORD);
await page.getByRole("button", { name: "Sign in", exact: true }).click();
await expect(
page.getByText("That username or password did not match an active account."),
).toBeVisible();
await page.getByLabel("Password", { exact: true }).fill(NEW_PASSWORD);
await page.getByRole("button", { name: "Sign in", exact: true }).click();
await expect(page.getByRole("heading", { name: "Schedule", level: 1 })).toBeVisible();
});
test("admin can set a password and issue a reset link", async ({ page, browser }) => {
await signInAs(page, "admin@glenloch.local");
const email = `e2e-reset-${Date.now()}@example.com`;
const createdRes = await page.request.post(`${BASE}/api/dev/users`, {
data: {
email,
firstName: "Reset",
lastName: "Test",
systemRole: "volunteer",
status: "active",
},
});
expect(createdRes.status()).toBe(201);
const created = (await createdRes.json()) as { user: { id: string } };
await page.goto(`${BASE}/admin`);
const row = page.getByRole("row", { name: new RegExp(email) });
await row.getByRole("button", { name: "Edit" }).click();
const editor = page.getByRole("form", { name: `Edit ${email}` });
await editor.getByLabel("New password").fill("admin-direct-123");
await editor.getByRole("button", { name: "Set password" }).click();
await expect(page.getByText("Password updated for this person.")).toBeVisible();
const directContext = await browser.newContext();
const directPage = await directContext.newPage();
await directPage.goto(`${BASE}/login`);
await directPage.getByLabel("Username or email").fill(email);
await directPage.getByLabel("Password", { exact: true }).fill("admin-direct-123");
await directPage.getByRole("button", { name: "Sign in", exact: true }).click();
await expect(
directPage.getByRole("heading", { name: "Schedule", level: 1 }),
).toBeVisible();
await directContext.close();
await editor.getByRole("button", { name: "Create reset link" }).click();
const resetNotice = page.getByText(/Reset link \(valid for 1 hour\)/);
await expect(resetNotice).toBeVisible();
const noticeText = (await resetNotice.textContent()) ?? "";
const resetUrl = noticeText.match(/https?:\/\/\S+/)?.[0];
expect(resetUrl, "the admin should receive a reset URL").toBeTruthy();
const resetContext = await browser.newContext();
const resetPage = await resetContext.newPage();
await resetPage.goto(resetUrl!);
await resetPage.getByLabel("New password").fill("reset-password-789");
await resetPage.getByLabel("Confirm password").fill("reset-password-789");
await resetPage.getByRole("button", { name: "Reset password" }).click();
await expect(
resetPage.getByText(/Your password has been updated/),
).toBeVisible();
await resetPage.goto(`${BASE}/login`);
await resetPage.getByLabel("Username or email").fill(email);
await resetPage.getByLabel("Password", { exact: true }).fill("reset-password-789");
await resetPage.getByRole("button", { name: "Sign in", exact: true }).click();
await expect(
resetPage.getByRole("heading", { name: "Schedule", level: 1 }),
).toBeVisible();
await resetContext.close();
expect(created.user.id).toBeTruthy();
});
test("admin can reveal and resend an emergency magic link", async ({
page,
browser,
}) => {
await signInAs(page, "admin@glenloch.local");
const email = `e2e-magic-${Date.now()}@example.com`;
const createdRes = await page.request.post(`${BASE}/api/dev/users`, {
data: {
email,
firstName: "Magic",
lastName: "Test",
systemRole: "volunteer",
status: "active",
},
});
expect(createdRes.status()).toBe(201);
await page.goto(`${BASE}/admin`);
const row = page.getByRole("row", { name: new RegExp(email) });
await row.getByRole("button", { name: "Edit" }).click();
const editor = page.getByRole("form", { name: `Edit ${email}` });
await editor.getByRole("button", { name: "Show magic link" }).click();
const linkNotice = page.getByText(/Magic link \(valid for 10 minutes\)/);
await expect(linkNotice).toBeVisible();
const linkText = (await linkNotice.textContent()) ?? "";
const magicUrl = linkText.match(/https?:\/\/\S+/)?.[0];
expect(magicUrl, "the admin should receive a magic URL").toBeTruthy();
// The revealed link signs the person in; a first-time account lands on the
// credential setup page.
const magicContext = await browser.newContext();
const magicPage = await magicContext.newPage();
await magicPage.goto(magicUrl!);
await expect(magicPage).toHaveURL(`${BASE}/welcome`);
await expect(
magicPage.getByRole("heading", { name: "Set your login" }),
).toBeVisible();
await magicContext.close();
await editor.getByRole("button", { name: "Resend magic link" }).click();
await expect(page.getByText(/Magic link sent via/)).toBeVisible();
});
});