| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const fs = require('fs'); | 
					
						
							|  |  |  | const path = require('path'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Helpers.
 | 
					
						
							| 
									
										
										
										
											2023-04-05 10:32:20 +02:00
										 |  |  | const { createTestBuilder } = require('api-tests/builder'); | 
					
						
							|  |  |  | const { createStrapiInstance } = require('api-tests/strapi'); | 
					
						
							|  |  |  | const { createAuthRequest } = require('api-tests/request'); | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | const builder = createTestBuilder(); | 
					
						
							|  |  |  | let strapi; | 
					
						
							|  |  |  | let rq; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const dogModel = { | 
					
						
							|  |  |  |   displayName: 'Dog', | 
					
						
							|  |  |  |   singularName: 'dog', | 
					
						
							|  |  |  |   pluralName: 'dogs', | 
					
						
							|  |  |  |   kind: 'collectionType', | 
					
						
							|  |  |  |   attributes: { | 
					
						
							|  |  |  |     profilePicture: { | 
					
						
							|  |  |  |       type: 'media', | 
					
						
							|  |  |  |     }, | 
					
						
							|  |  |  |   }, | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | describe('Upload', () => { | 
					
						
							|  |  |  |   beforeAll(async () => { | 
					
						
							|  |  |  |     await builder.addContentType(dogModel).build(); | 
					
						
							|  |  |  |     strapi = await createStrapiInstance(); | 
					
						
							|  |  |  |     rq = await createAuthRequest({ strapi }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   afterAll(async () => { | 
					
						
							|  |  |  |     await strapi.destroy(); | 
					
						
							|  |  |  |     await builder.cleanup(); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   describe('POST /upload => Upload a file', () => { | 
					
						
							|  |  |  |     test('Simple image upload', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/upload', | 
					
						
							|  |  |  |         formData: { | 
					
						
							|  |  |  |           files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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(), | 
					
						
							|  |  |  |           name: 'rec.jpg', | 
					
						
							|  |  |  |           ext: '.jpg', | 
					
						
							|  |  |  |           mime: 'image/jpeg', | 
					
						
							|  |  |  |           hash: expect.any(String), | 
					
						
							|  |  |  |           size: expect.any(Number), | 
					
						
							|  |  |  |           width: expect.any(Number), | 
					
						
							|  |  |  |           height: expect.any(Number), | 
					
						
							|  |  |  |           url: expect.any(String), | 
					
						
							|  |  |  |           provider: 'local', | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     test('Generates a thumbnail on large enough files', async () => { | 
					
						
							|  |  |  |       const res = await rq({ | 
					
						
							|  |  |  |         method: 'POST', | 
					
						
							|  |  |  |         url: '/upload', | 
					
						
							|  |  |  |         formData: { | 
					
						
							|  |  |  |           files: fs.createReadStream(path.join(__dirname, '../utils/thumbnail_target.png')), | 
					
						
							|  |  |  |         }, | 
					
						
							|  |  |  |       }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       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(), | 
					
						
							|  |  |  |           name: 'thumbnail_target.png', | 
					
						
							|  |  |  |           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', | 
					
						
							|  |  |  |           formats: { | 
					
						
							|  |  |  |             thumbnail: { | 
					
						
							|  |  |  |               name: 'thumbnail_thumbnail_target.png', | 
					
						
							|  |  |  |               hash: expect.any(String), | 
					
						
							|  |  |  |               ext: '.png', | 
					
						
							|  |  |  |               mime: 'image/png', | 
					
						
							|  |  |  |               size: expect.any(Number), | 
					
						
							| 
									
										
										
										
											2024-03-07 18:31:55 +01:00
										 |  |  |               sizeInBytes: expect.any(Number), | 
					
						
							| 
									
										
										
										
											2022-04-06 11:36:13 +02:00
										 |  |  |               width: expect.any(Number), | 
					
						
							|  |  |  |               height: expect.any(Number), | 
					
						
							|  |  |  |               url: expect.any(String), | 
					
						
							|  |  |  |               path: null, | 
					
						
							|  |  |  |             }, | 
					
						
							|  |  |  |           }, | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  |       ); | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |   }); | 
					
						
							|  |  |  | }); |