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
113 lines
4.4 KiB
TypeScript
113 lines
4.4 KiB
TypeScript
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
|
|
const h = vi.hoisted(() => ({ store: new Map<string, string>(), callMobileApi: vi.fn() }));
|
|
vi.mock("next/headers", () => ({
|
|
cookies: async () => ({
|
|
get: (n: string) => (h.store.has(n) ? { name: n, value: h.store.get(n)! } : undefined),
|
|
set: (name: string, value: string) => {
|
|
if (value) h.store.set(name, value);
|
|
else h.store.delete(name);
|
|
},
|
|
}),
|
|
}));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: h.callMobileApi }));
|
|
|
|
import { keepSessionAlive } from "./sessionKeepAlive";
|
|
import { resetSessionPolicyCache } from "./sessionPolicy";
|
|
import { sealSession } from "@/app/lib/sessionSeal";
|
|
import { sessionCookieName } from "@/app/lib/sessionCookieName";
|
|
import { SIGNED_IN_ELSEWHERE_COOKIE } from "@/app/lib/signedInElsewhere";
|
|
|
|
// one-session-per-account: a check-in that reports a click also asks mobile-api whether the session is still live, so
|
|
// a session ended by a sign-in elsewhere is noticed on the next click even when the click loads nothing.
|
|
|
|
const NOW = 1_800_000_000;
|
|
|
|
function jwt(exp: number): string {
|
|
return `h.${Buffer.from(JSON.stringify({ sub: "admin1", exp })).toString("base64url")}.s`;
|
|
}
|
|
|
|
function upstream(sessionCheck: unknown) {
|
|
h.callMobileApi.mockImplementation(async (path: string) =>
|
|
path === "/api/admin/auth/session-policy"
|
|
? { ok: true, data: { idleTtlSeconds: 3600, absoluteTtlSeconds: 28800 } }
|
|
: path === "/api/admin/auth/session"
|
|
? sessionCheck
|
|
: { ok: true, data: { token: jwt(NOW + 3600) } },
|
|
);
|
|
}
|
|
|
|
const sessionChecks = () => h.callMobileApi.mock.calls.filter(([p]) => p === "/api/admin/auth/session");
|
|
|
|
beforeEach(async () => {
|
|
h.store.clear();
|
|
h.callMobileApi.mockReset();
|
|
resetSessionPolicyCache();
|
|
vi.useFakeTimers();
|
|
vi.setSystemTime(NOW * 1000);
|
|
h.store.set(
|
|
sessionCookieName(),
|
|
await sealSession({ token: jwt(NOW + 3000), signedInAt: NOW - 600, lastActivity: NOW - 30, idleTtl: 3600, absoluteExpiry: NOW + 28000 }),
|
|
);
|
|
});
|
|
afterEach(() => vi.useRealTimers());
|
|
|
|
describe("keepSessionAlive: session replaced elsewhere", () => {
|
|
it("a click on a replaced session ends it and leaves the marker", async () => {
|
|
upstream({ ok: false, error: "Token revoked: signed in elsewhere", status: 401, code: "SESSION_REPLACED" });
|
|
|
|
expect(await keepSessionAlive(true)).toBe(false);
|
|
expect(h.store.has(sessionCookieName())).toBe(false);
|
|
expect(h.store.get(SIGNED_IN_ELSEWHERE_COOKIE)).toBe("1");
|
|
});
|
|
|
|
it("a click on a session revoked another way ends it without the marker", async () => {
|
|
upstream({ ok: false, error: "Token revoked", status: 401 });
|
|
|
|
expect(await keepSessionAlive(true)).toBe(false);
|
|
expect(h.store.has(sessionCookieName())).toBe(false);
|
|
expect(h.store.has(SIGNED_IN_ELSEWHERE_COOKIE)).toBe(false);
|
|
});
|
|
|
|
it("a click on a live session keeps it", async () => {
|
|
upstream({ ok: true, data: null });
|
|
|
|
expect(await keepSessionAlive(true)).toBe(true);
|
|
expect(sessionChecks()).toHaveLength(1);
|
|
});
|
|
|
|
it("an unreachable check does not end the session", async () => {
|
|
upstream({ ok: false, error: "Unable to reach the server. Please try again." });
|
|
|
|
expect(await keepSessionAlive(true)).toBe(true);
|
|
});
|
|
|
|
it("a timer call does not check the session", async () => {
|
|
upstream({ ok: false, error: "x", status: 401, code: "SESSION_REPLACED" });
|
|
|
|
expect(await keepSessionAlive(false)).toBe(true);
|
|
expect(sessionChecks()).toHaveLength(0);
|
|
});
|
|
});
|
|
|
|
describe("keepSessionAlive: a timer renewal refused because the account signed in elsewhere", () => {
|
|
it("ends the session and leaves the marker", async () => {
|
|
h.callMobileApi.mockImplementation(async (path: string) =>
|
|
path.endsWith("session-policy")
|
|
? { ok: true, data: { idleTtlSeconds: 3600, absoluteTtlSeconds: 28800 } }
|
|
: path === "/api/admin/auth/refresh"
|
|
? { ok: false, error: "x", status: 401, code: "SESSION_REPLACED" }
|
|
: { ok: true, data: null },
|
|
);
|
|
const store = h.store;
|
|
const name = sessionCookieName();
|
|
const { openSession } = await import("@/app/lib/sessionSeal");
|
|
const current = await openSession(store.get(name));
|
|
store.set(name, await sealSession({ ...current!, token: jwt(NOW + 100) }));
|
|
|
|
expect(await keepSessionAlive(false)).toBe(false);
|
|
expect(store.has(name)).toBe(false);
|
|
expect(store.get(SIGNED_IN_ELSEWHERE_COOKIE)).toBe("1");
|
|
});
|
|
});
|