An admin could set a colleague's password to a value of their choosing. Only the emailed temporary-password reset remains. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
91 lines
4.1 KiB
TypeScript
91 lines
4.1 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.queryByLabelText(/password/i)).not.toBeInTheDocument();
|
|
});
|
|
|
|
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.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");
|
|
await waitFor(() => expect(mocks.push).toHaveBeenCalledWith("/users/42?created=1"));
|
|
});
|
|
|
|
it("when the temp-password email fails, says so with a link to the users list instead of redirecting", async () => {
|
|
const { ApiError } = await vi.importActual<typeof import("@/app/lib/clientApi")>("@/app/lib/clientApi");
|
|
mocks.saveAdminUser.mockRejectedValue(
|
|
new ApiError("User created but the temporary password email was not sent; retry the password reset", 400, {
|
|
code: "TEMP_PASSWORD_NOT_SENT",
|
|
}),
|
|
);
|
|
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(
|
|
"User created, but the temporary password email could not be sent. Open the user from the users list and use Send password reset to try again.",
|
|
),
|
|
).toBeInTheDocument();
|
|
expect(screen.getByRole("link", { name: "Back to users" })).toHaveAttribute("href", "/users");
|
|
expect(mocks.push).not.toHaveBeenCalled();
|
|
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();
|
|
});
|
|
});
|