Files
co-lite/solvway-admin-portal/app/reset-password/page.test.tsx
T
ngqileandClaude Opus 5.5 5ee51d1913 chore: copy kept Tastefully modules and portals into Co-Lite
Backend modules from tastefully 8bd93d15: core-libs, core-session,
api-gateway, mobile-api, services-auth, services-system,
services-counter, services-member, services-log, services-email.
solvway-admin-portal from babyboo f64d096ea, solvway-customer-portal
from tastefully 8bd93d15. Copied as tracked files only.

Literal secrets replaced with environment placeholders:
- services-email: SMTP_PASSWORD, MAILGUN_API_KEY, SENDGRID_API_KEY,
  MAILJET_API_KEY, MAILJET_SECRET_KEY (optional, empty default)
- services-system: JUMIO_TOKEN, JUMIO_SECRET (optional)
- services-auth: USER_AUTH_API_SECRET (required); commented JWT secret removed
- core-libs: JWT_TOKEN default removed (required), XXL_JOB_TOKEN default
  removed, ipgeolocation key read from IPGEOLOCATION_API_KEY
- services-system EkycAppConfig: commented Jumio credentials removed

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 15:55:45 +08:00

92 lines
3.8 KiB
TypeScript

import { render, screen, waitFor, fireEvent } from "@testing-library/react";
import { describe, it, expect, vi, beforeEach } from "vitest";
const nav = vi.hoisted(() => ({ push: vi.fn() }));
vi.mock("next/navigation", () => ({ useRouter: () => ({ push: nav.push }) }));
vi.mock("@/app/lib/clientApi", () => ({
apiPost: vi.fn(),
ApiError: class ApiError extends Error {},
}));
const refresh = vi.fn();
vi.mock("@/app/lib/AdminAuthContext", () => ({ useAdminAuth: () => ({ refresh }) }));
import ResetPasswordPage from "./page";
import { apiPost } from "@/app/lib/clientApi";
const mockPost = apiPost as unknown as ReturnType<typeof vi.fn>;
function fill(next: string, confirm: string) {
fireEvent.change(screen.getByLabelText("New password"), { target: { value: next } });
fireEvent.change(screen.getByLabelText("Confirm new password"), { target: { value: confirm } });
fireEvent.submit(screen.getByRole("button", { name: /set password/i }));
}
beforeEach(() => {
mockPost.mockReset();
nav.push.mockReset();
refresh.mockReset().mockResolvedValue(undefined);
});
describe("ResetPasswordPage (first sign-in)", () => {
it("does not ask for the current password", () => {
render(<ResetPasswordPage />);
expect(screen.queryByLabelText(/current password/i)).toBeNull();
expect(screen.getByLabelText("New password")).toBeInTheDocument();
expect(screen.getByLabelText("Confirm new password")).toBeInTheDocument();
});
it("shows the choose-a-new-password intro, not the temporary-password one", () => {
const { container } = render(<ResetPasswordPage />);
expect(screen.getByText("Choose a new password to continue.")).toBeInTheDocument();
expect(container.textContent).not.toMatch(/temporary password/i);
});
it("renders a noValidate form so empty fields reach the JS validation", () => {
const { container } = render(<ResetPasswordPage />);
expect(container.querySelector("form")).toHaveAttribute("novalidate");
});
it("shows an inline error and posts nothing when a field is empty", async () => {
render(<ResetPasswordPage />);
fill("", "");
expect(await screen.findByRole("alert")).toHaveTextContent(/required/i);
expect(mockPost).not.toHaveBeenCalled();
});
it("shows an inline error and posts nothing when the confirmation differs", async () => {
render(<ResetPasswordPage />);
fill("New#Pass1", "New#Pass2");
expect(await screen.findByRole("alert")).toHaveTextContent(/do not match/i);
expect(mockPost).not.toHaveBeenCalled();
});
it("posts only the new password, then goes to the dashboard", async () => {
mockPost.mockResolvedValue({ ok: true });
render(<ResetPasswordPage />);
fill("New#Pass1", "New#Pass1");
await waitFor(() => expect(nav.push).toHaveBeenCalledWith("/"));
expect(mockPost).toHaveBeenCalledWith("/api/auth/first-login", {
newPassword: "New#Pass1",
});
});
it("shows the API error inline and stays on the page", async () => {
mockPost.mockRejectedValue(new Error("Password does not meet the password policy"));
render(<ResetPasswordPage />);
fill("weak", "weak");
expect(await screen.findByRole("alert")).toHaveTextContent("Password does not meet the password policy");
expect(nav.push).not.toHaveBeenCalled();
});
});
describe("Containerverse credit", () => {
it("is shown on the reset-password page, linked to containerverse.dev", () => {
render(<ResetPasswordPage />);
const link = screen.getByRole("link", { name: "Containerverse Sdn. Bhd." });
expect(link).toHaveAttribute("href", "https://containerverse.dev/");
expect(link).toHaveAttribute("target", "_blank");
expect(link).toHaveAttribute("rel", "noopener noreferrer");
expect(link.parentElement?.textContent).toBe("Powered by Containerverse Sdn. Bhd.");
});
});