accessProfile.spec.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. import { Ability } from '@casl/ability'
  2. import { createStore } from '~/tests/unit/Helpers'
  3. import { AbilitiesType, AccessStore } from '~/types/interfaces'
  4. import { $accessProfile } from '~/services/profile/accessProfile'
  5. import { ABILITIES } from '~/types/enums'
  6. import { accessProfile as accessModule } from '~/tests/unit/fixture/state/profile'
  7. let ability: Ability, store: AccessStore, accessProfile:any
  8. beforeEach(() => {
  9. ability = new Ability()
  10. store = createStore()
  11. store.registerModule('profile', {})
  12. store.registerModule(['profile', 'access'], accessModule)
  13. accessProfile = $accessProfile(store, ability)
  14. })
  15. describe('hasRole()', () => {
  16. it('should return true if there is no role', () => {
  17. expect(accessProfile.hasRole(null)).toBeTruthy()
  18. })
  19. it('should return false if profile dont have the role', () => {
  20. const role_to_have = ['ROLE_EVENT']
  21. expect(accessProfile.hasRole(role_to_have)).toBeFalsy()
  22. })
  23. it('should return true if profile have the role', () => {
  24. const role_to_have = ['ROLE_USERS']
  25. store.commit('access/setRoles', ['ROLE_USERS'])
  26. expect(accessProfile.hasRole(role_to_have)).toBeTruthy()
  27. })
  28. })
  29. describe('hasAbility()', () => {
  30. it('should return true if there is no ability', () => {
  31. expect(accessProfile.hasAbility(null)).toBeTruthy()
  32. })
  33. it('should return false if profile dont have the ability', () => {
  34. const ability_to_have:Array<AbilitiesType> = [{
  35. action: ABILITIES.MANAGE,
  36. subject: 'bills'
  37. }]
  38. expect(accessProfile.hasAbility(ability_to_have)).toBeFalsy()
  39. })
  40. it('should return true if profile dont have the ability', () => {
  41. const manage_bills:AbilitiesType = {
  42. action: ABILITIES.MANAGE,
  43. subject: 'bills'
  44. }
  45. const ability_to_have:Array<AbilitiesType> = [manage_bills]
  46. ability.update([manage_bills])
  47. expect(accessProfile.hasAbility(ability_to_have)).toBeTruthy()
  48. })
  49. })