abilitiesUtils.spec.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import {$abilitiesUtils} from "~/services/rights/abilitiesUtils";
  2. import {Ability} from "@casl/ability";
  3. import {createStore} from "~/tests/unit/Helpers";
  4. import {AnyStore} from "~/types/interfaces";
  5. import {$accessProfile} from "~/services/profile/accessProfile";
  6. import {$organizationProfile} from "~/services/profile/organizationProfile";
  7. import {accessProfile as accessModule, organizationProfile as organizationModule} from "~/tests/unit/fixture/state/profile";
  8. import {$roleUtils} from "~/services/rights/roleUtils";
  9. let ability: Ability, store:AnyStore, abilitiesUtils:any
  10. beforeEach(()=>{
  11. ability = new Ability();
  12. store = createStore()
  13. store.registerModule('profile',{})
  14. store.registerModule(['profile','access'], accessModule)
  15. store.registerModule(['profile','organization'], organizationModule)
  16. abilitiesUtils = $abilitiesUtils(store, ability)
  17. })
  18. describe('initFactory', ()=>{
  19. beforeEach(()=>{
  20. abilitiesUtils.initFactory()
  21. })
  22. it('should init the factory service and give access to AccessProfile service', ()=>{
  23. expect(abilitiesUtils.getFactory().access).toStrictEqual($accessProfile(store, ability))
  24. })
  25. it('should init the factory service and give access to OrganizationProfile service', ()=>{
  26. expect(abilitiesUtils.getFactory().organization).toStrictEqual($organizationProfile(store))
  27. })
  28. })
  29. describe('setAbilities', ()=>{
  30. it('should call ability.update once', ()=>{
  31. ability.update = jest.fn().mockReturnValue([])
  32. abilitiesUtils.setAbilities()
  33. expect(ability.update).toBeCalledTimes(1);
  34. })
  35. it('should call store.subscribeAction once', ()=>{
  36. store.subscribeAction = jest.fn().mockReturnValue([])
  37. abilitiesUtils.setAbilities()
  38. expect(store.subscribeAction).toBeCalledTimes(1);
  39. })
  40. })
  41. describe('getAbilities()', ()=>{
  42. it('should call getAbilitiesByRoles once', ()=>{
  43. abilitiesUtils.getAbilitiesByRoles = jest.fn().mockReturnValue([])
  44. abilitiesUtils.getAbilities()
  45. expect(abilitiesUtils.getAbilitiesByRoles).toBeCalledTimes(1);
  46. })
  47. it('should call getAbilitiesByConfig once', ()=>{
  48. abilitiesUtils.getAbilitiesByConfig = jest.fn()
  49. abilitiesUtils.getAbilities()
  50. expect(abilitiesUtils.getAbilitiesByConfig).toBeCalledTimes(1);
  51. })
  52. it('should concat abilities', ()=>{
  53. abilitiesUtils.getAbilitiesByRoles = jest.fn().mockReturnValue([{
  54. action: 'manage',
  55. subject: 'user'
  56. }])
  57. abilitiesUtils.getAbilitiesByConfig = jest.fn().mockReturnValue([{
  58. action: 'manage',
  59. subject: 'bills'
  60. }])
  61. expect(abilitiesUtils.getAbilities()).toStrictEqual([{
  62. action: 'manage',
  63. subject: 'user'
  64. },{
  65. action: 'manage',
  66. subject: 'bills'
  67. }])
  68. })
  69. })
  70. describe('getAbilitiesByRoles()', ()=>{
  71. it('should call $roleUtils.transformUnderscoreToHyphenBeforeCompleteMigration once', ()=>{
  72. $roleUtils.transformUnderscoreToHyphenBeforeCompleteMigration = jest.fn().mockReturnValue([])
  73. abilitiesUtils.getAbilitiesByRoles()
  74. expect($roleUtils.transformUnderscoreToHyphenBeforeCompleteMigration ).toBeCalledTimes(1);
  75. })
  76. it('should call $roleUtils.transformRoleToAbilities once', ()=>{
  77. $roleUtils.transformRoleToAbilities = jest.fn().mockReturnValue([])
  78. abilitiesUtils.getAbilitiesByRoles()
  79. expect($roleUtils.transformRoleToAbilities ).toBeCalledTimes(1);
  80. })
  81. })
  82. describe('getAbilitiesByConfig()', ()=>{
  83. it('should throw an error if the path is wrong', ()=>{
  84. expect(()=>abilitiesUtils.getAbilitiesByConfig('wrong_path')).toThrow()
  85. })
  86. })
  87. describe('transformAbilitiesConfigToAbility', ()=>{
  88. it('should transform a config array to an Abilities array', ()=>{
  89. const abilitiesConfig = {
  90. "accesses": {
  91. "action": "display",
  92. "services": {
  93. "access": [
  94. {
  95. "function": "hasAbility",
  96. "parameters": {
  97. "action": "read",
  98. "subject": "user"
  99. }
  100. }
  101. ],
  102. "organization": [
  103. {
  104. "function": "hasModule",
  105. "parameters": [
  106. "Users"
  107. ]
  108. }
  109. ]
  110. }
  111. },
  112. "student_registration": {
  113. "action": "display",
  114. "services": {
  115. "access": [
  116. {
  117. "function": "hasAbility",
  118. "parameters": {
  119. "action": "read",
  120. "subject": "student-registration"
  121. }
  122. }
  123. ],
  124. "organization": [
  125. {
  126. "function": "hasModule",
  127. "parameters": [
  128. "UsersSchool"
  129. ]
  130. }
  131. ]
  132. }
  133. }
  134. };
  135. let abilities_to_have = [
  136. {action: 'display', subject: 'accesses'},
  137. {action: 'display', subject: 'student_registration'}
  138. ]
  139. expect(abilitiesUtils.transformAbilitiesConfigToAbility(abilitiesConfig)).toStrictEqual(abilities_to_have);
  140. })
  141. })
  142. describe('canHaveTheAbility()', ()=>{
  143. const functionsServices = {
  144. access: [
  145. {function: 'hasAbility', parameters: [{action: 'read', subject: 'users'}]}
  146. ],
  147. organization: [
  148. {function: 'hasModule', parameters: ['Users']}
  149. ]
  150. }
  151. beforeEach(()=>{
  152. abilitiesUtils.initFactory()
  153. })
  154. it('should return false when we dont have the ability', ()=>{
  155. expect(abilitiesUtils.canHaveTheAbility(functionsServices)).toBeFalsy()
  156. })
  157. it('should return false when we user have the ability but organization dont have the module', ()=>{
  158. ability.update([{action: 'manage', subject: 'users'}])
  159. expect(abilitiesUtils.canHaveTheAbility(functionsServices)).toBeFalsy()
  160. })
  161. it('should return true when we user have the ability and organization have the module', ()=>{
  162. ability.update([{action: 'manage', subject: 'users'}])
  163. store.commit('organization/setModules', ['Users'])
  164. expect(abilitiesUtils.canHaveTheAbility(functionsServices)).toBeTruthy()
  165. })
  166. })