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
123 lines
4.9 KiB
TypeScript
123 lines
4.9 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
callMobileApi: vi.fn(),
|
|
getToken: vi.fn(),
|
|
readSession: vi.fn(),
|
|
setSession: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({
|
|
getToken: mocks.getToken,
|
|
readSession: mocks.readSession,
|
|
setSession: mocks.setSession,
|
|
}));
|
|
|
|
import { POST } from "./route";
|
|
|
|
function req(body: unknown) {
|
|
return new Request("http://localhost/api/member/otp/verify", {
|
|
method: "POST",
|
|
body: JSON.stringify(body),
|
|
}) as unknown as Parameters<typeof POST>[0];
|
|
}
|
|
|
|
const member = { memberCode: "1", name: "A", phoneNoPrefix: "60", phoneNo: "166789304" };
|
|
function signedIn(extra: Record<string, unknown> = {}) {
|
|
mocks.getToken.mockResolvedValue("jwt-xyz");
|
|
mocks.readSession.mockResolvedValue({ token: "jwt-xyz", member, resetRequired: false, phoneVerifyRequired: true, ...extra });
|
|
}
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.getToken.mockReset();
|
|
mocks.readSession.mockReset();
|
|
mocks.setSession.mockReset();
|
|
});
|
|
|
|
describe("POST /api/member/otp/verify", () => {
|
|
it("401s when there is no session token", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
mocks.readSession.mockResolvedValue(null);
|
|
const res = await POST(req({ code: "123456" }));
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("forwards the bearer token and code, returns verified", async () => {
|
|
signedIn();
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { result: "true" } });
|
|
const res = await POST(req({ code: "654321", password: "pw" }));
|
|
expect(res.status).toBe(200);
|
|
expect((await res.json()).verified).toBe(true);
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith(
|
|
"/api/member/otpVerification",
|
|
expect.objectContaining({ token: "jwt-xyz", body: { password: "pw", code: "654321" } }),
|
|
);
|
|
});
|
|
|
|
it("clears phoneVerifyRequired in the session on success", async () => {
|
|
signedIn();
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { result: "true" } });
|
|
await POST(req({ code: "654321", password: "pw" }));
|
|
expect(mocks.setSession).toHaveBeenCalledOnce();
|
|
expect(mocks.setSession).toHaveBeenCalledWith(member, "jwt-xyz", false, false);
|
|
});
|
|
|
|
it("400s with a plain invalid-passcode message and leaves the session untouched on failure", async () => {
|
|
signedIn();
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Param 'code' or 'password' does not match. " });
|
|
const res = await POST(req({ code: "000000", password: "pw" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).error).toBe("Invalid passcode. Please try again.");
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("maps an invalid code to the invalid-passcode message", async () => {
|
|
signedIn();
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Param 'code' is invalid. " });
|
|
const res = await POST(req({ code: "000000", password: "pw" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).error).toBe("Invalid passcode. Please try again.");
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("maps an expired code to the expired-passcode message", async () => {
|
|
signedIn();
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Param 'code' is expired. " });
|
|
const res = await POST(req({ code: "000000", password: "pw" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).error).toBe("This passcode has expired. Please tap Resend.");
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("keeps a non-passcode failure message unchanged", async () => {
|
|
signedIn();
|
|
const msg = "Something went wrong. Please try again.";
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: msg });
|
|
const res = await POST(req({ code: "000000", password: "pw" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).error).toBe(msg);
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("401s with the session-expired message when the upstream session expired", async () => {
|
|
signedIn();
|
|
const msg = "Your session has expired. Please sign in again.";
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: msg, expired: true });
|
|
const res = await POST(req({ code: "000000", password: "pw" }));
|
|
expect(res.status).toBe(401);
|
|
expect((await res.json()).error).toBe(msg);
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("400s 'Passcode is required.' when code is missing", async () => {
|
|
signedIn();
|
|
const res = await POST(req({ password: "pw" }));
|
|
expect(res.status).toBe(400);
|
|
expect((await res.json()).error).toBe("Passcode is required.");
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
});
|