Skip to content
This repository has been archived by the owner on Sep 12, 2023. It is now read-only.

Commit

Permalink
unit(policy): allow setting permission combinations that can bypass
Browse files Browse the repository at this point in the history
  • Loading branch information
KennethTrecy committed Sep 11, 2022
1 parent a4035b4 commit bbc20dc
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions server/policies/belongs_to_current_user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,48 @@ describe("Policy: Belongs to current user", () => {
requester.expectSuccess()
})

it("cannot allow different user", async() => {
it("can allow different user if permitted", async() => {
const userFactory = new UserFactory()
const role = await new RoleFactory().userFlags(permissions.generateMask("view")).insertOne()
const adminRole = await new RoleFactory()
.userFlags(permissions.generateMask("readDepartmentScope"))
.insertOne()
const user = await userFactory.attach(role).serializedOne(true)
const otherUser = await userFactory.attach(adminRole).serializedOne(true)
const pageGuard = new BelongsToCurrentUserPolicy(UserManager, {
"bypassNecessarilyWith": {
"group": permissions,
"combinations": [
[ "readDepartmentScope" ]
]
}
})
requester.customizeRequest({
"isAuthenticated": jest.fn().mockReturnValue(true),
"params": {
"id": String(user.data.id)
},
"user": otherUser
})

await requester.runMiddleware(pageGuard.intermediate.bind(pageGuard))

requester.expectSuccess()
})

it("cannot allow different user without required permission", async() => {
const userFactory = new UserFactory()
const role = await new RoleFactory().userFlags(permissions.generateMask("view")).insertOne()
const user = await userFactory.attach(role).serializedOne(true)
const otherUser = await userFactory.serializedOne(true)
const pageGuard = new BelongsToCurrentUserPolicy(UserManager)
const pageGuard = new BelongsToCurrentUserPolicy(UserManager,, {
"bypassNecessarilyWith": {
"group": permissions,
"combinations": [
[ "readDepartmentScope" ]
]
}
})
requester.customizeRequest({
"isAuthenticated": jest.fn().mockReturnValue(true),
"params": {
Expand Down

0 comments on commit bbc20dc

Please sign in to comment.