mirror of
https://github.com/strapi/strapi.git
synced 2025-09-16 20:10:05 +00:00
cleanup
This commit is contained in:
parent
20fe90de93
commit
a8d6c215d2
@ -281,7 +281,7 @@ describe('Permissions Engine', () => {
|
|||||||
expect(registerFunctions[1]).toBeCalledWith(permissions[1]);
|
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 = [
|
const permissions = [
|
||||||
{
|
{
|
||||||
action: 'read',
|
action: 'read',
|
||||||
@ -309,7 +309,7 @@ describe('Permissions Engine', () => {
|
|||||||
expect(registerFunctions[0]).toBeCalledTimes(0);
|
expect(registerFunctions[0]).toBeCalledTimes(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('register action when conditions are met', async () => {
|
it('registers an action when conditions are met', async () => {
|
||||||
const permissions = [
|
const permissions = [
|
||||||
{
|
{
|
||||||
action: 'read',
|
action: 'read',
|
||||||
@ -336,7 +336,8 @@ describe('Permissions Engine', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
describe('hooks', () => {
|
describe('hooks', () => {
|
||||||
it('format.permission can modify permissions', async () => {
|
describe('format.permission', () => {
|
||||||
|
it('modifies permissions correctly', async () => {
|
||||||
const permissions = [{ action: 'read', subject: 'article' }];
|
const permissions = [{ action: 'read', subject: 'article' }];
|
||||||
const newPermissions = [{ action: 'view', subject: 'article' }];
|
const newPermissions = [{ action: 'view', subject: 'article' }];
|
||||||
const { ability, registerFunctions } = await buildEngineWithAbility({
|
const { ability, registerFunctions } = await buildEngineWithAbility({
|
||||||
@ -359,8 +360,67 @@ describe('Permissions Engine', () => {
|
|||||||
expect(ability.can('view', 'article')).toBeTruthy();
|
expect(ability.can('view', 'article')).toBeTruthy();
|
||||||
expect(registerFunctions[0]).toBeCalledWith(newPermissions[0]);
|
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 permissions = [{ action: 'update' }, { action: 'delete' }, { action: 'view' }];
|
||||||
const newPermissions = [{ action: 'modify' }, { action: 'remove' }];
|
const newPermissions = [{ action: 'modify' }, { action: 'remove' }];
|
||||||
|
|
||||||
@ -414,52 +474,11 @@ describe('Permissions Engine', () => {
|
|||||||
expect(ability.can('remove')).toBeTruthy();
|
expect(ability.can('remove')).toBeTruthy();
|
||||||
expect(ability.can('view')).toBeFalsy();
|
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 () => {
|
describe('before-* hooks', () => {
|
||||||
const permissions = [
|
it('execute in the correct order', async () => {
|
||||||
{ 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 () => {
|
|
||||||
let called = '';
|
let called = '';
|
||||||
const beforeEvaluateFn = jest.fn(() => {
|
const beforeEvaluateFn = jest.fn(() => {
|
||||||
called = 'beforeEvaluate';
|
called = 'beforeEvaluate';
|
||||||
@ -495,4 +514,5 @@ describe('Permissions Engine', () => {
|
|||||||
});
|
});
|
||||||
expect(called).toEqual('beforeRegister');
|
expect(called).toEqual('beforeRegister');
|
||||||
});
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user