mirror of
https://github.com/strapi/strapi.git
synced 2025-09-25 16:29:34 +00:00
Add test to verify population in users-api
This commit is contained in:
parent
183bad03d3
commit
f8d920a332
@ -8,6 +8,12 @@ const { createContentAPIRequest } = require('../../../../../test/helpers/request
|
||||
let strapi;
|
||||
let rq;
|
||||
|
||||
const internals = {
|
||||
role: {
|
||||
name: 'Test Role',
|
||||
description: 'Some random test role',
|
||||
},
|
||||
};
|
||||
const data = {};
|
||||
|
||||
describe('Users API', () => {
|
||||
@ -20,11 +26,42 @@ describe('Users API', () => {
|
||||
await strapi.destroy();
|
||||
});
|
||||
|
||||
test('Create and get Role', async () => {
|
||||
const createRes = await rq({
|
||||
method: 'POST',
|
||||
url: '/users-permissions/roles',
|
||||
body: {
|
||||
...internals.role,
|
||||
permissions: [],
|
||||
},
|
||||
});
|
||||
|
||||
expect(createRes.statusCode).toBe(200);
|
||||
expect(createRes.body).toMatchObject({ ok: true });
|
||||
|
||||
const findRes = await rq({
|
||||
method: 'GET',
|
||||
url: '/users-permissions/roles',
|
||||
});
|
||||
|
||||
expect(findRes.statusCode).toBe(200);
|
||||
expect(findRes.body.roles).toEqual(
|
||||
expect.arrayContaining([expect.objectContaining(internals.role)])
|
||||
);
|
||||
|
||||
// eslint-disable-next-line no-unused-vars
|
||||
const { nb_users, ...role } = findRes.body.roles.find(r => r.name === internals.role.name);
|
||||
expect(role).toMatchObject(internals.role);
|
||||
|
||||
data.role = role;
|
||||
});
|
||||
|
||||
test('Create User', async () => {
|
||||
const user = {
|
||||
username: 'User 1',
|
||||
email: 'user1@strapi.io',
|
||||
password: 'test1234',
|
||||
role: data.role.id,
|
||||
};
|
||||
|
||||
const res = await rq({
|
||||
@ -37,6 +74,7 @@ describe('Users API', () => {
|
||||
expect(res.body).toMatchObject({
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
role: data.role,
|
||||
});
|
||||
|
||||
data.user = res.body;
|
||||
@ -87,6 +125,103 @@ describe('Users API', () => {
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should populate role', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: '/users?populate=role',
|
||||
});
|
||||
|
||||
const { statusCode, body } = res;
|
||||
|
||||
expect(statusCode).toBe(200);
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body).toHaveLength(1);
|
||||
expect(body).toMatchObject([
|
||||
{
|
||||
id: expect.anything(),
|
||||
username: data.user.username,
|
||||
email: data.user.email,
|
||||
role: data.role,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
test('should not populate users in role', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: '/users?populate[role][populate][0]=users',
|
||||
});
|
||||
|
||||
const { statusCode, body } = res;
|
||||
|
||||
expect(statusCode).toBe(200);
|
||||
expect(Array.isArray(body)).toBe(true);
|
||||
expect(body).toHaveLength(1);
|
||||
expect(body).toMatchObject([
|
||||
{
|
||||
id: expect.anything(),
|
||||
username: data.user.username,
|
||||
email: data.user.email,
|
||||
role: data.role,
|
||||
},
|
||||
]);
|
||||
expect(body[0].role).not.toHaveProperty('users');
|
||||
});
|
||||
});
|
||||
|
||||
describe('Read an user', () => {
|
||||
test('should populate role', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: `/users/${data.user.id}?populate=role`,
|
||||
});
|
||||
|
||||
const { statusCode, body } = res;
|
||||
|
||||
expect(statusCode).toBe(200);
|
||||
expect(body).toMatchObject({
|
||||
id: data.user.id,
|
||||
username: data.user.username,
|
||||
email: data.user.email,
|
||||
role: data.role,
|
||||
});
|
||||
});
|
||||
|
||||
test('should not populate role', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: `/users/${data.user.id}`,
|
||||
});
|
||||
|
||||
const { statusCode, body } = res;
|
||||
|
||||
expect(statusCode).toBe(200);
|
||||
expect(body).toMatchObject({
|
||||
id: data.user.id,
|
||||
username: data.user.username,
|
||||
email: data.user.email,
|
||||
});
|
||||
expect(body).not.toHaveProperty('role');
|
||||
});
|
||||
|
||||
test('should not populate users in role', async () => {
|
||||
const res = await rq({
|
||||
method: 'GET',
|
||||
url: `/users/${data.user.id}?populate[role][populate][0]=users`,
|
||||
});
|
||||
|
||||
const { statusCode, body } = res;
|
||||
|
||||
expect(statusCode).toBe(200);
|
||||
expect(body).toMatchObject({
|
||||
id: data.user.id,
|
||||
username: data.user.username,
|
||||
email: data.user.email,
|
||||
role: data.role,
|
||||
});
|
||||
expect(body.role).not.toHaveProperty('users');
|
||||
});
|
||||
});
|
||||
|
||||
test('Delete user', async () => {
|
||||
|
Loading…
x
Reference in New Issue
Block a user