Files
OpenHands/frontend/__tests__/hooks/use-permission.test.tsx
sp.wack cd2d0ee9a5 feat(frontend): Organizational support (#9496)
Co-authored-by: openhands <openhands@all-hands.dev>
Co-authored-by: Hiep Le <69354317+hieptl@users.noreply.github.com>
Co-authored-by: Abhay Mishra <grabhaymishra@gmail.com>
Co-authored-by: Hyun Han <62870362+smosco@users.noreply.github.com>
Co-authored-by: Nhan Nguyen <nhan13574@gmail.com>
Co-authored-by: Bharath A V <avbharath1221@gmail.com>
Co-authored-by: hieptl <hieptl.developer@gmail.com>
Co-authored-by: Chloe <chloe@openhands.com>
Co-authored-by: HeyItsChloe <54480367+HeyItsChloe@users.noreply.github.com>
2026-03-13 23:38:54 +07:00

99 lines
3.1 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { renderHook } from "@testing-library/react";
import { usePermission } from "#/hooks/organizations/use-permissions";
import { rolePermissions } from "#/utils/org/permissions";
import { OrganizationUserRole } from "#/types/org";
describe("usePermission", () => {
const setup = (role: OrganizationUserRole) =>
renderHook(() => usePermission(role)).result.current;
describe("hasPermission", () => {
it("returns true when the role has the permission", () => {
const { hasPermission } = setup("admin");
expect(hasPermission("invite_user_to_organization")).toBe(true);
});
it("returns false when the role does not have the permission", () => {
const { hasPermission } = setup("member");
expect(hasPermission("invite_user_to_organization")).toBe(false);
});
});
describe("rolePermissions integration", () => {
it("matches the permissions defined for the role", () => {
const { hasPermission } = setup("member");
rolePermissions.member.forEach((permission) => {
expect(hasPermission(permission)).toBe(true);
});
});
});
describe("change_user_role permission behavior", () => {
const run = (
activeUserRole: OrganizationUserRole,
targetUserId: string,
targetRole: OrganizationUserRole,
activeUserId = "123",
) => {
const { hasPermission } = renderHook(() =>
usePermission(activeUserRole),
).result.current;
// users can't change their own roles
if (activeUserId === targetUserId) return false;
return hasPermission(`change_user_role:${targetRole}`);
};
describe("member role", () => {
it("cannot change any roles", () => {
expect(run("member", "u2", "member")).toBe(false);
expect(run("member", "u2", "admin")).toBe(false);
expect(run("member", "u2", "owner")).toBe(false);
});
});
describe("admin role", () => {
it("cannot change owner role", () => {
expect(run("admin", "u2", "owner")).toBe(false);
});
it("can change member or admin roles", () => {
expect(run("admin", "u2", "member")).toBe(
rolePermissions.admin.includes("change_user_role:member")
);
expect(run("admin", "u2", "admin")).toBe(
rolePermissions.admin.includes("change_user_role:admin")
);
});
});
describe("owner role", () => {
it("can change owner, admin, and member roles", () => {
expect(run("owner", "u2", "admin")).toBe(
rolePermissions.owner.includes("change_user_role:admin"),
);
expect(run("owner", "u2", "member")).toBe(
rolePermissions.owner.includes("change_user_role:member"),
);
expect(run("owner", "u2", "owner")).toBe(
rolePermissions.owner.includes("change_user_role:owner"),
);
});
});
describe("self role change", () => {
it("is always disallowed", () => {
expect(run("owner", "u2", "member", "u2")).toBe(false);
expect(run("admin", "u2", "member", "u2")).toBe(false);
});
});
});
});