From 1d67233e691c0c7cdd03b273a4c1039c880d1af7 Mon Sep 17 00:00:00 2001 From: jorgep Date: Thu, 16 May 2024 16:29:42 +0200 Subject: [PATCH] feat: refs #6598 unit tests --- src/composables/useAcl.js | 15 +++-- .../__tests__/composables/useAcl.spec.js | 57 +++++++++++++++++++ .../__tests__/composables/useSession.spec.js | 9 +-- 3 files changed, 69 insertions(+), 12 deletions(-) create mode 100644 test/vitest/__tests__/composables/useAcl.spec.js diff --git a/src/composables/useAcl.js b/src/composables/useAcl.js index 362a7c551..46aaa3c25 100644 --- a/src/composables/useAcl.js +++ b/src/composables/useAcl.js @@ -16,14 +16,13 @@ export function useAcl() { state.setAcls(acls); } - function hasAny(model, property, accessType) { - const modelAcls = state.getAcls().value[model]; - if (!modelAcls) return false; - - return ['*', property].some(prop => { - const acl = modelAcls[prop]; - return acl && (acl['*'] || acl[accessType]); - }); + function hasAny(model, prop, accessType) { + const acls = state.getAcls().value[model]; + if (acls) + return ['*', prop].some((key) => { + const acl = acls[key]; + return acl && (acl['*'] || acl[accessType]); + }); } return { diff --git a/test/vitest/__tests__/composables/useAcl.spec.js b/test/vitest/__tests__/composables/useAcl.spec.js new file mode 100644 index 000000000..d468720e5 --- /dev/null +++ b/test/vitest/__tests__/composables/useAcl.spec.js @@ -0,0 +1,57 @@ +import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest'; +import { axios, flushPromises } from 'app/test/vitest/helper'; +import { useAcl } from 'src/composables/useAcl'; + +describe('useAcl', () => { + const acl = useAcl(); + const mockAcls = [ + { + model: 'Address', + property: '*', + accessType: '*', + permission: 'ALLOW', + principalType: 'ROLE', + principalId: 'employee', + id: 3, + }, + { + model: 'Worker', + property: 'holidays', + accessType: 'READ', + permission: 'ALLOW', + principalType: 'ROLE', + principalId: 'employee', + id: 13, + }, + ]; + const expectedAcls = { + Address: { '*': { '*': true } }, + Worker: { holidays: { READ: true } }, + }; + beforeAll(() => { + vi.spyOn(axios, 'get').mockResolvedValue({ data: mockAcls }); + vi.spyOn(acl.state, 'setAcls'); + }); + + beforeEach(async () => await acl.fetch()); + + afterEach(async () => { + await flushPromises(); + acl.state.setAcls([]); + }); + + describe('fetch', () => { + it('should call setUser and setRoles of the state with the expected data', async () => { + expect(acl.state.setAcls).toHaveBeenCalledWith(expectedAcls); + }); + }); + + describe('hasAny', () => { + it('should return true if a role matched', async () => + expect(acl.hasAny('Address', '*', 'WRITE')).toBeTruthy()); + + it('should return false if no roles matched', async () => { + expect(acl.hasAny('Worker', '*', 'WRITE')).toBeFalsy(); + }); + }); +}); diff --git a/test/vitest/__tests__/composables/useSession.spec.js b/test/vitest/__tests__/composables/useSession.spec.js index 6e08807cf..831acbf18 100644 --- a/test/vitest/__tests__/composables/useSession.spec.js +++ b/test/vitest/__tests__/composables/useSession.spec.js @@ -7,7 +7,6 @@ const session = useSession(); const state = useState(); describe('session', () => { - describe('getToken / setToken', () => { it('should return an empty string if no token is found in local or session storage', async () => { const expectedToken = ''; @@ -90,10 +89,12 @@ describe('session', () => { ]; beforeEach(() => { vi.spyOn(axios, 'get').mockImplementation((url) => { - if(url === 'VnUsers/acls') return Promise.resolve({data: []}); - return Promise.resolve({data: { roles: rolesData, user: expectedUser }}); + if (url === 'VnUsers/acls') return Promise.resolve({ data: [] }); + return Promise.resolve({ + data: { roles: rolesData, user: expectedUser }, + }); }); - }) + }); it('should fetch the user roles and then set token in the sessionStorage', async () => { const expectedRoles = ['salesPerson', 'admin'];