accessProfile.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. })
  50. describe('hasProfile()', () => {
  51. it('should return true if there is no profile', () => {
  52. expect(accessProfile.hasProfile(null)).toBeTruthy()
  53. })
  54. it('should return false if user do not have the profile', () =>{
  55. const profile_to_have = ['admin']
  56. expect(accessProfile.hasProfile(profile_to_have)).toBeFalsy()
  57. })
  58. it('should return true if user have the profile', () =>{
  59. const profile_to_have = ['admin']
  60. store.commit('access/setIsAdmin', true)
  61. expect(accessProfile.hasProfile(profile_to_have)).toBeTruthy()
  62. })
  63. })
  64. describe('testProfile()', () => {
  65. it('should return false if profile do not exist', () => {
  66. const profile_to_test = ['none']
  67. expect(accessProfile.testProfile(profile_to_test)).toBeFalsy()
  68. })
  69. })