Jelajahi Sumber

complete roleUtils tests

Olivier Massot 2 tahun lalu
induk
melakukan
d353a36b33

+ 1 - 0
services/rights/roleUtils.ts

@@ -112,6 +112,7 @@ class RoleUtils {
   }
 
   static roleToString(role: Role) {
+    //  TODO: est-ce qu'il faut retransformer les - en _ ?  (si oui, attention à maj les tests)
     return ['ROLE', role.subject, role.action].filter((s: string) => s.length > 0).join('_')
   }
 

+ 37 - 1
tests/units/services/rights/roleUtils.test.ts

@@ -26,8 +26,44 @@ describe('filterFunctionRoles', () => {
     })
 });
 
-describe('transformUnderscoreToHyphen', () => {
+describe('parseRole', () => {
+    test('simple call', () => {
+        expect(RoleUtils.parseRole('ROLE_MYSUBJECT_VIEW')).toEqual({ subject: 'MYSUBJECT', action: 'VIEW' })
+        expect(RoleUtils.parseRole('ROLE_MYSUBJECT_MANAGE')).toEqual({ subject: 'MYSUBJECT', action: 'MANAGE' })
+        expect(RoleUtils.parseRole('ROLE_MYSUBJECT_REFERENCE')).toEqual({ subject: 'MYSUBJECT', action: 'REFERENCE' })
+        expect(RoleUtils.parseRole('ROLE_MYSUBJECT')).toEqual({ subject: 'MYSUBJECT', action: '' })
+    })
+
+    test('multi-word subject', () => {
+        expect(RoleUtils.parseRole('ROLE_MY_SUBJECT_VIEW')).toEqual({ subject: 'MY-SUBJECT', action: 'VIEW' })
+        expect(RoleUtils.parseRole('ROLE_MY_OTHER_SUBJECT')).toEqual({ subject: 'MY-OTHER-SUBJECT', action: '' })
+    })
+
+    test('invalid input', () => {
+        expect(() => RoleUtils.parseRole('INVALID')).toThrowError('can not parse role')
+    })
+})
+
+describe('roleToString', () => {
+    test('simple calls', () => {
+        expect(RoleUtils.roleToString({ subject: 'MYSUBJECT', action: '' })).toEqual('ROLE_MYSUBJECT')
+        expect(RoleUtils.roleToString({ subject: 'MYSUBJECT', action: 'VIEW' })).toEqual('ROLE_MYSUBJECT_VIEW')
+        expect(RoleUtils.roleToString({ subject: 'MY_SUBJECT', action: '' })).toEqual('ROLE_MY_SUBJECT')
+        expect(RoleUtils.roleToString({ subject: 'MY_SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY_SUBJECT_VIEW')
+        expect(RoleUtils.roleToString({ subject: 'MY-SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY-SUBJECT_VIEW')
+        expect(RoleUtils.roleToString({ subject: 'MY-SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY-SUBJECT_VIEW')
+        expect(RoleUtils.roleToString({ subject: 'MY_OTHER-SUBJECT', action: '' })).toEqual('ROLE_MY_OTHER-SUBJECT')
+        expect(RoleUtils.roleToString({ subject: 'MY_OTHER-SUBJECT', action: 'VIEW' })).toEqual('ROLE_MY_OTHER-SUBJECT_VIEW')
+    })
+})
 
+describe('roleToAbility', () => {
+  test('simple calls', () => {
+      expect(RoleUtils.roleToAbility({ subject: 'MYSUBJECT', action: '' })).toEqual({ action: 'manage', subject: 'mysubject'})
+      expect(RoleUtils.roleToAbility({ subject: 'MYSUBJECT', action: 'VIEW' })).toEqual({ action: 'read', subject: 'mysubject'})
+      expect(RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: '' })).toEqual({ action: 'manage', subject: 'my-subject'})
+      expect(RoleUtils.roleToAbility({ subject: 'MY-SUBJECT', action: 'REFERENCE' })).toEqual({ action: undefined, subject: 'my-subject'})
+  })
 })
 
 describe('rolesToAbilities', () => {