Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -1,4 +1,5 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
import { getRlsExecutionContext, runWithRlsContext } from "@hivegrid/db/rls-context.server";
const dbMock = vi.hoisted(() => ({
userSession: {
@@ -10,11 +11,13 @@ const dbMock = vi.hoisted(() => ({
updateMany: vi.fn(),
},
}));
const resolveUserSessionRlsIdentityMock = vi.hoisted(() => vi.fn());
vi.mock("~/utils/db.server", () => ({ db: dbMock, dbRead: dbMock }));
vi.mock("@hivegrid/db/rls-resolvers.server", () => ({
resolveUserSessionRlsIdentity: resolveUserSessionRlsIdentityMock,
}));
vi.mock("~/utils/cache.server", () => ({
// Cache als Pass-through: getFreshValue direkt aufrufen, clearCacheKey als Spy.
cachified: vi.fn(async (opts: { getFreshValue: () => Promise<unknown> }) => opts.getFreshValue()),
clearCacheKey: vi.fn(),
}));
@@ -27,6 +30,7 @@ import {
listActiveUserSessions,
markSessionTrusted,
revokeAllUserSessionsExcept,
revokeAllUserSessionsForUser,
revokeSessionByToken,
revokeUserSession,
} from "./sessionStore.server";
@@ -42,6 +46,7 @@ function pastDate(msAgo = 60_000) {
beforeEach(() => {
vi.clearAllMocks();
resolveUserSessionRlsIdentityMock.mockResolvedValue({ resourceId: "s1", userId: "u1" });
});
describe("session token hashing", () => {
@@ -71,6 +76,7 @@ describe("createUserSession", () => {
expect(data.ipHash).toBe("deadbeef");
expect(data.trustedAt).toBeNull();
});
});
describe("getActiveSessionByToken", () => {
@@ -80,19 +86,71 @@ describe("getActiveSessionByToken", () => {
});
it("returns the session when active", async () => {
dbMock.userSession.findUnique.mockResolvedValue({ id: "s1", revokedAt: null, expiresAt: futureDate() });
dbMock.userSession.findUnique.mockResolvedValue({
id: "s1",
revokedAt: null,
expiresAt: futureDate(),
user: { active: true, pendingDeletion: false, pseudonymized: false },
});
expect(await getActiveSessionByToken("raw")).toMatchObject({ id: "s1" });
expect(dbMock.userSession.findUnique).toHaveBeenCalledWith(
expect.objectContaining({ include: { user: { select: expect.objectContaining({ active: true, pendingDeletion: true, pseudonymized: true }) } } })
);
});
it("fails closed when the bootstrap resolver cannot prove the token owner", async () => {
resolveUserSessionRlsIdentityMock.mockResolvedValueOnce(null).mockResolvedValueOnce(null);
await expect(getActiveSessionByToken("unknown")).resolves.toBeNull();
await expect(revokeSessionByToken("unknown")).resolves.toBe(false);
expect(dbMock.userSession.findUnique).not.toHaveBeenCalled();
});
it("checks the source of truth on every cross-request validation", async () => {
dbMock.userSession.findUnique.mockResolvedValue({
id: "s1",
revokedAt: null,
expiresAt: futureDate(),
user: { active: true, pendingDeletion: false, pseudonymized: false },
});
await getActiveSessionByToken("raw");
await getActiveSessionByToken("raw");
expect(dbMock.userSession.findUnique).toHaveBeenCalledTimes(2);
});
it("returns null for a revoked session", async () => {
dbMock.userSession.findUnique.mockResolvedValue({ id: "s1", revokedAt: pastDate(), expiresAt: futureDate() });
dbMock.userSession.findUnique.mockResolvedValue({
id: "s1",
revokedAt: pastDate(),
expiresAt: futureDate(),
user: { active: true, pendingDeletion: false, pseudonymized: false },
});
expect(await getActiveSessionByToken("raw")).toBeNull();
});
it("returns null for an expired session", async () => {
dbMock.userSession.findUnique.mockResolvedValue({ id: "s1", revokedAt: null, expiresAt: pastDate() });
dbMock.userSession.findUnique.mockResolvedValue({
id: "s1",
revokedAt: null,
expiresAt: pastDate(),
user: { active: true, pendingDeletion: false, pseudonymized: false },
});
expect(await getActiveSessionByToken("raw")).toBeNull();
});
it.each([
{ active: false, pendingDeletion: false, pseudonymized: false },
{ active: true, pendingDeletion: true, pseudonymized: false },
{ active: true, pendingDeletion: false, pseudonymized: true },
])("returns null when the session owner is unavailable ($active/$pendingDeletion/$pseudonymized)", async (user) => {
dbMock.userSession.findUnique.mockResolvedValue({ id: "s1", revokedAt: null, expiresAt: futureDate(), user });
expect(await getActiveSessionByToken("raw")).toBeNull();
});
});
describe("listActiveUserSessions", () => {
@@ -104,6 +162,17 @@ describe("listActiveUserSessions", () => {
expect(where.revokedAt).toBeNull();
expect(where.expiresAt).toHaveProperty("gt");
});
it("reuses an already-bound matching user RLS context", async () => {
dbMock.userSession.findMany.mockImplementationOnce(async () => {
expect(getRlsExecutionContext()).toEqual({ userId: "u1" });
return [];
});
await runWithRlsContext({ userId: "u1" }, () => listActiveUserSessions("u1"));
expect(getRlsExecutionContext()).toBeNull();
});
});
describe("revokeUserSession", () => {
@@ -149,6 +218,22 @@ describe("revokeAllUserSessionsExcept", () => {
});
});
describe("revokeAllUserSessionsForUser", () => {
it("revokes every active row after an external authorization change", async () => {
dbMock.userSession.findMany.mockResolvedValue([{ tokenHash: "h1" }, { tokenHash: "h2" }]);
dbMock.userSession.updateMany.mockResolvedValue({ count: 2 });
await expect(revokeAllUserSessionsForUser("u1")).resolves.toBe(2);
expect(dbMock.userSession.updateMany).toHaveBeenCalledWith({
where: { userId: "u1", revokedAt: null, expiresAt: { gt: expect.any(Date) } },
data: { revokedAt: expect.any(Date) },
});
expect(clearCacheKeyMock).toHaveBeenCalledWith("user-session:h1");
expect(clearCacheKeyMock).toHaveBeenCalledWith("user-session:h2");
});
});
describe("revokeSessionByToken", () => {
it("returns false for a missing token without touching the DB", async () => {
expect(await revokeSessionByToken(undefined)).toBe(false);