This commit is contained in:
Ben Irvin 2022-08-01 13:40:43 +02:00
parent 20fe90de93
commit a8d6c215d2

View File

@ -281,7 +281,7 @@ describe('Permissions Engine', () => {
expect(registerFunctions[1]).toBeCalledWith(permissions[1]);
});
it('does not register action when conditions not met', async () => {
it(`doesn't register action when conditions not met`, async () => {
const permissions = [
{
action: 'read',
@ -309,7 +309,7 @@ describe('Permissions Engine', () => {
expect(registerFunctions[0]).toBeCalledTimes(0);
});
it('register action when conditions are met', async () => {
it('registers an action when conditions are met', async () => {
const permissions = [
{
action: 'read',
@ -336,7 +336,8 @@ describe('Permissions Engine', () => {
});
describe('hooks', () => {
it('format.permission can modify permissions', async () => {
describe('format.permission', () => {
it('modifies permissions correctly', async () => {
const permissions = [{ action: 'read', subject: 'article' }];
const newPermissions = [{ action: 'view', subject: 'article' }];
const { ability, registerFunctions } = await buildEngineWithAbility({
@ -359,8 +360,67 @@ describe('Permissions Engine', () => {
expect(ability.can('view', 'article')).toBeTruthy();
expect(registerFunctions[0]).toBeCalledWith(newPermissions[0]);
});
});
it('validate hooks are called in the right order', async () => {
describe('before-format::validate.permission', () => {
it('before-format::validate.permission can prevent action register', async () => {
const permissions = [{ action: 'read', subject: 'article' }];
const newPermissions = [];
const { ability, registerFunctions, createRegisterFunction } = await buildEngineWithAbility(
{
permissions,
engineHooks: [
{
name: 'before-format::validate.permission',
fn: generateInvalidateActionHook('read'),
},
],
}
);
expect(ability.rules).toMatchObject(expectedAbilityRules(newPermissions));
expect(ability.can('read', 'article')).toBeFalsy();
expect(ability.can('read', 'user')).toBeFalsy();
expect(createRegisterFunction).toBeCalledTimes(1);
expect(registerFunctions[0]).toBeCalledTimes(0);
});
});
describe('post-format::validate.permission', () => {
it('can prevent action register', async () => {
const permissions = [
{ action: 'read', subject: 'article' },
{ action: 'read', subject: 'user' },
{ action: 'write', subject: 'article' },
];
const newPermissions = [{ action: 'write', subject: 'article' }];
const { ability, registerFunctions, createRegisterFunction } = await buildEngineWithAbility(
{
permissions,
engineHooks: [
{
name: 'post-format::validate.permission',
fn: generateInvalidateActionHook('read'),
},
],
}
);
expect(ability.rules).toMatchObject(expectedAbilityRules(newPermissions));
expect(ability.can('read', 'article')).toBeFalsy();
expect(ability.can('read', 'user')).toBeFalsy();
expect(ability.can('write', 'article')).toBeTruthy();
expect(createRegisterFunction).toBeCalledTimes(3);
expect(registerFunctions[0]).toBeCalledTimes(0);
expect(registerFunctions[1]).toBeCalledTimes(0);
expect(registerFunctions[2]).toBeCalledTimes(1);
});
});
describe('*validate* hooks', () => {
it('execute in the correct order', async () => {
const permissions = [{ action: 'update' }, { action: 'delete' }, { action: 'view' }];
const newPermissions = [{ action: 'modify' }, { action: 'remove' }];
@ -414,52 +474,11 @@ describe('Permissions Engine', () => {
expect(ability.can('remove')).toBeTruthy();
expect(ability.can('view')).toBeFalsy();
});
it('before-format::validate.permission can prevent action register', async () => {
const permissions = [{ action: 'read', subject: 'article' }];
const newPermissions = [];
const { ability, registerFunctions, createRegisterFunction } = await buildEngineWithAbility({
permissions,
engineHooks: [
{ name: 'before-format::validate.permission', fn: generateInvalidateActionHook('read') },
],
});
expect(ability.rules).toMatchObject(expectedAbilityRules(newPermissions));
expect(ability.can('read', 'article')).toBeFalsy();
expect(ability.can('read', 'user')).toBeFalsy();
expect(createRegisterFunction).toBeCalledTimes(1);
expect(registerFunctions[0]).toBeCalledTimes(0);
});
});
it('post-format::validate.permission can prevent action register', async () => {
const permissions = [
{ action: 'read', subject: 'article' },
{ action: 'read', subject: 'user' },
{ action: 'write', subject: 'article' },
];
const newPermissions = [{ action: 'write', subject: 'article' }];
const { ability, registerFunctions, createRegisterFunction } = await buildEngineWithAbility({
permissions,
engineHooks: [
{ name: 'post-format::validate.permission', fn: generateInvalidateActionHook('read') },
],
});
expect(ability.rules).toMatchObject(expectedAbilityRules(newPermissions));
expect(ability.can('read', 'article')).toBeFalsy();
expect(ability.can('read', 'user')).toBeFalsy();
expect(ability.can('write', 'article')).toBeTruthy();
expect(createRegisterFunction).toBeCalledTimes(3);
expect(registerFunctions[0]).toBeCalledTimes(0);
expect(registerFunctions[1]).toBeCalledTimes(0);
expect(registerFunctions[2]).toBeCalledTimes(1);
});
it('before-evaluate and before-register are called in the right order', async () => {
describe('before-* hooks', () => {
it('execute in the correct order', async () => {
let called = '';
const beforeEvaluateFn = jest.fn(() => {
called = 'beforeEvaluate';
@ -496,3 +515,4 @@ describe('Permissions Engine', () => {
expect(called).toEqual('beforeRegister');
});
});
});