| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							| 
									
										
										
										
											2020-10-27 11:27:17 +01:00
										 |  |  | const path = require('path'); | 
					
						
							| 
									
										
										
										
											2023-01-18 17:23:14 +01:00
										 |  |  | const get = require('lodash/get'); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | // Helpers.
 | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createContentAPIRequest } = require('api-tests/request'); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  | const builder = createTestBuilder(); | 
					
						
							| 
									
										
										
										
											2022-08-23 10:56:36 +02:00
										 |  |  | const data = { dogs: [] }; | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  | let strapi; | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  | let rq; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  | const dogModel = { | 
					
						
							|  |  |  |   displayName: 'Dog', | 
					
						
							|  |  |  |   singularName: 'dog', | 
					
						
							|  |  |  |   pluralName: 'dogs', | 
					
						
							|  |  |  |   kind: 'collectionType', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     profilePicture: { | 
					
						
							|  |  |  |       type: 'media', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-07 17:59:59 +01:00
										 |  |  | const todoListModel = { | 
					
						
							|  |  |  |   displayName: 'TodoList', | 
					
						
							|  |  |  |   singularName: 'todolist', | 
					
						
							|  |  |  |   pluralName: 'todolists', | 
					
						
							|  |  |  |   kind: 'collectionType', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     title: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     todo: { | 
					
						
							|  |  |  |       displayName: 'todo', | 
					
						
							|  |  |  |       type: 'component', | 
					
						
							|  |  |  |       repeatable: true, | 
					
						
							|  |  |  |       component: 'default.todo', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const todoComponent = { | 
					
						
							|  |  |  |   displayName: 'Todo', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     docs: { | 
					
						
							|  |  |  |       allowedTypes: ['images', 'files', 'videos', 'audios'], | 
					
						
							|  |  |  |       type: 'media', | 
					
						
							|  |  |  |       multiple: true, | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |     task: { | 
					
						
							|  |  |  |       type: 'string', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 10:56:36 +02:00
										 |  |  | describe('Upload plugin', () => { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |   beforeAll(async () => { | 
					
						
							| 
									
										
										
										
											2022-11-07 17:59:59 +01:00
										 |  |  |     await builder | 
					
						
							|  |  |  |       .addContentType(dogModel) | 
					
						
							|  |  |  |       .addComponent(todoComponent) | 
					
						
							|  |  |  |       .addContentType(todoListModel) | 
					
						
							|  |  |  |       .build(); | 
					
						
							| 
									
										
										
										
											2020-11-30 20:20:36 +01:00
										 |  |  |     strapi = await createStrapiInstance(); | 
					
						
							| 
									
										
										
										
											2023-02-09 11:35:50 +01:00
										 |  |  |     rq = createContentAPIRequest({ strapi }); | 
					
						
							| 
									
										
										
										
											2021-03-26 20:15:38 +01:00
										 |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |     await builder.cleanup(); | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |   describe('Create', () => { | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |     test('Simple image upload', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/upload', | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |         formData: { | 
					
						
							| 
									
										
										
										
											2022-03-30 16:26:09 +02:00
										 |  |  |           files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')), | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(Array.isArray(res.body)).toBe(true); | 
					
						
							|  |  |  |       expect(res.body.length).toBe(1); | 
					
						
							|  |  |  |       expect(res.body[0]).toEqual( | 
					
						
							|  |  |  |         expect.objectContaining({ | 
					
						
							|  |  |  |           id: expect.anything(), | 
					
						
							| 
									
										
										
										
											2020-06-25 11:13:42 +03:00
										 |  |  |           name: 'rec.jpg', | 
					
						
							|  |  |  |           ext: '.jpg', | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  |           mime: 'image/jpeg', | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |           hash: expect.any(String), | 
					
						
							| 
									
										
										
										
											2019-10-24 15:07:02 +01:00
										 |  |  |           size: expect.any(Number), | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  |           width: expect.any(Number), | 
					
						
							|  |  |  |           height: expect.any(Number), | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |           url: expect.any(String), | 
					
						
							|  |  |  |           provider: 'local', | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Rejects when no files are provided', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const res = await rq({ method: 'POST', url: '/upload', formData: {} }); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |       expect(res.statusCode).toBe(400); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     test('Generates a thumbnail on large enough files', async () => { | 
					
						
							| 
									
										
										
										
											2020-11-17 15:38:41 +01:00
										 |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/upload', | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  |         formData: { | 
					
						
							| 
									
										
										
										
											2022-03-30 16:26:09 +02:00
										 |  |  |           files: fs.createReadStream(path.join(__dirname, '../utils/thumbnail_target.png')), | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(Array.isArray(res.body)).toBe(true); | 
					
						
							|  |  |  |       expect(res.body.length).toBe(1); | 
					
						
							|  |  |  |       expect(res.body[0]).toEqual( | 
					
						
							|  |  |  |         expect.objectContaining({ | 
					
						
							|  |  |  |           id: expect.anything(), | 
					
						
							| 
									
										
										
										
											2020-06-25 11:13:42 +03:00
										 |  |  |           name: 'thumbnail_target.png', | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  |           ext: '.png', | 
					
						
							|  |  |  |           mime: 'image/png', | 
					
						
							|  |  |  |           hash: expect.any(String), | 
					
						
							|  |  |  |           size: expect.any(Number), | 
					
						
							|  |  |  |           width: expect.any(Number), | 
					
						
							|  |  |  |           height: expect.any(Number), | 
					
						
							|  |  |  |           url: expect.any(String), | 
					
						
							|  |  |  |           provider: 'local', | 
					
						
							| 
									
										
										
										
											2020-03-09 09:04:32 +01:00
										 |  |  |           formats: { | 
					
						
							|  |  |  |             thumbnail: { | 
					
						
							| 
									
										
										
										
											2020-06-25 11:13:42 +03:00
										 |  |  |               name: 'thumbnail_thumbnail_target.png', | 
					
						
							| 
									
										
										
										
											2020-03-09 09:04:32 +01:00
										 |  |  |               hash: expect.any(String), | 
					
						
							|  |  |  |               ext: '.png', | 
					
						
							|  |  |  |               mime: 'image/png', | 
					
						
							|  |  |  |               size: expect.any(Number), | 
					
						
							|  |  |  |               width: expect.any(Number), | 
					
						
							|  |  |  |               height: expect.any(Number), | 
					
						
							|  |  |  |               url: expect.any(String), | 
					
						
							| 
									
										
										
										
											2020-05-07 09:28:26 +02:00
										 |  |  |               path: null, | 
					
						
							| 
									
										
										
										
											2020-03-09 09:04:32 +01:00
										 |  |  |             }, | 
					
						
							| 
									
										
										
										
											2020-03-06 17:00:25 +01:00
										 |  |  |           }, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |   describe('Read', () => { | 
					
						
							|  |  |  |     test('Get files', async () => { | 
					
						
							|  |  |  |       const getRes = await rq({ method: 'GET', url: '/upload/files' }); | 
					
						
							| 
									
										
										
										
											2021-10-07 17:23:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |       expect(getRes.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(getRes.body).toEqual( | 
					
						
							|  |  |  |         expect.arrayContaining([ | 
					
						
							|  |  |  |           expect.objectContaining({ | 
					
						
							|  |  |  |             id: expect.anything(), | 
					
						
							|  |  |  |             url: expect.any(String), | 
					
						
							|  |  |  |           }), | 
					
						
							|  |  |  |         ]) | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2021-10-07 17:23:42 +02:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-01-18 17:23:14 +01:00
										 |  |  |     test('Get one file', async () => { | 
					
						
							| 
									
										
										
										
											2023-01-19 16:36:24 +01:00
										 |  |  |       const dogEntity = await strapi.entityService.create('api::dog.dog', { | 
					
						
							|  |  |  |         data: {}, | 
					
						
							|  |  |  |         files: { | 
					
						
							|  |  |  |           profilePicture: { | 
					
						
							|  |  |  |             path: path.join(__dirname, '../utils/rec.jpg'), | 
					
						
							|  |  |  |             name: 'rec', | 
					
						
							|  |  |  |             type: 'jpg', | 
					
						
							|  |  |  |             size: 0, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |         populate: 'profilePicture', | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const getRes = await rq({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: `/upload/files/${dogEntity.profilePicture.id}`, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2023-01-18 17:23:14 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |       expect(getRes.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(getRes.body).toEqual( | 
					
						
							|  |  |  |         expect.objectContaining({ | 
					
						
							|  |  |  |           id: expect.anything(), | 
					
						
							|  |  |  |           url: expect.any(String), | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							| 
									
										
										
										
											2023-01-19 16:36:24 +01:00
										 |  |  |       await strapi.entityService.delete('api::dog.dog', dogEntity.id); | 
					
						
							| 
									
										
										
										
											2023-01-23 16:54:57 +01:00
										 |  |  |       await strapi.entityService.delete('plugin::upload.file', dogEntity.profilePicture.id); | 
					
						
							| 
									
										
										
										
											2023-01-18 17:23:14 +01:00
										 |  |  |     }); | 
					
						
							| 
									
										
										
										
											2021-10-07 17:23:42 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |   describe('Create an entity with a file', () => { | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |     test('With an image', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |         url: '/dogs?populate=*', | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |         formData: { | 
					
						
							|  |  |  |           data: '{}', | 
					
						
							| 
									
										
										
										
											2022-03-30 16:26:09 +02:00
										 |  |  |           'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')), | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           attributes: { | 
					
						
							|  |  |  |             profilePicture: { | 
					
						
							|  |  |  |               data: { | 
					
						
							|  |  |  |                 id: expect.anything(), | 
					
						
							| 
									
										
										
										
											2022-03-21 23:49:27 -04:00
										 |  |  |                 attributes: { | 
					
						
							|  |  |  |                   provider: 'local', | 
					
						
							|  |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           id: expect.anything(), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2022-08-23 10:56:36 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |       data.dogs.push(res.body); | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('With a pdf', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |         url: '/dogs?populate=*', | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |         formData: { | 
					
						
							|  |  |  |           data: '{}', | 
					
						
							| 
									
										
										
										
											2022-03-30 16:26:09 +02:00
										 |  |  |           'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')), | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           attributes: { | 
					
						
							|  |  |  |             profilePicture: { | 
					
						
							|  |  |  |               data: { | 
					
						
							|  |  |  |                 id: expect.anything(), | 
					
						
							| 
									
										
										
										
											2022-03-21 23:49:27 -04:00
										 |  |  |                 attributes: { | 
					
						
							|  |  |  |                   provider: 'local', | 
					
						
							|  |  |  |                 }, | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |               }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           id: expect.anything(), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							| 
									
										
										
										
											2022-08-23 10:56:36 +02:00
										 |  |  |       data.dogs.push(res.body); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2023-01-18 17:23:14 +01:00
										 |  |  |     test('File should have related field', async () => { | 
					
						
							|  |  |  |       const fileId = get(data, 'dogs[0].data.attributes.profilePicture.data.id'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(fileId).toBeDefined(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const getRes = await rq({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: `/upload/files/${fileId}`, | 
					
						
							|  |  |  |         qs: { populate: '*' }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(getRes.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(getRes.body).toEqual( | 
					
						
							|  |  |  |         expect.objectContaining({ | 
					
						
							|  |  |  |           id: fileId, | 
					
						
							|  |  |  |           related: [expect.any(Object)], | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							| 
									
										
										
										
											2022-08-23 10:56:36 +02:00
										 |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-07 17:59:59 +01:00
										 |  |  |   describe('Create an entity with a component with a file', () => { | 
					
						
							|  |  |  |     test('With an image', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/todolists', | 
					
						
							|  |  |  |         formData: { | 
					
						
							|  |  |  |           data: '{"title":"Test todolist title","todo":[{"task":"First todo"},{"task":"Second todo"}]}', | 
					
						
							|  |  |  |           'files.todo.0.docs': fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           attributes: { | 
					
						
							|  |  |  |             title: 'Test todolist title', | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |           id: expect.anything(), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       const newlyCreatedTodolist = await rq({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: `/todolists/${res.body.data.id}`, | 
					
						
							|  |  |  |         qs: { | 
					
						
							|  |  |  |           populate: ['todo', 'todo.docs'], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(newlyCreatedTodolist.body).toBeDefined(); | 
					
						
							|  |  |  |       expect(newlyCreatedTodolist.body).toMatchObject({ | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           attributes: { | 
					
						
							|  |  |  |             title: 'Test todolist title', | 
					
						
							|  |  |  |             todo: [ | 
					
						
							|  |  |  |               { | 
					
						
							|  |  |  |                 id: expect.anything(), | 
					
						
							|  |  |  |                 task: 'First todo', | 
					
						
							|  |  |  |                 docs: { | 
					
						
							|  |  |  |                   data: [ | 
					
						
							|  |  |  |                     { | 
					
						
							|  |  |  |                       id: expect.anything(), | 
					
						
							|  |  |  |                       attributes: { | 
					
						
							|  |  |  |                         mime: 'image/jpeg', | 
					
						
							|  |  |  |                         name: 'rec.jpg', | 
					
						
							|  |  |  |                       }, | 
					
						
							|  |  |  |                     }, | 
					
						
							|  |  |  |                   ], | 
					
						
							|  |  |  |                 }, | 
					
						
							|  |  |  |               }, | 
					
						
							|  |  |  |               expect.any(Object), | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-23 10:56:36 +02:00
										 |  |  |   // see https://github.com/strapi/strapi/issues/14125
 | 
					
						
							|  |  |  |   describe('File relations are correctly removed', () => { | 
					
						
							|  |  |  |     test('Update an entity with a file correctly removes the relation between the entity and its old file', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'PUT', | 
					
						
							|  |  |  |         url: `/dogs/${data.dogs[0].data.id}?populate=*`, | 
					
						
							|  |  |  |         formData: { | 
					
						
							|  |  |  |           data: '{}', | 
					
						
							|  |  |  |           'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/strapi.jpg')), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       expect(res.statusCode).toBe(200); | 
					
						
							|  |  |  |       expect(res.body.data.attributes.profilePicture.data.id).not.toBe( | 
					
						
							|  |  |  |         data.dogs[0].data.attributes.profilePicture.data.id | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       data.dogs[0] = res.body; | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Update a file with an entity correctly removes the relation between the entity and its old file', async () => { | 
					
						
							|  |  |  |       const fileId = data.dogs[1].data.attributes.profilePicture.data.id; | 
					
						
							|  |  |  |       await strapi.entityService.update('plugin::upload.file', fileId, { | 
					
						
							|  |  |  |         data: { | 
					
						
							|  |  |  |           related: [ | 
					
						
							|  |  |  |             { | 
					
						
							|  |  |  |               id: data.dogs[0].data.id, | 
					
						
							|  |  |  |               __type: 'api::dog.dog', | 
					
						
							|  |  |  |               __pivot: { field: 'profilePicture' }, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           ], | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'GET', | 
					
						
							|  |  |  |         url: `/dogs/${data.dogs[0].data.id}?populate=*`, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  |       expect(res.body.data.attributes.profilePicture.data.id).toBe(fileId); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       data.dogs[0] = res.body; | 
					
						
							| 
									
										
										
										
											2022-02-28 17:44:18 +01:00
										 |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							| 
									
										
										
										
											2019-08-05 09:10:00 +02:00
										 |  |  | }); |