accessProfile.spec.ts 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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)
  14. accessProfile.setAbility(ability)
  15. })
  16. describe('hasRole()', () => {
  17. it('should return true if there is no role', () => {
  18. expect(accessProfile.hasRole(null)).toBeTruthy()
  19. })
  20. it('should return false if profile dont have the role', () => {
  21. const role_to_have = ['ROLE_EVENT']
  22. expect(accessProfile.hasRole(role_to_have)).toBeFalsy()
  23. })
  24. it('should return true if profile have the role', () => {
  25. const role_to_have = ['ROLE_USERS']
  26. store.commit('access/setRoles', ['ROLE_USERS'])
  27. expect(accessProfile.hasRole(role_to_have)).toBeTruthy()
  28. })
  29. })
  30. describe('hasAbility()', () => {
  31. it('should return true if there is no ability', () => {
  32. expect(accessProfile.hasAbility(null)).toBeTruthy()
  33. })
  34. it('should return false if profile dont have the ability', () => {
  35. const ability_to_have:Array<AbilitiesType> = [{
  36. action: ABILITIES.MANAGE,
  37. subject: 'bills'
  38. }]
  39. expect(accessProfile.hasAbility(ability_to_have)).toBeFalsy()
  40. })
  41. it('should return true if profile dont have the ability', () => {
  42. const manage_bills:AbilitiesType = {
  43. action: ABILITIES.MANAGE,
  44. subject: 'bills'
  45. }
  46. const ability_to_have:Array<AbilitiesType> = [manage_bills]
  47. ability.update([manage_bills])
  48. expect(accessProfile.hasAbility(ability_to_have)).toBeTruthy()
  49. })
  50. })
  51. describe('hasProfile()', () => {
  52. it('should return true if there is no profile', () => {
  53. expect(accessProfile.hasProfile(null)).toBeTruthy()
  54. })
  55. it('should return false if user do not have the profile', () =>{
  56. const profile_to_have = ['admin']
  57. expect(accessProfile.hasProfile(profile_to_have)).toBeFalsy()
  58. })
  59. it('should return true if user have the profile', () =>{
  60. const profile_to_have = ['admin']
  61. store.commit('access/setIsAdmin', true)
  62. expect(accessProfile.hasProfile(profile_to_have)).toBeTruthy()
  63. })
  64. })
  65. describe('testProfile()', () => {
  66. it('should return false if profile do not exist', () => {
  67. const profile_to_test = ['none']
  68. expect(accessProfile.testProfile(profile_to_test)).toBeFalsy()
  69. })
  70. })