import "@testing-library/jest-dom"; import { render, screen } from "@testing-library/react"; import { beforeEach, describe, expect, it, vi } from "vitest"; import { MemoryRouter } from "react-router"; import type { LoaderFunctionArgs } from "react-router"; vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn(async () => ({ t: (key: string) => key })), })); const translations: Record = { "settings.policies.eyebrow": "Workspace policy status", "settings.policies.heading": "Workspace policies", "settings.policies.description": "These read-only boundaries describe how settings changes stay scoped to the current workspace.", "settings.policies.resolvedWorkspace": "Resolved workspace:", "settings.policies.sections.membership.heading": "Membership policy", "settings.policies.sections.roles.heading": "Role policy", "settings.policies.sections.billing.heading": "Billing policy", "settings.policies.sections.notifications.heading": "Notification policy", "settings.policies.sections.audit.heading": "Data and audit policy", "settings.policies.sections.api.heading": "API policy", "settings.policies.sections.membership.links.members": "Review members", "settings.policies.sections.roles.links.roles": "Review roles", "settings.policies.sections.billing.links.billing": "Review billing", "settings.policies.sections.notifications.links.notifications": "Review notifications", "settings.policies.sections.audit.links.audit": "Review audit logs", "settings.policies.sections.api.links.api": "Review API settings", }; vi.mock("react-i18next", () => ({ useTranslation: () => ({ t: (key: string) => translations[key] ?? key }), })); vi.mock("~/utils/helpers/.server/PermissionsService", () => ({ verifyUserHasPermission: vi.fn(), })); vi.mock("~/utils/services/.server/urlService", () => ({ getTenantIdFromUrl: vi.fn(), })); vi.mock("~/utils/app/UrlUtils", () => ({ default: { currentTenantUrl: vi.fn((params: { tenant?: string }, path: string) => `/${params.tenant}/${path}`), }, })); vi.mock("react-router", async () => { const actual = await vi.importActual("react-router"); return { ...actual, useLoaderData: () => routeLoaderData, useParams: () => ({ tenant: "acme" }), }; }); import UrlUtils from "~/utils/app/UrlUtils"; import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService"; import { getTenantIdFromUrl } from "~/utils/services/.server/urlService"; import WorkspacePoliciesRoute, { loader } from "./policies"; let routeLoaderData: unknown; const request = new Request("https://app.example.test/acme/settings/policies"); const params = { tenant: "acme" }; function routeArgs(): LoaderFunctionArgs { return { request, params, context: {}, unstable_pattern: "app.$tenant/settings/policies" }; } describe("workspace policies settings route", () => { beforeEach(() => { vi.clearAllMocks(); process.env.APP_NAME = "ToolCTRL"; vi.mocked(getTenantIdFromUrl).mockResolvedValue("tenant_1"); vi.mocked(verifyUserHasPermission).mockResolvedValue(undefined as never); routeLoaderData = { title: "settings.policies.title | ToolCTRL", workspaceSlug: "acme" }; }); it("requires account settings permission for the resolved tenant before rendering policy data", async () => { const data = await loader(routeArgs()); expect(getTenantIdFromUrl).toHaveBeenCalledWith(params); expect(verifyUserHasPermission).toHaveBeenCalledWith(request, "app.settings.account.view", "tenant_1"); expect(vi.mocked(getTenantIdFromUrl).mock.invocationCallOrder[0]).toBeLessThan(vi.mocked(verifyUserHasPermission).mock.invocationCallOrder[0]); expect(data).toEqual({ title: "settings.policies.title | ToolCTRL", workspaceSlug: "acme" }); }); it("propagates permission rejection after tenant resolution", async () => { const rejection = new Response("Forbidden", { status: 403 }); vi.mocked(verifyUserHasPermission).mockRejectedValueOnce(rejection as never); await expect(loader(routeArgs())).rejects.toBe(rejection); expect(getTenantIdFromUrl).toHaveBeenCalledWith(params); expect(verifyUserHasPermission).toHaveBeenCalledWith(request, "app.settings.account.view", "tenant_1"); }); it("renders all workspace policy section headings and tenant-scoped settings links", () => { render( , ); expect(screen.getByRole("heading", { name: "Workspace policies" })).toBeInTheDocument(); expect(screen.getByText("Resolved workspace:")).toBeInTheDocument(); expect(screen.getByText("acme")).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "Membership policy" })).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "Role policy" })).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "Billing policy" })).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "Notification policy" })).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "Data and audit policy" })).toBeInTheDocument(); expect(screen.getByRole("heading", { name: "API policy" })).toBeInTheDocument(); expect(screen.getByRole("link", { name: "Review members" })).toHaveAttribute("href", "/acme/settings/members"); expect(screen.getByRole("link", { name: "Review roles" })).toHaveAttribute("href", "/acme/settings/roles-and-permissions"); expect(screen.getByRole("link", { name: "Review billing" })).toHaveAttribute("href", "/acme/settings/subscription"); expect(screen.getByRole("link", { name: "Review notifications" })).toHaveAttribute("href", "/acme/settings/notifications"); expect(screen.getByRole("link", { name: "Review audit logs" })).toHaveAttribute("href", "/acme/settings/logs"); expect(screen.getByRole("link", { name: "Review API settings" })).toHaveAttribute("href", "/acme/settings/api"); expect(UrlUtils.currentTenantUrl).toHaveBeenCalledWith({ tenant: "acme" }, "settings/api"); }); it("keeps rendered policy copy free of secret-like placeholders", () => { const { container } = render( , ); expect(container.textContent).not.toMatch(/token|secret|password|apiKey=/i); }); });