mirror of
https://github.com/strapi/strapi.git
synced 2025-07-18 14:32:56 +00:00
161 lines
4.3 KiB
JavaScript
161 lines
4.3 KiB
JavaScript
![]() |
const _ = require('lodash');
|
||
|
const { registerAndLogin } = require('../../../../test/helpers/auth');
|
||
|
const createModelsUtils = require('../../../../test/helpers/models');
|
||
|
const { createAuthRequest } = require('../../../../test/helpers/request');
|
||
|
|
||
|
let rq;
|
||
|
let modelsUtils;
|
||
|
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 () => {
|
||
|
const token = await registerAndLogin();
|
||
|
rq = createAuthRequest(token);
|
||
|
|
||
|
modelsUtils = createModelsUtils({ rq });
|
||
|
await modelsUtils.createContentTypes([product]);
|
||
|
}, 60000);
|
||
|
|
||
|
afterAll(async () => {
|
||
|
await modelsUtils.deleteContentTypes(['product']);
|
||
|
}, 60000);
|
||
|
|
||
|
test('Create product', async () => {
|
||
|
const product = {
|
||
|
name: 'Product 1',
|
||
|
description: 'Product description',
|
||
|
};
|
||
|
const res = await rq({
|
||
|
method: 'POST',
|
||
|
url: '/content-manager/explorer/application::product.product',
|
||
|
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',
|
||
|
url: '/content-manager/explorer/application::product.product',
|
||
|
});
|
||
|
|
||
|
expect(res.statusCode).toBe(200);
|
||
|
expect(Array.isArray(res.body)).toBe(true);
|
||
|
expect(res.body).toHaveLength(1);
|
||
|
expect(res.body).toEqual(
|
||
|
expect.arrayContaining([
|
||
|
expect.objectContaining({
|
||
|
name: 'Product 1',
|
||
|
description: 'Product description',
|
||
|
}),
|
||
|
])
|
||
|
);
|
||
|
res.body.forEach(p => expect(p.published_at).toBeUndefined());
|
||
|
});
|
||
|
|
||
|
test('Update product', async () => {
|
||
|
const product = {
|
||
|
name: 'Product 1 updated',
|
||
|
description: 'Updated Product description',
|
||
|
};
|
||
|
const res = await rq({
|
||
|
method: 'PUT',
|
||
|
url: `/content-manager/explorer/application::product.product/${data.products[0].id}`,
|
||
|
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',
|
||
|
url: `/content-manager/explorer/application::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.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',
|
||
|
url: '/content-manager/explorer/application::product.product',
|
||
|
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',
|
||
|
url: '/content-manager/explorer/application::product.product',
|
||
|
body: product,
|
||
|
});
|
||
|
|
||
|
expect(res.statusCode).toBe(400);
|
||
|
expect(_.get(res, 'body.data.0.errors.name.0')).toBe('name must be defined.');
|
||
|
});
|
||
|
|
||
|
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',
|
||
|
url: '/content-manager/explorer/application::product.product',
|
||
|
body: product,
|
||
|
});
|
||
|
|
||
|
expect(res.statusCode).toBe(400);
|
||
|
expect(_.get(res, 'body.data.errors.description.0')).toBe(
|
||
|
'description must be at most 30 characters'
|
||
|
);
|
||
|
});
|
||
|
});
|
||
|
});
|