0
0
Fork 0

feat: refs #6598 add more tests

This commit is contained in:
Jorge Penadés 2024-05-17 12:09:42 +02:00
parent 94bf6ff381
commit 12811692eb
1 changed files with 55 additions and 24 deletions

View File

@ -1,4 +1,4 @@
import { vi, describe, expect, it, beforeAll, afterEach, beforeEach } from 'vitest';
import { vi, describe, expect, it, beforeAll, afterAll } from 'vitest';
import { axios, flushPromises } from 'app/test/vitest/helper';
import { useAcl } from 'src/composables/useAcl';
@ -12,7 +12,6 @@ describe('useAcl', () => {
permission: 'ALLOW',
principalType: 'ROLE',
principalId: 'employee',
id: 3,
},
{
model: 'Worker',
@ -21,37 +20,69 @@ describe('useAcl', () => {
permission: 'ALLOW',
principalType: 'ROLE',
principalId: 'employee',
id: 13,
},
{
model: 'Url',
property: 'getByUser',
accessType: 'READ',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: '$everyone',
},
{
model: 'TpvTransaction',
property: 'start',
accessType: 'WRITE',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: '$authenticated',
},
];
const expectedAcls = {
Address: { '*': { '*': true } },
Worker: { holidays: { READ: true } },
};
beforeAll(() => {
beforeAll(async () => {
vi.spyOn(axios, 'get').mockResolvedValue({ data: mockAcls });
vi.spyOn(acl.state, 'setAcls');
await acl.fetch();
});
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);
});
});
afterAll(async () => await flushPromises());
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', 'updateAttributes', 'WRITE')).toBeFalsy();
});
it('should return false if no roles matched', async () => {
expect(acl.hasAny('Worker', '*', 'WRITE')).toBeFalsy();
expect(acl.hasAny('Worker', 'holidays', 'READ')).toBeTruthy();
});
describe('*', () => {
it('should return true if an acl matched', async () => {
expect(acl.hasAny('Address', '*', 'WRITE')).toBeTruthy();
});
it('should return false if no acls matched', async () => {
expect(acl.hasAny('Worker', '*', 'READ')).toBeFalsy();
});
});
describe('$authenticated', () => {
it('should return false if no acls matched', async () => {
expect(acl.hasAny('Url', 'getByUser', '*')).toBeFalsy();
});
it('should return true if an acl matched', async () => {
expect(acl.hasAny('Url', 'getByUser', 'READ')).toBeTruthy();
});
});
describe('$everyone', () => {
it('should return false if no acls matched', async () => {
expect(acl.hasAny('TpvTransaction', 'start', 'READ')).toBeFalsy();
});
it('should return false if an acl matched', async () => {
expect(acl.hasAny('TpvTransaction', 'start', 'WRITE')).toBeTruthy();
});
});
});
});