doesHaveBasePermission tests

This commit is contained in:
Mateus Badan de Pieri 2023-04-14 11:39:52 +01:00
parent 696eea9f92
commit 038476523f
1 changed files with 41 additions and 23 deletions

View File

@ -1,13 +1,21 @@
import { import {
doesHaveBasePermission, RoleHierarchy,
getBuiltinPermissionByID,
isPermissionLevelHigherThanRead,
PermissionLevel, PermissionLevel,
PermissionType, PermissionType,
levelToNumber, levelToNumber,
getAllowedLevels, getAllowedLevels,
BuiltinPermissionID,
getBuiltinPermissions,
getBuiltinPermissionByID,
doesHaveBasePermission,
isPermissionLevelHigherThanRead,
BUILDER,
} from "../permissions" } from "../permissions"
jest.mock("../permissions", () => ({
// getTenantId: jest.fn(() => "budibase"),
// DEFAULT_TENANT_ID: "default",
}))
describe("levelToNumber", () => { describe("levelToNumber", () => {
it("should return 0 for EXECUTE", () => { it("should return 0 for EXECUTE", () => {
expect(levelToNumber(PermissionLevel.EXECUTE)).toBe(0) expect(levelToNumber(PermissionLevel.EXECUTE)).toBe(0)
@ -65,28 +73,38 @@ describe("getAllowedLevels", () => {
}) })
describe("doesHaveBasePermission", () => { describe("doesHaveBasePermission", () => {
it("should return true if base permission has the required level", () => { const rolesHierarchy: RoleHierarchy = [
const permType = PermissionType.APP { permissionId: BuiltinPermissionID.READ_ONLY },
const permLevel = PermissionLevel.READ ]
const rolesHierarchy = [
{ roleId: "role1", permissionId: "permission1" }, it("should return true for read permission of read only role", () => {
{ roleId: "role2", permissionId: "permission2" }, expect(
] doesHaveBasePermission(
expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe( PermissionType.TABLE,
true PermissionLevel.READ,
) rolesHierarchy
)
).toBe(true)
}) })
it("should return false if base permission does not have the required level", () => { it("should return false for write permission of read only role", () => {
const permType = PermissionType.APP expect(
const permLevel = PermissionLevel.READ doesHaveBasePermission(
const rolesHierarchy = [ PermissionType.TABLE,
{ roleId: "role1", permissionId: "permission1" }, PermissionLevel.WRITE,
{ roleId: "role2", permissionId: "permission2" }, rolesHierarchy
] )
expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe( ).toBe(false)
false })
)
it("should return true for execute permission of public role", () => {
expect(
doesHaveBasePermission(
PermissionType.WEBHOOK,
PermissionLevel.EXECUTE,
rolesHierarchy
)
).toBe(true)
}) })
}) })