roleUtils.test.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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('invalid role name')
  14. })
  15. test('unknown profile name', () => {
  16. const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
  17. expect(RoleUtils.isA('unknown', roles)).toBeFalsy()
  18. })
  19. })
  20. describe('filterFunctionRoles', () => {
  21. test('exclude roles', () => {
  22. const roles = ['ROLE_EVENTS', 'ROLE_COURSES', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
  23. expect(RoleUtils.filterFunctionRoles(roles)).toEqual(['ROLE_EVENTS', 'ROLE_COURSES'])
  24. })
  25. });
  26. describe('parseRole', () => {
  27. test('simple call', () => {
  28. expect(RoleUtils.parseRole('ROLE_MYSUBJECT_VIEW')).toEqual({ subject: 'MYSUBJECT', action: 'VIEW' })
  29. expect(RoleUtils.parseRole('ROLE_MYSUBJECT_CORE')).toEqual({ subject: 'MYSUBJECT', action: 'CORE' })
  30. expect(RoleUtils.parseRole('ROLE_MYSUBJECT_REFERENCE')).toEqual({ subject: 'MYSUBJECT', action: 'REFERENCE' })
  31. expect(RoleUtils.parseRole('ROLE_MYSUBJECT')).toEqual({ subject: 'MYSUBJECT', action: '' })
  32. })
  33. test('multi-word subject', () => {
  34. expect(RoleUtils.parseRole('ROLE_MY_SUBJECT_VIEW')).toEqual({ subject: 'MY-SUBJECT', action: 'VIEW' })
  35. expect(RoleUtils.parseRole('ROLE_MY_OTHER_SUBJECT')).toEqual({ subject: 'MY-OTHER-SUBJECT', action: '' })
  36. })
  37. test('invalid input', () => {
  38. expect(() => RoleUtils.parseRole('INVALID')).toThrowError('can not parse role')
  39. })
  40. })
  41. describe('roleToString', () => {
  42. test('simple calls', () => {
  43. expect(RoleUtils.roleToString({ subject: 'MYSUBJECT', action: '' })).toEqual('ROLE_MYSUBJECT')
  44. expect(RoleUtils.roleToString({ subject: 'MYSUBJECT', action: 'VIEW' })).toEqual('ROLE_MYSUBJECT_VIEW')
  45. expect(RoleUtils.roleToString({ subject: 'MY_SUBJECT', action: '' })).toEqual('ROLE_MY_SUBJECT')
  46. expect(RoleUtils.roleToString({ subject: 'MY_SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY_SUBJECT_VIEW')
  47. expect(RoleUtils.roleToString({ subject: 'MY-SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY-SUBJECT_VIEW')
  48. expect(RoleUtils.roleToString({ subject: 'MY-SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY-SUBJECT_VIEW')
  49. expect(RoleUtils.roleToString({ subject: 'MY_OTHER-SUBJECT', action: '' })).toEqual('ROLE_MY_OTHER-SUBJECT')
  50. expect(RoleUtils.roleToString({ subject: 'MY_OTHER-SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY_OTHER-SUBJECT_VIEW')
  51. })
  52. })
  53. describe('roleToAbility', () => {
  54. test('simple calls', () => {
  55. expect(RoleUtils.roleToAbility({ subject: 'MYSUBJECT', action: '' })).toEqual({ action: 'manage', subject: 'mysubject'})
  56. expect(RoleUtils.roleToAbility({ subject: 'MYSUBJECT', action: 'VIEW' })).toEqual({ action: 'read', subject: 'mysubject'})
  57. expect(RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: '' })).toEqual({ action: 'manage', subject: 'my-subject'})
  58. expect(RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: 'REFERENCE' })).toEqual(null)
  59. expect(RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: 'CORE' })).toEqual(null)
  60. })
  61. })
  62. describe('rolesToAbilities', () => {
  63. test('exclude roles', () => {
  64. const roles = ['ROLE_EVENTS_VIEW', 'ROLE_COURSES', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
  65. const expected = [
  66. { subject: 'events', action: 'read' },
  67. { subject: 'courses', action: 'manage' },
  68. { subject: 'other', action: 'manage' },
  69. ]
  70. expect(RoleUtils.rolesToAbilities(roles)).toEqual(expected)
  71. })
  72. })