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
66 lines
2.4 KiB
TypeScript
66 lines
2.4 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 { GET } from "./route";
|
|
|
|
const member = { memberCode: "1", name: "A", phoneNoPrefix: "60", phoneNo: "166789304" };
|
|
function signedIn() {
|
|
mocks.getToken.mockResolvedValue("jwt-xyz");
|
|
mocks.readSession.mockResolvedValue({ token: "jwt-xyz", member, resetRequired: false, phoneVerifyRequired: true });
|
|
}
|
|
|
|
beforeEach(() => {
|
|
Object.values(mocks).forEach((m) => m.mockReset());
|
|
});
|
|
|
|
describe("GET /api/member/otp", () => {
|
|
it("401s without a session token", async () => {
|
|
mocks.getToken.mockResolvedValue(null);
|
|
mocks.readSession.mockResolvedValue(null);
|
|
const res = await GET();
|
|
expect(res.status).toBe(401);
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("clears phoneVerifyRequired when upstream says otp is not required", async () => {
|
|
signedIn();
|
|
// Real callMobileApi turns a 2xx body with result "false" into ok:false + extra.
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "Request failed", extra: { result: "false" } });
|
|
const res = await GET();
|
|
expect(res.status).toBe(200);
|
|
expect(await res.json()).toEqual({ result: "false" });
|
|
expect(mocks.setSession).toHaveBeenCalledOnce();
|
|
expect(mocks.setSession).toHaveBeenCalledWith(member, "jwt-xyz", false, false);
|
|
});
|
|
|
|
it("returns an error and leaves the session untouched on a genuine failure", async () => {
|
|
signedIn();
|
|
mocks.callMobileApi.mockResolvedValue({ ok: false, error: "boom", status: 500 });
|
|
const res = await GET();
|
|
expect(res.status).not.toBe(200);
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("leaves the session untouched and passes the body through when otp is required", async () => {
|
|
signedIn();
|
|
const data = { result: "true", otpBean: { maskedPhone: "60****9304", code: "c", identityRef: "i", shortRef: "s" } };
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data });
|
|
const res = await GET();
|
|
expect(await res.json()).toEqual(data);
|
|
expect(mocks.setSession).not.toHaveBeenCalled();
|
|
});
|
|
});
|