Files
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

60 lines
2.6 KiB
TypeScript

import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
const mocks = vi.hoisted(() => ({ callMobileApi: vi.fn() }));
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
import { getSessionPolicy, cachedSessionPolicy, resetSessionPolicyCache } from "./sessionPolicy";
beforeEach(() => {
resetSessionPolicyCache();
mocks.callMobileApi.mockReset();
vi.useFakeTimers();
vi.setSystemTime(new Date("2026-11-20T00:00:00Z"));
delete process.env.SESSION_IDLE_TTL;
delete process.env.SESSION_ABSOLUTE_TTL;
});
afterEach(() => vi.useRealTimers());
describe("session policy (enforce-session-timeouts)", () => {
it("reads the saved admin limits with the caller's token", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { idleTtlSeconds: 900, absoluteTtlSeconds: 7200 } });
expect(await getSessionPolicy("jwt")).toEqual({ idleTtlSeconds: 900, absoluteTtlSeconds: 7200 });
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/auth/session-policy", { token: "jwt" });
});
it("caches for 60 seconds, then reads again", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { idleTtlSeconds: 900, absoluteTtlSeconds: 7200 } });
await getSessionPolicy("jwt");
vi.advanceTimersByTime(59_000);
await getSessionPolicy("jwt");
expect(mocks.callMobileApi).toHaveBeenCalledTimes(1);
vi.advanceTimersByTime(2_000);
await getSessionPolicy("jwt");
expect(mocks.callMobileApi).toHaveBeenCalledTimes(2);
});
it("falls back to the env values when the policy cannot be read", async () => {
process.env.SESSION_IDLE_TTL = "1200";
process.env.SESSION_ABSOLUTE_TTL = "3600";
mocks.callMobileApi.mockResolvedValue({ ok: false, status: 503 });
expect(await getSessionPolicy("jwt")).toEqual({ idleTtlSeconds: 1200, absoluteTtlSeconds: 3600 });
});
it("ignores a malformed answer", async () => {
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { idleTtlSeconds: "x", absoluteTtlSeconds: -1 } });
expect(await getSessionPolicy("jwt")).toEqual({ idleTtlSeconds: 3600, absoluteTtlSeconds: 28800 });
});
it("cachedSessionPolicy never calls out: the last good value, else the env values", async () => {
expect(cachedSessionPolicy()).toEqual({ idleTtlSeconds: 3600, absoluteTtlSeconds: 28800 });
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { idleTtlSeconds: 900, absoluteTtlSeconds: 7200 } });
await getSessionPolicy("jwt");
expect(cachedSessionPolicy()).toEqual({ idleTtlSeconds: 900, absoluteTtlSeconds: 7200 });
expect(mocks.callMobileApi).toHaveBeenCalledTimes(1);
});
});