Files
co-lite/solvway-admin-portal/app/lib/server/session.seal.test.ts
T
ngqileandClaude Opus 5.5 1071ddaa2a feat(admin-portal): cut babyboo portal down to Co-Lite login, users and roles
Removes every other screen, API route, component, test and the dependencies only
they used. Cookie colite_admin, Co-Lite branding, BO_USERS/BO_ROLES modules only.
The user form no longer loads branches, events or country scope.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfj5akPxyikzGc2iAHM
2026-11-26 21:24:30 +08:00

45 lines
1.7 KiB
TypeScript

import { describe, it, expect, beforeEach, vi } from "vitest";
// sign-session-cookies (F32): the server session reader only trusts a signed cookie.
const h = vi.hoisted(() => ({ store: new Map<string, string>() }));
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) => {
h.store.set(name, value);
},
delete: (n: string) => {
h.store.delete(n);
},
}),
}));
import { encodeSession } from "@/app/lib/sessionCodec";
import { setSession, readSession } from "./session";
beforeEach(() => h.store.clear());
describe("session cookie signature", () => {
it("writes a signed cookie that reads back", async () => {
await setSession("jwt-xyz", true);
expect(JSON.parse(decodeURIComponent(h.store.get("__Host-colite_admin")!)).sig).toEqual(expect.any(String));
expect((await readSession())?.token).toBe("jwt-xyz");
});
it("treats an edited cookie as signed out", async () => {
await setSession("jwt-xyz", true);
const obj = JSON.parse(decodeURIComponent(h.store.get("__Host-colite_admin")!));
obj.resetPasswordRequired = false;
h.store.set("__Host-colite_admin", encodeURIComponent(JSON.stringify(obj)));
expect(await readSession()).toBeNull();
});
it("treats an unsigned cookie as signed out", async () => {
await setSession("jwt-xyz", true);
const { sig, ...unsigned } = JSON.parse(decodeURIComponent(h.store.get("__Host-colite_admin")!));
expect(sig).toEqual(expect.any(String));
h.store.set("__Host-colite_admin", encodeSession(unsigned));
expect(await readSession()).toBeNull();
});
});