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

96 lines
3.8 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 {
listAdminUsers,
getAdminUser,
saveAdminUser,
changeAdminUserPassword,
sendAdminUserPasswordReset,
USER_STATUS_FILTER_OPTIONS,
} from "./adminUsers";
import { DEFAULT_MESSAGES } from "@/app/lib/i18n/defaultMessages";
beforeEach(() => {
mocks.apiGet.mockReset();
mocks.apiPost.mockReset();
mocks.apiPut.mockReset();
});
describe("listAdminUsers", () => {
it("translates 1-based page to a 0-based query and normalizes the page shape", async () => {
mocks.apiGet.mockResolvedValue({ content: [{ id: 1 }], totalElements: 1 });
const result = await listAdminUsers({ page: 2, pageSize: 20 });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/users?page=1&size=20");
expect(result).toEqual({ items: [{ id: 1 }], total: 1 });
});
it("sends q and enabled when given", async () => {
mocks.apiGet.mockResolvedValue({ content: [], totalElements: 0 });
await listAdminUsers({ q: "jane", enabled: "false", page: 1, pageSize: 20 });
expect(mocks.apiGet).toHaveBeenCalledWith("/api/users?q=jane&enabled=false&page=0&size=20");
});
});
describe("saveAdminUser", () => {
it("POSTs when id is absent", async () => {
mocks.apiPost.mockResolvedValue({ id: 1 });
await saveAdminUser({ name: "Jane", username: "jane.doe", email: "j@x.com", roles: [], enabled: true });
expect(mocks.apiPost).toHaveBeenCalledWith("/api/users", expect.objectContaining({ username: "jane.doe" }));
});
it("PUTs to /api/users/{id} when id is present", async () => {
mocks.apiPut.mockResolvedValue({ id: 9 });
await saveAdminUser({ id: 9, name: "Jane", username: "jane.doe", email: "j@x.com", roles: [], enabled: true });
expect(mocks.apiPut).toHaveBeenCalledWith("/api/users/9", expect.objectContaining({ id: 9 }));
});
});
describe("changeAdminUserPassword", () => {
it("POSTs the password to /api/users/{id}/change-password", async () => {
mocks.apiPost.mockResolvedValue({ updated: 1 });
const result = await changeAdminUserPassword(9, "N3wPassw0rd!");
expect(mocks.apiPost).toHaveBeenCalledWith("/api/users/9/change-password", { password: "N3wPassw0rd!" });
expect(result).toEqual({ updated: 1 });
});
});
describe("sendAdminUserPasswordReset", () => {
it("POSTs to /api/users/{id}/reset-password", async () => {
mocks.apiPost.mockResolvedValue({ updated: 1 });
const result = await sendAdminUserPasswordReset(9);
expect(mocks.apiPost).toHaveBeenCalledWith("/api/users/9/reset-password", undefined);
expect(result).toEqual({ updated: 1 });
});
});
describe("normalizeAdminUserPage identity refs", () => {
it("resolves bare role ids to the first full role object in the response and drops unresolvable ones", async () => {
const { normalizeAdminUserPage } = await import("./adminUsers");
const { items } = normalizeAdminUserPage({
content: [
{ id: 1, username: "a", roles: [{ id: 117, name: "ROLE_CV" }] },
{ id: 2, username: "b", roles: [117] },
{ id: 3, username: "c", roles: [999] },
],
totalElements: 3,
});
expect(items[0].roles).toEqual([{ id: 117, name: "ROLE_CV" }]);
expect(items[1].roles).toEqual([{ id: 117, name: "ROLE_CV" }]);
expect(items[2].roles).toEqual([]);
});
});
// Copy lock (spec "portal-copy-i18n", A7): the status filter labels are message codes, rendered with t().
describe("USER_STATUS_FILTER_OPTIONS", () => {
it("labels the options with message codes", () => {
expect(USER_STATUS_FILTER_OPTIONS).toEqual([
{ value: "true", label: "Enabled" },
{ value: "false", label: "Disabled" },
]);
for (const o of USER_STATUS_FILTER_OPTIONS) expect(Object.hasOwn(DEFAULT_MESSAGES, o.label)).toBe(true);
});
});