| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-05-10 06:58:25 +02:00
										 |  |  | const { omit } = require('lodash/fp'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							|  |  |  | const { createAuthRequest } = require('api-tests/request'); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | let rq; | 
					
						
							| 
									
										
										
										
											2022-08-08 15:50:34 +02:00
										 |  |  | const data = { | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |   products: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const product = { | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       required: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     description: { | 
					
						
							|  |  |  |       type: 'text', | 
					
						
							|  |  |  |       minLength: 4, | 
					
						
							|  |  |  |       maxLength: 30, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2022-05-10 06:58:25 +02:00
										 |  |  |     hiddenAttribute: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   config: { | 
					
						
							|  |  |  |     attributes: { | 
					
						
							|  |  |  |       hiddenAttribute: { | 
					
						
							|  |  |  |         hidden: true, | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }, | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |   }, | 
					
						
							| 
									
										
										
										
											2021-09-13 16:57:04 +02:00
										 |  |  |   displayName: 'Product', | 
					
						
							|  |  |  |   singularName: 'product', | 
					
						
							|  |  |  |   pluralName: 'products', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |   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-30 20:20:36 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   test('Create product', async () => { | 
					
						
							|  |  |  |     const product = { | 
					
						
							|  |  |  |       name: 'Product 1', | 
					
						
							|  |  |  |       description: 'Product description', | 
					
						
							| 
									
										
										
										
											2022-05-10 06:58:25 +02:00
										 |  |  |       hiddenAttribute: 'Secret value', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     const res = await rq({ | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |       url: '/content-manager/collection-types/api::product.product', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |       body: product, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							| 
									
										
										
										
											2022-05-10 06:58:25 +02:00
										 |  |  |     expect(res.body).toMatchObject(omit('hiddenAttribute', product)); | 
					
						
							|  |  |  |     expect(res.body).not.toHaveProperty('hiddenAttribute'); | 
					
						
							| 
									
										
										
										
											2021-09-22 12:14:41 +02:00
										 |  |  |     expect(res.body.publishedAt).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     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', | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							|  |  |  |         }), | 
					
						
							|  |  |  |       ]) | 
					
						
							|  |  |  |     ); | 
					
						
							| 
									
										
										
										
											2022-08-08 23:33:39 +02:00
										 |  |  |     res.body.results.forEach((p) => expect(p.publishedAt).toBeUndefined()); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   test('Update product', async () => { | 
					
						
							|  |  |  |     const product = { | 
					
						
							|  |  |  |       name: 'Product 1 updated', | 
					
						
							|  |  |  |       description: 'Updated Product description', | 
					
						
							| 
									
										
										
										
											2022-05-10 06:58:25 +02:00
										 |  |  |       hiddenAttribute: 'Secret value', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     }; | 
					
						
							|  |  |  |     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}`, | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |       body: product, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(res.statusCode).toBe(200); | 
					
						
							| 
									
										
										
										
											2022-05-10 06:58:25 +02:00
										 |  |  |     expect(res.body).toMatchObject(omit('hiddenAttribute', product)); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     expect(res.body.id).toEqual(data.products[0].id); | 
					
						
							| 
									
										
										
										
											2021-09-22 12:14:41 +02:00
										 |  |  |     expect(res.body.publishedAt).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     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}`, | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							| 
									
										
										
										
											2021-09-22 12:14:41 +02:00
										 |  |  |     expect(res.body.publishedAt).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     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', | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |   describe('validators', () => { | 
					
						
							|  |  |  |     test('Cannot create a product - minLength', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         name: 'Product 1', | 
					
						
							|  |  |  |         description: '', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product.product', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         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 least 4 characters', | 
					
						
							|  |  |  |           name: 'ValidationError', | 
					
						
							|  |  |  |           details: { | 
					
						
							|  |  |  |             errors: [ | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 path: ['description'], | 
					
						
							|  |  |  |                 message: 'description must be at least 4 characters', | 
					
						
							|  |  |  |                 name: 'ValidationError', | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Cannot create a product - required', async () => { | 
					
						
							|  |  |  |       const product = { | 
					
						
							|  |  |  |         description: 'Product description', | 
					
						
							|  |  |  |       }; | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product.product', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         body: product, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							| 
									
										
										
										
											2021-10-20 17:30:05 +02:00
										 |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: null, | 
					
						
							|  |  |  |         error: { | 
					
						
							|  |  |  |           message: 'name must be defined.', | 
					
						
							|  |  |  |           name: 'ValidationError', | 
					
						
							|  |  |  |           details: { | 
					
						
							|  |  |  |             errors: [ | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 path: ['name'], | 
					
						
							|  |  |  |                 message: 'name must be defined.', | 
					
						
							|  |  |  |                 name: 'ValidationError', | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											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', | 
					
						
							| 
									
										
										
										
											2021-08-06 18:09:49 +02:00
										 |  |  |         url: '/content-manager/collection-types/api::product.product', | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |         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', | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-09-22 12:31:26 +02:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |