0
0
Fork 0

feat: refs #6598 unit tests

This commit is contained in:
Jorge Penadés 2024-05-16 16:29:42 +02:00
parent 74e330e523
commit 1d67233e69
3 changed files with 69 additions and 12 deletions

View File

@ -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 {

View File

@ -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();
});
});
});

View File

@ -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'];