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,173 @@
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
vi.mock("~/utils/db/permissions/userRoles.db.server", () => ({
|
||||
findPermissionByUser: vi.fn(),
|
||||
findUserRolesByIds: vi.fn(),
|
||||
getUserRoleInAdmin: vi.fn(),
|
||||
getUserRoles: vi.fn(),
|
||||
userHasPermission: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/permissions/permissions.db.server", () => ({
|
||||
getPermissionName: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/permissions/groups.db.server", () => ({
|
||||
getMyGroups: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/db/tenants.db.server", () => ({
|
||||
getTenantMember: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/session.server", () => ({
|
||||
getUserInfo: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock("~/utils/loaders.middleware", () => ({
|
||||
getIsSuperAdminByUserId: vi.fn(),
|
||||
}));
|
||||
|
||||
import { TenantUserStatus } from "~/application/enums/tenants/TenantUserStatus";
|
||||
import { getPermissionName } from "~/utils/db/permissions/permissions.db.server";
|
||||
import { userHasPermission } from "~/utils/db/permissions/userRoles.db.server";
|
||||
import { getTenantMember } from "~/utils/db/tenants.db.server";
|
||||
import { getIsSuperAdminByUserId } from "~/utils/loaders.middleware";
|
||||
import { getUserInfo } from "~/utils/session.server";
|
||||
import {
|
||||
buildPermissionDeniedUrl,
|
||||
shapePermissionCheckResult,
|
||||
verifyUserHasPermission,
|
||||
type PermissionCheckResult,
|
||||
} from "./PermissionsService";
|
||||
|
||||
const mocks = {
|
||||
getPermissionName: vi.mocked(getPermissionName),
|
||||
getTenantMember: vi.mocked(getTenantMember),
|
||||
getIsSuperAdminByUserId: vi.mocked(getIsSuperAdminByUserId),
|
||||
getUserInfo: vi.mocked(getUserInfo),
|
||||
userHasPermission: vi.mocked(userHasPermission),
|
||||
};
|
||||
|
||||
const permissionName = "app.dashboard.view";
|
||||
|
||||
async function expectRedirect(error: unknown) {
|
||||
expect(error).toBeInstanceOf(Response);
|
||||
const response = error as Response;
|
||||
expect(response.status).toBe(302);
|
||||
return response.headers.get("Location");
|
||||
}
|
||||
|
||||
describe("verifyUserHasPermission", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
mocks.getUserInfo.mockResolvedValue({ userId: "user_123" });
|
||||
mocks.getPermissionName.mockResolvedValue({ id: "perm_123", name: permissionName, description: "Dashboard" });
|
||||
mocks.getTenantMember.mockResolvedValue({ id: "tenant_user_123", status: TenantUserStatus.ACTIVE } as Awaited<ReturnType<typeof getTenantMember>>);
|
||||
mocks.getIsSuperAdminByUserId.mockResolvedValue(false);
|
||||
mocks.userHasPermission.mockResolvedValue(true);
|
||||
});
|
||||
|
||||
it("allows users with a known granted permission", async () => {
|
||||
await expect(verifyUserHasPermission(new Request("https://app.example.com/dashboard"), permissionName, "tenant_123")).resolves.toBe(true);
|
||||
|
||||
expect(mocks.getUserInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).toHaveBeenCalledWith(permissionName);
|
||||
expect(mocks.getTenantMember).toHaveBeenCalledWith("user_123", "tenant_123");
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledWith("user_123", "tenant_123", permissionName);
|
||||
});
|
||||
|
||||
it("fails closed for unknown permissions without counting user permissions", async () => {
|
||||
mocks.getPermissionName.mockResolvedValue(null);
|
||||
|
||||
await expect(verifyUserHasPermission(new Request("https://app.example.com/legacy"), permissionName, "tenant_123")).rejects.toThrow(
|
||||
`Unknown permission: ${permissionName}`
|
||||
);
|
||||
|
||||
expect(mocks.getUserInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getTenantMember).not.toHaveBeenCalled();
|
||||
expect(mocks.userHasPermission).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("denies tenant permissions when the user no longer has active tenant membership", async () => {
|
||||
mocks.getTenantMember.mockResolvedValue(null);
|
||||
|
||||
let location: string | null = null;
|
||||
try {
|
||||
await verifyUserHasPermission(new Request("https://app.example.com/acme/settings?token=secret"), permissionName, "tenant_123");
|
||||
} catch (error) {
|
||||
location = await expectRedirect(error);
|
||||
}
|
||||
|
||||
expect(location).toBe(`/unauthorized/${permissionName}/tenant_123?redirect=%2Facme%2Fsettings`);
|
||||
expect(mocks.getTenantMember).toHaveBeenCalledWith("user_123", "tenant_123");
|
||||
expect(mocks.userHasPermission).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("throws Unauthorized for unauthenticated users before permission lookup", async () => {
|
||||
mocks.getUserInfo.mockResolvedValue({ userId: null, cookies: [{ category: "analytics", allowed: true }] });
|
||||
|
||||
await expect(verifyUserHasPermission(new Request("https://app.example.com/dashboard?token=secret"), permissionName, "tenant_123")).rejects.toThrow(
|
||||
"Unauthorized"
|
||||
);
|
||||
|
||||
expect(mocks.getUserInfo).toHaveBeenCalledTimes(1);
|
||||
expect(mocks.getPermissionName).not.toHaveBeenCalled();
|
||||
expect(mocks.userHasPermission).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("redirects denied tenant permissions with a redacted pathname-only redirect", async () => {
|
||||
mocks.userHasPermission.mockResolvedValue(false);
|
||||
|
||||
let location: string | null = null;
|
||||
try {
|
||||
await verifyUserHasPermission(new Request("https://app.example.com/settings?token=secret&apiKey=key_123"), permissionName, "tenant_123");
|
||||
} catch (error) {
|
||||
location = await expectRedirect(error);
|
||||
}
|
||||
|
||||
expect(location).toBe(`/unauthorized/${permissionName}/tenant_123?redirect=%2Fsettings`);
|
||||
expect(location).not.toContain("token=secret");
|
||||
expect(location).not.toContain("apiKey=key_123");
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it("redirects denied admin permissions without tenant scope", async () => {
|
||||
mocks.userHasPermission.mockResolvedValue(false);
|
||||
|
||||
let location: string | null = null;
|
||||
try {
|
||||
await verifyUserHasPermission(new Request("https://admin.example.com/users?jwt=secret"), "admin.users.view", null);
|
||||
} catch (error) {
|
||||
location = await expectRedirect(error);
|
||||
}
|
||||
|
||||
expect(location).toBe("/unauthorized/admin.users.view?redirect=%2Fusers");
|
||||
expect(location).not.toContain("jwt=secret");
|
||||
expect(mocks.userHasPermission).toHaveBeenCalledWith("user_123", null, "admin.users.view");
|
||||
});
|
||||
});
|
||||
|
||||
describe("permission decision helpers", () => {
|
||||
it("builds denied URLs from pathname only", () => {
|
||||
expect(
|
||||
buildPermissionDeniedUrl({ permissionName, tenantId: "tenant_123", requestUrl: "https://app.example.com/accounts?bearer=secret" })
|
||||
).toBe(`/unauthorized/${permissionName}/tenant_123?redirect=%2Faccounts`);
|
||||
});
|
||||
|
||||
it("shapes redacted permission check results", () => {
|
||||
const denied: PermissionCheckResult = shapePermissionCheckResult({
|
||||
allowed: false,
|
||||
permissionName,
|
||||
tenantId: "tenant_123",
|
||||
reason: "permission-denied",
|
||||
});
|
||||
|
||||
expect(denied).toEqual({ allowed: false, permissionName, tenantId: "tenant_123", reason: "permission-denied" });
|
||||
expect(JSON.stringify(denied)).not.toContain("cookie");
|
||||
expect(JSON.stringify(denied)).not.toContain("secret");
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user