roleUtils.test.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import { describe, test, it, expect } from 'vitest'
  2. import RoleUtils from '~/services/rights/roleUtils'
  3. describe('isA', () => {
  4. test('has role', () => {
  5. const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
  6. expect(RoleUtils.isA('teacher', roles)).toBeTruthy()
  7. })
  8. test('has not role', () => {
  9. const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
  10. expect(RoleUtils.isA('financial_manager', roles)).toBeFalsy()
  11. })
  12. test('invalid profile name', () => {
  13. expect(() => RoleUtils.isA('profile-123', [])).toThrowError(
  14. 'invalid role name',
  15. )
  16. })
  17. test('unknown profile name', () => {
  18. const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
  19. expect(RoleUtils.isA('unknown', roles)).toBeFalsy()
  20. })
  21. })
  22. describe('filterFunctionRoles', () => {
  23. test('exclude roles', () => {
  24. const roles = [
  25. 'ROLE_EVENTS',
  26. 'ROLE_COURSES',
  27. 'ROLE_TEACHER_CORE',
  28. 'ROLE_OTHER',
  29. ]
  30. expect(RoleUtils.filterFunctionRoles(roles)).toEqual([
  31. 'ROLE_EVENTS',
  32. 'ROLE_COURSES',
  33. ])
  34. })
  35. })
  36. describe('parseRole', () => {
  37. test('simple call', () => {
  38. expect(RoleUtils.parseRole('ROLE_MYSUBJECT_VIEW')).toEqual({
  39. subject: 'MYSUBJECT',
  40. action: 'VIEW',
  41. })
  42. expect(RoleUtils.parseRole('ROLE_MYSUBJECT_CORE')).toEqual({
  43. subject: 'MYSUBJECT',
  44. action: 'CORE',
  45. })
  46. expect(RoleUtils.parseRole('ROLE_MYSUBJECT_REFERENCE')).toEqual({
  47. subject: 'MYSUBJECT',
  48. action: 'REFERENCE',
  49. })
  50. expect(RoleUtils.parseRole('ROLE_MYSUBJECT')).toEqual({
  51. subject: 'MYSUBJECT',
  52. action: '',
  53. })
  54. })
  55. test('multi-word subject', () => {
  56. expect(RoleUtils.parseRole('ROLE_MY_SUBJECT_VIEW')).toEqual({
  57. subject: 'MY-SUBJECT',
  58. action: 'VIEW',
  59. })
  60. expect(RoleUtils.parseRole('ROLE_MY_OTHER_SUBJECT')).toEqual({
  61. subject: 'MY-OTHER-SUBJECT',
  62. action: '',
  63. })
  64. })
  65. test('invalid input', () => {
  66. expect(() => RoleUtils.parseRole('INVALID')).toThrowError(
  67. 'can not parse role',
  68. )
  69. })
  70. })
  71. describe('roleToString', () => {
  72. test('simple calls', () => {
  73. expect(
  74. RoleUtils.roleToString({ subject: 'MYSUBJECT', action: '' }),
  75. ).toEqual('ROLE_MYSUBJECT')
  76. expect(
  77. RoleUtils.roleToString({ subject: 'MYSUBJECT', action: 'VIEW' }),
  78. ).toEqual('ROLE_MYSUBJECT_VIEW')
  79. expect(
  80. RoleUtils.roleToString({ subject: 'MY_SUBJECT', action: '' }),
  81. ).toEqual('ROLE_MY_SUBJECT')
  82. expect(
  83. RoleUtils.roleToString({ subject: 'MY_SUBJECT', action: 'VIEW' }),
  84. ).toEqual('ROLE_MY_SUBJECT_VIEW')
  85. expect(
  86. RoleUtils.roleToString({ subject: 'MY-SUBJECT', action: 'VIEW' }),
  87. ).toEqual('ROLE_MY-SUBJECT_VIEW')
  88. expect(
  89. RoleUtils.roleToString({ subject: 'MY-SUBJECT', action: 'VIEW' }),
  90. ).toEqual('ROLE_MY-SUBJECT_VIEW')
  91. expect(
  92. RoleUtils.roleToString({ subject: 'MY_OTHER-SUBJECT', action: '' }),
  93. ).toEqual('ROLE_MY_OTHER-SUBJECT')
  94. expect(
  95. RoleUtils.roleToString({ subject: 'MY_OTHER-SUBJECT', action: 'VIEW' }),
  96. ).toEqual('ROLE_MY_OTHER-SUBJECT_VIEW')
  97. })
  98. })
  99. describe('roleToAbility', () => {
  100. test('simple calls', () => {
  101. expect(
  102. RoleUtils.roleToAbility({ subject: 'MYSUBJECT', action: '' }),
  103. ).toEqual({ action: 'manage', subject: 'mysubject' })
  104. expect(
  105. RoleUtils.roleToAbility({ subject: 'MYSUBJECT', action: 'VIEW' }),
  106. ).toEqual({ action: 'read', subject: 'mysubject' })
  107. expect(
  108. RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: '' }),
  109. ).toEqual({ action: 'manage', subject: 'my-subject' })
  110. expect(
  111. RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: 'REFERENCE' }),
  112. ).toEqual(null)
  113. expect(
  114. RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: 'CORE' }),
  115. ).toEqual(null)
  116. })
  117. })
  118. describe('rolesToAbilities', () => {
  119. test('exclude roles', () => {
  120. const roles = [
  121. 'ROLE_EVENTS_VIEW',
  122. 'ROLE_COURSES',
  123. 'ROLE_TEACHER_CORE',
  124. 'ROLE_OTHER',
  125. ]
  126. const expected = [
  127. { subject: 'events', action: 'read' },
  128. { subject: 'courses', action: 'manage' },
  129. { subject: 'other', action: 'manage' },
  130. ]
  131. expect(RoleUtils.rolesToAbilities(roles)).toEqual(expected)
  132. })
  133. })