Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/api/src/auth/session/session-store.test.ts
T

155 lines
5.2 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { createApiSessionStore } from "./session-store.js";
const START = new Date("2026-03-01T10:00:00.000Z");
function createDeterministicStore(ids: string[] = ["session-1", "session-2", "session-3"]) {
let nowMs = START.getTime();
let idIndex = 0;
return {
store: createApiSessionStore({
now: () => new Date(nowMs),
generateSessionId: () => ids[idIndex++] ?? `session-${idIndex}`,
}),
advance(ms: number) {
nowMs += ms;
},
};
}
describe("createApiSessionStore", () => {
test("creates active sessions with non-secret metadata and supports active lookup", () => {
const { store } = createDeterministicStore();
const session = store.createSession("user-1");
expect(session).toEqual({
sessionId: "session-1",
userId: "user-1",
createdAt: START,
revokedAt: undefined,
revokeReason: undefined,
});
expect(store.getActiveSession("session-1")).toEqual(session);
expect(JSON.stringify(session)).not.toContain("jwt");
expect(JSON.stringify(session)).not.toContain("cookie");
expect(JSON.stringify(session)).not.toContain("token");
});
test("returns cloned records so callers cannot mutate stored state", () => {
const { store } = createDeterministicStore();
const session = store.createSession("user-1");
session.createdAt.setUTCFullYear(2000);
session.userId = "changed";
expect(store.getSession("session-1")).toMatchObject({
sessionId: "session-1",
userId: "user-1",
createdAt: START,
});
});
test("revoke is idempotent and unknown revocation is a safe no-op", () => {
const { store, advance } = createDeterministicStore();
store.createSession("user-1");
advance(1_000);
const revoked = store.revokeSession("session-1", "logout");
advance(1_000);
const revokedAgain = store.revokeSession("session-1", "test");
expect(store.revokeSession("missing", "logout")).toBeUndefined();
expect(revoked).toMatchObject({
sessionId: "session-1",
revokedAt: new Date(START.getTime() + 1_000),
revokeReason: "logout",
});
expect(revokedAgain).toEqual(revoked);
expect(store.getActiveSession("session-1")).toBeUndefined();
});
test("rotate revokes the old session and activates the replacement", () => {
const { store, advance } = createDeterministicStore();
store.createSession("user-1");
advance(5_000);
const result = store.rotateSession("session-1", "refresh");
expect(result).toEqual({
rotated: true,
previous: {
sessionId: "session-1",
userId: "user-1",
createdAt: START,
revokedAt: new Date(START.getTime() + 5_000),
revokeReason: "refresh",
},
session: {
sessionId: "session-2",
userId: "user-1",
createdAt: new Date(START.getTime() + 5_000),
revokedAt: undefined,
revokeReason: undefined,
},
});
expect(store.getActiveSession("session-1")).toBeUndefined();
expect(result.rotated).toBe(true);
if (!result.rotated) {
throw new Error("Expected session rotation to succeed");
}
expect(store.getActiveSession("session-2")).toEqual(result.session);
});
test("unknown and revoked sessions cannot rotate", () => {
const { store } = createDeterministicStore();
store.createSession("user-1");
store.revokeSession("session-1", "logout");
expect(store.rotateSession("missing")).toEqual({ rotated: false, reason: "unknown" });
expect(store.rotateSession("session-1")).toEqual({
rotated: false,
reason: "revoked",
previous: store.getSession("session-1"),
});
});
test("happy path: createSession inserts on a single try without a retry loop", () => {
const ids: string[] = [];
const { store } = createDeterministicStore(["session-1"]);
// Wrap generateSessionId via a fresh store-spy: easier to assert call count
// through the public surface by tracking ids generated above.
const session = store.createSession("user-1");
ids.push(session.sessionId);
expect(ids).toEqual(["session-1"]);
expect(store.getActiveSession("session-1")).toMatchObject({ userId: "user-1" });
});
test("uuid collision is treated as a systemic error and throws (no retry loop)", () => {
// Force the id generator to return the same value twice; the legacy
// implementation looped to a fresh id, masking the underlying entropy bug.
const { store } = createDeterministicStore(["session-1", "session-1"]);
store.createSession("user-1");
expect(() => store.createSession("user-2")).toThrow(/UUID collision/);
// Original session must remain untouched.
expect(store.getActiveSession("session-1")).toMatchObject({ userId: "user-1" });
});
test("reset clears all sessions and isolates store state", () => {
const { store } = createDeterministicStore();
store.createSession("user-1");
store.createSession("user-2");
store.reset();
expect(store.getSession("session-1")).toBeUndefined();
expect(store.getActiveSession("session-2")).toBeUndefined();
expect(store.rotateSession("session-1")).toEqual({ rotated: false, reason: "unknown" });
});
});