2020-10-27 11:27:17 +01:00
|
|
|
'use strict';
|
|
|
|
|
2020-09-22 12:31:26 +02:00
|
|
|
const _ = require('lodash');
|
2020-11-17 15:38:41 +01:00
|
|
|
const { createStrapiInstance } = require('../../../../test/helpers/strapi');
|
|
|
|
const { createTestBuilder } = require('../../../../test/helpers/builder');
|
2020-09-22 12:31:26 +02:00
|
|
|
const { createAuthRequest } = require('../../../../test/helpers/request');
|
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const builder = createTestBuilder();
|
|
|
|
let strapi;
|
2020-09-22 12:31:26 +02:00
|
|
|
let rq;
|
|
|
|
let data = {
|
|
|
|
products: [],
|
|
|
|
};
|
|
|
|
|
|
|
|
const product = {
|
|
|
|
attributes: {
|
|
|
|
name: {
|
|
|
|
type: 'string',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
description: {
|
|
|
|
type: 'text',
|
|
|
|
minLength: 4,
|
|
|
|
maxLength: 30,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
connection: 'default',
|
|
|
|
name: 'product',
|
|
|
|
description: '',
|
|
|
|
collectionName: '',
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('CM API - Basic', () => {
|
|
|
|
beforeAll(async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
await builder.addContentType(product).build();
|
2020-09-22 12:31:26 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
strapi = await createStrapiInstance({ ensureSuperAdmin: true });
|
|
|
|
rq = await createAuthRequest({ strapi });
|
2020-09-22 12:31:26 +02:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
afterAll(async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
await strapi.destroy();
|
|
|
|
await builder.cleanup();
|
2020-09-22 12:31:26 +02:00
|
|
|
}, 60000);
|
|
|
|
|
|
|
|
test('Create product', async () => {
|
|
|
|
const product = {
|
|
|
|
name: 'Product 1',
|
|
|
|
description: 'Product description',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: '/content-manager/collection-types/application::product.product',
|
2020-09-22 12:31:26 +02:00
|
|
|
body: product,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject(product);
|
|
|
|
expect(res.body.published_at).toBeUndefined();
|
|
|
|
data.products.push(res.body);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Read product', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'GET',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: '/content-manager/collection-types/application::product.product',
|
2020-09-22 12:31:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2020-11-03 13:42:01 +01:00
|
|
|
expect(Array.isArray(res.body.results)).toBe(true);
|
|
|
|
expect(res.body.results).toHaveLength(1);
|
|
|
|
expect(res.body.results).toEqual(
|
2020-09-22 12:31:26 +02:00
|
|
|
expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
name: 'Product 1',
|
|
|
|
description: 'Product description',
|
|
|
|
}),
|
|
|
|
])
|
|
|
|
);
|
2020-11-03 13:42:01 +01:00
|
|
|
res.body.results.forEach(p => expect(p.published_at).toBeUndefined());
|
2020-09-22 12:31:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Update product', async () => {
|
|
|
|
const product = {
|
|
|
|
name: 'Product 1 updated',
|
|
|
|
description: 'Updated Product description',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
method: 'PUT',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: `/content-manager/collection-types/application::product.product/${data.products[0].id}`,
|
2020-09-22 12:31:26 +02:00
|
|
|
body: product,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject(product);
|
|
|
|
expect(res.body.id).toEqual(data.products[0].id);
|
|
|
|
expect(res.body.published_at).toBeUndefined();
|
|
|
|
data.products[0] = res.body;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Delete product', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'DELETE',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: `/content-manager/collection-types/application::product.product/${data.products[0].id}`,
|
2020-09-22 12:31:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject(data.products[0]);
|
|
|
|
expect(res.body.id).toEqual(data.products[0].id);
|
|
|
|
expect(res.body.published_at).toBeUndefined();
|
|
|
|
data.products.shift();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('validators', () => {
|
|
|
|
test('Cannot create a product - minLength', async () => {
|
|
|
|
const product = {
|
|
|
|
name: 'Product 1',
|
|
|
|
description: '',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: '/content-manager/collection-types/application::product.product',
|
2020-09-22 12:31:26 +02:00
|
|
|
body: product,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(_.get(res, 'body.data.errors.description.0')).toBe(
|
|
|
|
'description must be at least 4 characters'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Cannot create a product - required', async () => {
|
|
|
|
const product = {
|
|
|
|
description: 'Product description',
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: '/content-manager/collection-types/application::product.product',
|
2020-09-22 12:31:26 +02:00
|
|
|
body: product,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
2020-11-02 12:40:35 +01:00
|
|
|
expect(_.get(res, 'body.data.errors.name.0')).toBe('name must be defined.');
|
2020-09-22 12:31:26 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('Cannot create a product - maxLength', async () => {
|
|
|
|
const product = {
|
|
|
|
name: 'Product 1',
|
|
|
|
description: "I'm a product description that is very long. At least thirty characters.",
|
|
|
|
};
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
2020-11-02 12:40:35 +01:00
|
|
|
url: '/content-manager/collection-types/application::product.product',
|
2020-09-22 12:31:26 +02:00
|
|
|
body: product,
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(400);
|
|
|
|
expect(_.get(res, 'body.data.errors.description.0')).toBe(
|
|
|
|
'description must be at most 30 characters'
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|