Track HiveGrid Cloud planning reference
This commit is contained in:
1 parent
b3d5ea6764
commit
e341c5938a
5784 files changed
+1104929
-3
No files matched your search
@@ -0,0 +1,141 @@
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
isolationApiKeyLogA,
|
||||
isolationApiKeyLogB,
|
||||
isolationTenantA,
|
||||
isolationTenantB,
|
||||
makeAdminHostGetRequest,
|
||||
} from "./fixtures/user-session-isolation";
|
||||
|
||||
const globalMockStoreKey = "__adminRoutesIsolationMocks__";
|
||||
const globalMockStore = globalThis as typeof globalThis & {
|
||||
__adminRoutesIsolationMocks__?: {
|
||||
verifyUserHasPermission: ReturnType<typeof vi.fn>;
|
||||
requireAuth: ReturnType<typeof vi.fn>;
|
||||
getAllApiKeys: ReturnType<typeof vi.fn>;
|
||||
getDetails: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
};
|
||||
|
||||
globalMockStore.__adminRoutesIsolationMocks__ ??= {
|
||||
verifyUserHasPermission: vi.fn(),
|
||||
requireAuth: vi.fn(),
|
||||
getAllApiKeys: vi.fn(),
|
||||
getDetails: vi.fn(),
|
||||
};
|
||||
|
||||
const mocks = globalMockStore[globalMockStoreKey];
|
||||
|
||||
vi.mock("~/components/core/apiKeys/ApiKeysTable", () => ({ default: () => null }));
|
||||
vi.mock("~/components/ui/layouts/EditPageLayout", () => ({ default: ({ children }: { children: React.ReactNode }) => children }));
|
||||
vi.mock("~/utils/data/useAdminData", () => ({ useAdminData: vi.fn() }));
|
||||
vi.mock("~/utils/helpers/PermissionsHelper", () => ({ getUserHasPermission: vi.fn() }));
|
||||
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: mocks.verifyUserHasPermission }));
|
||||
vi.mock("~/utils/loaders.middleware", () => ({ requireAuth: mocks.requireAuth }));
|
||||
vi.mock("~/utils/db/apiKeys.db.server", () => ({ getAllApiKeys: mocks.getAllApiKeys }));
|
||||
vi.mock("~/utils/session.server", () => ({ validateCSRFToken: vi.fn() }));
|
||||
vi.mock("~/modules/api/services/ApiKeyLogService", () => ({ default: { getDetails: mocks.getDetails } }));
|
||||
vi.mock("~/modules/api/components/ApiKeyLogsDetails", () => ({ default: () => null }));
|
||||
vi.mock("~/application/dtos/data/FilterablePropertyDto", () => ({}));
|
||||
vi.mock("~/application/dtos/data/PaginationDto", () => ({}));
|
||||
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() }));
|
||||
|
||||
const tenantAApiKey = {
|
||||
id: "api_key_session_iso_a",
|
||||
tenantId: isolationTenantA.id,
|
||||
tenant: isolationTenantA,
|
||||
alias: "Tenant A key",
|
||||
};
|
||||
|
||||
const tenantBApiKey = {
|
||||
id: "api_key_session_iso_b",
|
||||
tenantId: isolationTenantB.id,
|
||||
tenant: isolationTenantB,
|
||||
alias: "Tenant B key",
|
||||
};
|
||||
|
||||
const tenantALog = {
|
||||
...isolationApiKeyLogA,
|
||||
tenantId: isolationTenantA.id,
|
||||
tenant: isolationTenantA,
|
||||
endpoint: "/api/parents",
|
||||
};
|
||||
|
||||
const tenantBLog = {
|
||||
...isolationApiKeyLogB,
|
||||
tenantId: isolationTenantB.id,
|
||||
tenant: isolationTenantB,
|
||||
endpoint: "/api/parents",
|
||||
};
|
||||
|
||||
async function callApiKeysLoader(request = makeAdminHostGetRequest("/api/keys")) {
|
||||
const { loader } = await import("../../routes/admin/api/keys");
|
||||
return loader({ request, params: {}, context: {} } as unknown as LoaderFunctionArgs);
|
||||
}
|
||||
|
||||
async function callApiLogsLoader(request = makeAdminHostGetRequest("/api/logs?page=1")) {
|
||||
const { loader } = await import("../../routes/admin/api/logs");
|
||||
return loader({ request, params: {}, context: {} } as unknown as LoaderFunctionArgs);
|
||||
}
|
||||
|
||||
describe("admin API route isolation", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
vi.resetModules();
|
||||
mocks.verifyUserHasPermission.mockResolvedValue(true);
|
||||
mocks.requireAuth.mockResolvedValue({ userId: "user_session_iso_admin", tenantId: null });
|
||||
mocks.getAllApiKeys.mockResolvedValue([tenantAApiKey, tenantBApiKey]);
|
||||
mocks.getDetails.mockResolvedValue({
|
||||
items: [tenantALog, tenantBLog],
|
||||
filterableProperties: [{ name: "tenantId", title: "models.tenant.object", options: [] }],
|
||||
pagination: { page: 1, pageSize: 10, totalItems: 2, totalPages: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it("loads tenant A and tenant B API keys only after admin permission", async () => {
|
||||
const request = makeAdminHostGetRequest("/api/keys");
|
||||
|
||||
const data = await callApiKeysLoader(request);
|
||||
|
||||
expect(mocks.verifyUserHasPermission).toHaveBeenCalledWith(request, "admin.apiKeys.view");
|
||||
expect(mocks.getAllApiKeys).toHaveBeenCalledWith();
|
||||
expect(mocks.getAllApiKeys.mock.calls[0]).toHaveLength(0);
|
||||
expect(data).toEqual({ apiKeys: [tenantAApiKey, tenantBApiKey] });
|
||||
});
|
||||
|
||||
it("skips cross-tenant API-key access when admin permission is denied", async () => {
|
||||
const denied = Object.assign(new Error("admin.apiKeys.view denied"), { status: 403 });
|
||||
mocks.verifyUserHasPermission.mockRejectedValueOnce(denied);
|
||||
|
||||
await expect(callApiKeysLoader()).rejects.toBe(denied);
|
||||
|
||||
expect(mocks.getAllApiKeys).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("loads tenant A and tenant B API logs only after admin auth and permission", async () => {
|
||||
const request = makeAdminHostGetRequest("/api/logs?page=1");
|
||||
|
||||
const data = await callApiLogsLoader(request);
|
||||
|
||||
expect(mocks.requireAuth).toHaveBeenCalledWith({ request, params: {}, adminOnly: true });
|
||||
expect(mocks.verifyUserHasPermission).toHaveBeenCalledWith(request, "admin.apiKeys.view");
|
||||
expect(mocks.getDetails).toHaveBeenCalledWith({ request, params: {} });
|
||||
expect(data).toEqual({
|
||||
items: [tenantALog, tenantBLog],
|
||||
filterableProperties: [{ name: "tenantId", title: "models.tenant.object", options: [] }],
|
||||
pagination: { page: 1, pageSize: 10, totalItems: 2, totalPages: 1 },
|
||||
});
|
||||
});
|
||||
|
||||
it("skips cross-tenant API-log access when admin auth is denied", async () => {
|
||||
const denied = Object.assign(new Error("admin auth denied"), { status: 401 });
|
||||
mocks.requireAuth.mockRejectedValueOnce(denied);
|
||||
|
||||
await expect(callApiLogsLoader()).rejects.toBe(denied);
|
||||
|
||||
expect(mocks.verifyUserHasPermission).not.toHaveBeenCalled();
|
||||
expect(mocks.getDetails).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user