| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-18 09:26:13 +01:00
										 |  |  | const { mapAsync } = require('@strapi/utils'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-11 14:15:43 +01:00
										 |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createAuthRequest, createRequest } = require('api-tests/request'); | 
					
						
							|  |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							|  |  |  | const { describeOnCondition } = require('api-tests/utils'); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-23 12:48:17 +00:00
										 |  |  | const { | 
					
						
							|  |  |  |   STAGE_MODEL_UID, | 
					
						
							|  |  |  |   WORKFLOW_MODEL_UID, | 
					
						
							|  |  |  |   ENTITY_STAGE_ATTRIBUTE, | 
					
						
							| 
									
										
										
										
											2023-04-11 14:15:43 +01:00
										 |  |  | } = require('../../../../packages/core/admin/ee/server/constants/workflows'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const defaultStages = require('../../../../packages/core/admin/ee/server/constants/default-stages.json'); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | const edition = process.env.STRAPI_DISABLE_EE === 'true' ? 'CE' : 'EE'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | const productUID = 'api::product.product'; | 
					
						
							|  |  |  | const model = { | 
					
						
							|  |  |  |   draftAndPublish: true, | 
					
						
							|  |  |  |   pluginOptions: {}, | 
					
						
							|  |  |  |   singularName: 'product', | 
					
						
							|  |  |  |   pluralName: 'products', | 
					
						
							|  |  |  |   displayName: 'Product', | 
					
						
							|  |  |  |   kind: 'collectionType', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  | describeOnCondition(edition === 'EE')('Review workflows', () => { | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |   const builder = createTestBuilder(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |   const requests = { | 
					
						
							|  |  |  |     public: null, | 
					
						
							|  |  |  |     admin: null, | 
					
						
							|  |  |  |   }; | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |   let strapi; | 
					
						
							|  |  |  |   let hasRW; | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |   let defaultStage; | 
					
						
							| 
									
										
										
										
											2023-01-31 15:30:19 +01:00
										 |  |  |   let secondStage; | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |   let testWorkflow; | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |   const createEntry = async (uid, data) => { | 
					
						
							|  |  |  |     const { body } = await requests.admin({ | 
					
						
							|  |  |  |       method: 'POST', | 
					
						
							|  |  |  |       url: `/content-manager/collection-types/${uid}`, | 
					
						
							|  |  |  |       body: data, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return body; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-25 10:22:35 +02:00
										 |  |  |   const updateEntry = async (uid, id, data) => { | 
					
						
							|  |  |  |     const { body } = await requests.admin({ | 
					
						
							|  |  |  |       method: 'PUT', | 
					
						
							|  |  |  |       url: `/content-manager/collection-types/${uid}/${id}`, | 
					
						
							|  |  |  |       body: data, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return body; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-29 15:33:10 +01:00
										 |  |  |   const findAll = async (uid) => { | 
					
						
							|  |  |  |     const { body } = await requests.admin({ | 
					
						
							|  |  |  |       method: 'GET', | 
					
						
							|  |  |  |       url: `/content-manager/collection-types/${uid}`, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     return body; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |   const updateContentType = async (uid, data) => { | 
					
						
							|  |  |  |     const result = await requests.admin({ | 
					
						
							|  |  |  |       method: 'PUT', | 
					
						
							|  |  |  |       url: `/content-type-builder/content-types/${uid}`, | 
					
						
							|  |  |  |       body: data, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     expect(result.statusCode).toBe(201); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   const restart = async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |     requests.admin = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							|  |  |  |     await builder.addContentTypes([model]).build(); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |     // eslint-disable-next-line node/no-extraneous-require
 | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |     hasRW = require('@strapi/strapi/lib/utils/ee').features.isEnabled('review-workflows'); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |     requests.public = createRequest({ strapi }); | 
					
						
							|  |  |  |     requests.admin = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |     defaultStage = await strapi.query(STAGE_MODEL_UID).create({ | 
					
						
							|  |  |  |       data: { name: 'Stage' }, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-01-31 15:30:19 +01:00
										 |  |  |     secondStage = await strapi.query(STAGE_MODEL_UID).create({ | 
					
						
							|  |  |  |       data: { name: 'Stage 2' }, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |     testWorkflow = await strapi.query(WORKFLOW_MODEL_UID).create({ | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |       data: { | 
					
						
							|  |  |  |         uid: 'workflow', | 
					
						
							| 
									
										
										
										
											2023-01-31 15:30:19 +01:00
										 |  |  |         stages: [defaultStage.id, secondStage.id], | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |     await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |   beforeEach(async () => { | 
					
						
							|  |  |  |     testWorkflow = await strapi.query(WORKFLOW_MODEL_UID).update({ | 
					
						
							|  |  |  |       where: { id: testWorkflow.id }, | 
					
						
							|  |  |  |       data: { | 
					
						
							|  |  |  |         uid: 'workflow', | 
					
						
							|  |  |  |         stages: [defaultStage.id, secondStage.id], | 
					
						
							|  |  |  |       }, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     await updateContentType(productUID, { | 
					
						
							|  |  |  |       components: [], | 
					
						
							|  |  |  |       contentType: model, | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |   describe('Get workflows', () => { | 
					
						
							| 
									
										
										
										
											2023-01-27 15:11:43 +01:00
										 |  |  |     test("It shouldn't be available for public", async () => { | 
					
						
							| 
									
										
										
										
											2023-01-27 11:54:12 +01:00
										 |  |  |       const res = await requests.public.get('/admin/review-workflows/workflows'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(401); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(Array.isArray(res.body)).toBeFalsy(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     test('It should be available for every connected users (admin)', async () => { | 
					
						
							|  |  |  |       const res = await requests.admin.get('/admin/review-workflows/workflows'); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(200); | 
					
						
							| 
									
										
										
										
											2023-01-30 12:26:06 +01:00
										 |  |  |         expect(Array.isArray(res.body.data)).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         // Why 2 workflows ? One added by the test, the other one should be the default workflow added in bootstrap
 | 
					
						
							|  |  |  |         expect(res.body.data).toHaveLength(2); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(Array.isArray(res.body)).toBeFalsy(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Get one workflow', () => { | 
					
						
							| 
									
										
										
										
											2023-01-27 15:11:43 +01:00
										 |  |  |     test("It shouldn't be available for public", async () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |       const res = await requests.public.get(`/admin/review-workflows/workflows/${testWorkflow.id}`); | 
					
						
							| 
									
										
										
										
											2023-01-27 11:54:12 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(401); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(res.body.data).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     test('It should be available for every connected users (admin)', async () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |       const res = await requests.admin.get(`/admin/review-workflows/workflows/${testWorkflow.id}`); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(200); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |         expect(res.body.data).toBeInstanceOf(Object); | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         expect(res.body.data).toEqual(testWorkflow); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |         expect(res.body.data).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Get workflow stages', () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |     test("It shouldn't be available for public", async () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |       const res = await requests.public.get( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}?populate=stages` | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(401); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(res.body.data).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     test('It should be available for every connected users (admin)', async () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |       const res = await requests.admin.get( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}?populate=stages` | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(200); | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         expect(res.body.data).toBeInstanceOf(Object); | 
					
						
							|  |  |  |         expect(res.body.data.stages).toBeInstanceOf(Array); | 
					
						
							|  |  |  |         expect(res.body.data.stages).toHaveLength(2); | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(Array.isArray(res.body)).toBeFalsy(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Get stages', () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |     test("It shouldn't be available for public", async () => { | 
					
						
							|  |  |  |       const res = await requests.public.get( | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages` | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(401); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(res.body.data).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     test('It should be available for every connected users (admin)', async () => { | 
					
						
							|  |  |  |       const res = await requests.admin.get( | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages` | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(200); | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |         expect(Array.isArray(res.body.data)).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2023-01-31 15:30:19 +01:00
										 |  |  |         expect(res.body.data).toHaveLength(2); | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(Array.isArray(res.body)).toBeFalsy(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Get stage by id', () => { | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |     test("It shouldn't be available for public", async () => { | 
					
						
							|  |  |  |       const res = await requests.public.get( | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages/${secondStage.id}` | 
					
						
							| 
									
										
										
										
											2023-01-31 12:26:58 +01:00
										 |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(401); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(res.body.data).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     test('It should be available for every connected users (admin)', async () => { | 
					
						
							|  |  |  |       const res = await requests.admin.get( | 
					
						
							| 
									
										
										
										
											2023-01-31 20:16:53 +01:00
										 |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages/${secondStage.id}` | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(res.status).toBe(200); | 
					
						
							|  |  |  |         expect(res.body.data).toBeInstanceOf(Object); | 
					
						
							| 
									
										
										
										
											2023-01-31 15:30:19 +01:00
										 |  |  |         expect(res.body.data).toEqual(secondStage); | 
					
						
							| 
									
										
										
										
											2023-01-26 12:17:36 +01:00
										 |  |  |       } else { | 
					
						
							|  |  |  |         expect(res.status).toBe(404); | 
					
						
							|  |  |  |         expect(res.body.data).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Replace stages of a workflow', () => { | 
					
						
							|  |  |  |     let stagesUpdateData; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     beforeEach(() => { | 
					
						
							|  |  |  |       stagesUpdateData = [ | 
					
						
							|  |  |  |         defaultStage, | 
					
						
							|  |  |  |         { id: secondStage.id, name: 'new_name' }, | 
					
						
							|  |  |  |         { name: 'new stage' }, | 
					
						
							|  |  |  |       ]; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 13:28:44 +02:00
										 |  |  |     test("It should assign a default color to stages if they don't have one", async () => { | 
					
						
							|  |  |  |       await requests.admin.put(`/admin/review-workflows/workflows/${testWorkflow.id}/stages`, { | 
					
						
							|  |  |  |         body: { | 
					
						
							| 
									
										
										
										
											2023-04-28 16:58:57 +02:00
										 |  |  |           data: [defaultStage, { id: secondStage.id, name: secondStage.name, color: '#000000' }], | 
					
						
							| 
									
										
										
										
											2023-04-24 13:28:44 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const workflowRes = await requests.admin.get( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}?populate=*` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(workflowRes.status).toBe(200); | 
					
						
							|  |  |  |       expect(workflowRes.body.data.stages[0].color).toBe('#4945FF'); | 
					
						
							|  |  |  |       expect(workflowRes.body.data.stages[1].color).toBe('#000000'); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |     test("It shouldn't be available for public", async () => { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |       const stagesRes = await requests.public.put( | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages`, | 
					
						
							| 
									
										
										
										
											2023-04-24 13:28:44 +02:00
										 |  |  |         { body: { data: stagesUpdateData } } | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |       const workflowRes = await requests.public.get( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}` | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |         expect(stagesRes.status).toBe(401); | 
					
						
							|  |  |  |         expect(workflowRes.status).toBe(401); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |         expect(stagesRes.status).toBe(404); | 
					
						
							|  |  |  |         expect(stagesRes.body.data).toBeUndefined(); | 
					
						
							|  |  |  |         expect(workflowRes.status).toBe(404); | 
					
						
							|  |  |  |         expect(workflowRes.body.data).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     test('It should be available for every connected users (admin)', async () => { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |       const stagesRes = await requests.admin.put( | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages`, | 
					
						
							| 
									
										
										
										
											2023-02-13 11:06:19 +01:00
										 |  |  |         { body: { data: stagesUpdateData } } | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |       const workflowRes = await requests.admin.get( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}?populate=*` | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |         expect(stagesRes.status).toBe(200); | 
					
						
							|  |  |  |         expect(stagesRes.body.data).toBeInstanceOf(Object); | 
					
						
							|  |  |  |         expect(stagesRes.body.data.id).toEqual(testWorkflow.id); | 
					
						
							|  |  |  |         expect(workflowRes.status).toBe(200); | 
					
						
							|  |  |  |         expect(workflowRes.body.data).toBeInstanceOf(Object); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages).toBeInstanceOf(Array); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages[0]).toMatchObject(stagesUpdateData[0]); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages[1]).toMatchObject(stagesUpdateData[1]); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages[2]).toMatchObject({ | 
					
						
							|  |  |  |           id: expect.any(Number), | 
					
						
							|  |  |  |           ...stagesUpdateData[2], | 
					
						
							|  |  |  |         }); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |       } else { | 
					
						
							| 
									
										
										
										
											2023-02-07 14:49:29 +01:00
										 |  |  |         expect(stagesRes.status).toBe(404); | 
					
						
							|  |  |  |         expect(stagesRes.body.data).toBeUndefined(); | 
					
						
							|  |  |  |         expect(workflowRes.status).toBe(404); | 
					
						
							|  |  |  |         expect(workflowRes.body.data).toBeUndefined(); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-02-10 16:09:20 +01:00
										 |  |  |     test('It should throw an error if trying to delete all stages in a workflow', async () => { | 
					
						
							|  |  |  |       const stagesRes = await requests.admin.put( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages`, | 
					
						
							|  |  |  |         { body: { data: [] } } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |       const workflowRes = await requests.admin.get( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}?populate=*` | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(stagesRes.status).toBe(400); | 
					
						
							|  |  |  |         expect(stagesRes.body.error).toBeDefined(); | 
					
						
							|  |  |  |         expect(stagesRes.body.error.name).toEqual('ApplicationError'); | 
					
						
							|  |  |  |         expect(stagesRes.body.error.message).toBeDefined(); | 
					
						
							|  |  |  |         expect(workflowRes.status).toBe(200); | 
					
						
							|  |  |  |         expect(workflowRes.body.data).toBeInstanceOf(Object); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages).toBeInstanceOf(Array); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages[0]).toMatchObject({ id: defaultStage.id }); | 
					
						
							|  |  |  |         expect(workflowRes.body.data.stages[1]).toMatchObject({ id: secondStage.id }); | 
					
						
							|  |  |  |       } else { | 
					
						
							|  |  |  |         expect(stagesRes.status).toBe(404); | 
					
						
							|  |  |  |         expect(stagesRes.body.data).toBeUndefined(); | 
					
						
							|  |  |  |         expect(workflowRes.status).toBe(404); | 
					
						
							|  |  |  |         expect(workflowRes.body.data).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-04-24 11:52:05 +02:00
										 |  |  |     test('It should throw an error if trying to create more than 200 stages', async () => { | 
					
						
							|  |  |  |       const stagesRes = await requests.admin.put( | 
					
						
							|  |  |  |         `/admin/review-workflows/workflows/${testWorkflow.id}/stages`, | 
					
						
							|  |  |  |         { body: { data: Array(201).fill({ name: 'new stage' }) } } | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (hasRW) { | 
					
						
							|  |  |  |         expect(stagesRes.status).toBe(400); | 
					
						
							|  |  |  |         expect(stagesRes.body.error).toBeDefined(); | 
					
						
							|  |  |  |         expect(stagesRes.body.error.name).toEqual('ValidationError'); | 
					
						
							|  |  |  |         expect(stagesRes.body.error.message).toBeDefined(); | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-02-03 16:47:34 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:38:02 +00:00
										 |  |  |   describe('Enabling/Disabling review workflows on a content type', () => { | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  |     beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |       await createEntry(productUID, { name: 'Product' }); | 
					
						
							|  |  |  |       await createEntry(productUID, { name: 'Product 1' }); | 
					
						
							|  |  |  |       await createEntry(productUID, { name: 'Product 2' }); | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  |     test('when enabled on a content type, entries of this type should be added to the first stage of the workflow', async () => { | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |       await updateContentType(productUID, { | 
					
						
							|  |  |  |         components: [], | 
					
						
							|  |  |  |         contentType: { ...model, reviewWorkflows: true }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:38:02 +00:00
										 |  |  |       await restart(); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |       const response = await requests.admin({ | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: `/content-type-builder/content-types/api::product.product`, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  |       expect(response.body.data.schema.reviewWorkflows).toBeTruthy(); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 14:03:30 +02:00
										 |  |  |       const { | 
					
						
							|  |  |  |         body: { results }, | 
					
						
							|  |  |  |       } = await requests.admin({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: '/content-manager/collection-types/api::product.product', | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 14:03:30 +02:00
										 |  |  |       expect(results.length).toEqual(3); | 
					
						
							|  |  |  |       for (let i = 0; i < results.length; i += 1) { | 
					
						
							|  |  |  |         expect(results[i][ENTITY_STAGE_ATTRIBUTE]).toBeDefined(); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |       } | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     test('when disabled entries in the content type should be removed from any workflow stage', async () => { | 
					
						
							| 
									
										
										
										
											2023-03-08 16:38:02 +00:00
										 |  |  |       await updateContentType(productUID, { | 
					
						
							|  |  |  |         components: [], | 
					
						
							|  |  |  |         contentType: { ...model, reviewWorkflows: false }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |       await restart(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |       const response = await requests.admin({ | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: `/content-type-builder/content-types/api::product.product`, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-03-09 10:25:50 +00:00
										 |  |  |       expect(response.body.data.schema.reviewWorkflows).toBeFalsy(); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-17 14:03:30 +02:00
										 |  |  |       const { | 
					
						
							|  |  |  |         body: { results }, | 
					
						
							|  |  |  |       } = await requests.admin({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: '/content-manager/collection-types/api::product.product', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       for (let i = 0; i < results.length; i += 1) { | 
					
						
							|  |  |  |         expect(results[i][ENTITY_STAGE_ATTRIBUTE]).toBeUndefined(); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 11:52:05 +02:00
										 |  |  |   describe('Update a stage on an entity', () => { | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |     describe('Review Workflow is enabled', () => { | 
					
						
							|  |  |  |       beforeAll(async () => { | 
					
						
							|  |  |  |         await updateContentType(productUID, { | 
					
						
							|  |  |  |           components: [], | 
					
						
							|  |  |  |           contentType: { ...model, reviewWorkflows: true }, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         await restart(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       test('Should update the accordingly on an entity', async () => { | 
					
						
							|  |  |  |         const entry = await createEntry(productUID, { name: 'Product' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const response = await requests.admin({ | 
					
						
							|  |  |  |           method: 'PUT', | 
					
						
							|  |  |  |           url: `/admin/content-manager/collection-types/${productUID}/${entry.id}/stage`, | 
					
						
							|  |  |  |           body: { | 
					
						
							|  |  |  |             data: { id: secondStage.id }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(response.status).toEqual(200); | 
					
						
							| 
									
										
										
										
											2023-04-12 14:18:40 +01:00
										 |  |  |         expect(response.body.data[ENTITY_STAGE_ATTRIBUTE]).toEqual( | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |           expect.objectContaining({ id: secondStage.id }) | 
					
						
							|  |  |  |         ); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-04-11 17:28:17 +02:00
										 |  |  |       test('Should throw an error if stage does not exist', async () => { | 
					
						
							|  |  |  |         const entry = await createEntry(productUID, { name: 'Product' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const response = await requests.admin({ | 
					
						
							|  |  |  |           method: 'PUT', | 
					
						
							|  |  |  |           url: `/admin/content-manager/collection-types/${productUID}/${entry.id}/stage`, | 
					
						
							|  |  |  |           body: { | 
					
						
							|  |  |  |             data: { id: 1234 }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(response.status).toEqual(400); | 
					
						
							|  |  |  |         expect(response.body.error).toBeDefined(); | 
					
						
							|  |  |  |         expect(response.body.error.name).toEqual('ApplicationError'); | 
					
						
							|  |  |  |         expect(response.body.error.message).toEqual('Selected stage does not exist'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-03-21 14:59:20 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |     describe('Review Workflow is disabled', () => { | 
					
						
							|  |  |  |       beforeAll(async () => { | 
					
						
							|  |  |  |         await updateContentType(productUID, { | 
					
						
							|  |  |  |           components: [], | 
					
						
							|  |  |  |           contentType: { ...model, reviewWorkflows: false }, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         await restart(); | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       test('Should not update the entity', async () => { | 
					
						
							|  |  |  |         const entry = await createEntry(productUID, { name: 'Product' }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         const response = await requests.admin({ | 
					
						
							|  |  |  |           method: 'PUT', | 
					
						
							|  |  |  |           url: `/admin/content-manager/collection-types/${productUID}/${entry.id}/stage`, | 
					
						
							|  |  |  |           body: { | 
					
						
							|  |  |  |             data: { id: secondStage.id }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         expect(response.status).toEqual(400); | 
					
						
							|  |  |  |         expect(response.body.error).toBeDefined(); | 
					
						
							|  |  |  |         expect(response.body.error.name).toBe('ApplicationError'); | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-03-08 16:12:53 +00:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-03-22 11:55:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Creating an entity in a review workflow content type', () => { | 
					
						
							|  |  |  |     beforeAll(async () => { | 
					
						
							|  |  |  |       await updateContentType(productUID, { | 
					
						
							|  |  |  |         components: [], | 
					
						
							|  |  |  |         contentType: { ...model, reviewWorkflows: true }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       await restart(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('when review workflows is enabled on a content type, new entries should be added to the first stage of the default workflow', async () => { | 
					
						
							| 
									
										
										
										
											2023-03-23 12:48:17 +00:00
										 |  |  |       const adminResponse = await createEntry(productUID, { name: 'Product' }); | 
					
						
							|  |  |  |       expect(await adminResponse[ENTITY_STAGE_ATTRIBUTE].name).toEqual(defaultStages[0].name); | 
					
						
							| 
									
										
										
										
											2023-03-22 11:55:22 +00:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-03-29 15:33:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-24 18:55:09 +02:00
										 |  |  |   //FIXME Flaky test
 | 
					
						
							|  |  |  |   describe.skip('Deleting a stage when content already exists', () => { | 
					
						
							| 
									
										
										
										
											2023-03-29 15:33:10 +01:00
										 |  |  |     test('When content exists in a review stage and this stage is deleted, the content should be moved to the nearest available stage', async () => { | 
					
						
							| 
									
										
										
										
											2023-04-25 10:22:35 +02:00
										 |  |  |       const products = await findAll(productUID); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-25 16:21:13 +02:00
										 |  |  |       // Move half of the entries to the last stage,
 | 
					
						
							|  |  |  |       // and the other half to the first stage
 | 
					
						
							| 
									
										
										
										
											2023-04-25 10:22:35 +02:00
										 |  |  |       await mapAsync(products.results, async (entity) => | 
					
						
							|  |  |  |         updateEntry(productUID, entity.id, { | 
					
						
							|  |  |  |           [ENTITY_STAGE_ATTRIBUTE]: entity.id % 2 ? defaultStage.id : secondStage.id, | 
					
						
							|  |  |  |         }) | 
					
						
							| 
									
										
										
										
											2023-04-18 09:26:13 +01:00
										 |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-03-29 15:33:10 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-04-25 10:22:35 +02:00
										 |  |  |       // Delete last stage stage of the default workflow
 | 
					
						
							|  |  |  |       await requests.admin.put(`/admin/review-workflows/workflows/${testWorkflow.id}/stages`, { | 
					
						
							|  |  |  |         body: { data: [defaultStage] }, | 
					
						
							| 
									
										
										
										
											2023-03-29 15:33:10 +01:00
										 |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       // Expect the content in these stages to be moved to the nearest available stage
 | 
					
						
							|  |  |  |       const productsAfter = await findAll(productUID); | 
					
						
							| 
									
										
										
										
											2023-04-25 10:22:35 +02:00
										 |  |  |       for (const entry of productsAfter.results) { | 
					
						
							|  |  |  |         expect(entry[ENTITY_STAGE_ATTRIBUTE].name).toEqual(defaultStage.name); | 
					
						
							|  |  |  |       } | 
					
						
							| 
									
										
										
										
											2023-03-29 15:33:10 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2023-01-25 19:13:48 +01:00
										 |  |  | }); |