Andrey Hohutkin 94e031eba5
Fixes for Media Library upload (#5971)
* Fix formatFileInfo

Keep original file name including extension

* Fix file name for generated media

File name was not set for all generated media: thumbnail and resized pictures.

* Generate hash based on file name without extension

* cleanup PR

Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>

* Fix unit tests
Exclude temporarily unit test 'Replaces reserved and unsafe characters for URLs and files in hash' because it did not work properly ever.

* Remove path delimiter from filename because it is correct character for file path

* Fix e2e

* Continue fixes for e2e

* Fix createRequest content type

* Correct thumbnail name in e2e test

Co-authored-by: Alexandre BODIN <alexandrebodin@users.noreply.github.com>
Co-authored-by: Alexandre Bodin <bodin.alex@gmail.com>
Co-authored-by: Andrey Hohutkin <none@none>
2020-06-25 10:13:42 +02:00

141 lines
3.9 KiB
JavaScript

'use strict';
const fs = require('fs');
// Helpers.
const { registerAndLogin } = require('../../../test/helpers/auth');
const { createAuthRequest } = require('../../../test/helpers/request');
let rq;
describe('Upload plugin end to end tests', () => {
beforeAll(async () => {
const token = await registerAndLogin();
rq = createAuthRequest(token);
}, 60000);
describe('GET /upload/settings => Get settings for an environment', () => {
test('Returns the settings', async () => {
const res = await rq.get('/upload/settings');
expect(res.statusCode).toBe(200);
expect(res.body).toEqual({
data: {
sizeOptimization: true,
responsiveDimensions: true,
},
});
});
});
describe('PUT /upload/settings/:environment', () => {
test('Updates an environment config correctly', async () => {
const updateRes = await rq.put('/upload/settings', {
body: {
sizeOptimization: true,
responsiveDimensions: true,
},
});
expect(updateRes.statusCode).toBe(200);
expect(updateRes.body).toEqual({
data: {
sizeOptimization: true,
responsiveDimensions: true,
},
});
const getRes = await rq.get('/upload/settings');
expect(getRes.statusCode).toBe(200);
expect(getRes.body).toEqual({
data: {
sizeOptimization: true,
responsiveDimensions: true,
},
});
});
});
describe('POST /upload => Upload a file', () => {
test('Simple image upload', async () => {
const res = await rq.post('/upload', {
formData: {
files: fs.createReadStream(__dirname + '/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('Rejects when no files are provided', async () => {
const res = await rq.post('/upload', {
formData: {},
});
expect(res.statusCode).toBe(400);
});
test('Generates a thumbnail on large enough files', async () => {
const res = await rq.post('/upload', {
formData: {
files: fs.createReadStream(__dirname + '/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),
width: expect.any(Number),
height: expect.any(Number),
url: expect.any(String),
path: null,
},
},
})
);
});
});
describe('GET /upload/files => Find files', () => {});
describe('GET /upload/files/count => Count available files', () => {});
describe('GET /upload/files/:id => Find one file', () => {});
describe('GET /upload/search/:id => Search files', () => {});
describe('DELETE /upload/files/:id => Delete a file ', () => {});
});