101 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
// Test a simple default API with no relations
2021-09-07 21:03:30 +02:00
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
const { createContentAPIRequest } = require('../../../../../test/helpers/request');
let strapi;
let rq;
2021-09-07 21:03:30 +02:00
const data = {};
describe('Users API', () => {
beforeAll(async () => {
strapi = await createStrapiInstance();
2021-09-07 21:03:30 +02:00
rq = await createContentAPIRequest({ strapi });
});
afterAll(async () => {
await strapi.destroy();
});
test('Create User', async () => {
const user = {
username: 'User 1',
email: 'user1@strapi.io',
password: 'test1234',
};
const res = await rq({
method: 'POST',
2021-09-07 21:54:27 +02:00
url: '/users',
body: user,
});
2021-09-07 21:03:30 +02:00
expect(res.statusCode).toBe(201);
expect(res.body).toMatchObject({
2021-09-07 21:03:30 +02:00
username: user.username,
email: user.email,
});
2021-09-07 21:03:30 +02:00
data.user = res.body;
});
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,
},
]);
});
});
test('Delete user', async () => {
const res = await rq({
method: 'DELETE',
2021-09-07 21:54:27 +02:00
url: `/users/${data.user.id}`,
});
expect(res.statusCode).toBe(200);
});
});