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
+90
@@ -0,0 +1,90 @@
|
||||
// @vitest-environment node
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
import type { LoaderFunctionArgs } from "react-router";
|
||||
import { getRlsExecutionContext } from "@hivegrid/db/rls-context.server";
|
||||
import { getTranslations } from "~/locale/i18next.server";
|
||||
import { getTenant } from "~/utils/db/tenants.db.server";
|
||||
import { adminGetAllTenantUsers } from "~/utils/db/users.db.server";
|
||||
import { getTenantSubscription } from "~/utils/db/tenantSubscriptions.db.server";
|
||||
import { getAllSubscriptionProducts } from "~/utils/db/subscriptionProducts.db.server";
|
||||
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
|
||||
import { getTenantConnectorRows } from "~/services/admin/subscription-assembly.server";
|
||||
import { getTenantNoteRows } from "~/services/admin/tenant-notes.server";
|
||||
import { db } from "~/utils/db.server";
|
||||
import { loader } from "./$id";
|
||||
|
||||
vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => key }) }));
|
||||
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() }));
|
||||
vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn() }));
|
||||
vi.mock("~/services/admin/subscription-assembly.server", async () => {
|
||||
const actual = await vi.importActual<typeof import("~/services/admin/subscription-assembly.server")>("~/services/admin/subscription-assembly.server");
|
||||
return { ...actual, getTenantConnectorRows: vi.fn(), setTenantFeatureConnector: vi.fn() };
|
||||
});
|
||||
vi.mock("~/services/admin/tenant-notes.server", async () => {
|
||||
const actual = await vi.importActual<typeof import("~/services/admin/tenant-notes.server")>("~/services/admin/tenant-notes.server");
|
||||
return { ...actual, getTenantNoteRows: vi.fn(), createTenantNote: vi.fn(), deleteTenantNote: vi.fn() };
|
||||
});
|
||||
vi.mock("~/utils/db.server", () => ({ db: { tenantSubscription: { findUnique: vi.fn() } } }));
|
||||
vi.mock("~/utils/db/tenants.db.server", async () => {
|
||||
const actual = await vi.importActual<typeof import("~/utils/db/tenants.db.server")>("~/utils/db/tenants.db.server");
|
||||
return { ...actual, getTenant: vi.fn() };
|
||||
});
|
||||
vi.mock("~/utils/db/users.db.server", async () => {
|
||||
const actual = await vi.importActual<typeof import("~/utils/db/users.db.server")>("~/utils/db/users.db.server");
|
||||
return { ...actual, adminGetAllTenantUsers: vi.fn() };
|
||||
});
|
||||
vi.mock("~/utils/db/tenantSubscriptions.db.server", () => ({ getTenantSubscription: vi.fn() }));
|
||||
vi.mock("~/utils/db/subscriptionProducts.db.server", () => ({ getAllSubscriptionProducts: vi.fn() }));
|
||||
|
||||
describe("admin account tenant RLS context", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
process.env.APP_NAME = "HiveGrid";
|
||||
vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never);
|
||||
vi.mocked(getTranslations).mockResolvedValue({ t: (key: string) => key } as never);
|
||||
vi.mocked(getTenant).mockImplementation(async () => {
|
||||
expect(getRlsExecutionContext()?.tenantId).toBe("tenant_1");
|
||||
return {
|
||||
id: "tenant_1",
|
||||
name: "Acme",
|
||||
slug: "acme",
|
||||
active: true,
|
||||
deactivatedReason: null,
|
||||
users: [],
|
||||
} as never;
|
||||
});
|
||||
vi.mocked(adminGetAllTenantUsers).mockImplementation(async () => {
|
||||
expect(getRlsExecutionContext()?.tenantId).toBe("tenant_1");
|
||||
return [];
|
||||
});
|
||||
vi.mocked(getTenantSubscription).mockImplementation(async () => {
|
||||
expect(getRlsExecutionContext()?.tenantId).toBe("tenant_1");
|
||||
return null;
|
||||
});
|
||||
vi.mocked(getAllSubscriptionProducts).mockResolvedValue([]);
|
||||
vi.mocked(getTenantConnectorRows).mockImplementation(async () => {
|
||||
expect(getRlsExecutionContext()?.tenantId).toBe("tenant_1");
|
||||
return [];
|
||||
});
|
||||
vi.mocked(getTenantNoteRows).mockImplementation(async () => {
|
||||
expect(getRlsExecutionContext()?.tenantId).toBe("tenant_1");
|
||||
return [];
|
||||
});
|
||||
vi.mocked(db.tenantSubscription.findUnique).mockImplementation((async () => {
|
||||
expect(getRlsExecutionContext()?.tenantId).toBe("tenant_1");
|
||||
return null;
|
||||
}) as never);
|
||||
});
|
||||
|
||||
it("binds the target tenant only after the admin permission check", async () => {
|
||||
const data = await loader({
|
||||
request: new Request("https://admin.example.test/accounts/tenant_1"),
|
||||
params: { id: "tenant_1" },
|
||||
context: {},
|
||||
} as LoaderFunctionArgs);
|
||||
|
||||
expect(verifyUserHasPermission).toHaveBeenCalledWith(expect.any(Request), "admin.account.view");
|
||||
expect(vi.mocked(verifyUserHasPermission).mock.invocationCallOrder[0]).toBeLessThan(vi.mocked(getTenant).mock.invocationCallOrder[0]);
|
||||
expect(data.tenant.id).toBe("tenant_1");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user