|
|
@@ -1,18 +1,13 @@
|
|
|
-import { describe, test, it, expect } from 'vitest'
|
|
|
+import { describe, test, expect } from 'vitest'
|
|
|
import {MongoAbility} from "@casl/ability/dist/types/Ability";
|
|
|
import {AbilitiesType, AccessProfile, organizationState} from "~/types/interfaces";
|
|
|
import AbilityBuilder from "~/services/rights/abilityBuilder";
|
|
|
import {ABILITIES} from "~/types/enum/enums";
|
|
|
-import yaml from "yaml-import";
|
|
|
|
|
|
let ability: MongoAbility
|
|
|
let accessProfile: AccessProfile
|
|
|
let organizationProfile: organizationState
|
|
|
-let abilityBuilder: TestableAbilityBuilder
|
|
|
-
|
|
|
-class TestableAbilityBuilder extends AbilityBuilder {
|
|
|
- public execAndValidateCondition(condition: any, subject: string = '') { return super.execAndValidateCondition(condition, subject) }
|
|
|
-}
|
|
|
+let abilityBuilder: AbilityBuilder
|
|
|
|
|
|
// Mock the content of the config yaml files
|
|
|
// > This must be done in the global scope: https://vitest.dev/api/vi.html#vi-mock
|
|
|
@@ -49,7 +44,7 @@ beforeEach(() => {
|
|
|
accessProfile = vi.fn() as any as AccessProfile
|
|
|
organizationProfile = vi.fn() as any as organizationState
|
|
|
|
|
|
- abilityBuilder = new TestableAbilityBuilder(ability, accessProfile, organizationProfile)
|
|
|
+ abilityBuilder = new AbilityBuilder(ability, accessProfile, organizationProfile)
|
|
|
})
|
|
|
|
|
|
describe('buildAbilities', () => {
|
|
|
@@ -149,6 +144,7 @@ describe('execAndValidateCondition', () => {
|
|
|
})
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{
|
|
|
'function': 'accessHasAllRoleAbilities',
|
|
|
@@ -160,6 +156,7 @@ describe('execAndValidateCondition', () => {
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{
|
|
|
'function': 'accessHasAllRoleAbilities',
|
|
|
@@ -177,6 +174,7 @@ describe('execAndValidateCondition', () => {
|
|
|
})
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{
|
|
|
'function': 'accessHasAnyRoleAbility',
|
|
|
@@ -188,6 +186,7 @@ describe('execAndValidateCondition', () => {
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'accessHasAnyRoleAbility', parameters: [{action: ABILITIES.READ, subject: 'subject2'}]})
|
|
|
).toBeFalsy()
|
|
|
@@ -199,18 +198,21 @@ describe('execAndValidateCondition', () => {
|
|
|
accessProfile.isPayer = true
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'accessHasAnyProfile', parameters: ['guardian', 'payer']}
|
|
|
)
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'accessHasAnyProfile', parameters: ['guardian', 'caMember']}
|
|
|
)
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'accessHasAnyProfile', parameters: ['caMember']}
|
|
|
)
|
|
|
@@ -224,18 +226,21 @@ describe('execAndValidateCondition', () => {
|
|
|
)
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'organizationHasAllModules', parameters: ['module1', 'module2']}
|
|
|
)
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'organizationHasAllModules', parameters: ['module1', 'module3']}
|
|
|
)
|
|
|
).toBeFalsy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'organizationHasAllModules', parameters: ['module3']}
|
|
|
)
|
|
|
@@ -249,18 +254,21 @@ describe('execAndValidateCondition', () => {
|
|
|
)
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'organizationHasAnyModule', parameters: ['module1', 'module2']}
|
|
|
)
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'organizationHasAnyModule', parameters: ['module1', 'module3']}
|
|
|
)
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition(
|
|
|
{'function': 'organizationHasAnyModule', parameters: ['module3']}
|
|
|
)
|
|
|
@@ -270,90 +278,108 @@ describe('execAndValidateCondition', () => {
|
|
|
test('organizationHasAnyModule', () => {
|
|
|
// @ts-ignore
|
|
|
accessProfile.isAdminAccount = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'accessIsAdminAccount'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
accessProfile.isAdminAccount = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'accessIsAdminAccount'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationIsSchool', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isSchool = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsSchool'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isSchool = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsSchool'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationIsArtist', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isArtist = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsArtist'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isArtist = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsArtist'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationIsManagerProduct', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isManagerProduct = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsManagerProduct'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isManagerProduct = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsManagerProduct'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationHasChildren', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.hasChildren = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationHasChildren'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.hasChildren = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationHasChildren'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationIsAssociation', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isAssociation = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsAssociation'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isAssociation = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsAssociation'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationIsShowAdherentList', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isShowAdherentList = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsShowAdherentList'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isShowAdherentList = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsShowAdherentList'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationIsCmf', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isCmf = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsCmf'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.isCmf = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationIsCmf'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('organizationHasWebsite', () => {
|
|
|
// @ts-ignore
|
|
|
organizationProfile.getWebsite = true
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationHasWebsite'})).toBeTruthy()
|
|
|
|
|
|
// @ts-ignore
|
|
|
organizationProfile.getWebsite = false
|
|
|
+ // @ts-ignore
|
|
|
expect(abilityBuilder.execAndValidateCondition({'function': 'organizationHasWebsite'})).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
@@ -362,16 +388,19 @@ describe('execAndValidateCondition', () => {
|
|
|
organizationProfile.getWebsite = true
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition({'function': 'organizationHasWebsite', expectedResult: true})
|
|
|
).toBeTruthy()
|
|
|
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
abilityBuilder.execAndValidateCondition({'function': 'organizationHasWebsite', expectedResult: 'abc'})
|
|
|
).toBeFalsy()
|
|
|
})
|
|
|
|
|
|
test('invalid function', () => {
|
|
|
expect(
|
|
|
+ // @ts-ignore
|
|
|
() => abilityBuilder.execAndValidateCondition({'function': 'invalid'})
|
|
|
).toThrowError('unknown condition function : invalid')
|
|
|
})
|