282 lines
7.4 KiB
JavaScript
Raw Normal View History

'use strict';
const { omit } = require('lodash/fp');
const { createStrapiInstance } = require('api-tests/strapi');
const { createTestBuilder } = require('api-tests/builder');
const { createAuthRequest } = require('api-tests/request');
const builder = createTestBuilder();
let strapi;
let rq;
2022-08-08 15:50:34 +02:00
const data = {
products: [],
};
const product = {
attributes: {
name: {
type: 'string',
required: true,
},
description: {
type: 'text',
minLength: 4,
maxLength: 30,
},
hiddenAttribute: {
type: 'string',
},
},
config: {
attributes: {
hiddenAttribute: {
hidden: true,
},
},
},
2021-09-13 16:57:04 +02:00
displayName: 'Product',
singularName: 'product',
pluralName: 'products',
description: '',
collectionName: '',
};
describe('CM API - Basic', () => {
beforeAll(async () => {
await builder.addContentType(product).build();
strapi = await createStrapiInstance();
rq = await createAuthRequest({ strapi });
});
afterAll(async () => {
await strapi.destroy();
await builder.cleanup();
});
test('Create product', async () => {
const product = {
name: 'Product 1',
description: 'Product description',
hiddenAttribute: 'Secret value',
};
const res = await rq({
method: 'POST',
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
body: product,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(omit('hiddenAttribute', product));
expect(res.body).not.toHaveProperty('hiddenAttribute');
expect(res.body.publishedAt).toBeDefined();
data.products.push(res.body);
});
test('Read product', async () => {
const res = await rq({
method: 'GET',
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
});
expect(res.statusCode).toBe(200);
expect(Array.isArray(res.body.results)).toBe(true);
expect(res.body.results).toHaveLength(1);
expect(res.body.results).toEqual(
expect.arrayContaining([
expect.objectContaining({
name: 'Product 1',
description: 'Product description',
}),
])
);
res.body.results.forEach((p) => expect(p.publishedAt).toBeDefined());
});
test('Update product', async () => {
const product = {
name: 'Product 1 updated',
description: 'Updated Product description',
hiddenAttribute: 'Secret value',
};
const res = await rq({
method: 'PUT',
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::product.product/${data.products[0].id}`,
body: product,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(omit('hiddenAttribute', product));
expect(res.body.id).toEqual(data.products[0].id);
expect(res.body.publishedAt).toBeDefined();
data.products[0] = res.body;
});
test('Delete product', async () => {
const res = await rq({
method: 'DELETE',
2021-08-06 18:09:49 +02:00
url: `/content-manager/collection-types/api::product.product/${data.products[0].id}`,
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(data.products[0]);
expect(res.body.id).toEqual(data.products[0].id);
expect(res.body.publishedAt).toBeDefined();
data.products.shift();
});
2023-03-28 11:04:01 +02:00
test('Clone product', async () => {
const product = {
name: 'Product 1',
description: 'Product description',
hiddenAttribute: 'Secret value',
};
const { body: createdProduct } = await rq({
method: 'POST',
url: '/content-manager/collection-types/api::product.product',
body: product,
});
const res = await rq({
method: 'POST',
url: `/content-manager/collection-types/api::product.product/clone/${createdProduct.id}`,
body: {},
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject(omit('hiddenAttribute', product));
});
test('Clone and update product', async () => {
const product = {
name: 'Product 1',
description: 'Product description',
hiddenAttribute: 'Secret value',
};
const { body: createdProduct } = await rq({
method: 'POST',
url: '/content-manager/collection-types/api::product.product',
body: product,
});
const res = await rq({
method: 'POST',
url: `/content-manager/collection-types/api::product.product/clone/${createdProduct.id}`,
body: {
name: 'Product 1 updated',
},
});
expect(res.statusCode).toBe(200);
expect(res.body).toMatchObject({
name: 'Product 1 updated',
description: 'Product description',
});
});
describe('validators', () => {
test('Cannot publish a product - minLength', async () => {
const product = {
name: 'Product 1',
description: '',
};
const creationRes = await rq({
method: 'POST',
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
body: product,
});
const res = await rq({
method: 'POST',
url: `/content-manager/collection-types/api::product.product/${creationRes.body.id}/actions/publish`,
});
expect(res.statusCode).toBe(400);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
message: 'description must be at least 4 characters',
name: 'ValidationError',
details: {
errors: [
{
path: ['description'],
message: 'description must be at least 4 characters',
name: 'ValidationError',
},
],
},
},
});
});
test('Cannot publish a product - required', async () => {
const product = {
description: 'Product description',
};
const creationRes = await rq({
method: 'POST',
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
body: product,
});
const res = await rq({
method: 'POST',
url: `/content-manager/collection-types/api::product.product/${creationRes.body.id}/actions/publish`,
});
expect(res.statusCode).toBe(400);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
message: 'name must be a `string` type, but the final value was: `null`.',
2021-10-20 17:30:05 +02:00
name: 'ValidationError',
details: {
errors: [
{
path: ['name'],
message: 'name must be a `string` type, but the final value was: `null`.',
2021-10-20 17:30:05 +02:00
name: 'ValidationError',
},
],
},
},
});
});
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',
2021-08-06 18:09:49 +02:00
url: '/content-manager/collection-types/api::product.product',
body: product,
});
expect(res.statusCode).toBe(400);
2021-10-20 17:30:05 +02:00
expect(res.body).toMatchObject({
data: null,
error: {
message: 'description must be at most 30 characters',
name: 'ValidationError',
details: {
errors: [
{
path: ['description'],
message: 'description must be at most 30 characters',
name: 'ValidationError',
},
],
},
},
});
});
});
});