2020-05-27 13:15:52 +02:00
|
|
|
const _ = require('lodash');
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
const { registerAndLogin } = require('../../../test/helpers/auth');
|
|
|
|
const { createAuthRequest } = require('../../../test/helpers/request');
|
|
|
|
|
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
|
|
|
let rq;
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
roles: [],
|
|
|
|
};
|
2020-05-22 12:58:14 +02:00
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
describe('Role CRUD End to End', () => {
|
|
|
|
beforeAll(async () => {
|
|
|
|
const token = await registerAndLogin();
|
|
|
|
rq = createAuthRequest(token);
|
|
|
|
}, 60000);
|
|
|
|
|
2020-05-22 12:58:14 +02:00
|
|
|
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' }],
|
|
|
|
];
|
|
|
|
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,
|
|
|
|
created_at: expect.anything(),
|
|
|
|
updated_at: expect.anything(),
|
|
|
|
});
|
|
|
|
data.roles.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-27 13:15:52 +02:00
|
|
|
const role = _.pick(data.roles[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-22 12:58:14 +02:00
|
|
|
describe('Find a role', () => {
|
|
|
|
test('Can find a role successfully', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.roles[0].id}`,
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject(data.roles[0]);
|
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 () => {
|
|
|
|
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);
|
|
|
|
expect(res.body.data).toMatchObject(data.roles);
|
|
|
|
});
|
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({
|
|
|
|
url: `/admin/roles/${data.roles[0].id}`,
|
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
...data.roles[0],
|
|
|
|
...updates,
|
|
|
|
updated_at: expect.anything(),
|
|
|
|
});
|
|
|
|
data.roles[0] = res.body.data;
|
2020-05-18 16:29:32 +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({
|
|
|
|
url: `/admin/roles/${data.roles[0].id}`,
|
|
|
|
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);
|
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
...data.roles[0],
|
|
|
|
...updates,
|
|
|
|
updated_at: expect.anything(),
|
|
|
|
});
|
|
|
|
data.roles[0] = res.body.data;
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
test('Cannot update the name of a role if already exists', async () => {
|
|
|
|
const updates = {
|
|
|
|
name: data.roles[0].name,
|
|
|
|
description: 'new description - Cannot update the name of a role if already exists',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
url: `/admin/roles/${data.roles[1].id}`,
|
|
|
|
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: [
|
|
|
|
`The name must be unique and a role with name \`${data.roles[0].name}\` already exists.`,
|
|
|
|
],
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
2020-05-25 11:22:35 +02:00
|
|
|
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/1000', // id that doesn't exist
|
|
|
|
method: 'PUT',
|
|
|
|
body: updates,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
statusCode: 404,
|
|
|
|
error: 'Not Found',
|
|
|
|
message: 'entry.notFound',
|
|
|
|
});
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
2020-05-27 13:15:52 +02:00
|
|
|
describe('Delete roles', () => {
|
|
|
|
test('Can delete a role', async () => {
|
|
|
|
let res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'DELETE',
|
|
|
|
body: { ids: [data.roles[0].id] },
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject([data.roles[0]]);
|
|
|
|
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${data.roles[0].id}`,
|
|
|
|
method: 'GET',
|
|
|
|
body: { ids: data.roles[0].id },
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
|
|
|
|
|
|
data.roles.shift();
|
|
|
|
});
|
|
|
|
test('Can delete two roles', async () => {
|
|
|
|
const roles = data.roles.slice(0, 2);
|
|
|
|
const rolesIds = roles.map(r => r.id);
|
|
|
|
|
|
|
|
let res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'DELETE',
|
|
|
|
body: { ids: rolesIds },
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toMatchObject(roles);
|
|
|
|
|
|
|
|
for (let roleId of rolesIds) {
|
|
|
|
res = await rq({
|
|
|
|
url: `/admin/roles/${roleId}`,
|
|
|
|
method: 'GET',
|
|
|
|
body: { ids: data.roles[0].id },
|
|
|
|
});
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
|
|
data.roles.shift();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
test("No error if deleting a role that doesn't exist", async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: '/admin/roles',
|
|
|
|
method: 'DELETE',
|
|
|
|
body: { ids: ['id-that-doesnt-exist'] },
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data).toEqual([]);
|
|
|
|
});
|
|
|
|
});
|
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-05-22 12:58:14 +02:00
|
|
|
expect(res.statusCode).toBe(404);
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
|
|
|
});
|
2020-05-22 12:58:14 +02:00
|
|
|
}
|
2020-05-19 14:51:08 +02:00
|
|
|
});
|