// @vitest-environment node import type { LoaderFunctionArgs } from "react-router"; import { beforeEach, describe, expect, it, vi } from "vitest"; const mocks = vi.hoisted(() => ({ granted: new Set(), modules: new Set(), demoEnabled: true, portalDomainEnabled: false, })); vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })) })); vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: vi.fn(async () => "tenant-1") })); vi.mock("~/utils/session.server", () => ({ getUserInfo: vi.fn(async () => ({ userId: "user-1" })) })); vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn(async (_request: Request, permission: string) => { if (!mocks.granted.has(permission)) throw new Response("Not Found", { status: 404 }); }), })); vi.mock("~/utils/connector/tenantConnector.server", () => ({ tenantHasModule: vi.fn(async ({ moduleKey }: { moduleKey: string }) => mocks.modules.has(moduleKey)), })); vi.mock("~/utils/db/appConfiguration.db.server", () => ({ getAppConfiguration: vi.fn(async () => ({ app: { features: { tenantApiKeys: true } } })), })); vi.mock("~/utils/services/.server/demoDataUi.server", () => ({ isDemoDataUiEnabled: vi.fn(() => mocks.demoEnabled), })); vi.mock("~/services/customer-portal/portal-custom-domain-runtime.server", () => ({ isPortalCustomDomainRuntimeEnabled: vi.fn(() => mocks.portalDomainEnabled), })); import { loader as teamLoader } from "./team"; import { loader as workspaceLoader } from "./workspace"; import { loader as billingLoader } from "./billing"; import { loader as integrationsLoader } from "./integrations"; import { loader as complianceLoader } from "./compliance"; import { loader as customerPortalLoader } from "./customer-portal"; function args(path: string): LoaderFunctionArgs { return { request: new Request(`https://app.example.test${path}`), params: { tenant: "acme" }, context: {}, unstable_pattern: "settings-hub-test", } as LoaderFunctionArgs; } async function expectRedirect(run: Promise, location: string) { try { await run; throw new Error("expected redirect"); } catch (error) { expect(error).toBeInstanceOf(Response); expect((error as Response).status).toBe(302); expect((error as Response).headers.get("Location")).toBe(location); } } describe("settings hub loaders", () => { beforeEach(() => { mocks.granted.clear(); mocks.modules.clear(); mocks.demoEnabled = true; mocks.portalDomainEnabled = false; }); it("redirects roles-only users away from the inaccessible team index", async () => { mocks.granted.add("app.settings.roles.view"); await expectRedirect(teamLoader(args("/acme/settings/team")), "/acme/settings/team/roles"); const data = await teamLoader(args("/acme/settings/team/roles")); expect(data.tabs.map((tab) => tab.id)).toEqual(["roles"]); }); it("redirects invoice-only users away from the subscription overview", async () => { mocks.granted.add("app.settings.subscription.invoices.view"); await expectRedirect(billingLoader(args("/acme/settings/billing?from=rail")), "/acme/settings/billing/invoices?from=rail"); }); it("redirects demo-only users to the enabled workspace child", async () => { mocks.granted.add("app.onboarding.demoData.manage"); await expectRedirect(workspaceLoader(args("/acme/settings/workspace")), "/acme/settings/workspace/demo-data"); }); it("uses the email module and atomic email permission independently of integrations.read", async () => { mocks.granted.add("email.read_own"); mocks.modules.add("email-inbox"); await expectRedirect(integrationsLoader(args("/acme/settings/integrations")), "/acme/settings/integrations/email"); const data = await integrationsLoader(args("/acme/settings/integrations/email")); expect(data.tabs.map((tab) => tab.id)).toEqual(["email"]); }); it("uses each privacy leaf permission instead of the broad account permission", async () => { mocks.granted.add("privacy.avv.read"); await expectRedirect(complianceLoader(args("/acme/settings/compliance")), "/acme/settings/compliance/avv"); const data = await complianceLoader(args("/acme/settings/compliance/avv")); expect(data.tabs.map((tab) => tab.id)).toEqual(["avv"]); }); it("keeps portal.read users on the overview and routes admin-only users to an admin tab", async () => { mocks.granted.add("portal.read"); const readData = await customerPortalLoader(args("/acme/settings/customer-portal")); expect(readData.tabs.map((tab) => tab.id)).toEqual(["settings"]); mocks.granted.clear(); mocks.granted.add("portal.admin"); await expectRedirect(customerPortalLoader(args("/acme/settings/customer-portal")), "/acme/settings/customer-portal/email-templates"); }); it("returns an opaque 404 when no tab is reachable", async () => { await expect(complianceLoader(args("/acme/settings/compliance"))).rejects.toMatchObject({ status: 404 }); }); });