final functions being tested

This commit is contained in:
Mateus Badan de Pieri 2023-04-14 12:16:01 +01:00
parent b0075998bf
commit 5180ac29d2
2 changed files with 38 additions and 7 deletions

View File

@ -24,7 +24,7 @@ export enum PermissionType {
QUERY = "query", QUERY = "query",
} }
class Permission { export class Permission {
type: PermissionType type: PermissionType
level: PermissionLevel level: PermissionLevel
@ -81,7 +81,7 @@ export enum BuiltinPermissionID {
POWER = "power", POWER = "power",
} }
const BUILTIN_PERMISSIONS = { export const BUILTIN_PERMISSIONS = {
PUBLIC: { PUBLIC: {
_id: BuiltinPermissionID.PUBLIC, _id: BuiltinPermissionID.PUBLIC,
name: "Public", name: "Public",

View File

@ -1,3 +1,4 @@
import { cloneDeep } from "lodash"
import { import {
doesHaveBasePermission, doesHaveBasePermission,
getBuiltinPermissionByID, getBuiltinPermissionByID,
@ -6,7 +7,12 @@ import {
PermissionType, PermissionType,
levelToNumber, levelToNumber,
getAllowedLevels, getAllowedLevels,
BuiltinPermissionID,
getBuiltinPermissions,
BUILTIN_PERMISSIONS,
Permission,
} from "../permissions" } from "../permissions"
import { BUILTIN_ROLE_IDS } from "../roles"
describe("levelToNumber", () => { describe("levelToNumber", () => {
it("should return 0 for EXECUTE", () => { it("should return 0 for EXECUTE", () => {
@ -66,11 +72,13 @@ describe("getAllowedLevels", () => {
describe("doesHaveBasePermission", () => { describe("doesHaveBasePermission", () => {
it("should return true if base permission has the required level", () => { it("should return true if base permission has the required level", () => {
const permType = PermissionType.APP const permType = PermissionType.USER
const permLevel = PermissionLevel.READ const permLevel = PermissionLevel.READ
const rolesHierarchy = [ const rolesHierarchy = [
{ roleId: "role1", permissionId: "permission1" }, {
{ roleId: "role2", permissionId: "permission2" }, roleId: BUILTIN_ROLE_IDS.ADMIN,
permissionId: BuiltinPermissionID.ADMIN,
},
] ]
expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe( expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe(
true true
@ -81,8 +89,10 @@ describe("doesHaveBasePermission", () => {
const permType = PermissionType.APP const permType = PermissionType.APP
const permLevel = PermissionLevel.READ const permLevel = PermissionLevel.READ
const rolesHierarchy = [ const rolesHierarchy = [
{ roleId: "role1", permissionId: "permission1" }, {
{ roleId: "role2", permissionId: "permission2" }, roleId: BUILTIN_ROLE_IDS.PUBLIC,
permissionId: BuiltinPermissionID.PUBLIC,
},
] ]
expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe( expect(doesHaveBasePermission(permType, permLevel, rolesHierarchy)).toBe(
false false
@ -99,3 +109,24 @@ describe("isPermissionLevelHigherThanRead", () => {
expect(isPermissionLevelHigherThanRead(PermissionLevel.READ)).toBe(false) expect(isPermissionLevelHigherThanRead(PermissionLevel.READ)).toBe(false)
}) })
}) })
describe("getBuiltinPermissions", () => {
test("returns a clone of the builtin permissions", () => {
const builtins = getBuiltinPermissions()
expect(builtins).toEqual(cloneDeep(BUILTIN_PERMISSIONS))
expect(builtins).not.toBe(BUILTIN_PERMISSIONS)
})
})
describe("getBuiltinPermissionByID", () => {
test("returns correct permission object for valid ID", () => {
const expectedPermission = {
_id: BuiltinPermissionID.PUBLIC,
name: "Public",
permissions: [
new Permission(PermissionType.WEBHOOK, PermissionLevel.EXECUTE),
],
}
expect(getBuiltinPermissionByID("public")).toEqual(expectedPermission)
})
})