This commit is contained in:
parent
74e330e523
commit
1d67233e69
|
@ -16,14 +16,13 @@ export function useAcl() {
|
||||||
state.setAcls(acls);
|
state.setAcls(acls);
|
||||||
}
|
}
|
||||||
|
|
||||||
function hasAny(model, property, accessType) {
|
function hasAny(model, prop, accessType) {
|
||||||
const modelAcls = state.getAcls().value[model];
|
const acls = state.getAcls().value[model];
|
||||||
if (!modelAcls) return false;
|
if (acls)
|
||||||
|
return ['*', prop].some((key) => {
|
||||||
return ['*', property].some(prop => {
|
const acl = acls[key];
|
||||||
const acl = modelAcls[prop];
|
return acl && (acl['*'] || acl[accessType]);
|
||||||
return acl && (acl['*'] || acl[accessType]);
|
});
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|
|
@ -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();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
|
@ -7,7 +7,6 @@ const session = useSession();
|
||||||
const state = useState();
|
const state = useState();
|
||||||
|
|
||||||
describe('session', () => {
|
describe('session', () => {
|
||||||
|
|
||||||
describe('getToken / setToken', () => {
|
describe('getToken / setToken', () => {
|
||||||
it('should return an empty string if no token is found in local or session storage', async () => {
|
it('should return an empty string if no token is found in local or session storage', async () => {
|
||||||
const expectedToken = '';
|
const expectedToken = '';
|
||||||
|
@ -90,10 +89,12 @@ describe('session', () => {
|
||||||
];
|
];
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.spyOn(axios, 'get').mockImplementation((url) => {
|
vi.spyOn(axios, 'get').mockImplementation((url) => {
|
||||||
if(url === 'VnUsers/acls') return Promise.resolve({data: []});
|
if (url === 'VnUsers/acls') return Promise.resolve({ data: [] });
|
||||||
return Promise.resolve({data: { roles: rolesData, user: expectedUser }});
|
return Promise.resolve({
|
||||||
|
data: { roles: rolesData, user: expectedUser },
|
||||||
|
});
|
||||||
});
|
});
|
||||||
})
|
});
|
||||||
|
|
||||||
it('should fetch the user roles and then set token in the sessionStorage', async () => {
|
it('should fetch the user roles and then set token in the sessionStorage', async () => {
|
||||||
const expectedRoles = ['salesPerson', 'admin'];
|
const expectedRoles = ['salesPerson', 'admin'];
|
||||||
|
|
Loading…
Reference in New Issue