Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
+186
-7
@@ -1,14 +1,16 @@
|
||||
// @vitest-environment node
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const testWebConfig = {
|
||||
nodeEnv: "test",
|
||||
sessionSecret: "dGVzdC1zZXNzaW9uLXNlY3JldC13aXRoLWVub3VnaC1lbnRyb3B5LTEyMzQ1Njc4OTA=",
|
||||
i18n: { enabledLocales: ["de", "en"] as const },
|
||||
routing: {
|
||||
appBaseDomain: "localhost",
|
||||
protocol: "http" as const,
|
||||
},
|
||||
crisp: { chatWebsiteId: undefined },
|
||||
webPush: { vapidPublicKey: undefined },
|
||||
};
|
||||
|
||||
vi.doMock("@hivegrid/config", async () => {
|
||||
@@ -28,12 +30,23 @@ vi.mock("./db/users.db.server", () => ({
|
||||
getUser: vi.fn(),
|
||||
}));
|
||||
|
||||
const authStoreMock = vi.hoisted(() => ({ getActiveSessionByToken: vi.fn() }));
|
||||
const sessionLifecycleMock = vi.hoisted(() => ({ endServerSession: vi.fn(), startServerSession: vi.fn() }));
|
||||
vi.mock("~/services/auth/sessionStore.server", () => ({
|
||||
getActiveSessionByToken: (...args: unknown[]) => authStoreMock.getActiveSessionByToken(...args),
|
||||
}));
|
||||
vi.mock("~/services/auth/sessionLifecycle.server", () => ({
|
||||
endServerSession: (...args: unknown[]) => sessionLifecycleMock.endServerSession(...args),
|
||||
startServerSession: (...args: unknown[]) => sessionLifecycleMock.startServerSession(...args),
|
||||
}));
|
||||
|
||||
vi.mock("@hivegrid/db/auth-boundary.server", () => ({
|
||||
getAuthUserWithoutPasswordById: vi.fn(),
|
||||
getTenantById: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/locale/i18next.server", () => ({
|
||||
enabledLocales: ["de", "en"],
|
||||
i18nCookie: { serialize: vi.fn(async () => "i18n=de") },
|
||||
remixI18Next: { getLocale: vi.fn(async () => "de") },
|
||||
}));
|
||||
@@ -82,7 +95,9 @@ const {
|
||||
getUserInfo,
|
||||
getUserSession,
|
||||
logout,
|
||||
requireUserId,
|
||||
storage,
|
||||
validateAdminImpersonationSessionState,
|
||||
validateImpersonationSessionState,
|
||||
} = await import("./session.server");
|
||||
const { getAuthUserWithoutPasswordById } = await import("@hivegrid/db/auth-boundary.server");
|
||||
@@ -98,7 +113,12 @@ const rootTestUsers = {
|
||||
|
||||
async function requestFromSession(sessionValues: Record<string, unknown>) {
|
||||
const session = await storage.getSession();
|
||||
for (const [key, value] of Object.entries(sessionValues)) {
|
||||
const values = { ...sessionValues };
|
||||
if (typeof values.userId === "string" && values.sessionToken === undefined) {
|
||||
const ownerUserId = typeof values.impersonatingFromUserId === "string" ? values.impersonatingFromUserId : values.userId;
|
||||
values.sessionToken = `tok:${ownerUserId}`;
|
||||
}
|
||||
for (const [key, value] of Object.entries(values)) {
|
||||
session.set(key, value);
|
||||
}
|
||||
|
||||
@@ -107,7 +127,90 @@ async function requestFromSession(sessionValues: Record<string, unknown>) {
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
authStoreMock.getActiveSessionByToken.mockReset();
|
||||
authStoreMock.getActiveSessionByToken.mockImplementation(async (rawToken: string) => ({
|
||||
id: `session:${rawToken}`,
|
||||
userId: rawToken.startsWith("tok:") ? rawToken.slice(4) : "unknown",
|
||||
}));
|
||||
sessionLifecycleMock.endServerSession.mockReset();
|
||||
sessionLifecycleMock.endServerSession.mockResolvedValue(undefined);
|
||||
sessionLifecycleMock.startServerSession.mockReset();
|
||||
sessionLifecycleMock.startServerSession.mockImplementation(async ({ userId }: { userId: string }) => `tok:${userId}`);
|
||||
});
|
||||
|
||||
describe("impersonation session TTL", () => {
|
||||
it("accepts only complete, fresh admin impersonation state", () => {
|
||||
expect(
|
||||
validateAdminImpersonationSessionState(
|
||||
{
|
||||
impersonatingFromAdminUserId: "admin-1",
|
||||
impersonatingFromAdminEmail: "admin@example.test",
|
||||
impersonatingStartedAt: freshStartedAt,
|
||||
userId: "user-1",
|
||||
},
|
||||
now
|
||||
)
|
||||
).toBe(true);
|
||||
expect(
|
||||
validateAdminImpersonationSessionState(
|
||||
{
|
||||
impersonatingFromAdminUserId: "admin-1",
|
||||
impersonatingStartedAt: freshStartedAt,
|
||||
userId: "user-1",
|
||||
},
|
||||
now
|
||||
)
|
||||
).toBe(false);
|
||||
});
|
||||
|
||||
it("fails closed instead of restoring a tenant target from stale admin impersonation state", async () => {
|
||||
const request = await requestFromSession({
|
||||
userId: "user-1",
|
||||
impersonatingFromAdminUserId: "admin-1",
|
||||
impersonatingFromAdminEmail: "admin@example.test",
|
||||
impersonatingStartedAt: new Date(Date.now() - IMPERSONATION_SESSION_TTL_MS - 1).toISOString(),
|
||||
});
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({
|
||||
userId: null,
|
||||
impersonatingFromAdminUserId: undefined,
|
||||
impersonatingFromAdminEmail: undefined,
|
||||
impersonatingStartedAt: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("creates a target-owned tenant session with an auditable admin banner marker", async () => {
|
||||
const startedAt = createImpersonationStartedAt(new Date());
|
||||
const response = await createUserSession(
|
||||
{
|
||||
userId: "user-1",
|
||||
lightOrDarkMode: "",
|
||||
lng: "de",
|
||||
cookies: [],
|
||||
impersonatingFromAdminUserId: "admin-1",
|
||||
impersonatingFromAdminEmail: "admin@example.test",
|
||||
impersonatingStartedAt: startedAt,
|
||||
},
|
||||
"/app",
|
||||
{ request: new Request("https://example.test/admin/impersonate") }
|
||||
);
|
||||
const request = new Request("https://example.test/app", {
|
||||
headers: { Cookie: (response.headers.get("Set-Cookie") ?? "").split(";")[0] },
|
||||
});
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({
|
||||
userId: "user-1",
|
||||
impersonatingFromAdminUserId: "admin-1",
|
||||
impersonatingFromAdminEmail: "admin@example.test",
|
||||
impersonatingStartedAt: startedAt,
|
||||
});
|
||||
expect(sessionLifecycleMock.startServerSession).toHaveBeenCalledWith({
|
||||
request: expect.any(Request),
|
||||
userId: "user-1",
|
||||
});
|
||||
});
|
||||
|
||||
it("accepts fresh impersonation state", () => {
|
||||
expect(
|
||||
validateImpersonationSessionState(
|
||||
@@ -190,7 +293,7 @@ describe("impersonation session TTL", () => {
|
||||
});
|
||||
});
|
||||
|
||||
it("omits stale or malformed impersonation state from getUserInfo", async () => {
|
||||
it("restores the actor instead of degrading invalid impersonation state into a target session", async () => {
|
||||
const request = await requestFromSession({
|
||||
userId: "user-1",
|
||||
impersonatingFromUserId: "admin-1",
|
||||
@@ -198,7 +301,30 @@ describe("impersonation session TTL", () => {
|
||||
});
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({
|
||||
userId: "admin-1",
|
||||
impersonatingFromUserId: undefined,
|
||||
impersonatingStartedAt: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
it("restores the actor in requireUserId after the impersonation TTL expires", async () => {
|
||||
const request = await requestFromSession({
|
||||
userId: "user-1",
|
||||
impersonatingFromUserId: "admin-1",
|
||||
impersonatingStartedAt: new Date(Date.now() - IMPERSONATION_SESSION_TTL_MS - 1).toISOString(),
|
||||
});
|
||||
|
||||
await expect(requireUserId(request)).resolves.toBe("admin-1");
|
||||
});
|
||||
|
||||
it("fails closed when invalid impersonation state has no actor to restore", async () => {
|
||||
const request = await requestFromSession({
|
||||
userId: "user-1",
|
||||
impersonatingStartedAt: "not-a-date",
|
||||
});
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({
|
||||
userId: null,
|
||||
impersonatingFromUserId: undefined,
|
||||
impersonatingStartedAt: undefined,
|
||||
});
|
||||
@@ -213,12 +339,14 @@ describe("impersonation session TTL", () => {
|
||||
cookies: [],
|
||||
impersonatingFromUserId: "admin-1",
|
||||
},
|
||||
"/app"
|
||||
"/app",
|
||||
{ request: new Request("https://example.test/admin/impersonate") },
|
||||
);
|
||||
const cookie = response.headers.get("Set-Cookie") ?? "";
|
||||
const request = new Request("https://example.test/app", { headers: { Cookie: cookie.split(";")[0] } });
|
||||
|
||||
await expect(getUserInfo(request)).resolves.toMatchObject({
|
||||
userId: "admin-1",
|
||||
impersonatingFromUserId: undefined,
|
||||
impersonatingStartedAt: undefined,
|
||||
});
|
||||
@@ -235,11 +363,12 @@ describe("impersonation session TTL", () => {
|
||||
impersonatingFromUserId: "admin-1",
|
||||
impersonatingStartedAt: createImpersonationStartedAt(now),
|
||||
},
|
||||
"/app"
|
||||
"/app",
|
||||
{ request: new Request("https://example.test/admin/impersonate") },
|
||||
);
|
||||
const cookie = response.headers.get("Set-Cookie") ?? "";
|
||||
|
||||
expect(cookie).toContain("hivegrid_session=");
|
||||
expect(cookie).toContain("hivegrid_app_session=");
|
||||
expect(cookie).not.toContain("csrf-secret-token");
|
||||
expect(cookie).not.toContain("password");
|
||||
expect(cookie).not.toContain("totp");
|
||||
@@ -258,7 +387,8 @@ describe("impersonation session TTL", () => {
|
||||
impersonatingStartedAt: startedAt,
|
||||
passwordPolicyOutdated: true,
|
||||
},
|
||||
"/app"
|
||||
"/app",
|
||||
{ request: new Request("https://example.test/admin/impersonate") },
|
||||
);
|
||||
const request = new Request("https://example.test/app", {
|
||||
headers: { Cookie: (response.headers.get("Set-Cookie") ?? "").split(";")[0] },
|
||||
@@ -270,6 +400,53 @@ describe("impersonation session TTL", () => {
|
||||
impersonatingStartedAt: startedAt,
|
||||
passwordPolicyOutdated: true,
|
||||
});
|
||||
expect(sessionLifecycleMock.startServerSession).toHaveBeenCalledWith({
|
||||
request: expect.any(Request),
|
||||
userId: "admin-1",
|
||||
});
|
||||
});
|
||||
|
||||
it("reuses an active actor-owned token when switching into impersonation", async () => {
|
||||
const startedAt = createImpersonationStartedAt(new Date());
|
||||
const request = new Request("https://example.test/admin/impersonate");
|
||||
|
||||
const response = await createUserSession(
|
||||
{
|
||||
userId: "user-1",
|
||||
lightOrDarkMode: "",
|
||||
lng: "de",
|
||||
cookies: [],
|
||||
sessionToken: "tok:admin-1",
|
||||
impersonatingFromUserId: "admin-1",
|
||||
impersonatingStartedAt: startedAt,
|
||||
},
|
||||
"/app",
|
||||
{ request },
|
||||
);
|
||||
|
||||
expect(response.status).toBe(302);
|
||||
expect(authStoreMock.getActiveSessionByToken).toHaveBeenCalledWith("tok:admin-1");
|
||||
expect(sessionLifecycleMock.startServerSession).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("rejects an impersonation cookie backed by a token owned by the target", async () => {
|
||||
const startedAt = createImpersonationStartedAt(new Date());
|
||||
|
||||
await expect(
|
||||
createUserSession(
|
||||
{
|
||||
userId: "user-1",
|
||||
lightOrDarkMode: "",
|
||||
lng: "de",
|
||||
cookies: [],
|
||||
sessionToken: "tok:user-1",
|
||||
impersonatingFromUserId: "admin-1",
|
||||
impersonatingStartedAt: startedAt,
|
||||
},
|
||||
"/app",
|
||||
{ request: new Request("https://example.test/admin/impersonate") },
|
||||
),
|
||||
).rejects.toThrow("owner-mismatch");
|
||||
});
|
||||
|
||||
it("clears impersonation state on logout", async () => {
|
||||
@@ -326,9 +503,11 @@ describe("impersonation session TTL", () => {
|
||||
|
||||
expect(staleData.userSession.impersonatingFromUserId).toBeUndefined();
|
||||
expect(staleData.userSession.impersonatingStartedAt).toBeUndefined();
|
||||
expect(staleData.userSession.userId).toBe("admin-1");
|
||||
expect(staleData.impersonatingSession).toBeNull();
|
||||
expect(malformedData.userSession.impersonatingFromUserId).toBeUndefined();
|
||||
expect(malformedData.userSession.impersonatingStartedAt).toBeUndefined();
|
||||
expect(malformedData.userSession.userId).toBe("admin-1");
|
||||
expect(malformedData.impersonatingSession).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user