2020-05-18 17:54:20 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const _ = require('lodash');
|
|
|
|
const roleService = require('../role');
|
|
|
|
|
|
|
|
describe('Role', () => {
|
|
|
|
describe('create', () => {
|
|
|
|
test('Creates a role', async () => {
|
|
|
|
const dbCreate = jest.fn(role => Promise.resolve(role));
|
2020-05-25 11:22:35 +02:00
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
2020-05-18 17:54:20 +02:00
|
|
|
|
|
|
|
global.strapi = {
|
2020-05-25 11:22:35 +02:00
|
|
|
query: () => ({ create: dbCreate, count: dbCount }),
|
2020-05-18 17:54:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const input = { name: 'super_admin', description: "Have all permissions. Can't be delete" };
|
|
|
|
|
|
|
|
const createdRole = await roleService.create(input);
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
expect(dbCreate).toHaveBeenCalledWith(input);
|
2020-05-18 17:54:20 +02:00
|
|
|
expect(createdRole).toStrictEqual(input);
|
|
|
|
});
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
describe('findOne', () => {
|
2020-05-19 14:51:08 +02:00
|
|
|
test('Finds a role', async () => {
|
2020-05-18 17:54:20 +02:00
|
|
|
const role = {
|
|
|
|
id: 1,
|
|
|
|
name: 'super_admin',
|
|
|
|
description: "Have all permissions. Can't be delete",
|
|
|
|
};
|
2020-05-18 18:37:14 +02:00
|
|
|
const dbFindOne = jest.fn(({ id }) => Promise.resolve(_.find([role], { id })));
|
2020-05-18 17:54:20 +02:00
|
|
|
|
|
|
|
global.strapi = {
|
|
|
|
query: () => ({ findOne: dbFindOne }),
|
|
|
|
};
|
|
|
|
|
2020-05-19 14:51:08 +02:00
|
|
|
const foundRole = await roleService.findOne({ id: role.id });
|
2020-05-18 17:54:20 +02:00
|
|
|
|
2020-05-29 17:23:42 +02:00
|
|
|
expect(dbFindOne).toHaveBeenCalledWith({ id: role.id }, []);
|
2020-05-19 14:51:08 +02:00
|
|
|
expect(foundRole).toStrictEqual(role);
|
2020-05-18 17:54:20 +02:00
|
|
|
});
|
2020-05-29 18:59:47 +02:00
|
|
|
test('Finds a role with usersCount', async () => {
|
|
|
|
const role = {
|
|
|
|
id: 1,
|
|
|
|
name: 'super_admin',
|
|
|
|
description: "Have all permissions. Can't be delete",
|
|
|
|
usersCount: 0,
|
|
|
|
};
|
|
|
|
const dbFindOne = jest.fn(({ id }) =>
|
|
|
|
Promise.resolve(_.find([_.omit(role, ['usersCount'])], { id }))
|
|
|
|
);
|
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
|
|
|
global.strapi = {
|
|
|
|
query: () => ({ findOne: dbFindOne, count: dbCount }),
|
|
|
|
};
|
|
|
|
|
2020-06-01 09:56:53 +02:00
|
|
|
const foundRole = await roleService.findOneWithUsersCount({ id: role.id });
|
2020-05-29 18:59:47 +02:00
|
|
|
|
|
|
|
expect(dbFindOne).toHaveBeenCalledWith({ id: role.id }, []);
|
|
|
|
expect(dbCount).toHaveBeenCalledWith({ 'roles.id': role.id });
|
|
|
|
expect(foundRole).toStrictEqual(role);
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
|
|
|
describe('find', () => {
|
2020-05-19 15:42:20 +02:00
|
|
|
test('Finds roles', async () => {
|
|
|
|
const roles = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'super_admin',
|
|
|
|
description: "Have all permissions. Can't be delete",
|
|
|
|
},
|
|
|
|
];
|
|
|
|
const dbFind = jest.fn(() => Promise.resolve(roles));
|
|
|
|
|
|
|
|
global.strapi = {
|
|
|
|
query: () => ({ find: dbFind }),
|
|
|
|
};
|
|
|
|
|
|
|
|
const foundRoles = await roleService.find();
|
|
|
|
|
2020-05-29 17:23:42 +02:00
|
|
|
expect(dbFind).toHaveBeenCalledWith({}, []);
|
2020-05-19 15:42:20 +02:00
|
|
|
expect(foundRoles).toStrictEqual(roles);
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
|
|
|
describe('findAll', () => {
|
2020-05-18 16:29:32 +02:00
|
|
|
test('Finds all roles', async () => {
|
|
|
|
const roles = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'super_admin',
|
|
|
|
description: "Have all permissions. Can't be delete",
|
2020-05-29 18:19:12 +02:00
|
|
|
usersCount: 0,
|
2020-05-18 16:29:32 +02:00
|
|
|
},
|
|
|
|
];
|
2020-05-29 18:19:12 +02:00
|
|
|
const dbFind = jest.fn(() =>
|
|
|
|
Promise.resolve(roles.map(role => _.omit(role, ['usersCount'])))
|
|
|
|
);
|
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
2020-05-18 16:29:32 +02:00
|
|
|
|
|
|
|
global.strapi = {
|
2020-05-29 18:19:12 +02:00
|
|
|
query: () => ({ find: dbFind, count: dbCount }),
|
2020-05-18 16:29:32 +02:00
|
|
|
};
|
|
|
|
|
2020-05-29 18:19:12 +02:00
|
|
|
const foundRoles = await roleService.findAllWithUsersCount();
|
2020-05-18 16:29:32 +02:00
|
|
|
|
2020-05-29 17:23:42 +02:00
|
|
|
expect(dbFind).toHaveBeenCalledWith({ _limit: -1 }, []);
|
2020-05-18 16:29:32 +02:00
|
|
|
expect(foundRoles).toStrictEqual(roles);
|
|
|
|
});
|
2020-05-19 16:11:19 +02:00
|
|
|
});
|
|
|
|
describe('update', () => {
|
2020-05-22 12:58:14 +02:00
|
|
|
test('Updates a role', async () => {
|
2020-05-19 16:11:19 +02:00
|
|
|
const role = {
|
|
|
|
id: 1,
|
|
|
|
name: 'super_admin',
|
|
|
|
description: 'AAA',
|
|
|
|
};
|
|
|
|
const expectedUpdatedRole = {
|
|
|
|
id: 1,
|
|
|
|
name: 'super_admin_updated',
|
|
|
|
description: 'AAA_updated',
|
|
|
|
};
|
|
|
|
const dbUpdate = jest.fn(() => Promise.resolve(expectedUpdatedRole));
|
2020-05-25 11:22:35 +02:00
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
2020-05-18 18:33:56 +02:00
|
|
|
|
|
|
|
global.strapi = {
|
2020-05-25 11:22:35 +02:00
|
|
|
query: () => ({ update: dbUpdate, count: dbCount }),
|
2020-05-18 18:33:56 +02:00
|
|
|
};
|
|
|
|
|
2020-05-19 16:11:19 +02:00
|
|
|
const updatedRole = await roleService.update(
|
|
|
|
{
|
|
|
|
id: role.id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: expectedUpdatedRole.name,
|
|
|
|
description: expectedUpdatedRole.description,
|
|
|
|
}
|
|
|
|
);
|
2020-05-18 18:33:56 +02:00
|
|
|
|
2020-05-19 16:11:19 +02:00
|
|
|
expect(dbUpdate).toHaveBeenCalledWith(
|
|
|
|
{
|
|
|
|
id: role.id,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: expectedUpdatedRole.name,
|
|
|
|
description: expectedUpdatedRole.description,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
expect(updatedRole).toStrictEqual(expectedUpdatedRole);
|
2020-05-18 18:33:56 +02:00
|
|
|
});
|
2020-05-29 18:19:12 +02:00
|
|
|
});
|
|
|
|
describe('count', () => {
|
2020-05-29 18:59:47 +02:00
|
|
|
test('getUsersCount', async () => {
|
|
|
|
const roleId = 1;
|
2020-05-29 18:19:12 +02:00
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
|
|
|
global.strapi = {
|
|
|
|
query: () => ({ count: dbCount }),
|
|
|
|
};
|
|
|
|
|
2020-05-29 18:59:47 +02:00
|
|
|
const usersCount = await roleService.getUsersCount(roleId);
|
2020-05-29 18:19:12 +02:00
|
|
|
|
2020-05-29 18:59:47 +02:00
|
|
|
expect(dbCount).toHaveBeenCalledWith({ 'roles.id': roleId });
|
|
|
|
expect(usersCount).toEqual(0);
|
2020-05-29 18:19:12 +02:00
|
|
|
});
|
2020-05-18 17:54:20 +02:00
|
|
|
});
|
2020-05-27 13:15:52 +02:00
|
|
|
describe('delete', () => {
|
|
|
|
test('Delete a role', async () => {
|
|
|
|
const role = {
|
2020-05-28 17:32:44 +02:00
|
|
|
id: 3,
|
2020-05-27 13:15:52 +02:00
|
|
|
name: 'admin',
|
|
|
|
description: 'Description',
|
|
|
|
users: [],
|
|
|
|
};
|
2020-05-29 18:59:47 +02:00
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
2020-05-27 13:15:52 +02:00
|
|
|
const dbDelete = jest.fn(() => Promise.resolve(role));
|
2020-05-29 11:09:17 +02:00
|
|
|
const dbDeleteByRolesIds = jest.fn(() => Promise.resolve());
|
2020-05-27 13:15:52 +02:00
|
|
|
|
|
|
|
global.strapi = {
|
2020-05-29 18:59:47 +02:00
|
|
|
query: () => ({ delete: dbDelete, count: dbCount }),
|
2020-05-29 17:23:42 +02:00
|
|
|
admin: {
|
|
|
|
services: {
|
|
|
|
permission: { deleteByRolesIds: dbDeleteByRolesIds },
|
|
|
|
},
|
|
|
|
},
|
2020-05-27 13:15:52 +02:00
|
|
|
};
|
|
|
|
|
2020-05-28 17:32:44 +02:00
|
|
|
const deletedRoles = await roleService.deleteByIds([role.id]);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
2020-05-29 18:59:47 +02:00
|
|
|
expect(dbCount).toHaveBeenCalledWith({ 'roles.id': role.id });
|
2020-05-27 13:15:52 +02:00
|
|
|
expect(dbDelete).toHaveBeenCalledWith({ id_in: [role.id] });
|
|
|
|
expect(deletedRoles).toStrictEqual([role]);
|
|
|
|
});
|
|
|
|
test('Delete two roles', async () => {
|
|
|
|
const roles = [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
name: 'admin 1',
|
|
|
|
description: 'Description',
|
|
|
|
users: [],
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 2,
|
|
|
|
name: 'admin 2',
|
|
|
|
description: 'Description',
|
|
|
|
users: [],
|
|
|
|
},
|
|
|
|
];
|
2020-05-29 18:59:47 +02:00
|
|
|
const dbCount = jest.fn(() => Promise.resolve(0));
|
2020-05-27 13:15:52 +02:00
|
|
|
const rolesIds = roles.map(r => r.id);
|
|
|
|
const dbDelete = jest.fn(() => Promise.resolve(roles));
|
2020-05-29 18:59:47 +02:00
|
|
|
const dbGetUsersCount = jest.fn(() => Promise.resolve(0));
|
2020-05-29 11:09:17 +02:00
|
|
|
const dbDeleteByRolesIds = jest.fn(() => Promise.resolve());
|
2020-05-27 13:15:52 +02:00
|
|
|
|
|
|
|
global.strapi = {
|
2020-05-29 18:59:47 +02:00
|
|
|
query: () => ({ delete: dbDelete, count: dbCount }),
|
2020-05-29 17:23:42 +02:00
|
|
|
admin: {
|
|
|
|
services: {
|
|
|
|
permission: { deleteByRolesIds: dbDeleteByRolesIds },
|
2020-05-29 18:59:47 +02:00
|
|
|
role: { getUsersCount: dbGetUsersCount },
|
2020-05-29 17:23:42 +02:00
|
|
|
},
|
|
|
|
},
|
2020-05-27 13:15:52 +02:00
|
|
|
};
|
|
|
|
|
2020-05-28 17:32:44 +02:00
|
|
|
const deletedRoles = await roleService.deleteByIds(rolesIds);
|
2020-05-27 13:15:52 +02:00
|
|
|
|
2020-05-29 18:59:47 +02:00
|
|
|
expect(dbCount).toHaveBeenNthCalledWith(1, { 'roles.id': rolesIds[0] });
|
|
|
|
expect(dbCount).toHaveBeenNthCalledWith(2, { 'roles.id': rolesIds[1] });
|
|
|
|
expect(dbCount).toHaveBeenCalledTimes(2);
|
2020-05-27 13:15:52 +02:00
|
|
|
expect(dbDelete).toHaveBeenCalledWith({ id_in: rolesIds });
|
|
|
|
expect(deletedRoles).toStrictEqual(roles);
|
|
|
|
});
|
|
|
|
});
|
2020-05-18 17:54:20 +02:00
|
|
|
});
|