mirror of
https://github.com/strapi/strapi.git
synced 2025-10-27 16:10:08 +00:00
Merge pull request #3258 from strapi/fix/beta-crud-role-with-users
Fix crud role with users
This commit is contained in:
commit
e73f3dafcc
@ -178,9 +178,9 @@ export class EditPage extends React.Component {
|
||||
number: size(get(this.props.editPage, ['modifiedData', 'users'])),
|
||||
},
|
||||
}}
|
||||
onClickAdd={() => {
|
||||
onClickAdd={itemToAdd => {
|
||||
this.context.emitEvent('didAssociateUserToRole');
|
||||
this.props.onClickAdd();
|
||||
this.props.onClickAdd(itemToAdd);
|
||||
}}
|
||||
onClickDelete={this.props.onClickDelete}
|
||||
name="users"
|
||||
|
||||
@ -26,6 +26,7 @@ module.exports = {
|
||||
|
||||
ctx.send({ ok: true });
|
||||
} catch (err) {
|
||||
strapi.log.error(err)
|
||||
ctx.badRequest(null, [{ messages: [{ id: 'An error occured' }] }]);
|
||||
}
|
||||
},
|
||||
@ -66,6 +67,7 @@ module.exports = {
|
||||
|
||||
ctx.send({ ok: true });
|
||||
} catch (err) {
|
||||
strapi.log.error(err)
|
||||
ctx.badRequest(null, [{ messages: [{ id: 'Bad request' }] }]);
|
||||
}
|
||||
},
|
||||
@ -173,7 +175,8 @@ module.exports = {
|
||||
strapi.emit('didOpenAccessToFetchContentTypeEntries', ctx.request.body);
|
||||
|
||||
ctx.send({ ok: true });
|
||||
} catch (error) {
|
||||
} catch (err) {
|
||||
strapi.log.error(err)
|
||||
ctx.badRequest(null, [{ messages: [{ id: 'An error occurred' }] }]);
|
||||
}
|
||||
},
|
||||
|
||||
@ -559,7 +559,7 @@ module.exports = {
|
||||
);
|
||||
|
||||
// stringify mongoDB _id for add/remove matching
|
||||
if (role._id ? '_id' : 'id' === '_id') {
|
||||
if (role._id) {
|
||||
role.users.reduce((acc, user) => {
|
||||
const key = role._id ? '_id' : 'id';
|
||||
user[key] = user[key].toString();
|
||||
|
||||
@ -0,0 +1,87 @@
|
||||
// Test a simple default API with no relations
|
||||
|
||||
const { registerAndLogin } = require('../../../test/helpers/auth');
|
||||
const { createAuthRequest } = require('../../../test/helpers/request');
|
||||
|
||||
let rq;
|
||||
let data = {};
|
||||
let internals = {
|
||||
user: {
|
||||
username: 'User 1',
|
||||
email: 'user1@strapi.io',
|
||||
password: 'test1234',
|
||||
},
|
||||
role: {
|
||||
name: 'Test Role',
|
||||
description: 'Some random test role',
|
||||
},
|
||||
};
|
||||
|
||||
/**
|
||||
* Utils for this test files
|
||||
*/
|
||||
const createTestUser = () =>
|
||||
rq({
|
||||
method: 'POST',
|
||||
url: '/auth/local/register',
|
||||
body: internals.user,
|
||||
});
|
||||
|
||||
const deleteTestUser = () =>
|
||||
rq({
|
||||
method: 'DELETE',
|
||||
url: `/users/${data.user.id}`,
|
||||
});
|
||||
|
||||
/*****************************
|
||||
* TESTS
|
||||
*****************************/
|
||||
describe('Roles API', () => {
|
||||
beforeAll(async () => {
|
||||
const token = await registerAndLogin();
|
||||
rq = createAuthRequest(token);
|
||||
|
||||
const res = await createTestUser();
|
||||
data.user = res.body.user;
|
||||
}, 60000);
|
||||
|
||||
afterAll(() => deleteTestUser());
|
||||
|
||||
test('Create Role', async () => {
|
||||
const res = await rq({
|
||||
method: 'POST',
|
||||
url: '/users-permissions/roles',
|
||||
body: {
|
||||
...internals.role,
|
||||
permissions: [],
|
||||
users: [data.user],
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toMatchObject({ ok: true });
|
||||
});
|
||||
|
||||
test('List Roles', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: '/users-permissions/roles',
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body.roles).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining(internals.role)])
|
||||
);
|
||||
|
||||
data.role = res.body.roles.find(r => r.name === internals.role.name);
|
||||
});
|
||||
|
||||
test('Delete Role', async () => {
|
||||
const res = await rq({
|
||||
method: 'DELETE',
|
||||
url: `/users-permissions/roles/${data.role.id}`,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
});
|
||||
});
|
||||
@ -0,0 +1,47 @@
|
||||
// Test a simple default API with no relations
|
||||
|
||||
const { registerAndLogin } = require('../../../test/helpers/auth');
|
||||
const { createAuthRequest } = require('../../../test/helpers/request');
|
||||
|
||||
let rq;
|
||||
let data = {};
|
||||
|
||||
describe('Users API', () => {
|
||||
beforeAll(async () => {
|
||||
const token = await registerAndLogin();
|
||||
rq = createAuthRequest(token);
|
||||
}, 60000);
|
||||
|
||||
test('Create User', async () => {
|
||||
const user = {
|
||||
username: 'User 1',
|
||||
email: 'user1@strapi.io',
|
||||
password: 'test1234',
|
||||
};
|
||||
|
||||
const res = await rq({
|
||||
method: 'POST',
|
||||
url: '/auth/local/register',
|
||||
body: user,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
expect(res.body).toMatchObject({
|
||||
jwt: expect.any(String),
|
||||
user: {
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
},
|
||||
});
|
||||
data.user = res.body.user;
|
||||
});
|
||||
|
||||
test('Delete user', async () => {
|
||||
const res = await rq({
|
||||
method: 'DELETE',
|
||||
url: `/users/${data.user.id}`,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user