The public /r/<code> page (no admin shell, never redirects a visitor to the staff login), its proxy sending only the last X-Forwarded-For hop, the event page's Registration QR card, a My QR codes page in the user menu, and the super-admin privacy notice editor on Companies. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
86 lines
4.2 KiB
TypeScript
86 lines
4.2 KiB
TypeScript
import { render, screen, waitFor } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
import { ApiError } from "@/app/lib/clientApi";
|
|
|
|
vi.mock("next/navigation", () => ({
|
|
useRouter: () => ({ replace: vi.fn(), refresh: vi.fn() }),
|
|
usePathname: () => "/",
|
|
}));
|
|
vi.mock("@/app/lib/useAclSidebar", () => ({ useAclSidebar: () => [] }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({
|
|
useAdminAuth: () => ({ name: "Admin User", hasModule: () => false }),
|
|
}));
|
|
vi.mock("@/app/lib/theme", () => ({ useTheme: () => ({ theme: "light", toggleTheme: vi.fn() }) }));
|
|
const mocks = vi.hoisted(() => ({ changeOwnPassword: vi.fn() }));
|
|
vi.mock("@/app/lib/services/adminUsers", () => ({ changeOwnPassword: mocks.changeOwnPassword }));
|
|
|
|
import Header from "./Header";
|
|
|
|
beforeEach(() => {
|
|
mocks.changeOwnPassword.mockReset();
|
|
});
|
|
|
|
async function openDialog() {
|
|
await userEvent.click(screen.getByRole("button", { name: /admin user/i }));
|
|
await userEvent.click(await screen.findByText("Change password"));
|
|
}
|
|
|
|
describe("Header: change password", () => {
|
|
it("lists Change password above Sign out", async () => {
|
|
render(<Header />);
|
|
await userEvent.click(screen.getByRole("button", { name: /admin user/i }));
|
|
const items = (await screen.findAllByRole("menuitem")).map((i) => i.textContent);
|
|
expect(items).toEqual(["My QR codes", "Change password", "Logout"]);
|
|
});
|
|
|
|
it("blocks empty fields and mismatched passwords without calling the server", async () => {
|
|
render(<Header />);
|
|
await openDialog();
|
|
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("Fill in all the password fields.");
|
|
|
|
await userEvent.type(screen.getByLabelText("Current password"), "Old#1");
|
|
await userEvent.type(screen.getByLabelText("New password"), "New#1");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "Other#1");
|
|
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("The passwords do not match.");
|
|
expect(mocks.changeOwnPassword).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("submits, closes, confirms, and starts empty next time", async () => {
|
|
mocks.changeOwnPassword.mockResolvedValue(undefined);
|
|
render(<Header />);
|
|
await openDialog();
|
|
expect(screen.getByLabelText("Current password")).toHaveAttribute("autocomplete", "current-password");
|
|
await userEvent.type(screen.getByLabelText("Current password"), "Old#1");
|
|
await userEvent.type(screen.getByLabelText("New password"), "New#1");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "New#1");
|
|
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
|
|
|
|
await waitFor(() => expect(mocks.changeOwnPassword).toHaveBeenCalledWith("Old#1", "New#1", "New#1"));
|
|
await waitFor(() => expect(screen.queryByLabelText("Current password")).not.toBeInTheDocument());
|
|
expect(screen.getByText("Your password has been changed.")).toBeInTheDocument();
|
|
|
|
await openDialog();
|
|
expect(screen.getByLabelText("Current password")).toHaveValue("");
|
|
expect(screen.getByLabelText("New password")).toHaveValue("");
|
|
});
|
|
|
|
it("shows the server message on failure and clears the fields on cancel", async () => {
|
|
mocks.changeOwnPassword.mockRejectedValue(new ApiError("Current password is incorrect.", 400, { code: "CURRENT_PASSWORD_WRONG" }));
|
|
render(<Header />);
|
|
await openDialog();
|
|
await userEvent.type(screen.getByLabelText("Current password"), "bad");
|
|
await userEvent.type(screen.getByLabelText("New password"), "New#1");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "New#1");
|
|
await userEvent.click(screen.getByRole("button", { name: "Change password" }));
|
|
expect(await screen.findByRole("alert")).toHaveTextContent("Current password is incorrect.");
|
|
|
|
await userEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
|
await openDialog();
|
|
expect(screen.getByLabelText("Current password")).toHaveValue("");
|
|
expect(screen.queryByRole("alert")).not.toBeInTheDocument();
|
|
});
|
|
});
|