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
128 lines
4.3 KiB
TypeScript
128 lines
4.3 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
|
|
const mocks = vi.hoisted(() => ({
|
|
callMobileApi: vi.fn(),
|
|
readSession: vi.fn(),
|
|
rewriteSessionWithToken: vi.fn(),
|
|
}));
|
|
vi.mock("@/app/lib/server/mobileApi", () => ({ callMobileApi: mocks.callMobileApi }));
|
|
vi.mock("@/app/lib/server/session", () => ({
|
|
readSession: mocks.readSession,
|
|
rewriteSessionWithToken: mocks.rewriteSessionWithToken,
|
|
sessionAbsoluteTtlSeconds: () => 28800,
|
|
}));
|
|
|
|
import { maybeRefreshSession, refreshGraceSeconds } from "./sessionRefresh";
|
|
|
|
function b64url(s: string): string {
|
|
return Buffer.from(s).toString("base64").replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
}
|
|
|
|
function jwtWithExp(exp: number): string {
|
|
return `h.${b64url(JSON.stringify({ exp }))}.s`;
|
|
}
|
|
|
|
const NOW = 1_800_000_000;
|
|
|
|
beforeEach(() => {
|
|
mocks.callMobileApi.mockReset();
|
|
mocks.readSession.mockReset();
|
|
mocks.rewriteSessionWithToken.mockReset();
|
|
vi.spyOn(Date, "now").mockReturnValue(NOW * 1000);
|
|
});
|
|
|
|
describe("maybeRefreshSession — sliding admin session", () => {
|
|
it("refreshes near expiry through the upstream refresh endpoint", async () => {
|
|
mocks.readSession.mockResolvedValue({
|
|
token: jwtWithExp(NOW + 120), // 2 minutes left, inside the 5-minute grace
|
|
resetPasswordRequired: false,
|
|
absoluteExpiry: NOW + 28800,
|
|
});
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { token: "new-jwt" } });
|
|
|
|
const outcome = await maybeRefreshSession();
|
|
|
|
expect(outcome).toEqual({ refreshed: true, token: "new-jwt" });
|
|
expect(mocks.callMobileApi).toHaveBeenCalledWith("/api/admin/auth/refresh", {
|
|
method: "POST",
|
|
token: jwtWithExp(NOW + 120),
|
|
});
|
|
expect(mocks.rewriteSessionWithToken).toHaveBeenCalledWith("new-jwt", {
|
|
token: jwtWithExp(NOW + 120),
|
|
resetPasswordRequired: false,
|
|
absoluteExpiry: NOW + 28800,
|
|
});
|
|
});
|
|
|
|
it("does not refresh when the token has plenty of life left", async () => {
|
|
mocks.readSession.mockResolvedValue({
|
|
token: jwtWithExp(NOW + 1800),
|
|
resetPasswordRequired: false,
|
|
absoluteExpiry: NOW + 28800,
|
|
});
|
|
|
|
const outcome = await maybeRefreshSession();
|
|
|
|
expect(outcome).toEqual({ refreshed: false, reason: "not-due" });
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("stops refreshing at the absolute cap even while the token is near expiry", async () => {
|
|
mocks.readSession.mockResolvedValue({
|
|
token: jwtWithExp(NOW + 60),
|
|
resetPasswordRequired: false,
|
|
absoluteExpiry: NOW - 10, // cap already passed
|
|
});
|
|
|
|
const outcome = await maybeRefreshSession();
|
|
|
|
expect(outcome).toEqual({ refreshed: false, reason: "absolute-cap" });
|
|
expect(mocks.callMobileApi).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("treats a session without a recorded cap as capped by the configured absolute TTL", async () => {
|
|
mocks.readSession.mockResolvedValue({
|
|
token: jwtWithExp(NOW + 60),
|
|
resetPasswordRequired: false,
|
|
// legacy cookie: no absoluteExpiry
|
|
});
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { token: "new-jwt" } });
|
|
|
|
const outcome = await maybeRefreshSession();
|
|
|
|
expect(outcome.refreshed).toBe(true);
|
|
});
|
|
|
|
it("does nothing without a session", async () => {
|
|
mocks.readSession.mockResolvedValue(null);
|
|
|
|
const outcome = await maybeRefreshSession();
|
|
|
|
expect(outcome).toEqual({ refreshed: false, reason: "no-session" });
|
|
});
|
|
|
|
it("keeps the current cookie when the upstream declines the refresh", async () => {
|
|
mocks.readSession.mockResolvedValue({
|
|
token: jwtWithExp(NOW + 60),
|
|
resetPasswordRequired: false,
|
|
absoluteExpiry: NOW + 28800,
|
|
});
|
|
mocks.callMobileApi.mockResolvedValue({ ok: true, data: { token: null } });
|
|
|
|
const outcome = await maybeRefreshSession();
|
|
|
|
expect(outcome).toEqual({ refreshed: false, reason: "upstream-unavailable" });
|
|
expect(mocks.rewriteSessionWithToken).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("exposes the refresh grace from configuration", () => {
|
|
const previous = process.env.SESSION_REFRESH_GRACE;
|
|
process.env.SESSION_REFRESH_GRACE = "90";
|
|
expect(refreshGraceSeconds()).toBe(90);
|
|
delete process.env.SESSION_REFRESH_GRACE;
|
|
expect(refreshGraceSeconds()).toBe(300);
|
|
if (previous === undefined) delete process.env.SESSION_REFRESH_GRACE;
|
|
else process.env.SESSION_REFRESH_GRACE = previous;
|
|
});
|
|
});
|