2020-06-09 11:48:49 +02:00
|
|
|
'use strict';
|
|
|
|
|
2020-05-27 13:15:52 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2021-08-02 17:54:49 +02:00
|
|
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
|
|
|
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
2020-05-19 14:51:08 +02:00
|
|
|
|
2020-05-25 11:22:35 +02:00
|
|
|
const edition = process.env.STRAPI_DISABLE_EE === 'true' ? 'CE' : 'EE';
|
2020-05-22 12:58:14 +02:00
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
const data = {
|
2020-05-29 11:09:17 +02:00
|
|
|
rolesWithUsers: [],
|
|
|
|
rolesWithoutUsers: [],
|
|
|
|
users: [],
|
2020-05-29 12:02:29 +02:00
|
|
|
deleteRolesIds: [],
|
2020-06-15 11:54:44 +02:00
|
|
|
superAdminRole: undefined,
|
2020-06-15 19:11:36 +02:00
|
|
|
authorRole: undefined,
|
2020-11-17 15:38:41 +01:00
|
|
|
editorRole: undefined,
|
2020-05-19 14:51:08 +02:00
|
|
|
};
|
2020-05-22 12:58:14 +02:00
|
|
|
|
2021-09-22 10:49:43 +02:00
|
|
|
const omitTimestamps = obj => _.omit(obj, ['updatedAt', 'createdAt']);
|
2020-05-29 12:02:29 +02:00
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
describe('Role CRUD End to End', () => {
|
2020-11-10 14:15:31 +01:00
|
|
|
let rq;
|
|
|
|
let strapi;
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
beforeAll(async () => {
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-10 14:15:31 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2020-05-19 14:51:08 +02:00
|
|
|
|
2020-11-10 14:15:31 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2020-11-10 14:15:31 +01:00
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
describe('Default roles', () => {
|
|
|
|
test('Default roles are created', async () => {
|
|
|
|
const defaultsRoles = [
|
|
|
|
{
|
|
|
|
name: 'Super Admin',
|
|
|
|
code: 'strapi-super-admin',
|
|
|
|
description: 'Super Admins can access and manage all features and settings.',
|
|
|
|
usersCount: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Editor',
|
|
|
|
code: 'strapi-editor',
|
|
|
|
description: 'Editors can manage and publish contents including those of other users.',
|
|
|
|
usersCount: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'Author',
|
|
|
|
code: 'strapi-author',
|
2020-08-13 14:17:38 +02:00
|
|
|
description: 'Authors can manage the content they have created.',
|
2020-06-15 19:11:36 +02:00
|
|
|
usersCount: 0,
|
|
|
|
},
|
|
|
|
];
|
2020-06-15 11:54:44 +02:00
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toHaveLength(3);
|
|
|
|
expect(res.body.data).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining(defaultsRoles[0]),
|
|
|
|
expect.objectContaining(defaultsRoles[1]),
|
|
|
|
expect.objectContaining(defaultsRoles[2]),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
data.superAdminRole = res.body.data.find(r => r.code === 'strapi-super-admin');
|
|
|
|
data.authorRole = res.body.data.find(r => r.code === 'strapi-author');
|
|
|
|
data.editorRole = res.body.data.find(r => r.code === 'strapi-editor');
|
|
|
|
});
|
|
|
|
|
2020-06-18 15:34:09 +02:00
|
|
|
test('Author have admin::is-creator condition for every permission', async () => {
|
2020-06-15 19:11:36 +02:00
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.authorRole.id}/permissions`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.data)).toBe(true);
|
2020-07-08 13:53:56 +02:00
|
|
|
expect(res.body.data).toHaveLength(5);
|
2020-06-18 18:10:12 +02:00
|
|
|
res.body.data
|
2021-08-06 18:09:49 +02:00
|
|
|
.filter(p => !p.action.includes('plugin::upload'))
|
2020-06-18 18:10:12 +02:00
|
|
|
.forEach(permission => {
|
|
|
|
expect(permission.conditions).toEqual(['admin::is-creator']);
|
|
|
|
});
|
2020-06-15 19:11:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test("Editor's permissions don't have any conditions", async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.editorRole.id}/permissions`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(Array.isArray(res.body.data)).toBe(true);
|
2020-07-08 13:53:56 +02:00
|
|
|
expect(res.body.data).toHaveLength(5);
|
2020-06-18 18:10:12 +02:00
|
|
|
res.body.data
|
2021-08-06 18:09:49 +02:00
|
|
|
.filter(p => !p.action.includes('plugin::upload'))
|
2020-06-18 18:10:12 +02:00
|
|
|
.forEach(permission => {
|
|
|
|
expect(permission.conditions).toEqual([]);
|
|
|
|
});
|
2020-06-15 19:11:36 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (edition === 'EE') {
|
|
|
|
const newPermissions = [
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.update',
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.create',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
test('Conditions of editors and author can be modified', async () => {
|
|
|
|
let res = await rq({
|
|
|
|
url: `/admin/roles/${data.editorRole.id}/permissions`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: { permissions: newPermissions },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toHaveLength(2);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.update',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: {},
|
2020-06-15 19:11:36 +02:00
|
|
|
conditions: [],
|
2021-03-25 14:59:44 +01:00
|
|
|
subject: null,
|
2020-06-15 19:11:36 +02:00
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.create',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
});
|
|
|
|
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${data.authorRole.id}/permissions`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: { permissions: newPermissions },
|
2020-06-15 11:54:44 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-06-15 19:11:36 +02:00
|
|
|
expect(res.body.data).toHaveLength(2);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.update',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: {},
|
2020-06-15 19:11:36 +02:00
|
|
|
conditions: [],
|
2021-03-25 14:59:44 +01:00
|
|
|
subject: null,
|
2020-06-15 19:11:36 +02:00
|
|
|
}),
|
|
|
|
expect.objectContaining({
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.create',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
});
|
2020-06-15 11:54:44 +02:00
|
|
|
});
|
2020-06-15 19:11:36 +02:00
|
|
|
} else if (edition === 'CE') {
|
|
|
|
const newPermissions = [
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.update',
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.read',
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.create',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.update',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.delete',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.read',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 15:34:09 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-06-15 19:11:36 +02:00
|
|
|
},
|
|
|
|
];
|
|
|
|
|
|
|
|
test("Conditions of editors and author can't be modified", async () => {
|
|
|
|
let res = await rq({
|
|
|
|
url: `/admin/roles/${data.editorRole.id}/permissions`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: { permissions: newPermissions },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toHaveLength(6);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: expect.arrayContaining(
|
|
|
|
newPermissions
|
|
|
|
.slice(3, 6)
|
|
|
|
.map(p => ({ ...p, conditions: [] }))
|
2021-06-29 16:27:35 +02:00
|
|
|
.map(perm => expect.objectContaining(perm))
|
2020-06-15 19:11:36 +02:00
|
|
|
),
|
|
|
|
});
|
2020-06-15 11:54:44 +02:00
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${data.authorRole.id}/permissions`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: { permissions: newPermissions },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toHaveLength(6);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: expect.arrayContaining(
|
|
|
|
newPermissions
|
|
|
|
.slice(3, 6)
|
2020-06-18 15:34:09 +02:00
|
|
|
.map(p => ({ ...p, conditions: ['admin::is-creator'] }))
|
2020-06-15 19:11:36 +02:00
|
|
|
.map(expect.objectContaining)
|
|
|
|
),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if (edition === 'EE') {
|
2020-05-27 13:15:52 +02:00
|
|
|
describe('Create some roles', () => {
|
|
|
|
const rolesToCreate = [
|
|
|
|
[{ name: 'new role 0', description: 'description' }],
|
|
|
|
[{ name: 'new role 1', description: 'description' }],
|
|
|
|
[{ name: 'new role 2', description: 'description' }],
|
|
|
|
[{ name: 'new role 3', description: 'description' }],
|
|
|
|
[{ name: 'new role 4', description: 'description' }],
|
|
|
|
[{ name: 'new role 5', description: 'description' }],
|
|
|
|
];
|
2021-07-19 12:39:43 +02:00
|
|
|
|
2020-05-27 13:15:52 +02:00
|
|
|
test.each(rolesToCreate)('can create %p', async role => {
|
|
|
|
let res = await rq({
|
2020-05-22 12:58:14 +02:00
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'POST',
|
|
|
|
body: role,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(201);
|
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
id: expect.anything(),
|
|
|
|
name: role.name,
|
|
|
|
description: role.description,
|
|
|
|
});
|
2020-05-29 11:09:17 +02:00
|
|
|
data.rolesWithoutUsers.push(res.body.data);
|
2020-05-19 14:51:08 +02:00
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
test('Cannot create a role already existing', async () => {
|
2020-05-29 11:09:17 +02:00
|
|
|
const role = _.pick(data.rolesWithoutUsers[0], ['name', 'description']);
|
2020-05-22 12:58:14 +02:00
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'POST',
|
|
|
|
body: role,
|
|
|
|
});
|
2020-05-19 14:51:08 +02:00
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(res.body.data).toMatchObject({
|
2020-05-27 13:15:52 +02:00
|
|
|
name: [`The name must be unique and a role with name \`${role.name}\` already exists.`],
|
2020-05-22 12:58:14 +02:00
|
|
|
});
|
2020-05-19 14:51:08 +02:00
|
|
|
});
|
2020-05-29 11:09:17 +02:00
|
|
|
test('Can create a user with a role', async () => {
|
|
|
|
const user = {
|
|
|
|
email: 'new-user@strapi.io',
|
|
|
|
firstname: 'New',
|
|
|
|
lastname: 'User',
|
|
|
|
roles: [data.rolesWithoutUsers[5].id],
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/users',
|
|
|
|
method: 'POST',
|
|
|
|
body: user,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(201);
|
|
|
|
|
|
|
|
data.users.push(res.body.data);
|
|
|
|
data.rolesWithUsers.push(data.rolesWithoutUsers[5]);
|
|
|
|
data.rolesWithoutUsers.splice(5, 1);
|
|
|
|
});
|
2020-05-19 14:51:08 +02:00
|
|
|
});
|
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
describe('Find a role', () => {
|
|
|
|
test('Can find a role successfully', async () => {
|
|
|
|
const res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}`,
|
2020-05-22 12:58:14 +02:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-05-29 18:59:47 +02:00
|
|
|
expect(res.body.data).toMatchObject({
|
2020-05-28 13:02:06 +02:00
|
|
|
id: data.rolesWithoutUsers[0].id,
|
|
|
|
name: data.rolesWithoutUsers[0].name,
|
|
|
|
description: data.rolesWithoutUsers[0].description,
|
2020-05-29 18:59:47 +02:00
|
|
|
usersCount: 0,
|
2020-06-19 18:54:37 +02:00
|
|
|
code: expect.anything(),
|
2020-05-29 18:59:47 +02:00
|
|
|
});
|
2020-06-19 18:54:37 +02:00
|
|
|
expect(res.body.data.code.startsWith('new-role-0')).toBe(true);
|
2020-05-19 14:51:08 +02:00
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('Find all roles', () => {
|
|
|
|
test('Can find all roles successfully', async () => {
|
2020-05-29 18:59:47 +02:00
|
|
|
const expectedRolesWithoutUser = data.rolesWithoutUsers.map(r => ({ ...r, usersCount: 0 }));
|
|
|
|
const expectedRolesWithUser = data.rolesWithUsers.map(r => ({ ...r, usersCount: 1 }));
|
|
|
|
const expectedRoles = expectedRolesWithoutUser.concat(expectedRolesWithUser);
|
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'GET',
|
|
|
|
});
|
2020-05-19 14:51:08 +02:00
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
expect(res.statusCode).toBe(200);
|
2020-05-28 13:02:06 +02:00
|
|
|
expectedRoles.forEach(role => {
|
|
|
|
expect(res.body.data).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
id: role.id,
|
|
|
|
name: role.name,
|
|
|
|
description: role.description,
|
2020-06-01 10:15:32 +02:00
|
|
|
usersCount: role.usersCount,
|
2020-06-19 18:54:37 +02:00
|
|
|
code: expect.anything(),
|
2020-05-28 13:02:06 +02:00
|
|
|
}),
|
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
});
|
2020-05-18 16:29:32 +02:00
|
|
|
});
|
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
describe('Update a role', () => {
|
|
|
|
test('Can update name and description of a role successfully', async () => {
|
|
|
|
const updates = {
|
|
|
|
name: 'new name - Cannot update the name of a role',
|
|
|
|
description: 'new description - Can update a role successfully',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}`,
|
2020-05-22 12:58:14 +02:00
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-05-29 12:02:29 +02:00
|
|
|
expect(omitTimestamps(res.body.data)).toMatchObject({
|
|
|
|
...omitTimestamps(data.rolesWithoutUsers[0]),
|
2020-05-22 12:58:14 +02:00
|
|
|
...updates,
|
|
|
|
});
|
2020-05-29 11:09:17 +02:00
|
|
|
data.rolesWithoutUsers[0] = res.body.data;
|
2020-05-18 16:29:32 +02:00
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
test('Can update description of a role successfully', async () => {
|
|
|
|
const updates = {
|
|
|
|
name: 'new name - Cannot update the name of a role',
|
|
|
|
description: 'new description - Can update description of a role successfully',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}`,
|
2020-05-22 12:58:14 +02:00
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
2020-05-18 16:29:32 +02:00
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
expect(res.statusCode).toBe(200);
|
2020-05-29 12:02:29 +02:00
|
|
|
expect(omitTimestamps(res.body.data)).toMatchObject({
|
|
|
|
...omitTimestamps(data.rolesWithoutUsers[0]),
|
2020-05-22 12:58:14 +02:00
|
|
|
...updates,
|
|
|
|
});
|
2020-05-29 11:09:17 +02:00
|
|
|
data.rolesWithoutUsers[0] = res.body.data;
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
test('Cannot update the name of a role if already exists', async () => {
|
|
|
|
const updates = {
|
2020-05-29 11:09:17 +02:00
|
|
|
name: data.rolesWithoutUsers[0].name,
|
2020-05-22 12:58:14 +02:00
|
|
|
description: 'new description - Cannot update the name of a role if already exists',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[1].id}`,
|
2020-05-22 12:58:14 +02:00
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
name: [
|
2020-05-29 11:09:17 +02:00
|
|
|
`The name must be unique and a role with name \`${data.rolesWithoutUsers[0].name}\` already exists.`,
|
2020-05-22 12:58:14 +02:00
|
|
|
],
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
2020-07-20 11:39:26 +02:00
|
|
|
|
2020-10-08 19:50:39 +02:00
|
|
|
test('Cannot update super admin role', async () => {
|
2020-07-20 11:39:26 +02:00
|
|
|
const updates = {
|
|
|
|
name: 'new name - Cannot update the name of a role',
|
|
|
|
description: 'new description - Can update a role successfully',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.superAdminRole.id}`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
2020-07-20 11:39:26 +02:00
|
|
|
|
2020-05-27 13:15:52 +02:00
|
|
|
describe('Delete roles', () => {
|
2020-05-27 16:27:09 +02:00
|
|
|
describe('batch-delete', () => {
|
2020-05-29 11:09:17 +02:00
|
|
|
test("Don't delete the roles if some still have assigned users", async () => {
|
|
|
|
const roles = [data.rolesWithUsers[0], data.rolesWithUsers[0]];
|
|
|
|
const rolesIds = roles.map(r => r.id);
|
|
|
|
let res = await rq({
|
|
|
|
url: '/admin/roles/batch-delete',
|
|
|
|
method: 'POST',
|
|
|
|
body: { ids: rolesIds },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(res.body.data).toMatchObject({
|
2020-12-21 17:11:48 +01:00
|
|
|
ids: ['Some roles are still assigned to some users'],
|
2020-05-29 11:09:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
for (let role of roles) {
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${role.id}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject(role);
|
|
|
|
}
|
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-27 16:27:09 +02:00
|
|
|
test('Can delete a role', async () => {
|
|
|
|
let res = await rq({
|
|
|
|
url: '/admin/roles/batch-delete',
|
|
|
|
method: 'POST',
|
2020-05-29 11:09:17 +02:00
|
|
|
body: { ids: [data.rolesWithoutUsers[0].id] },
|
2020-05-27 16:27:09 +02:00
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-05-29 11:09:17 +02:00
|
|
|
expect(res.body.data).toMatchObject([data.rolesWithoutUsers[0]]);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
2020-05-27 16:27:09 +02:00
|
|
|
res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}`,
|
2020-05-27 16:27:09 +02:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
|
|
|
2020-05-29 12:02:29 +02:00
|
|
|
data.deleteRolesIds.push(data.rolesWithoutUsers[0].id);
|
2020-05-29 11:09:17 +02:00
|
|
|
data.rolesWithoutUsers.shift();
|
2020-05-27 13:15:52 +02:00
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-27 16:27:09 +02:00
|
|
|
test('Can delete two roles', async () => {
|
2020-05-29 11:09:17 +02:00
|
|
|
const roles = data.rolesWithoutUsers.slice(0, 2);
|
2020-05-27 16:27:09 +02:00
|
|
|
const rolesIds = roles.map(r => r.id);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
2020-05-27 16:27:09 +02:00
|
|
|
let res = await rq({
|
|
|
|
url: '/admin/roles/batch-delete',
|
|
|
|
method: 'POST',
|
|
|
|
body: { ids: rolesIds },
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject(roles);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
2020-05-27 16:27:09 +02:00
|
|
|
for (let roleId of rolesIds) {
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${roleId}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(404);
|
2020-05-29 12:02:29 +02:00
|
|
|
data.deleteRolesIds.push(data.rolesWithoutUsers[0].id);
|
2020-05-29 11:09:17 +02:00
|
|
|
data.rolesWithoutUsers.shift();
|
2020-05-27 16:27:09 +02:00
|
|
|
}
|
2020-05-27 13:15:52 +02:00
|
|
|
});
|
2020-05-27 16:27:09 +02:00
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-27 16:27:09 +02:00
|
|
|
describe('simple delete', () => {
|
|
|
|
test('Can delete a role', async () => {
|
|
|
|
let res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}`,
|
2020-05-27 16:27:09 +02:00
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-05-29 11:09:17 +02:00
|
|
|
expect(res.body.data).toMatchObject(data.rolesWithoutUsers[0]);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
|
|
|
res = await rq({
|
2020-05-29 11:09:17 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}`,
|
2020-05-27 13:15:52 +02:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(404);
|
2020-05-27 16:27:09 +02:00
|
|
|
|
2020-05-29 12:02:29 +02:00
|
|
|
data.deleteRolesIds.push(data.rolesWithoutUsers[0].id);
|
2020-05-29 11:09:17 +02:00
|
|
|
data.rolesWithoutUsers.shift();
|
2020-05-27 13:15:52 +02:00
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-29 11:09:17 +02:00
|
|
|
test("Don't delete a role if it still has assigned users", async () => {
|
|
|
|
let res = await rq({
|
|
|
|
url: `/admin/roles/${data.rolesWithUsers[0].id}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(res.body.data).toMatchObject({
|
2020-12-21 17:11:48 +01:00
|
|
|
id: ['Some roles are still assigned to some users'],
|
2020-05-29 11:09:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${data.rolesWithUsers[0].id}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject(data.rolesWithUsers[0]);
|
2020-05-27 16:27:09 +02:00
|
|
|
});
|
2020-06-15 11:54:44 +02:00
|
|
|
|
|
|
|
test("Can't delete super admin role", async () => {
|
|
|
|
let res = await rq({
|
|
|
|
url: `/admin/roles/${data.superAdminRole.id}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
id: ['You cannot delete the super admin role'],
|
|
|
|
});
|
|
|
|
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${data.rolesWithUsers[0].id}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject(data.rolesWithUsers[0]);
|
|
|
|
});
|
2020-05-27 13:15:52 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-29 12:02:29 +02:00
|
|
|
describe("Roles don't exist", () => {
|
|
|
|
test("Cannot update a role if it doesn't exist", async () => {
|
|
|
|
const updates = {
|
|
|
|
name: "new name - Cannot update a role if it doesn't exist",
|
|
|
|
description: "new description - Cannot update a role if it doesn't exist",
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.deleteRolesIds[0]}`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
statusCode: 404,
|
|
|
|
error: 'Not Found',
|
2020-07-20 11:39:26 +02:00
|
|
|
message: 'role.notFound',
|
2020-05-29 12:02:29 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-05-29 12:02:29 +02:00
|
|
|
test("Simple delete - No error if deleting a role that doesn't exist", async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.deleteRolesIds[0]}`,
|
|
|
|
method: 'DELETE',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toEqual(null);
|
|
|
|
});
|
2020-06-15 11:54:44 +02:00
|
|
|
test("Batch Delete - No error if deleting a role that doesn't exist", async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/roles/batch-delete',
|
|
|
|
method: 'POST',
|
|
|
|
body: { ids: [data.deleteRolesIds[0]] },
|
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
2020-06-15 11:54:44 +02:00
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toEqual([]);
|
2020-05-29 12:02:29 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-28 13:02:06 +02:00
|
|
|
|
|
|
|
describe('get & update Permissions', () => {
|
|
|
|
test('get permissions on empty role', async () => {
|
|
|
|
const res = await rq({
|
2020-06-01 10:15:32 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}/permissions`,
|
2020-05-28 13:02:06 +02:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: [],
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
test('assign permissions on role', async () => {
|
|
|
|
const res = await rq({
|
2020-06-01 10:15:32 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}/permissions`,
|
2020-05-28 13:02:06 +02:00
|
|
|
method: 'PUT',
|
|
|
|
body: {
|
|
|
|
permissions: [
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.update',
|
2020-05-28 13:02:06 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.create',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-06-18 11:19:27 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
2020-05-28 13:02:06 +02:00
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data.length > 0).toBe(true);
|
|
|
|
res.body.data.forEach(permission => {
|
|
|
|
expect(permission).toMatchObject({
|
|
|
|
id: expect.anything(),
|
|
|
|
action: expect.any(String),
|
|
|
|
subject: expect.stringOrNull(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (permission.conditions.length > 0) {
|
|
|
|
expect(permission.conditions).toEqual(expect.arrayContaining([expect.any(String)]));
|
|
|
|
}
|
2020-06-18 11:40:50 +02:00
|
|
|
if (permission.fields && permission.fields.length > 0) {
|
2020-05-28 13:02:06 +02:00
|
|
|
expect(permission.fields).toEqual(expect.arrayContaining([expect.any(String)]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-07-20 17:40:01 +02:00
|
|
|
test('assign permissions on role with an unknown condition', async () => {
|
|
|
|
const permissions = [
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::users-permissions.roles.update',
|
2020-07-20 17:40:01 +02:00
|
|
|
},
|
|
|
|
{
|
2021-08-06 18:09:49 +02:00
|
|
|
action: 'plugin::content-manager.explorer.create',
|
|
|
|
subject: 'plugin::users-permissions.user',
|
2021-03-25 14:59:44 +01:00
|
|
|
properties: { fields: ['username'], locales: [] },
|
2020-07-20 17:40:01 +02:00
|
|
|
conditions: ['admin::is-creator'],
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}/permissions`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: {
|
|
|
|
permissions: [
|
|
|
|
permissions[0],
|
|
|
|
{
|
|
|
|
...permissions[1],
|
|
|
|
conditions: [...permissions[1].conditions, 'unknown-condition'],
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2021-03-29 14:44:10 +02:00
|
|
|
expect(res.body.data).toHaveLength(1);
|
|
|
|
expect(res.body.data[0]).toMatchObject(permissions[1]);
|
2020-07-20 17:40:01 +02:00
|
|
|
});
|
|
|
|
|
2020-06-04 18:30:26 +02:00
|
|
|
test("can't assign non-existing permissions on role", async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}/permissions`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: {
|
|
|
|
permissions: [
|
|
|
|
{
|
|
|
|
action: 'non.existing.action',
|
|
|
|
},
|
|
|
|
],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
statusCode: 400,
|
|
|
|
error: 'Bad Request',
|
2020-06-09 17:45:53 +02:00
|
|
|
message: 'ValidationError',
|
2021-03-25 14:59:44 +01:00
|
|
|
data: { 'permissions[0].action': ['action is not an existing permission action'] },
|
2020-06-04 18:30:26 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-05-28 13:02:06 +02:00
|
|
|
test('get permissions role', async () => {
|
|
|
|
const res = await rq({
|
2020-06-01 10:15:32 +02:00
|
|
|
url: `/admin/roles/${data.rolesWithoutUsers[0].id}/permissions`,
|
2020-05-28 13:02:06 +02:00
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data.length > 0).toBe(true);
|
|
|
|
res.body.data.forEach(permission => {
|
|
|
|
expect(permission).toMatchObject({
|
|
|
|
id: expect.anything(),
|
|
|
|
action: expect.any(String),
|
|
|
|
subject: expect.stringOrNull(),
|
|
|
|
});
|
|
|
|
|
|
|
|
if (permission.conditions.length > 0) {
|
|
|
|
expect(permission.conditions).toEqual(expect.arrayContaining([expect.any(String)]));
|
|
|
|
}
|
2020-06-18 11:40:50 +02:00
|
|
|
if (permission.fields && permission.fields.length > 0) {
|
2020-05-28 13:02:06 +02:00
|
|
|
expect(permission.fields).toEqual(expect.arrayContaining([expect.any(String)]));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (edition === 'CE') {
|
|
|
|
describe('Cannot create a new role', () => {
|
|
|
|
test('Cannot create a role successfully', async () => {
|
|
|
|
const role = {
|
|
|
|
name: 'new role',
|
|
|
|
description: 'Description of new role',
|
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'POST',
|
|
|
|
body: role,
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
|
2020-06-15 19:11:36 +02:00
|
|
|
expect(res.statusCode).toBe(405);
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
}
|
2020-05-19 14:51:08 +02:00
|
|
|
});
|