Files
co-lite/solvway-customer-portal/app/lib/server/session.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

85 lines
2.8 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from "vitest";
const h = vi.hoisted(() => {
const store = new Map<string, string>();
const setSpy = vi.fn();
return { store, setSpy };
});
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, opts: unknown) => {
h.store.set(name, value);
h.setSpy(name, value, opts);
},
delete: (n: string) => {
h.store.delete(n);
},
}),
}));
import { encodeSession, decodeSession } from "@/app/lib/sessionCodec";
import { setSession, readSession, getToken, clearSession } from "./session";
beforeEach(() => {
h.store.clear();
h.setSpy.mockClear();
});
describe("sessionCodec", () => {
it("round-trips session data", () => {
const data = { token: "jwt", member: { memberCode: "73774867", name: "Tëst Lim" }, resetRequired: false };
expect(decodeSession(encodeSession(data))).toEqual(data);
});
it("returns null for garbage", () => {
expect(decodeSession("not-json")).toBeNull();
});
});
describe("session cookie I/O", () => {
it("sets an httpOnly, Secure, SameSite=Lax cookie", async () => {
await setSession({ memberCode: "73774867", name: "Test" }, "jwt-xyz");
expect(h.setSpy).toHaveBeenCalledTimes(1);
const [name, , opts] = h.setSpy.mock.calls[0];
expect(name).toBe("te_session");
expect(opts).toMatchObject({ httpOnly: true, secure: true, sameSite: "lax", path: "/" });
});
it("reads the session back", async () => {
await setSession({ memberCode: "73774867", name: "Test" }, "jwt-xyz");
const s = await readSession();
expect(s?.token).toBe("jwt-xyz");
expect(s?.member.memberCode).toBe("73774867");
expect(await getToken()).toBe("jwt-xyz");
});
it("returns null when no cookie is present", async () => {
expect(await readSession()).toBeNull();
expect(await getToken()).toBeNull();
});
it("clears the session", async () => {
await setSession({ memberCode: "1", name: "A" }, "jwt");
await clearSession();
expect(await readSession()).toBeNull();
});
});
describe("phoneVerifyRequired flag", () => {
it("persists and reads back phoneVerifyRequired", async () => {
await setSession({ memberCode: "1", name: "A" }, "jwt", false, true);
expect((await readSession())?.phoneVerifyRequired).toBe(true);
});
it("defaults phoneVerifyRequired to false", async () => {
await setSession({ memberCode: "1", name: "A" }, "jwt");
expect((await readSession())?.phoneVerifyRequired).toBe(false);
});
it("codec round-trips phoneVerifyRequired", () => {
const data = { token: "jwt", member: { memberCode: "1", name: "A" }, resetRequired: false, phoneVerifyRequired: true };
expect(decodeSession(encodeSession(data))).toEqual(data);
});
});