Browse Source

tests: roleUtils and sseSource (ongoing), and improve i18nUtils

Olivier Massot 3 năm trước cách đây
mục cha
commit
1824cdec07

+ 7 - 1
services/rights/roleUtils.ts

@@ -59,9 +59,11 @@ class RoleUtils {
    */
   static isA (profileName: string, roles: Array<string>): boolean {
     profileName = profileName.toUpperCase()
-    if (!profileName.match(/[A-Z_]+/)) {
+    if (!profileName.match(/^[A-Z_]+$/)) {
       throw new Error('invalid role name')
     }
+    // TODO: actuellement, passer un profil ne corresondant à aucun rôle ne lèvera aucune erreur, et se contentera de
+    //       retourner false; ce serait pas mal de lever une erreur, ce ne serait pas normal de demander un rôle inexistant
     return roles.includes('ROLE_' + profileName + '_CORE')
   }
 
@@ -86,7 +88,9 @@ class RoleUtils {
    * @return {Array<string>}
    */
   static transformUnderscoreToHyphen (roles: Array<string>): Array<string> {
+    // TODO: clarifier le fonctionnement de cette méthode et de la regex, qu'est-ce qui sépare le groupe 2 et 3 au final?
     const regex = /(ROLE_)([A-Z]*_[A-Z]*)([A-Z_]*)*/i
+
     let match
     roles = roles.map((role) => {
       if (rolesToChange.includes(role)) {
@@ -118,6 +122,8 @@ class RoleUtils {
 
     roles = RoleUtils.transformUnderscoreToHyphen(roles)
 
+    // TODO: on pourrait peut-être faciliter la lecture en réécrivant la regex en `ROLE_([A-Z-]*)(_[A-Z]+)?`, ou
+    //       même en faisant un simple split sur le '_'
     const regex = /(ROLE_)([A-Z-]*)([_A-Z]*)/i
     let match
 

+ 45 - 0
tests/units/services/rights/roleUtils.test.ts

@@ -0,0 +1,45 @@
+import { describe, test, it, expect } from 'vitest'
+import RoleUtils from "~/services/rights/roleUtils";
+
+describe('isA', () => {
+    test('has role', () => {
+        const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
+        expect(RoleUtils.isA('teacher', roles)).toBeTruthy()
+    })
+    test('has not role', () => {
+        const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
+        expect(RoleUtils.isA('financial_manager', roles)).toBeFalsy()
+    })
+    test('invalid profile name', () => {
+        expect(() => RoleUtils.isA('profile-123', [])).toThrowError('invalid role name')
+    })
+    test('unknown profile name', () => {
+        const roles = ['ROLE_PEDAGOGICS_MANAGER', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
+        expect(RoleUtils.isA('unknown', roles)).toBeFalsy()
+    })
+})
+
+describe('filterFunctionRoles', () => {
+    test('exclude roles', () => {
+        const roles = ['ROLE_EVENTS', 'ROLE_COURSES', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
+        expect(RoleUtils.filterFunctionRoles(roles)).toEqual(['ROLE_EVENTS', 'ROLE_COURSES'])
+    })
+});
+
+describe('transformUnderscoreToHyphen', () => {
+
+})
+
+describe('rolesToAbilities', () => {
+    test('exclude roles', () => {
+        const roles = ['ROLE_EVENTS_VIEW', 'ROLE_COURSES', 'ROLE_TEACHER_CORE', 'ROLE_OTHER']
+        const expected = [
+            { subject: 'events', action: 'read' },
+            { subject: 'courses', action: 'manage' },
+            { subject: 'teacher', action: 'manage' },
+            { subject: 'other', action: 'manage' },
+        ]
+
+        expect(RoleUtils.rolesToAbilities(roles)).toEqual(expected)
+    })
+})

+ 22 - 0
tests/units/services/sse/sseSource.test.ts

@@ -0,0 +1,22 @@
+import { describe, test, it, expect } from 'vitest'
+
+
+beforeEach(() => {
+    
+})
+
+describe('createEventSource', () => {
+
+})
+
+describe('isConnected', () => {
+
+})
+
+describe('subscribe', () => {
+
+})
+
+describe('unsubscribe', () => {
+
+})

+ 13 - 31
tests/units/services/utils/i18nUtils.test.ts

@@ -1,28 +1,19 @@
 import { describe, test, it, expect } from 'vitest'
 import {createI18n, VueI18n} from 'vue-i18n'
 import I18nUtils from "~/services/utils/i18nUtils";
-import { config } from "@vue/test-utils"
 import {EnumChoices} from "~/types/interfaces";
 
-// config.global.mocks = {
-//     $t: (tKey: string) => tKey  // just return translation key
-// };
-
-// const i18n = createI18n({})
-// config.global.plugins = [i18n]
-//
-// config.global.mocks["t"] = (msg: string) => 'test'
-
 describe('translateEnum', () => {
-    test('with simple enum', () => {
+    let i18nUtils: I18nUtils
+
+    beforeEach(() => {
         // @ts-ignore
         const i18n = vi.fn() as VueI18n;
-
+        i18nUtils = new I18nUtils(i18n)
         i18n.t = vi.fn((msg: string) => msg.replace('This is the letter', 'C\'est la lettre'))
+    })
 
-        // @ts-ignore
-        const i18nUtils = new I18nUtils(i18n)
-
+    test('with simple enum', () => {
         const input: EnumChoices = [
             { value: 'Alpha', label: 'This is the letter A' },
             { value: 'Beta', label: 'This is the letter B' },
@@ -39,14 +30,6 @@ describe('translateEnum', () => {
     })
 
     test('with simple enum and sorting enabled', () => {
-        // @ts-ignore
-        const i18n = vi.fn() as VueI18n;
-
-        i18n.t = vi.fn((msg: string) => msg.replace('This is the letter', 'C\'est la lettre'))
-
-        // @ts-ignore
-        const i18nUtils = new I18nUtils(i18n)
-
         const input: EnumChoices = [
             { value: 'Epsilon', label: 'This is the letter E' },
             { value: 'Alpha', label: 'This is the letter A' },
@@ -64,24 +47,23 @@ describe('translateEnum', () => {
 })
 
 describe('formatPhoneNumber', () => {
-    test('with valid international phone number', () => {
+    let i18nUtils: I18nUtils
+
+    beforeEach(() => {
         // @ts-ignore
-        const i18nUtils = new I18nUtils(i18n)
+        const i18n = vi.fn() as VueI18n;
+        i18nUtils = new I18nUtils(i18n)
+    })
+    test('with valid international phone number', () => {
         expect(i18nUtils.formatPhoneNumber('+33611223344')).toEqual('06 11 22 33 44')
     })
     test('with valid non-formatted phone number', () => {
-        // @ts-ignore
-        const i18nUtils = new I18nUtils(i18n)
         expect(i18nUtils.formatPhoneNumber('0611223344', 'FR')).toEqual('06 11 22 33 44')
     })
     test('with empty string', () => {
-        // @ts-ignore
-        const i18nUtils = new I18nUtils(i18n)
         expect(() => i18nUtils.formatPhoneNumber('', 'FR')).toThrowError('NOT_A_NUMBER')
     })
     test('with invalid string', () => {
-        // @ts-ignore
-        const i18nUtils = new I18nUtils(i18n)
         expect(() => i18nUtils.formatPhoneNumber('abcd', 'FR')).toThrowError('NOT_A_NUMBER')
     })
 })