Skip to content

Commit

Permalink
refacto
Browse files Browse the repository at this point in the history
Signed-off-by: Pierre No毛l <petersg83@gmail.com>
  • Loading branch information
petersg83 committed Oct 19, 2020
1 parent 1bc8ae4 commit 5f9b6c8
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 269 deletions.
2 changes: 1 addition & 1 deletion packages/strapi-admin/config/functions/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module.exports = async () => {
await strapi.admin.services.permission.ensureBoundPermissionsInDatabase();
await strapi.admin.services.user.migrateUsers();
await strapi.admin.services.role.createRolesIfNoneExist();
await strapi.admin.services.permission.resetSuperAdminPermissions();
await strapi.admin.services.role.resetSuperAdminPermissions();
await strapi.admin.services.role.displayWarningIfNoSuperAdmin();
await strapi.admin.services.user.displayWarningIfUsersDontHaveRole();
};
5 changes: 4 additions & 1 deletion packages/strapi-admin/controllers/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,10 @@ module.exports = {
permissionsToAssign = input.permissions;
}

const permissions = await strapi.admin.services.permission.assign(role.id, permissionsToAssign);
const permissions = await strapi.admin.services.role.assignPermissions(
role.id,
permissionsToAssign
);

ctx.body = {
data: permissions,
Expand Down
5 changes: 4 additions & 1 deletion packages/strapi-admin/ee/controllers/role.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,10 @@ module.exports = {
return ctx.notFound('role.notFound');
}

const permissions = await strapi.admin.services.permission.assign(role.id, input.permissions);
const permissions = await strapi.admin.services.role.assignPermissions(
role.id,
input.permissions
);

ctx.body = {
data: permissions,
Expand Down
146 changes: 0 additions & 146 deletions packages/strapi-admin/services/__tests__/permission.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,86 +19,6 @@ describe('Permission Service', () => {
});
});

describe('Assign permissions', () => {
test('Delete previous permissions', async () => {
const createMany = jest.fn(() => Promise.resolve([]));
const getSuperAdmin = jest.fn(() => Promise.resolve({ id: 0 }));
const sendDidUpdateRolePermissions = jest.fn();
const find = jest.fn(() => Promise.resolve([{ id: 3 }]));
const deleteFn = jest.fn();
const getAll = jest.fn(() => []);

global.strapi = {
admin: {
services: {
metrics: { sendDidUpdateRolePermissions },
permission: { actionProvider: { getAll } },
role: { getSuperAdmin },
}
},
query() {
return { delete: deleteFn, createMany, find };
},
};

await permissionService.assign(1, []);

expect(deleteFn).toHaveBeenCalledWith({ id_in: [3] });
});

test('Create new permissions', async () => {
const permissions = Array(5)
.fill(0)
.map((v, i) => ({ action: `action-${i}` }));

const deleteFn = jest.fn(() => Promise.resolve([]));
const createMany = jest.fn(() => Promise.resolve([]));
const getSuperAdmin = jest.fn(() => Promise.resolve({ id: 0 }));
const sendDidUpdateRolePermissions = jest.fn();
const find = jest.fn(() => Promise.resolve([]));
const getAll = jest.fn(() => permissions.map(perm => ({ actionId: perm.action })));
const removeUnkownConditionIds = jest.fn(conds => _.intersection(conds, ['cond']));

global.strapi = {
admin: {
services: {
metrics: { sendDidUpdateRolePermissions },
role: { getSuperAdmin },
permission: {
actionProvider: { getAll },
conditionProvider: {
getAll: jest.fn(() => [{ id: 'admin::is-creator' }]),
},
},
condition: {
removeUnkownConditionIds,
},
},
},
query() {
return { delete: deleteFn, createMany, find };
},
};

const permissionsToAssign = [...permissions];
permissionsToAssign[4] = {
...permissions[4],
conditions: ['cond', 'unknown-cond'],
};

await permissionService.assign(1, permissionsToAssign);

expect(createMany).toHaveBeenCalledTimes(1);
expect(createMany).toHaveBeenCalledWith([
{ action: 'action-0', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-1', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-2', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-3', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-4', conditions: ['cond'], fields: null, role: 1, subject: null },
]);
});
});

describe('Find User Permissions', () => {
test('Find calls the right db query', async () => {
const find = jest.fn(({ role_in }) => role_in);
Expand Down Expand Up @@ -229,70 +149,4 @@ describe('Permission Service', () => {
permissionService.actionProvider.getAllByMap = prevGetAllByMap;
});
});

describe('resetSuperAdminPermissions', () => {
test('No superAdmin role exist', async () => {
const getSuperAdmin = jest.fn(() => Promise.resolve(undefined));
const createMany = jest.fn();

global.strapi = {
query: () => ({ createMany }),
admin: { services: { role: { getSuperAdmin } } },
};

await permissionService.resetSuperAdminPermissions();

expect(createMany).toHaveBeenCalledTimes(0);
});
test('Reset super admin permissions', async () => {
const actions = [
{
actionId: 'action-1',
subjects: ['country'],
section: 'contentTypes',
},
];
const permissions = [
{
action: 'action-1',
subject: 'country',
fields: ['name'],
conditions: [],
},
];
const getAll = jest.fn(() => actions);
const getAllConditions = jest.fn(() => []);
const find = jest.fn(() => [{ action: 'action-2', id: 2 }]);
const deleteFn = jest.fn(() => []);
const getPermissionsWithNestedFields = jest.fn(() => [...permissions]); // cloned, otherwise it is modified inside resetSuperAdminPermissions()
const getSuperAdmin = jest.fn(() => Promise.resolve({ id: 1 }));
const createMany = jest.fn(() => Promise.resolve([{ ...permissions[0], role: { id: 1 } }]));
const removeUnkownConditionIds = jest.fn(conds => conds);

global.strapi = {
query: () => ({ createMany, find, delete: deleteFn }),
admin: {
services: {
permission: {
actionProvider: { getAll },
conditionProvider: { getAll: getAllConditions },
},
condition: { removeUnkownConditionIds },
'content-type': { getPermissionsWithNestedFields },
role: { getSuperAdmin },
},
},
};

await permissionService.resetSuperAdminPermissions();

expect(deleteFn).toHaveBeenCalledWith({ id_in: [2] });
expect(createMany).toHaveBeenCalledWith([
{
...permissions[0],
role: 1,
},
]);
});
});
});
172 changes: 158 additions & 14 deletions packages/strapi-admin/services/__tests__/role.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,6 @@ describe('Role', () => {
});
});
});

describe('Count roles', () => {
test('Count roles without params', async () => {
const count = jest.fn(() => Promise.resolve(2));
Expand All @@ -302,7 +301,6 @@ describe('Role', () => {
expect(count).toHaveBeenCalledWith(params);
});
});

describe('createRolesIfNoneExist', () => {
test("Don't create roles if one already exist", async () => {
const count = jest.fn(() => Promise.resolve(1));
Expand Down Expand Up @@ -405,21 +403,26 @@ describe('Role', () => {
restrictedSubjects: ['plugins::users-permissions.user'],
});
expect(createMany).toHaveBeenCalledTimes(2);
expect(createMany).toHaveBeenNthCalledWith(1, 2, [
...permissions,
...defaultPermissions.map(d => ({
...d,
conditions: [],
})),
]);
expect(createMany).toHaveBeenNthCalledWith(
1,
[
...permissions,
...defaultPermissions.map(d => ({
...d,
conditions: [],
})),
].map(p => ({ ...p, role: 2 }))
);

expect(createMany).toHaveBeenNthCalledWith(2, 3, [
{ ...permissions[0], conditions: ['admin::is-creator'] },
...defaultPermissions,
]);
expect(createMany).toHaveBeenNthCalledWith(
2,
[
{ ...permissions[0], conditions: ['admin::is-creator'] },
...defaultPermissions,
].map(p => ({ ...p, role: 3 }))
);
});
});

describe('displayWarningIfNoSuperAdmin', () => {
test('superAdmin role exists & a user is superAdmin', async () => {
const findOne = jest.fn(() => ({ id: 1 }));
Expand Down Expand Up @@ -470,4 +473,145 @@ describe('Role', () => {
expect(warn).toHaveBeenCalledWith("Your application doesn't have a super admin user.");
});
});
describe('resetSuperAdminPermissions', () => {
test('No superAdmin role exist', async () => {
const getSuperAdmin = jest.fn(() => Promise.resolve(undefined));
const createMany = jest.fn();

global.strapi = {
query: () => ({ createMany }),
admin: { services: { role: { getSuperAdmin } } },
};

await roleService.resetSuperAdminPermissions();

expect(createMany).toHaveBeenCalledTimes(0);
});
test('Reset super admin permissions', async () => {
const actions = [
{
actionId: 'action-1',
subjects: ['country'],
section: 'contentTypes',
},
];
const permissions = [
{
action: 'action-1',
subject: 'country',
fields: ['name'],
conditions: [],
},
];
const getAll = jest.fn(() => actions);
const getAllConditions = jest.fn(() => []);
const find = jest.fn(() => [{ action: 'action-2', id: 2 }]);
const getPermissionsWithNestedFields = jest.fn(() => [...permissions]); // cloned, otherwise it is modified inside resetSuperAdminPermissions()
const deleteByIds = jest.fn();
const getSuperAdmin = jest.fn(() => Promise.resolve({ id: 1 }));
const createMany = jest.fn(() => Promise.resolve([{ ...permissions[0], role: { id: 1 } }]));
const removeUnkownConditionIds = jest.fn(conds => conds);

global.strapi = {
admin: {
services: {
permission: {
createMany,
find,
actionProvider: { getAll },
conditionProvider: { getAll: getAllConditions },
deleteByIds,
},
condition: { removeUnkownConditionIds },
'content-type': { getPermissionsWithNestedFields },
role: { getSuperAdmin },
},
},
};

await roleService.resetSuperAdminPermissions();

expect(deleteByIds).toHaveBeenCalledWith([2]);
expect(createMany).toHaveBeenCalledWith([
{
...permissions[0],
role: 1,
},
]);
});
});
describe('Assign permissions', () => {
test('Delete previous permissions', async () => {
const createMany = jest.fn(() => Promise.resolve([]));
const getSuperAdmin = jest.fn(() => Promise.resolve({ id: 0 }));
const sendDidUpdateRolePermissions = jest.fn();
const find = jest.fn(() => Promise.resolve([{ id: 3 }]));
const deleteByIds = jest.fn();
const getAll = jest.fn(() => []);

global.strapi = {
admin: {
services: {
metrics: { sendDidUpdateRolePermissions },
permission: { find, createMany, actionProvider: { getAll }, deleteByIds },
role: { getSuperAdmin },
},
},
};

await roleService.assignPermissions(1, []);

expect(deleteByIds).toHaveBeenCalledWith([3]);
});

test('Create new permissions', async () => {
const permissions = Array(5)
.fill(0)
.map((v, i) => ({ action: `action-${i}` }));

const createMany = jest.fn(() => Promise.resolve([]));
const getSuperAdmin = jest.fn(() => Promise.resolve({ id: 0 }));
const sendDidUpdateRolePermissions = jest.fn();
const find = jest.fn(() => Promise.resolve([]));
const getAll = jest.fn(() => permissions.map(perm => ({ actionId: perm.action })));
const removeUnkownConditionIds = jest.fn(conds => _.intersection(conds, ['cond']));

global.strapi = {
admin: {
services: {
metrics: { sendDidUpdateRolePermissions },
role: { getSuperAdmin },
permission: {
find,
createMany,
actionProvider: { getAll },
conditionProvider: {
getAll: jest.fn(() => [{ id: 'admin::is-creator' }]),
},
},
condition: {
removeUnkownConditionIds,
},
},
},
};

const permissionsToAssign = [...permissions];
permissionsToAssign[4] = {
...permissions[4],
conditions: ['cond', 'unknown-cond'],
};

await roleService.assignPermissions(1, permissionsToAssign);

expect(createMany).toHaveBeenCalledTimes(1);
expect(createMany).toHaveBeenCalledWith([
{ action: 'action-0', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-1', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-2', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-3', conditions: [], fields: null, role: 1, subject: null },
{ action: 'action-4', conditions: ['cond'], fields: null, role: 1, subject: null },
]);
});
});
});

0 comments on commit 5f9b6c8

Please sign in to comment.