| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-29 11:11:46 +02:00
										 |  |  | const { createTestBuilder } = require('../../../../test/helpers/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('../../../../test/helpers/strapi'); | 
					
						
							|  |  |  | const { createAuthRequest } = require('../../../../test/helpers/request'); | 
					
						
							|  |  |  | const modelsUtils = require('../../../../test/helpers/models'); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | let rq; | 
					
						
							|  |  |  | let data = { | 
					
						
							|  |  |  |   dogs: [], | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dogModel = { | 
					
						
							|  |  |  |   draftAndPublish: false, | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     name: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |       required: false, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   connection: 'default', | 
					
						
							|  |  |  |   name: 'dog', | 
					
						
							|  |  |  |   description: '', | 
					
						
							|  |  |  |   collectionName: '', | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dogs = [ | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     name: null, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  |   { | 
					
						
							|  |  |  |     name: 'Atos', | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | ]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | const restart = async () => { | 
					
						
							|  |  |  |   await strapi.destroy(); | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |   strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | describe('Migration - required attribute', () => { | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     await builder | 
					
						
							|  |  |  |       .addContentType(dogModel) | 
					
						
							|  |  |  |       .addFixtures(dogModel.name, dogs) | 
					
						
							|  |  |  |       .build(); | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     data.dogs = builder.sanitizedFixturesFor(dogModel.name, strapi); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +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-10-14 19:04:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   describe('Required: false -> true', () => { | 
					
						
							|  |  |  |     test('Can be null before migration', async () => { | 
					
						
							|  |  |  |       let { body } = await rq({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  |         url: '/content-manager/collection-types/application::dog.dog', | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  |       expect(body.results.length).toBe(2); | 
					
						
							|  |  |  |       const dogWithNameNull = body.results.find(dog => dog.name === null); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |       expect(dogWithNameNull).toBeTruthy(); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Cannot create an entry with null after migration', async () => { | 
					
						
							|  |  |  |       // remove null values otherwise the migration would fail
 | 
					
						
							| 
									
										
										
										
											2020-11-23 14:43:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |       const { body } = await rq({ | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |         method: 'PUT', | 
					
						
							| 
									
										
										
										
											2020-11-03 13:42:01 +01:00
										 |  |  |         url: `/content-manager/collection-types/application::dog.dog/${data.dogs[0].id}`, | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |         body: { name: 'Nelson' }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |       data.dogs[0] = body; | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // migration
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const schema = await modelsUtils.getContentTypeSchema(dogModel.name, { strapi }); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |       schema.attributes.name.required = true; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       await modelsUtils.modifyContentType(schema, { strapi }); | 
					
						
							|  |  |  |       await restart(); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Try to create an entry with null
 | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  |         url: '/content-manager/collection-types/application::dog.dog', | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |         body: { name: null }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       expect(res.body.message).toBe('ValidationError'); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('Required: true -> false', () => { | 
					
						
							|  |  |  |     test('Can create an entry with null after migration', async () => { | 
					
						
							|  |  |  |       // migration
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const schema = await modelsUtils.getContentTypeSchema(dogModel.name, { strapi }); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |       schema.attributes.name.required = false; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       await modelsUtils.modifyContentType(schema, { strapi }); | 
					
						
							|  |  |  |       await restart(); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // Try to create an entry with null
 | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							| 
									
										
										
										
											2020-11-02 17:19:42 +01:00
										 |  |  |         url: `/content-manager/collection-types/application::dog.dog`, | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |         method: 'POST', | 
					
						
							|  |  |  |         body: { name: null }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ name: null }); | 
					
						
							| 
									
										
										
										
											2020-11-06 14:27:57 +01:00
										 |  |  |       data.dogs.push(res.body); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-10-14 19:04:22 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | }); |