Files
co-lite/solvway-admin-portal/app/lib/services/adminCompanyAccount.test.ts
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

106 lines
3.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from "vitest";
const mocks = vi.hoisted(() => ({ apiGet: vi.fn(), apiPost: vi.fn(), apiPut: vi.fn() }));
vi.mock("@/app/lib/clientApi", () => mocks);
import {
listCompanyAccounts,
createCompanyAccount,
updateCompanyAccount,
deactivateCompanyAccount,
reactivateCompanyAccount,
} from "./adminCompanyAccount";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
mocks.apiPut.mockReset();
});
describe("listCompanyAccounts", () => {
it("GETs /api/admin/company-accounts/{prefix} and returns the rows", async () => {
const rows = [
{
prefix: "MY",
memberCode: "MYHQ00002",
name: "HQ Two",
weight: 50,
active: "Y",
configVersion: 3,
directDownline: 2,
allocatedCount: 10,
},
];
mocks.apiGet.mockResolvedValue(rows);
const result = await listCompanyAccounts("MY");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/company-accounts/MY");
expect(result).toEqual(rows);
});
it("encodes the prefix in the URL", async () => {
mocks.apiGet.mockResolvedValue([]);
await listCompanyAccounts("my sg");
expect(mocks.apiGet).toHaveBeenCalledWith("/api/admin/company-accounts/my%20sg");
});
});
describe("createCompanyAccount", () => {
it("POSTs the full payload to /api/admin/company-accounts", async () => {
const payload = {
prefix: "MY",
memberCode: "MYHQ00002",
name: "HQ Two",
weight: 50,
};
mocks.apiPost.mockResolvedValue({ memberId: "1", loginRequested: false, credentialCreated: false });
const result = await createCompanyAccount(payload);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts", payload);
expect(result).toEqual({ memberId: "1", loginRequested: false, credentialCreated: false });
});
it("includes email/password when an optional login is requested", async () => {
const payload = {
prefix: "MY",
memberCode: "MYHQ00003",
name: "HQ Three",
weight: 25,
email: "hq3@babyboo.my",
password: "Qweqwe123",
};
mocks.apiPost.mockResolvedValue({
memberId: "2",
loginRequested: true,
credentialCreated: true,
message: "Account and login created.",
});
const result = await createCompanyAccount(payload);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts", payload);
expect(result.credentialCreated).toBe(true);
});
});
describe("updateCompanyAccount", () => {
it("PUTs to /api/admin/company-accounts with prefix/memberCode locating the row", async () => {
const payload = { prefix: "MY", memberCode: "MYHQ00002", weight: 75, active: "Y" as const };
mocks.apiPut.mockResolvedValue({ ...payload, name: "HQ Two", configVersion: 4 });
await updateCompanyAccount(payload);
expect(mocks.apiPut).toHaveBeenCalledWith("/api/admin/company-accounts", payload);
});
});
describe("deactivateCompanyAccount", () => {
it("POSTs to /api/admin/company-accounts/{prefix}/{memberCode}/deactivate", async () => {
mocks.apiPost.mockResolvedValue(undefined);
await deactivateCompanyAccount("MY", "MYHQ00002");
expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts/MY/MYHQ00002/deactivate");
});
});
describe("reactivateCompanyAccount", () => {
it("POSTs to /api/admin/company-accounts/{prefix}/{memberCode}/reactivate", async () => {
mocks.apiPost.mockResolvedValue(undefined);
await reactivateCompanyAccount("MY", "MYHQ00002");
expect(mocks.apiPost).toHaveBeenCalledWith("/api/admin/company-accounts/MY/MYHQ00002/reactivate");
});
});