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
102 lines
3.6 KiB
TypeScript
102 lines
3.6 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn() }));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
|
|
import { POST } from "./route";
|
|
|
|
function req(body: unknown) {
|
|
return new Request("http://localhost/api/member/forgot-password", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof POST>[0];
|
|
}
|
|
|
|
beforeEach(() => mocks.callMobileApi.mockReset());
|
|
|
|
describe("POST /api/member/forgot-password", () => {
|
|
it("returns success on accepted reset", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { success: true } });
|
|
const res = await POST(req({ identifier: "60166789304" }));
|
|
expect(res.status).toBe(200);
|
|
expect((await res.json()).success).toBe(true);
|
|
});
|
|
|
|
it("surfaces multipleEmailDetected", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Multiple accounts",
|
|
extra: { multipleEmailDetected: true },
|
|
});
|
|
const res = await POST(req({ identifier: "shared@email.com" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).multipleEmailDetected).toBe(true);
|
|
});
|
|
|
|
it("responds neutral success for an unregistered or inactive account", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Request failed",
|
|
extra: { success: false },
|
|
});
|
|
const res = await POST(req({ identifier: "nobody@example.com" }));
|
|
expect(res.status).toBe(200);
|
|
expect((await res.json()).success).toBe(true);
|
|
});
|
|
|
|
it("surfaces multipleEmailDetected when upstream also reports success:false", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "x",
|
|
extra: { success: false, multipleEmailDetected: true },
|
|
});
|
|
const res = await POST(req({ identifier: "shared@email.com" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).multipleEmailDetected).toBe(true);
|
|
});
|
|
|
|
it("returns an error when the server is unreachable", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Unable to reach the server. Please try again.",
|
|
});
|
|
const res = await POST(req({ identifier: "60166789304" }));
|
|
expect(res.status).not.toBe(200);
|
|
expect((await res.json()).error).toBe("Unable to reach the server. Please try again.");
|
|
});
|
|
|
|
it("returns an error on upstream timeout", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({
|
|
ok: false,
|
|
error: "Request timed out. Please try again.",
|
|
timedOut: true,
|
|
});
|
|
const res = await POST(req({ identifier: "60166789304" }));
|
|
expect(res.status).not.toBe(200);
|
|
});
|
|
|
|
it("strips spaces and dashes from a phone identifier", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { success: true } });
|
|
await POST(req({ identifier: "+60 12-345 6789" }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith(
|
|
"/api/member/forgetPassword",
|
|
expect.objectContaining({ body: { phoneNo: "+60123456789" } }),
|
|
);
|
|
});
|
|
|
|
it("trims an email identifier and leaves it otherwise unchanged", async () => {
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { success: true } });
|
|
await POST(req({ identifier: " uat.tester@example.com " }));
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith(
|
|
"/api/member/forgetPassword",
|
|
expect.objectContaining({ body: { phoneNo: "uat.tester@example.com" } }),
|
|
);
|
|
});
|
|
|
|
it("400s when identifier is missing", async () => {
|
|
const res = await POST(req({}));
|
|
expect(res.status).toBe(400);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
});
|