Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/tests/e2e/forgot-password.a11y.spec.ts
T

120 lines
6.0 KiB
TypeScript

import { expect, test } from "@playwright/test";
import { expectNoCriticalA11yViolations } from "./utils/axe";
import { expectNoHorizontalOverflow, narrowMobileViewport } from "./utils/mobile";
import { escapeRegExp, interceptCurrentRoutePost, postDataIncludesField } from "./utils/navigation";
const forgotForm = '[data-ui-id="ui.auth.forgot.form"], form';
const forgotEmailInput = '[data-ui-id="ui.auth.forgot.email"], input[name="email"]';
const forgotResetButton = '[data-ui-id="ui.auth.forgot.submit"], button[name="mode"][value="reset"]';
const resetForm = '[data-ui-id="ui.auth.reset.form"], form';
const resetEmailInput = '[data-ui-id="ui.auth.reset.email"], input[type="email"], input[name="email"], input#email';
const resetPasswordInput = '[data-ui-id="ui.auth.reset.password"], input[name="password"]';
const resetPasswordConfirmInput = '[data-ui-id="ui.auth.reset.passwordConfirm"], input[name="password-confirm"]';
const resetSubmitButton = '[data-ui-id="ui.auth.reset.submit"], button[type="submit"]';
test.describe("forgot-password page a11y", () => {
for (const route of ["/forgot-password", "/reset", "/auth/magic-link"]) {
test(`${route} has no critical or serious axe violations`, async ({ page }) => {
await page.goto(route, { waitUntil: "domcontentloaded" });
await expectNoCriticalA11yViolations(page);
});
}
test("submits reset requests without account enumeration", async ({ page }) => {
await page.goto("/forgot-password", { waitUntil: "domcontentloaded" });
const form = page.locator(forgotForm).first();
const csrfInput = form.locator('input[name="csrf"]').first();
if ((await csrfInput.count()) > 0) {
await expect(csrfInput).not.toHaveValue("");
}
await form.locator(forgotEmailInput).fill("unknown-e2e-user@example.invalid");
await form.locator(forgotResetButton).click();
await expect(page.getByText(/if an account exists|wenn es ein konto/iu)).toBeVisible({ timeout: 5_000 });
});
test("submits reset-token form with csrf and token fields without mutating data", async ({ page }) => {
await page.goto("/reset?e=contract%40example.test&t=dummy-token", { waitUntil: "domcontentloaded" });
const form = page.locator(resetForm).first();
const csrfInput = form.locator('input[name="csrf"]').first();
const tokenInput = form.locator('input[name="verify-token"]').first();
const emailInput = form.locator(resetEmailInput).first();
const passwordInput = form.locator(resetPasswordInput).first();
const confirmInput = form.locator(resetPasswordConfirmInput).first();
const submit = form.locator(resetSubmitButton).first();
await expect(form).toBeVisible();
await expect(csrfInput).not.toHaveValue("");
await expect(tokenInput).toHaveValue("dummy-token");
await expect(emailInput).toHaveValue("contract@example.test");
await expect(passwordInput).toBeEditable();
await expect(confirmInput).toBeEditable();
await expect(submit).toBeEnabled();
const submitted = await interceptCurrentRoutePost(page, { status: 204 });
await passwordInput.fill("LongEnough1!");
await confirmInput.fill("LongEnough1!");
await submit.click();
await expect.poll(() => submitted.value, { timeout: 5_000 }).not.toBeNull();
expect(postDataIncludesField(submitted.value, "csrf", /.+/u)).toBe(true);
expect(postDataIncludesField(submitted.value, "verify-token", /^dummy-token$/u)).toBe(true);
expect(postDataIncludesField(submitted.value, "email", /^contract@example\.test$/u)).toBe(true);
expect(postDataIncludesField(submitted.value, "password", /^LongEnough1!$/u)).toBe(true);
expect(postDataIncludesField(submitted.value, "password-confirm", /^LongEnough1!$/u)).toBe(true);
});
test("shows an accessible error for an invalid reset token without losing context", async ({ page }) => {
await page.goto("/reset?e=contract%40example.test&t=dummy-token", { waitUntil: "domcontentloaded" });
const form = page.locator(resetForm).first();
await expect(form).toBeVisible();
await form.locator(resetPasswordInput).fill("LongEnough1!");
await form.locator(resetPasswordConfirmInput).fill("LongEnough1!");
await form.locator(resetSubmitButton).click();
await expect(page.getByRole("alert")).toContainText(/invalid or expired|ungültig|abgelaufen/i);
await expect(form.locator('input[name="verify-token"]')).toHaveValue("dummy-token");
await expect(form.locator(resetEmailInput)).toHaveValue("contract@example.test");
await expect(page).toHaveURL(/\/reset\?e=contract%40example\.test&t=dummy-token$/);
await expectNoCriticalA11yViolations(page);
});
});
test.describe("forgot-password mobile contract a11y", () => {
test.use({ viewport: narrowMobileViewport, isMobile: true, hasTouch: true });
test("posts the reset contract on mobile without sending mail", async ({ page }) => {
const email = "mobile-reset-contract@example.invalid";
await page.goto("/forgot-password", { waitUntil: "domcontentloaded" });
const form = page.locator(forgotForm).first();
const emailInput = form.locator(forgotEmailInput).first();
const submitButton = form.locator(forgotResetButton).first();
await expect(form).toBeVisible();
await expect(emailInput).toBeVisible();
await expect(submitButton).toBeVisible();
await emailInput.fill(email);
await expectNoHorizontalOverflow(page);
await expectNoCriticalA11yViolations(page);
const submitted = await interceptCurrentRoutePost(page, {
status: 200,
body: "<!doctype html><title>Forgot password intercepted</title>",
contentType: "text/html",
});
await submitButton.click();
await expect.poll(() => submitted.value, { timeout: 5_000 }).not.toBeNull();
expect(postDataIncludesField(submitted.value, "csrf", /.+/u)).toBe(true);
expect(postDataIncludesField(submitted.value, "email", new RegExp(`^${escapeRegExp(email)}$`, "u"))).toBe(true);
expect(postDataIncludesField(submitted.value, "mode", /^reset$/u)).toBe(true);
await page.unroute("**/*");
});
});