99 lines
2.4 KiB
JavaScript
Raw Normal View History

2021-04-08 19:36:28 +02:00
'use strict';
2021-04-09 16:20:53 +02:00
const { getAllowedActionsForRole } = require('../action');
2021-04-08 19:36:28 +02:00
const { AUTHOR_CODE, PUBLISH_ACTION } = require('../constants');
const fixtures = [
{
actionId: 'test.action',
},
{
actionId: PUBLISH_ACTION,
},
{
actionId: 'plugins::test.action',
},
];
describe('Action', () => {
2021-04-09 16:20:53 +02:00
describe('getAllowedActionsForRole', () => {
2021-04-08 19:36:28 +02:00
test('returns every action if role is not provided', async () => {
global.strapi = {
admin: {
services: {
permission: {
actionProvider: {
values() {
return fixtures;
},
},
},
},
},
};
2021-04-09 16:20:53 +02:00
const actions = await getAllowedActionsForRole();
2021-04-08 19:36:28 +02:00
expect(actions.length).toBe(fixtures.length);
expect(actions).toEqual(expect.arrayContaining(fixtures));
});
test('returns every action if role is not the author role', async () => {
const findOneRoleMock = jest.fn(() => ({ code: 'custom-code ' }));
const roleId = 1;
global.strapi = {
admin: {
services: {
role: {
findOne: findOneRoleMock,
},
permission: {
actionProvider: {
values() {
return fixtures;
},
},
},
},
},
};
2021-04-09 16:20:53 +02:00
const actions = await getAllowedActionsForRole(roleId);
2021-04-08 19:36:28 +02:00
expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId });
expect(actions.length).toBe(fixtures.length);
expect(actions).toEqual(expect.arrayContaining(fixtures));
});
test('excludes publish action for author role', async () => {
const findOneRoleMock = jest.fn(() => ({ code: AUTHOR_CODE }));
const roleId = 1;
global.strapi = {
admin: {
services: {
role: {
findOne: findOneRoleMock,
},
permission: {
actionProvider: {
values() {
return fixtures;
},
},
},
},
},
};
2021-04-09 16:20:53 +02:00
const actions = await getAllowedActionsForRole(roleId);
2021-04-08 19:36:28 +02:00
expect(findOneRoleMock).toHaveBeenCalledWith({ id: roleId });
expect(actions.length).toBe(fixtures.length - 1);
expect(actions).toEqual(
expect.arrayContaining(fixtures.filter(f => f.actionId !== PUBLISH_ACTION))
);
});
});
});