import { expect, test } from "@playwright/test"; import { mockApi } from "./support/network.js"; function json(route, body, status = 200) { return route.fulfill({ status, contentType: "application/json", body: JSON.stringify(body), }); } test("loads the complete-registration page and completes subuser setup from the token link", async ({ page }) => { await mockApi(page, { authenticated: false }); let submittedPayload = null; await page.route("**/subusers/setup*", async (route) => { const request = route.request(); const _url = new URL(request.url()); if (request.method() === "GET") { await json(route, { data: { message: "Token is valid", subuser_id: 91, }, }); return; } submittedPayload = request.postDataJSON(); await json(route, { data: { message: "Complete registration successful", }, }); }); await page.goto("/complete-registration?token=valid-setup-token"); await expect(page.locator("#complete-registration-form")).toBeVisible(); await page.fill("#complete-registration-name", "Driver Demo"); await page.fill("#complete-registration-username", "driver.demo"); await page.fill("#complete-registration-email", "driver@example.com"); await page.fill("#complete-registration-password", "ValidPass123"); await page.fill("#complete-registration-confirm-password", "ValidPass123"); await page.click("#complete-registration-submit"); await expect(page.locator("#complete_registration_alert_success")).toHaveText("Complete registration successful"); await expect(page.locator('a[href="/login/driver"]')).toBeVisible(); expect(submittedPayload).toMatchObject({ token: "valid-setup-token", name: "Driver Demo", username: "driver.demo", email: "driver@example.com", password: "ValidPass123", }); }); test("shows an inline error when the complete-registration token is invalid", async ({ page }) => { await mockApi(page, { authenticated: false }); await page.route("**/subusers/setup*", async (route) => { await json( route, { data: { message: "Invalid or expired token", }, }, 400 ); }); await page.goto("/complete-registration?token=invalid-setup-token"); await expect(page.locator("#complete_registration_alert_error")).toHaveText("Invalid or expired token"); await expect(page.locator("#complete-registration-invalid")).toBeVisible(); });