/forgot-password always shows the same message; the route never forwards upstream errors or a token. /api/admin/me now returns id and username so the user form can lock the Active switch on your own account. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
60 lines
2.3 KiB
TypeScript
60 lines
2.3 KiB
TypeScript
import { render, screen, waitFor, fireEvent } from "@testing-library/react";
|
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
vi.mock("@/app/lib/clientApi", () => ({ apiPost: vi.fn() }));
|
|
|
|
import ForgotPasswordPage from "./page";
|
|
import { apiPost } from "@/app/lib/clientApi";
|
|
|
|
const mockPost = apiPost as unknown as ReturnType<typeof vi.fn>;
|
|
const FIXED = "If the details match an account, we have emailed a temporary password.";
|
|
|
|
function fill(username: string, email: string) {
|
|
fireEvent.change(screen.getByLabelText("Username"), { target: { value: username } });
|
|
fireEvent.change(screen.getByLabelText("Email"), { target: { value: email } });
|
|
}
|
|
const submit = () => fireEvent.submit(screen.getByRole("button", { name: /temporary password/i }));
|
|
|
|
beforeEach(() => {
|
|
mockPost.mockReset();
|
|
});
|
|
|
|
describe("ForgotPasswordPage", () => {
|
|
it("posts username and email and shows the fixed message with a link back to login", async () => {
|
|
mockPost.mockResolvedValue({ ok: true });
|
|
render(<ForgotPasswordPage />);
|
|
fill("jane", "jane@example.com");
|
|
submit();
|
|
|
|
expect(await screen.findByText(FIXED)).toBeInTheDocument();
|
|
expect(mockPost).toHaveBeenCalledWith("/api/auth/forgot-password", { username: "jane", email: "jane@example.com" });
|
|
expect(screen.getByRole("link", { name: /back to log ?in/i })).toHaveAttribute("href", "/login");
|
|
});
|
|
|
|
it("shows the same message when the request fails", async () => {
|
|
mockPost.mockImplementation(() => Promise.reject(new Error("Network down")));
|
|
render(<ForgotPasswordPage />);
|
|
fill("jane", "jane@example.com");
|
|
submit();
|
|
|
|
expect(await screen.findByText(FIXED)).toBeInTheDocument();
|
|
expect(screen.queryByText(/network down/i)).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("requires both fields and does not call the API otherwise", async () => {
|
|
render(<ForgotPasswordPage />);
|
|
fill("jane", "");
|
|
submit();
|
|
|
|
expect(await screen.findByRole("alert")).toBeInTheDocument();
|
|
expect(mockPost).not.toHaveBeenCalled();
|
|
expect(screen.queryByText(FIXED)).not.toBeInTheDocument();
|
|
});
|
|
|
|
it("does not show the message before submit", async () => {
|
|
render(<ForgotPasswordPage />);
|
|
await waitFor(() => expect(screen.getByLabelText("Username")).toBeInTheDocument());
|
|
expect(screen.queryByText(FIXED)).not.toBeInTheDocument();
|
|
});
|
|
});
|