0
0
Fork 0
salix-front-mindshore-fork2/test/vitest/__tests__/composables/useAcl.spec.js

89 lines
2.8 KiB
JavaScript
Raw Normal View History

2024-05-17 10:09:42 +00:00
import { vi, describe, expect, it, beforeAll, afterAll } from 'vitest';
2024-05-16 14:29:42 +00:00
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',
},
{
model: 'Worker',
property: 'holidays',
accessType: 'READ',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: 'employee',
2024-05-17 10:09:42 +00:00
},
{
model: 'Url',
property: 'getByUser',
accessType: 'READ',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: '$everyone',
},
{
model: 'TpvTransaction',
property: 'start',
accessType: 'WRITE',
permission: 'ALLOW',
principalType: 'ROLE',
principalId: '$authenticated',
2024-05-16 14:29:42 +00:00
},
];
2024-05-17 10:09:42 +00:00
beforeAll(async () => {
2024-05-16 14:29:42 +00:00
vi.spyOn(axios, 'get').mockResolvedValue({ data: mockAcls });
2024-05-17 10:09:42 +00:00
await acl.fetch();
2024-05-16 14:29:42 +00:00
});
2024-05-17 10:09:42 +00:00
afterAll(async () => await flushPromises());
2024-05-16 14:29:42 +00:00
2024-05-17 10:09:42 +00:00
describe('hasAny', () => {
it('should return false if no roles matched', async () => {
expect(acl.hasAny('Worker', 'updateAttributes', 'WRITE')).toBeFalsy();
});
2024-05-16 14:29:42 +00:00
2024-05-17 10:09:42 +00:00
it('should return false if no roles matched', async () => {
expect(acl.hasAny('Worker', 'holidays', 'READ')).toBeTruthy();
2024-05-16 14:29:42 +00:00
});
2024-05-17 10:09:42 +00:00
describe('*', () => {
it('should return true if an acl matched', async () => {
expect(acl.hasAny('Address', '*', 'WRITE')).toBeTruthy();
});
2024-05-16 14:29:42 +00:00
2024-05-17 10:09:42 +00:00
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();
});
2024-05-16 14:29:42 +00:00
});
});
});