primary test cases, level to number working already

This commit is contained in:
Mateus Badan de Pieri 2023-04-14 10:25:38 +01:00
parent 1d7ba831ca
commit 62b5e08cde
2 changed files with 75 additions and 1 deletions

View File

@ -34,7 +34,7 @@ class Permission {
}
}
function levelToNumber(perm: PermissionLevel) {
export function levelToNumber(perm: PermissionLevel) {
switch (perm) {
// not everything has execute privileges
case PermissionLevel.EXECUTE:

View File

@ -0,0 +1,74 @@
import {
doesHaveBasePermission,
getBuiltinPermissionByID,
isPermissionLevelHigherThanRead,
PermissionLevel,
PermissionType,
levelToNumber,
} from "../permissions"
describe("levelToNumber", () => {
it("should return 0 for EXECUTE", () => {
expect(levelToNumber(PermissionLevel.EXECUTE)).toBe(0)
})
it("should return 1 for READ", () => {
expect(levelToNumber(PermissionLevel.READ)).toBe(1)
})
it("should return 2 for WRITE", () => {
expect(levelToNumber(PermissionLevel.WRITE)).toBe(2)
})
it("should return 3 for ADMIN", () => {
expect(levelToNumber(PermissionLevel.ADMIN)).toBe(3)
})
it("should return -1 for an unknown permission level", () => {
expect(levelToNumber("unknown" as PermissionLevel)).toBe(-1)
})
})
describe("getBuiltinPermissionByID", () => {
it("should return the correct permission object", () => {
const id = "123"
const permission = { _id: id, name: "Test Permission" }
expect(getBuiltinPermissionByID(id)).toEqual(permission)
expect(getBuiltinPermissionByID("456")).toBeUndefined()
})
})
describe("doesHaveBasePermission", () => {
it("should return true if base permission has the required level", () => {
const permType = PermissionType.APP
const permLevel = PermissionLevel.READ
const rolesHierarchy = [
{ roleId: "role1", permissionId: "permission1" },
{ roleId: "role2", permissionId: "permission2" },
]
expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe(
true
)
})
it("should return false if base permission does not have the required level", () => {
const permType = PermissionType.APP
const permLevel = PermissionLevel.READ
const rolesHierarchy = [
{ roleId: "role1", permissionId: "permission1" },
{ roleId: "role2", permissionId: "permission2" },
]
expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe(
false
)
})
})
describe("isPermissionLevelHigherThanRead", () => {
it("should return true if level is higher than read", () => {
expect(isPermissionLevelHigherThanRead(PermissionLevel.WRITE)).toBe(true)
})
it("should return false if level is read or lower", () => {
expect(isPermissionLevelHigherThanRead(PermissionLevel.READ)).toBe(false)
})
})