Create shows a link back to Users when the user was saved but the password was not stored. The spec's secure-generator rule now names admin temporary passwords only, and the reset requirement no longer implies the emailed reset refuses the caller's own account. The design and handover note that mobile-api must stay the only gateway client allowed to reach /user/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
137 lines
6.8 KiB
TypeScript
137 lines
6.8 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";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
saveAdminUser: vi.fn(),
|
|
useAdminAuth: vi.fn(),
|
|
push: vi.fn(),
|
|
listAdminRoles: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminUsers", () => ({
|
|
saveAdminUser: mocks.saveAdminUser,
|
|
getAdminUser: vi.fn(),
|
|
sendAdminUserPasswordReset: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/services/adminRoles", () => ({ listAdminRoles: mocks.listAdminRoles }));
|
|
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: mocks.useAdminAuth }));
|
|
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: mocks.push }) }));
|
|
|
|
import NewUserPage from "./page";
|
|
|
|
beforeEach(() => {
|
|
mocks.saveAdminUser.mockReset();
|
|
mocks.push.mockReset();
|
|
mocks.listAdminRoles.mockReset().mockResolvedValue([]);
|
|
mocks.useAdminAuth.mockReset().mockReturnValue({ loading: false, hasModule: () => true });
|
|
});
|
|
|
|
describe("NewUserPage", () => {
|
|
it("renders the title through the shared page header", async () => {
|
|
render(<NewUserPage />);
|
|
expect(screen.getByRole("heading", { level: 1, name: "New user" })).toBeInTheDocument();
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
});
|
|
|
|
it("renders an empty UserForm", async () => {
|
|
render(<NewUserPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toHaveValue(""));
|
|
expect(screen.getByLabelText("Username")).toHaveValue("");
|
|
expect(screen.getByLabelText("Email")).toHaveValue("");
|
|
expect(screen.getByLabelText("Password")).toHaveValue("");
|
|
expect(screen.getByLabelText("Confirm password")).toHaveValue("");
|
|
});
|
|
|
|
it("calls saveAdminUser without an id and redirects to the returned user's id", async () => {
|
|
mocks.saveAdminUser.mockResolvedValue({ id: 42, username: "jane.doe" });
|
|
render(<NewUserPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
|
|
await userEvent.type(screen.getByLabelText("Email"), "jane@example.com");
|
|
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "Good#Pass1");
|
|
await userEvent.click(screen.getByRole("button", { name: /create user/i }));
|
|
|
|
await waitFor(() => expect(mocks.saveAdminUser).toHaveBeenCalledTimes(1));
|
|
const payload = mocks.saveAdminUser.mock.calls[0][0];
|
|
expect(payload.id).toBeUndefined();
|
|
expect(payload.username).toBe("jane.doe");
|
|
expect(payload.password).toBe("Good#Pass1");
|
|
expect(payload.confirmPassword).toBe("Good#Pass1");
|
|
await waitFor(() => expect(mocks.push).toHaveBeenCalledWith("/users/42?created=1"));
|
|
});
|
|
|
|
it("catches a mismatched confirmation before submitting", async () => {
|
|
render(<NewUserPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
|
|
await userEvent.type(screen.getByLabelText("Email"), "jane@example.com");
|
|
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "Other#Pass1");
|
|
await userEvent.click(screen.getByRole("button", { name: /create user/i }));
|
|
|
|
expect(await screen.findByText("The passwords do not match.")).toBeInTheDocument();
|
|
expect(mocks.saveAdminUser).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires a password", async () => {
|
|
render(<NewUserPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
|
|
await userEvent.type(screen.getByLabelText("Email"), "jane@example.com");
|
|
await userEvent.click(screen.getByRole("button", { name: /create user/i }));
|
|
|
|
expect(await screen.findByText("Password is required.")).toBeInTheDocument();
|
|
expect(mocks.saveAdminUser).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("shows the server's policy message and stays on the form", async () => {
|
|
const { ApiError } = await vi.importActual<typeof import("@/app/lib/clientApi")>("@/app/lib/clientApi");
|
|
mocks.saveAdminUser.mockRejectedValue(
|
|
new ApiError("Password does not meet the password policy", 400, { code: "PASSWORD_POLICY" }),
|
|
);
|
|
render(<NewUserPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
|
|
await userEvent.type(screen.getByLabelText("Email"), "jane@example.com");
|
|
await userEvent.type(screen.getByLabelText("Password"), "weak");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "weak");
|
|
await userEvent.click(screen.getByRole("button", { name: /create user/i }));
|
|
|
|
expect(await screen.findByText("Password does not meet the password policy")).toBeInTheDocument();
|
|
expect(mocks.push).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("when the user was created but the password was not stored, links back to the users list", async () => {
|
|
const { ApiError } = await vi.importActual<typeof import("@/app/lib/clientApi")>("@/app/lib/clientApi");
|
|
mocks.saveAdminUser.mockRejectedValue(
|
|
new ApiError("User created but the password was not stored; set a temporary password", 500, { code: "SAVE_FAILED" }),
|
|
);
|
|
render(<NewUserPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Name")).toBeInTheDocument());
|
|
await userEvent.type(screen.getByLabelText("Name"), "Jane");
|
|
await userEvent.type(screen.getByLabelText("Username"), "jane.doe");
|
|
await userEvent.type(screen.getByLabelText("Email"), "jane@example.com");
|
|
await userEvent.type(screen.getByLabelText("Password"), "Good#Pass1");
|
|
await userEvent.type(screen.getByLabelText("Confirm password"), "Good#Pass1");
|
|
await userEvent.click(screen.getByRole("button", { name: /create user/i }));
|
|
|
|
expect(
|
|
await screen.findByText("User created, but the password was not stored. Open the user from the users list and use Set temporary password."),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: "Back to users" })).toHaveAttribute("href", "/users");
|
|
expect(screen.queryByRole("button", { name: /create user/i })).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("renders NotAuthorized when the Users module is absent from aclMenu", () => {
|
|
mocks.useAdminAuth.mockReturnValue({ loading: false, hasModule: () => false });
|
|
render(<NewUserPage />);
|
|
expect(screen.getByTestId("not-authorized")).toBeInTheDocument();
|
|
});
|
|
});
|