fix conditions test

This commit is contained in:
Ben Irvin 2022-07-29 12:22:22 +02:00
parent 526d2e9025
commit 46ea867a27

View File

@ -5,18 +5,20 @@ const permissions = require('../');
// TODO: test abilityBuilderFactory // TODO: test abilityBuilderFactory
// TODO: test generateAbility with options // TODO: test generateAbility with options
describe('Permissions Engine', () => { describe('Permissions Engine', () => {
const allowedCondition = 'isAuthor';
const deniedCondition = 'isAdmin';
const providers = { const providers = {
action: { get: jest.fn() }, action: { get: jest.fn() },
condition: { condition: {
// TODO: mock these once I figure out how they actually work // TODO: mock these
get(condition) { get() {
console.log('get conditions', JSON.stringify(condition)); return {
}, async handler({ permission }) {
values(condition) { if (permission.conditions.includes(deniedCondition)) return false;
console.log('values', condition); if (permission.conditions.includes(allowedCondition)) return true;
}, return false;
register(condition) { },
console.log('register', condition); };
}, },
}, },
}; };
@ -47,7 +49,7 @@ describe('Permissions Engine', () => {
// //
}); });
it('register action', async () => { it('registers action (string)', async () => {
const { ability } = await buildEngineWithAbility({ const { ability } = await buildEngineWithAbility({
permissions: [{ action: 'read' }], permissions: [{ action: 'read' }],
}); });
@ -91,7 +93,7 @@ describe('Permissions Engine', () => {
expect(ability.can('read', 'article', 'name')).toBeFalsy(); expect(ability.can('read', 'article', 'name')).toBeFalsy();
}); });
describe.skip('conditions', () => { describe('conditions', () => {
it('does not register action when conditions not met', async () => { it('does not register action when conditions not met', async () => {
const { ability } = await buildEngineWithAbility({ const { ability } = await buildEngineWithAbility({
permissions: [ permissions: [
@ -99,7 +101,7 @@ describe('Permissions Engine', () => {
action: 'read', action: 'read',
subject: 'article', subject: 'article',
properties: { fields: ['title'] }, properties: { fields: ['title'] },
conditions: ['isAuthor'], conditions: [deniedCondition],
}, },
], ],
}); });
@ -119,7 +121,7 @@ describe('Permissions Engine', () => {
action: 'read', action: 'read',
subject: 'article', subject: 'article',
properties: { fields: ['title'] }, properties: { fields: ['title'] },
conditions: ['isAuthor'], conditions: [allowedCondition],
}, },
], ],
}); });
@ -263,19 +265,4 @@ describe('Permissions Engine', () => {
expect(beforeEvaluateFn).toBeCalledTimes(1); expect(beforeEvaluateFn).toBeCalledTimes(1);
expect(called).toEqual('beforeRegister'); expect(called).toEqual('beforeRegister');
}); });
it.skip('before-register.permission is called', async () => {
// const { ability } = await buildEngineWithAbility({
// permissions: [{ action: 'read', subject: 'article' }],
// engineHooks: [
// {
// name: 'before-evaluate.permission',
// fn(permissions) {
// console.log('permissions', permissions);
// return;
// },
// },
// ],
// });
});
}); });