import { describe, it, expect, vi, beforeEach, afterEach } from "vitest"; const h = vi.hoisted(() => ({ store: new Map(), 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 { readSession, setSession } from "./session"; import { resetSessionPolicyCache } from "./sessionPolicy"; import { sealSession, openSession } from "@/app/lib/sessionSeal"; import { sessionCookieName } from "@/app/lib/sessionCookieName"; import type { SessionData } from "@/app/lib/sessionCodec"; const NOW = 1_800_000_000; function jwt(exp: number): string { const b = Buffer.from(JSON.stringify({ sub: "admin1", exp })).toString("base64url"); return `h.${b}.s`; } async function put(data: SessionData) { h.store.set(sessionCookieName(), await sealSession(data)); } async function raw(): Promise { return openSession(h.store.get(sessionCookieName())); } function policy(idle: number, absolute: number) { h.callMobileApi.mockImplementation(async (path: string) => path === "/api/admin/auth/session-policy" ? { ok: true, data: { idleTtlSeconds: idle, absoluteTtlSeconds: absolute } } : { ok: true, data: { token: jwt(NOW + 3600) } }, ); } beforeEach(() => { h.store.clear(); h.callMobileApi.mockReset(); resetSessionPolicyCache(); vi.useFakeTimers(); vi.setSystemTime(NOW * 1000); }); afterEach(() => vi.useRealTimers()); describe("keepSessionAlive (enforce-session-timeouts)", () => { it("refuses with no session", async () => { expect(await keepSessionAlive(true)).toBe(false); }); it("records activity when the browser reports input", async () => { policy(900, 28800); await put({ token: jwt(NOW + 3000), signedInAt: NOW - 600, lastActivity: NOW - 300, idleTtl: 900, absoluteExpiry: NOW + 28200 }); expect(await keepSessionAlive(true)).toBe(true); expect((await raw())?.lastActivity).toBe(NOW); }); it("does not count a timer call as activity", async () => { policy(900, 28800); await put({ token: jwt(NOW + 3000), signedInAt: NOW - 600, lastActivity: NOW - 300, idleTtl: 900, absoluteExpiry: NOW + 28200 }); expect(await keepSessionAlive(false)).toBe(true); expect((await raw())?.lastActivity).toBe(NOW - 300); }); it("applies the saved limits to the cookie", async () => { policy(600, 7200); await put({ token: jwt(NOW + 3000), signedInAt: NOW - 600, lastActivity: NOW - 100, idleTtl: 3600, absoluteExpiry: NOW + 28200 }); await keepSessionAlive(false); const s = await raw(); expect(s?.idleTtl).toBe(600); expect(s?.absoluteExpiry).toBe(NOW - 600 + 7200); }); it("ends an idle session: false and the cookie is cleared", async () => { policy(900, 28800); await put({ token: jwt(NOW + 3000), signedInAt: NOW - 2000, lastActivity: NOW - 901, idleTtl: 900, absoluteExpiry: NOW + 20000 }); expect(await keepSessionAlive(true)).toBe(false); expect(h.store.has(sessionCookieName())).toBe(false); }); it("a lowered cap ends an open session", async () => { policy(900, 7200); await put({ token: jwt(NOW + 3000), signedInAt: NOW - 3 * 3600, lastActivity: NOW - 10, idleTtl: 900, absoluteExpiry: NOW + 5 * 3600 }); expect(await keepSessionAlive(true)).toBe(false); expect(h.store.has(sessionCookieName())).toBe(false); }); it("a lowered idle limit ends a session already idle that long", async () => { policy(300, 28800); await put({ token: jwt(NOW + 3000), signedInAt: NOW - 1000, lastActivity: NOW - 400, idleTtl: 900, absoluteExpiry: NOW + 20000 }); expect(await keepSessionAlive(false)).toBe(false); }); it("renews the token when 5 minutes or less are left", async () => { policy(3600, 28800); await put({ token: jwt(NOW + 200), signedInAt: NOW - 3400, lastActivity: NOW - 30, idleTtl: 3600, absoluteExpiry: NOW + 25000 }); expect(await keepSessionAlive(false)).toBe(true); expect(h.callMobileApi).toHaveBeenCalledWith("/api/admin/auth/refresh", expect.objectContaining({ method: "POST" })); expect((await raw())?.token).toBe(jwt(NOW + 3600)); }); it("does not renew while plenty of the token is left", async () => { policy(3600, 28800); await put({ token: jwt(NOW + 1000), signedInAt: NOW - 100, lastActivity: NOW - 30, idleTtl: 3600, absoluteExpiry: NOW + 25000 }); await keepSessionAlive(true); expect(h.callMobileApi).not.toHaveBeenCalledWith("/api/admin/auth/refresh", expect.anything()); }); it("a cookie from before the change starts counting now", async () => { policy(900, 7200); await put({ token: jwt(NOW + 3000), resetPasswordRequired: false, absoluteExpiry: NOW + 100 }); expect(await keepSessionAlive(false)).toBe(true); const s = await raw(); expect(s?.signedInAt).toBe(NOW); expect(s?.lastActivity).toBe(NOW); expect(s?.absoluteExpiry).toBe(NOW + 7200); }); it("keeps the reset-password flag", async () => { policy(900, 7200); await put({ token: jwt(NOW + 3000), resetPasswordRequired: true, signedInAt: NOW - 10, lastActivity: NOW - 10, idleTtl: 900, absoluteExpiry: NOW + 7000 }); await keepSessionAlive(true); expect((await raw())?.resetPasswordRequired).toBe(true); }); }); describe("readSession with timeouts", () => { it("treats an idle session as signed out", async () => { await put({ token: jwt(NOW + 3000), signedInAt: NOW - 2000, lastActivity: NOW - 1000, idleTtl: 900, absoluteExpiry: NOW + 9000 }); expect(await readSession()).toBeNull(); }); it("treats a session at its cap as signed out", async () => { await put({ token: jwt(NOW + 3000), signedInAt: NOW - 7200, lastActivity: NOW - 5, idleTtl: 900, absoluteExpiry: NOW }); expect(await readSession()).toBeNull(); }); it("a new sign-in records sign-in and activity times", async () => { await setSession(jwt(NOW + 3600)); const s = await raw(); expect(s?.signedInAt).toBe(NOW); expect(s?.lastActivity).toBe(NOW); expect(s?.idleTtl).toBe(3600); expect(s?.absoluteExpiry).toBe(NOW + 28800); }); });