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

101 lines
2.5 KiB
JavaScript

const uploadService = require('../Upload');
describe('Upload service', () => {
describe('formatFileInfo', () => {
test('Generates hash', () => {
const fileData = {
filename: 'File Name.png',
type: 'image/png',
size: 1000 * 1000,
};
expect(uploadService.formatFileInfo(fileData)).toMatchObject({
name: 'File Name.png',
hash: expect.stringContaining('File_Name'),
ext: '.png',
mime: 'image/png',
size: 1000,
});
});
test('Replaces reserved and unsafe characters for URLs and files in hash', () => {
const fileData = {
filename: 'File%&Näme<>:"|?*.png',
type: 'image/png',
size: 1000 * 1000,
};
expect(uploadService.formatFileInfo(fileData)).toMatchObject({
name: 'File%&Näme<>:"|?*.png',
hash: expect.stringContaining('File_and_Naeme'),
ext: '.png',
mime: 'image/png',
size: 1000,
});
});
test('Overrides name with fileInfo', () => {
const fileData = {
filename: 'File Name.png',
type: 'image/png',
size: 1000 * 1000,
};
const fileInfo = {
name: 'Custom File Name.png',
};
expect(uploadService.formatFileInfo(fileData, fileInfo)).toMatchObject({
name: fileInfo.name,
hash: expect.stringContaining('Custom_File_Name'),
ext: '.png',
mime: 'image/png',
size: 1000,
});
});
test('Sets alternativeText and caption', () => {
const fileData = {
filename: 'File Name.png',
type: 'image/png',
size: 1000 * 1000,
};
const fileInfo = {
alternativeText: 'some text',
caption: 'caption this',
};
expect(uploadService.formatFileInfo(fileData, fileInfo)).toMatchObject({
name: 'File Name.png',
caption: fileInfo.caption,
alternativeText: fileInfo.alternativeText,
hash: expect.stringContaining('File_Name'),
ext: '.png',
mime: 'image/png',
size: 1000,
});
});
test('Set a path folder', () => {
const fileData = {
filename: 'File Name.png',
type: 'image/png',
size: 1000 * 1000,
};
const fileMetas = {
path: 'folder',
};
expect(uploadService.formatFileInfo(fileData, {}, fileMetas)).toMatchObject({
name: 'File Name.png',
ext: '.png',
mime: 'image/png',
size: 1000,
path: expect.stringContaining('folder'),
});
});
});
});