2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2019-05-14 12:41:17 +02:00
|
|
|
// Test a simple default API with no relations
|
|
|
|
|
2023-04-05 10:32:20 +02:00
|
|
|
const { createStrapiInstance } = require('api-tests/strapi');
|
|
|
|
const { createContentAPIRequest } = require('api-tests/request');
|
2019-05-14 12:41:17 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
let strapi;
|
2019-05-14 12:41:17 +02:00
|
|
|
let rq;
|
2021-09-07 21:03:30 +02:00
|
|
|
|
2022-03-03 22:57:37 +09:00
|
|
|
const internals = {
|
|
|
|
role: {
|
|
|
|
name: 'Test Role',
|
|
|
|
description: 'Some random test role',
|
|
|
|
},
|
|
|
|
};
|
2021-12-04 21:50:54 +09:00
|
|
|
const data = {};
|
2019-05-14 12:41:17 +02:00
|
|
|
|
|
|
|
describe('Users API', () => {
|
|
|
|
beforeAll(async () => {
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2021-09-07 21:03:30 +02:00
|
|
|
rq = await createContentAPIRequest({ strapi });
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2019-05-14 12:41:17 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
});
|
|
|
|
|
2022-03-03 22:57:37 +09:00
|
|
|
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
|
2022-08-08 23:33:39 +02:00
|
|
|
const { nb_users: nbUsers, ...role } = findRes.body.roles.find(
|
|
|
|
(r) => r.name === internals.role.name
|
|
|
|
);
|
|
|
|
|
2022-03-03 22:57:37 +09:00
|
|
|
expect(role).toMatchObject(internals.role);
|
|
|
|
|
|
|
|
data.role = role;
|
|
|
|
});
|
|
|
|
|
2019-05-14 12:41:17 +02:00
|
|
|
test('Create User', async () => {
|
|
|
|
const user = {
|
|
|
|
username: 'User 1',
|
|
|
|
email: 'user1@strapi.io',
|
|
|
|
password: 'test1234',
|
2022-03-03 22:57:37 +09:00
|
|
|
role: data.role.id,
|
2019-05-14 12:41:17 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
2021-09-07 21:54:27 +02:00
|
|
|
url: '/users',
|
2019-05-14 12:41:17 +02:00
|
|
|
body: user,
|
|
|
|
});
|
|
|
|
|
2021-09-07 21:03:30 +02:00
|
|
|
expect(res.statusCode).toBe(201);
|
2019-05-14 12:41:17 +02:00
|
|
|
expect(res.body).toMatchObject({
|
2021-09-07 21:03:30 +02:00
|
|
|
username: user.username,
|
|
|
|
email: user.email,
|
2022-03-03 22:57:37 +09:00
|
|
|
role: data.role,
|
2019-05-14 12:41:17 +02:00
|
|
|
});
|
2021-09-07 21:03:30 +02:00
|
|
|
|
|
|
|
data.user = res.body;
|
2019-05-14 12:41:17 +02:00
|
|
|
});
|
|
|
|
|
2022-05-19 22:08:58 +02:00
|
|
|
test('Updating unknown user returns 404', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
|
|
|
url: '/users/99999999',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(404);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
error: {
|
|
|
|
message: 'User not found',
|
|
|
|
name: 'NotFoundError',
|
|
|
|
status: 404,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2021-12-04 21:50:54 +09:00
|
|
|
describe('Read users', () => {
|
|
|
|
test('without filter', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'GET',
|
|
|
|
url: '/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,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('with filter equals', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'GET',
|
|
|
|
url: '/users',
|
|
|
|
qs: {
|
|
|
|
filters: {
|
|
|
|
username: 'User 1',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
});
|
2022-03-03 22:57:37 +09:00
|
|
|
|
|
|
|
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');
|
|
|
|
});
|
2021-12-04 21:50:54 +09:00
|
|
|
});
|
|
|
|
|
2019-05-14 12:41:17 +02:00
|
|
|
test('Delete user', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'DELETE',
|
2021-09-07 21:54:27 +02:00
|
|
|
url: `/users/${data.user.id}`,
|
2019-05-14 12:41:17 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
});
|
2020-10-27 11:27:17 +01:00
|
|
|
});
|