mirror of
https://github.com/strapi/strapi.git
synced 2025-11-06 04:51:54 +00:00
replace path by location + add location to files
This commit is contained in:
parent
906f625c30
commit
587b62ea7b
@ -95,5 +95,10 @@ module.exports = {
|
|||||||
target: 'plugin::upload.folder',
|
target: 'plugin::upload.folder',
|
||||||
inversedBy: 'files',
|
inversedBy: 'files',
|
||||||
},
|
},
|
||||||
|
location: {
|
||||||
|
type: 'string',
|
||||||
|
min: 1,
|
||||||
|
required: true,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
@ -45,7 +45,7 @@ module.exports = {
|
|||||||
target: 'plugin::upload.file',
|
target: 'plugin::upload.file',
|
||||||
mappedBy: 'folder',
|
mappedBy: 'folder',
|
||||||
},
|
},
|
||||||
path: {
|
location: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
min: 1,
|
min: 1,
|
||||||
required: true,
|
required: true,
|
||||||
|
|||||||
@ -39,10 +39,10 @@ module.exports = {
|
|||||||
|
|
||||||
await validateCreateFolder(body);
|
await validateCreateFolder(body);
|
||||||
|
|
||||||
const { setPathAndUID } = getService('folder');
|
const { setLocationAndUID } = getService('folder');
|
||||||
|
|
||||||
// TODO: wrap with a transaction
|
// TODO: wrap with a transaction
|
||||||
const enrichFolder = pipeAsync(setPathAndUID, setCreatorFields({ user }));
|
const enrichFolder = pipeAsync(setLocationAndUID, setCreatorFields({ user }));
|
||||||
const enrichedFolder = await enrichFolder(body);
|
const enrichedFolder = await enrichFolder(body);
|
||||||
|
|
||||||
const folder = await strapi.entityService.create(folderModel, {
|
const folder = await strapi.entityService.create(folderModel, {
|
||||||
|
|||||||
27
packages/core/upload/server/services/__tests__/file.test.js
Normal file
27
packages/core/upload/server/services/__tests__/file.test.js
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const { getLocation } = require('../file');
|
||||||
|
|
||||||
|
const folderLocation = '/9bc2352b-e29b-4ba3-810f-7b91033222de';
|
||||||
|
|
||||||
|
describe('file', () => {
|
||||||
|
describe('getLocation', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
global.strapi = {
|
||||||
|
entityService: {
|
||||||
|
findOne: jest.fn(() => ({ location: folderLocation })),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
test.each([
|
||||||
|
[[1, 'myFile.txt'], folderLocation],
|
||||||
|
[[undefined, 'myFile.txt'], '/'],
|
||||||
|
[[null, 'myFile.txt'], '/'],
|
||||||
|
])('inputs %s should give %s', async (args, expectedResult) => {
|
||||||
|
const result = await getLocation(...args);
|
||||||
|
|
||||||
|
expect(result).toBe(expectedResult);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,38 +1,38 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const { setPathAndUID } = require('../folder');
|
const { setLocationAndUID } = require('../folder');
|
||||||
|
|
||||||
const folderUID = '9bc2352b-e29b-4ba3-810f-7b91033222de';
|
const folderUID = '9bc2352b-e29b-4ba3-810f-7b91033222de';
|
||||||
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
||||||
const rootPathRegex = /^\/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
const rootLocationRegex = /^\/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
||||||
const folderPathRegex = new RegExp(
|
const folderLocationRegex = new RegExp(
|
||||||
'^/' + folderUID + '/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
|
'^/' + folderUID + '/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
|
||||||
'i'
|
'i'
|
||||||
);
|
);
|
||||||
|
|
||||||
describe('folder', () => {
|
describe('folder', () => {
|
||||||
describe('setPathAndUID', () => {
|
describe('setLocationAndUID', () => {
|
||||||
beforeAll(() => {
|
beforeAll(() => {
|
||||||
global.strapi = {
|
global.strapi = {
|
||||||
entityService: {
|
entityService: {
|
||||||
findOne: jest.fn(() => ({ uid: `/${folderUID}` })),
|
findOne: jest.fn(() => ({ location: `/${folderUID}` })),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
test.each([
|
test.each([
|
||||||
[{ parent: 1 }, folderPathRegex],
|
[{ parent: 1 }, folderLocationRegex],
|
||||||
[{}, rootPathRegex],
|
[{}, rootLocationRegex],
|
||||||
[{ parent: null }, rootPathRegex],
|
[{ parent: null }, rootLocationRegex],
|
||||||
])('inputs %s', async (folder, expectedPath) => {
|
])('inputs %s', async (folder, expectedLocation) => {
|
||||||
const clonedFolder = { ...folder };
|
const clonedFolder = { ...folder };
|
||||||
const result = await setPathAndUID(clonedFolder);
|
const result = await setLocationAndUID(clonedFolder);
|
||||||
|
|
||||||
expect(result).toBe(clonedFolder);
|
expect(result).toBe(clonedFolder);
|
||||||
expect(result).toMatchObject({
|
expect(result).toMatchObject({
|
||||||
...folder,
|
...folder,
|
||||||
uid: expect.stringMatching(uuidRegex),
|
uid: expect.stringMatching(uuidRegex),
|
||||||
path: expect.stringMatching(expectedPath),
|
location: expect.stringMatching(expectedLocation),
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -3,6 +3,20 @@
|
|||||||
const uploadService = require('../upload')({});
|
const uploadService = require('../upload')({});
|
||||||
|
|
||||||
describe('Upload service', () => {
|
describe('Upload service', () => {
|
||||||
|
beforeAll(() => {
|
||||||
|
global.strapi = {
|
||||||
|
plugins: {
|
||||||
|
upload: {
|
||||||
|
services: {
|
||||||
|
file: {
|
||||||
|
getLocation: () => '/a-location',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
describe('formatFileInfo', () => {
|
describe('formatFileInfo', () => {
|
||||||
test('Generates hash', async () => {
|
test('Generates hash', async () => {
|
||||||
const fileData = {
|
const fileData = {
|
||||||
|
|||||||
15
packages/core/upload/server/services/file.js
Normal file
15
packages/core/upload/server/services/file.js
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const folderModel = 'plugin::upload.folder';
|
||||||
|
|
||||||
|
const getLocation = async folderId => {
|
||||||
|
if (!folderId) return '/';
|
||||||
|
|
||||||
|
const parentFolder = await strapi.entityService.findOne(folderModel, folderId);
|
||||||
|
|
||||||
|
return parentFolder.location;
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
getLocation,
|
||||||
|
};
|
||||||
@ -23,17 +23,17 @@ const joinBy = (joint, ...args) => {
|
|||||||
|
|
||||||
const generateUID = () => uuid();
|
const generateUID = () => uuid();
|
||||||
|
|
||||||
const setPathAndUID = async folder => {
|
const setLocationAndUID = async folder => {
|
||||||
const uid = generateUID();
|
const uid = generateUID();
|
||||||
let parentPath = '/';
|
let parentLocation = '/';
|
||||||
if (folder.parent) {
|
if (folder.parent) {
|
||||||
const parentFolder = await strapi.entityService.findOne(folderModel, folder.parent);
|
const parentFolder = await strapi.entityService.findOne(folderModel, folder.parent);
|
||||||
parentPath = parentFolder.path;
|
parentLocation = parentFolder.location;
|
||||||
}
|
}
|
||||||
|
|
||||||
return Object.assign(folder, {
|
return Object.assign(folder, {
|
||||||
uid,
|
uid,
|
||||||
path: joinBy('/', parentPath, uid),
|
location: joinBy('/', parentLocation, uid),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -61,5 +61,5 @@ const exists = async (params = {}) => {
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
exists,
|
exists,
|
||||||
deleteByIds,
|
deleteByIds,
|
||||||
setPathAndUID,
|
setLocationAndUID,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -4,10 +4,12 @@ const provider = require('./provider');
|
|||||||
const upload = require('./upload');
|
const upload = require('./upload');
|
||||||
const imageManipulation = require('./image-manipulation');
|
const imageManipulation = require('./image-manipulation');
|
||||||
const folder = require('./folder');
|
const folder = require('./folder');
|
||||||
|
const file = require('./file');
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
provider,
|
provider,
|
||||||
upload,
|
upload,
|
||||||
folder,
|
folder,
|
||||||
|
file,
|
||||||
'image-manipulation': imageManipulation,
|
'image-manipulation': imageManipulation,
|
||||||
};
|
};
|
||||||
|
|||||||
@ -64,6 +64,8 @@ module.exports = ({ strapi }) => ({
|
|||||||
},
|
},
|
||||||
|
|
||||||
async formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
|
async formatFileInfo({ filename, type, size }, fileInfo = {}, metas = {}) {
|
||||||
|
const fileService = getService('file');
|
||||||
|
|
||||||
const ext = path.extname(filename);
|
const ext = path.extname(filename);
|
||||||
const basename = path.basename(fileInfo.name || filename, ext);
|
const basename = path.basename(fileInfo.name || filename, ext);
|
||||||
const usedName = fileInfo.name || filename;
|
const usedName = fileInfo.name || filename;
|
||||||
@ -73,6 +75,7 @@ module.exports = ({ strapi }) => ({
|
|||||||
alternativeText: fileInfo.alternativeText,
|
alternativeText: fileInfo.alternativeText,
|
||||||
caption: fileInfo.caption,
|
caption: fileInfo.caption,
|
||||||
folder: fileInfo.folder,
|
folder: fileInfo.folder,
|
||||||
|
location: await fileService.getLocation(fileInfo.folder),
|
||||||
hash: generateFileName(basename),
|
hash: generateFileName(basename),
|
||||||
ext,
|
ext,
|
||||||
mime: type,
|
mime: type,
|
||||||
@ -205,12 +208,15 @@ module.exports = ({ strapi }) => ({
|
|||||||
throw new NotFoundError();
|
throw new NotFoundError();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const fileService = getService('file');
|
||||||
|
|
||||||
const newName = _.isNil(name) ? dbFile.name : name;
|
const newName = _.isNil(name) ? dbFile.name : name;
|
||||||
const newInfos = {
|
const newInfos = {
|
||||||
name: newName,
|
name: newName,
|
||||||
alternativeText: _.isNil(alternativeText) ? dbFile.alternativeText : alternativeText,
|
alternativeText: _.isNil(alternativeText) ? dbFile.alternativeText : alternativeText,
|
||||||
caption: _.isNil(caption) ? dbFile.caption : caption,
|
caption: _.isNil(caption) ? dbFile.caption : caption,
|
||||||
folder: _.isUndefined(folder) ? dbFile.folder : folder,
|
folder: _.isUndefined(folder) ? dbFile.folder : folder,
|
||||||
|
location: _.isUndefined(folder) ? dbFile.path : await fileService.getLocation(folder),
|
||||||
};
|
};
|
||||||
|
|
||||||
return this.update(id, newInfos, { user });
|
return this.update(id, newInfos, { user });
|
||||||
|
|||||||
387
packages/core/upload/tests/admin/file-folder.test.e2e.js
Normal file
387
packages/core/upload/tests/admin/file-folder.test.e2e.js
Normal file
@ -0,0 +1,387 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
||||||
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
||||||
|
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
||||||
|
|
||||||
|
let strapi;
|
||||||
|
let rq;
|
||||||
|
let data = {
|
||||||
|
folders: [],
|
||||||
|
files: [],
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('File', () => {
|
||||||
|
const builder = createTestBuilder();
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
strapi = await createStrapiInstance();
|
||||||
|
rq = await createAuthRequest({ strapi });
|
||||||
|
|
||||||
|
// create 2 folders
|
||||||
|
for (let i = 1; i <= 2; i += 1) {
|
||||||
|
const folderRes = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/upload/folders',
|
||||||
|
body: { name: `my folder ${i}` },
|
||||||
|
});
|
||||||
|
data.folders.push(folderRes.body.data);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/upload/folders/batch-delete',
|
||||||
|
body: {
|
||||||
|
ids: data.folders.map(f => f.id),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
await strapi.destroy();
|
||||||
|
await builder.cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('create', () => {
|
||||||
|
test('Can create a file at root level', 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);
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body[0].id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
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',
|
||||||
|
folder: null,
|
||||||
|
location: '/',
|
||||||
|
});
|
||||||
|
|
||||||
|
data.files.push(file);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Can create a file inside a folder', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/upload',
|
||||||
|
formData: {
|
||||||
|
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
||||||
|
fileInfo: JSON.stringify({
|
||||||
|
folder: data.folders[0].id,
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(Array.isArray(res.body)).toBe(true);
|
||||||
|
expect(res.body.length).toBe(1);
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body[0].id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
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',
|
||||||
|
folder: { id: data.folders[0].id },
|
||||||
|
location: data.folders[0].location,
|
||||||
|
});
|
||||||
|
|
||||||
|
data.files.push(file);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("Cannot create a file inside a folder that doesn't exist", async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: '/upload',
|
||||||
|
formData: {
|
||||||
|
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
||||||
|
fileInfo: JSON.stringify({
|
||||||
|
folder: '1234', // id that doesn't exist
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error.message).toBe("the folder doesn't exist");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Update info', () => {
|
||||||
|
describe('Move a file from a folder to another folder', () => {
|
||||||
|
test('when replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[1].id}`,
|
||||||
|
formData: {
|
||||||
|
files: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
||||||
|
fileInfo: JSON.stringify({ folder: data.folders[1].id }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({ id: data.files[1].id });
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body.id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
id: expect.anything(),
|
||||||
|
name: 'rec.pdf',
|
||||||
|
ext: '.jpg',
|
||||||
|
mime: 'application/pdf',
|
||||||
|
hash: expect.any(String),
|
||||||
|
size: expect.any(Number),
|
||||||
|
width: expect.any(Number),
|
||||||
|
height: expect.any(Number),
|
||||||
|
url: expect.any(String),
|
||||||
|
provider: 'local',
|
||||||
|
folder: { id: data.folders[1].id },
|
||||||
|
location: data.folders[1].location,
|
||||||
|
});
|
||||||
|
data.files[1] = file;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[1].id}`,
|
||||||
|
formData: {
|
||||||
|
fileInfo: JSON.stringify({ folder: data.folders[0].id }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({ id: data.files[1].id });
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body.id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
id: expect.anything(),
|
||||||
|
name: 'rec.pdf',
|
||||||
|
ext: '.jpg',
|
||||||
|
mime: 'application/pdf',
|
||||||
|
hash: expect.any(String),
|
||||||
|
size: expect.any(Number),
|
||||||
|
width: expect.any(Number),
|
||||||
|
height: expect.any(Number),
|
||||||
|
url: expect.any(String),
|
||||||
|
provider: 'local',
|
||||||
|
folder: { id: data.folders[0].id },
|
||||||
|
location: data.folders[0].location,
|
||||||
|
});
|
||||||
|
data.files[1] = file;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
describe('Move a file from root level to a folder', () => {
|
||||||
|
test('when replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[0].id}`,
|
||||||
|
formData: {
|
||||||
|
files: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
||||||
|
fileInfo: JSON.stringify({ folder: data.folders[0].id }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({ id: data.files[0].id });
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body.id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
id: expect.anything(),
|
||||||
|
name: 'rec.pdf',
|
||||||
|
ext: '.jpg',
|
||||||
|
mime: 'application/pdf',
|
||||||
|
hash: expect.any(String),
|
||||||
|
size: expect.any(Number),
|
||||||
|
width: expect.any(Number),
|
||||||
|
height: expect.any(Number),
|
||||||
|
url: expect.any(String),
|
||||||
|
provider: 'local',
|
||||||
|
folder: { id: data.folders[0].id },
|
||||||
|
location: data.folders[0].location,
|
||||||
|
});
|
||||||
|
data.files[0] = file;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[1].id}`,
|
||||||
|
formData: {
|
||||||
|
fileInfo: JSON.stringify({ folder: data.folders[1].id }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({ id: data.files[1].id });
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body.id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
id: expect.anything(),
|
||||||
|
name: 'rec.pdf',
|
||||||
|
ext: '.jpg',
|
||||||
|
mime: 'application/pdf',
|
||||||
|
hash: expect.any(String),
|
||||||
|
size: expect.any(Number),
|
||||||
|
width: expect.any(Number),
|
||||||
|
height: expect.any(Number),
|
||||||
|
url: expect.any(String),
|
||||||
|
provider: 'local',
|
||||||
|
folder: { id: data.folders[1].id },
|
||||||
|
location: data.folders[1].location,
|
||||||
|
});
|
||||||
|
data.files[1] = file;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('Move a file from folder to the root level', () => {
|
||||||
|
test('when replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[0].id}`,
|
||||||
|
formData: {
|
||||||
|
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
||||||
|
fileInfo: JSON.stringify({ folder: null }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({ id: data.files[0].id });
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body.id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
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',
|
||||||
|
folder: null,
|
||||||
|
location: '/',
|
||||||
|
});
|
||||||
|
data.files[0] = file;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('without replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[1].id}`,
|
||||||
|
formData: {
|
||||||
|
fileInfo: JSON.stringify({ folder: null }),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toMatchObject({ id: data.files[1].id });
|
||||||
|
|
||||||
|
const { body: file } = await rq({
|
||||||
|
method: 'GET',
|
||||||
|
url: `/upload/files/${res.body.id}`,
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(file).toMatchObject({
|
||||||
|
id: expect.anything(),
|
||||||
|
name: 'rec.pdf',
|
||||||
|
ext: '.jpg',
|
||||||
|
mime: 'application/pdf',
|
||||||
|
hash: expect.any(String),
|
||||||
|
size: expect.any(Number),
|
||||||
|
width: expect.any(Number),
|
||||||
|
height: expect.any(Number),
|
||||||
|
url: expect.any(String),
|
||||||
|
provider: 'local',
|
||||||
|
folder: null,
|
||||||
|
location: '/',
|
||||||
|
});
|
||||||
|
data.files[1] = file;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe("Cannot create a file inside a folder that doesn't exist", () => {
|
||||||
|
test('when replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[1].id}`,
|
||||||
|
formData: {
|
||||||
|
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
||||||
|
fileInfo: JSON.stringify({
|
||||||
|
folder: '1234', // id that doesn't exist
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error.message).toBe("the folder doesn't exist");
|
||||||
|
});
|
||||||
|
|
||||||
|
test('whithout replacing the file', async () => {
|
||||||
|
const res = await rq({
|
||||||
|
method: 'POST',
|
||||||
|
url: `/upload?id=${data.files[1].id}`,
|
||||||
|
formData: {
|
||||||
|
fileInfo: JSON.stringify({
|
||||||
|
folder: '1234', // id that doesn't exist
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(res.status).toBe(400);
|
||||||
|
expect(res.body.error.message).toBe("the folder doesn't exist");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
109
packages/core/upload/tests/admin/file-image.test.e2e.js
Normal file
109
packages/core/upload/tests/admin/file-image.test.e2e.js
Normal file
@ -0,0 +1,109 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
// Helpers.
|
||||||
|
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
||||||
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
||||||
|
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
||||||
|
|
||||||
|
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),
|
||||||
|
width: expect.any(Number),
|
||||||
|
height: expect.any(Number),
|
||||||
|
url: expect.any(String),
|
||||||
|
path: null,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,379 +1,62 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
const fs = require('fs');
|
|
||||||
const path = require('path');
|
|
||||||
|
|
||||||
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
||||||
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
||||||
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
||||||
|
|
||||||
|
const builder = createTestBuilder();
|
||||||
let strapi;
|
let strapi;
|
||||||
let rq;
|
let rq;
|
||||||
let data = {
|
|
||||||
folders: [],
|
const dogModel = {
|
||||||
files: [],
|
displayName: 'Dog',
|
||||||
|
singularName: 'dog',
|
||||||
|
pluralName: 'dogs',
|
||||||
|
kind: 'collectionType',
|
||||||
|
attributes: {
|
||||||
|
profilePicture: {
|
||||||
|
type: 'media',
|
||||||
|
},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('File', () => {
|
describe('Upload', () => {
|
||||||
const builder = createTestBuilder();
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
beforeAll(async () => {
|
||||||
|
await builder.addContentType(dogModel).build();
|
||||||
strapi = await createStrapiInstance();
|
strapi = await createStrapiInstance();
|
||||||
rq = await createAuthRequest({ strapi });
|
rq = await createAuthRequest({ strapi });
|
||||||
|
|
||||||
// create 2 folders
|
|
||||||
for (let i = 1; i <= 2; i += 1) {
|
|
||||||
const folderRes = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: '/upload/folders',
|
|
||||||
body: { name: `my folder ${i}` },
|
|
||||||
});
|
|
||||||
data.folders.push(folderRes.body.data);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: '/upload/folders/batch-delete',
|
|
||||||
body: {
|
|
||||||
ids: data.folders.map(f => f.id),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
await strapi.destroy();
|
await strapi.destroy();
|
||||||
await builder.cleanup();
|
await builder.cleanup();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('create', () => {
|
describe('Create', () => {
|
||||||
test('Can create a file at root level', async () => {
|
test('Rejects when no files are provided', async () => {
|
||||||
const res = await rq({
|
const res = await rq({ method: 'POST', url: '/upload', formData: {} });
|
||||||
method: 'POST',
|
expect(res.statusCode).toBe(400);
|
||||||
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);
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body[0].id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
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',
|
|
||||||
folder: null,
|
|
||||||
});
|
|
||||||
|
|
||||||
data.files.push(file);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('Can create a file inside a folder', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: '/upload',
|
|
||||||
formData: {
|
|
||||||
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
|
||||||
fileInfo: JSON.stringify({
|
|
||||||
folder: data.folders[0].id,
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(Array.isArray(res.body)).toBe(true);
|
|
||||||
expect(res.body.length).toBe(1);
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body[0].id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
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',
|
|
||||||
folder: { id: data.folders[0].id },
|
|
||||||
});
|
|
||||||
|
|
||||||
data.files.push(file);
|
|
||||||
});
|
|
||||||
|
|
||||||
test("Cannot create a file inside a folder that doesn't exist", async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: '/upload',
|
|
||||||
formData: {
|
|
||||||
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
|
||||||
fileInfo: JSON.stringify({
|
|
||||||
folder: '1234', // id that doesn't exist
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body.error.message).toBe("the folder doesn't exist");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Update info', () => {
|
describe('Read', () => {
|
||||||
describe('Move a file from a folder to another folder', () => {
|
test('GET /upload/files => Find files', async () => {
|
||||||
test('when replacing the file', async () => {
|
const getRes = await rq({ method: 'GET', url: '/upload/files' });
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[1].id}`,
|
|
||||||
formData: {
|
|
||||||
files: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
|
||||||
fileInfo: JSON.stringify({ folder: data.folders[1].id }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
expect(getRes.statusCode).toBe(200);
|
||||||
expect(res.body).toMatchObject({ id: data.files[1].id });
|
expect(getRes.body).toEqual({
|
||||||
|
results: expect.arrayContaining([
|
||||||
const { body: file } = await rq({
|
expect.objectContaining({
|
||||||
method: 'GET',
|
id: expect.anything(),
|
||||||
url: `/upload/files/${res.body.id}`,
|
url: expect.any(String),
|
||||||
});
|
}),
|
||||||
|
]),
|
||||||
expect(file).toMatchObject({
|
pagination: {
|
||||||
id: expect.anything(),
|
page: expect.any(Number),
|
||||||
name: 'rec.pdf',
|
pageSize: expect.any(Number),
|
||||||
ext: '.jpg',
|
pageCount: expect.any(Number),
|
||||||
mime: 'application/pdf',
|
total: expect.any(Number),
|
||||||
hash: expect.any(String),
|
},
|
||||||
size: expect.any(Number),
|
|
||||||
width: expect.any(Number),
|
|
||||||
height: expect.any(Number),
|
|
||||||
url: expect.any(String),
|
|
||||||
provider: 'local',
|
|
||||||
folder: { id: data.folders[1].id },
|
|
||||||
});
|
|
||||||
data.files[1] = file;
|
|
||||||
});
|
|
||||||
|
|
||||||
test('without replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[1].id}`,
|
|
||||||
formData: {
|
|
||||||
fileInfo: JSON.stringify({ folder: data.folders[0].id }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toMatchObject({ id: data.files[1].id });
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body.id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
id: expect.anything(),
|
|
||||||
name: 'rec.pdf',
|
|
||||||
ext: '.jpg',
|
|
||||||
mime: 'application/pdf',
|
|
||||||
hash: expect.any(String),
|
|
||||||
size: expect.any(Number),
|
|
||||||
width: expect.any(Number),
|
|
||||||
height: expect.any(Number),
|
|
||||||
url: expect.any(String),
|
|
||||||
provider: 'local',
|
|
||||||
folder: { id: data.folders[0].id },
|
|
||||||
});
|
|
||||||
data.files[1] = file;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
describe('Move a file from root level to a folder', () => {
|
|
||||||
test('when replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[0].id}`,
|
|
||||||
formData: {
|
|
||||||
files: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
|
||||||
fileInfo: JSON.stringify({ folder: data.folders[0].id }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toMatchObject({ id: data.files[0].id });
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body.id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
id: expect.anything(),
|
|
||||||
name: 'rec.pdf',
|
|
||||||
ext: '.jpg',
|
|
||||||
mime: 'application/pdf',
|
|
||||||
hash: expect.any(String),
|
|
||||||
size: expect.any(Number),
|
|
||||||
width: expect.any(Number),
|
|
||||||
height: expect.any(Number),
|
|
||||||
url: expect.any(String),
|
|
||||||
provider: 'local',
|
|
||||||
folder: { id: data.folders[0].id },
|
|
||||||
});
|
|
||||||
data.files[0] = file;
|
|
||||||
});
|
|
||||||
|
|
||||||
test('without replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[1].id}`,
|
|
||||||
formData: {
|
|
||||||
fileInfo: JSON.stringify({ folder: data.folders[1].id }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toMatchObject({ id: data.files[1].id });
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body.id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
id: expect.anything(),
|
|
||||||
name: 'rec.pdf',
|
|
||||||
ext: '.jpg',
|
|
||||||
mime: 'application/pdf',
|
|
||||||
hash: expect.any(String),
|
|
||||||
size: expect.any(Number),
|
|
||||||
width: expect.any(Number),
|
|
||||||
height: expect.any(Number),
|
|
||||||
url: expect.any(String),
|
|
||||||
provider: 'local',
|
|
||||||
folder: { id: data.folders[1].id },
|
|
||||||
});
|
|
||||||
data.files[1] = file;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('Move a file from folder to the root level', () => {
|
|
||||||
test('when replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[0].id}`,
|
|
||||||
formData: {
|
|
||||||
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
|
||||||
fileInfo: JSON.stringify({ folder: null }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toMatchObject({ id: data.files[0].id });
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body.id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
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',
|
|
||||||
folder: null,
|
|
||||||
});
|
|
||||||
data.files[0] = file;
|
|
||||||
});
|
|
||||||
|
|
||||||
test('without replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[1].id}`,
|
|
||||||
formData: {
|
|
||||||
fileInfo: JSON.stringify({ folder: null }),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toMatchObject({ id: data.files[1].id });
|
|
||||||
|
|
||||||
const { body: file } = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: `/upload/files/${res.body.id}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(file).toMatchObject({
|
|
||||||
id: expect.anything(),
|
|
||||||
name: 'rec.pdf',
|
|
||||||
ext: '.jpg',
|
|
||||||
mime: 'application/pdf',
|
|
||||||
hash: expect.any(String),
|
|
||||||
size: expect.any(Number),
|
|
||||||
width: expect.any(Number),
|
|
||||||
height: expect.any(Number),
|
|
||||||
url: expect.any(String),
|
|
||||||
provider: 'local',
|
|
||||||
folder: null,
|
|
||||||
});
|
|
||||||
data.files[1] = file;
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe("Cannot create a file inside a folder that doesn't exist", () => {
|
|
||||||
test('when replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[1].id}`,
|
|
||||||
formData: {
|
|
||||||
files: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
|
||||||
fileInfo: JSON.stringify({
|
|
||||||
folder: '1234', // id that doesn't exist
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
console.log('res.body', res.body);
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body.error.message).toBe("the folder doesn't exist");
|
|
||||||
});
|
|
||||||
|
|
||||||
test('whithout replacing the file', async () => {
|
|
||||||
const res = await rq({
|
|
||||||
method: 'POST',
|
|
||||||
url: `/upload?id=${data.files[1].id}`,
|
|
||||||
formData: {
|
|
||||||
fileInfo: JSON.stringify({
|
|
||||||
folder: '1234', // id that doesn't exist
|
|
||||||
}),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body.error.message).toBe("the folder doesn't exist");
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@ -15,8 +15,8 @@ let data = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
const uuidRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
||||||
const rootPathRegex = /^\/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
const rootLocationRegex = /^\/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
|
||||||
const getFolderPathRegex = uid =>
|
const getFolderLocationRegex = uid =>
|
||||||
new RegExp(
|
new RegExp(
|
||||||
'^/' + uid + '/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
|
'^/' + uid + '/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
|
||||||
'i'
|
'i'
|
||||||
@ -51,12 +51,12 @@ describe('Folder', () => {
|
|||||||
id: expect.anything(),
|
id: expect.anything(),
|
||||||
name: 'folder 1',
|
name: 'folder 1',
|
||||||
uid: expect.stringMatching(uuidRegex),
|
uid: expect.stringMatching(uuidRegex),
|
||||||
path: expect.stringMatching(rootPathRegex),
|
location: expect.stringMatching(rootLocationRegex),
|
||||||
createdAt: expect.anything(),
|
createdAt: expect.anything(),
|
||||||
updatedAt: expect.anything(),
|
updatedAt: expect.anything(),
|
||||||
parent: null,
|
parent: null,
|
||||||
});
|
});
|
||||||
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
|
expect(res.body.data.uid).toBe(res.body.data.location.split('/').pop());
|
||||||
|
|
||||||
data.folders.push(omit('parent', res.body.data));
|
data.folders.push(omit('parent', res.body.data));
|
||||||
});
|
});
|
||||||
@ -75,12 +75,12 @@ describe('Folder', () => {
|
|||||||
id: expect.anything(),
|
id: expect.anything(),
|
||||||
name: 'folder-2',
|
name: 'folder-2',
|
||||||
uid: expect.stringMatching(uuidRegex),
|
uid: expect.stringMatching(uuidRegex),
|
||||||
path: expect.stringMatching(getFolderPathRegex(data.folders[0].uid)),
|
location: expect.stringMatching(getFolderLocationRegex(data.folders[0].uid)),
|
||||||
createdAt: expect.anything(),
|
createdAt: expect.anything(),
|
||||||
updatedAt: expect.anything(),
|
updatedAt: expect.anything(),
|
||||||
parent: data.folders[0],
|
parent: data.folders[0],
|
||||||
});
|
});
|
||||||
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
|
expect(res.body.data.uid).toBe(res.body.data.location.split('/').pop());
|
||||||
|
|
||||||
data.folders.push(omit('parent', res.body.data));
|
data.folders.push(omit('parent', res.body.data));
|
||||||
});
|
});
|
||||||
@ -188,7 +188,10 @@ describe('Folder', () => {
|
|||||||
username: null,
|
username: null,
|
||||||
},
|
},
|
||||||
files: { count: 0 },
|
files: { count: 0 },
|
||||||
parent: pick(['createdAt', 'id', 'name', 'path', 'uid', 'updatedAt'], data.folders[0]),
|
parent: pick(
|
||||||
|
['createdAt', 'id', 'name', 'location', 'uid', 'updatedAt'],
|
||||||
|
data.folders[0]
|
||||||
|
),
|
||||||
updatedBy: {
|
updatedBy: {
|
||||||
firstname: expect.anything(),
|
firstname: expect.anything(),
|
||||||
id: expect.anything(),
|
id: expect.anything(),
|
||||||
@ -213,8 +216,8 @@ describe('Folder', () => {
|
|||||||
|
|
||||||
expect(res.body.data).toEqual(
|
expect(res.body.data).toEqual(
|
||||||
expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
pick(['id', 'name', 'path', 'uid', 'updatedAt', 'createdAt'])(data.folders[0]),
|
pick(['id', 'name', 'location', 'uid', 'updatedAt', 'createdAt'])(data.folders[0]),
|
||||||
pick(['id', 'name', 'path', 'uid', 'updatedAt', 'createdAt'])(data.folders[1]),
|
pick(['id', 'name', 'location', 'uid', 'updatedAt', 'createdAt'])(data.folders[1]),
|
||||||
])
|
])
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
81
packages/core/upload/tests/admin/settings.test.e2e.js
Normal file
81
packages/core/upload/tests/admin/settings.test.e2e.js
Normal file
@ -0,0 +1,81 @@
|
|||||||
|
'use strict';
|
||||||
|
|
||||||
|
// Helpers.
|
||||||
|
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
||||||
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
||||||
|
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
||||||
|
|
||||||
|
const builder = createTestBuilder();
|
||||||
|
let strapi;
|
||||||
|
let rq;
|
||||||
|
|
||||||
|
const dogModel = {
|
||||||
|
displayName: 'Dog',
|
||||||
|
singularName: 'dog',
|
||||||
|
pluralName: 'dogs',
|
||||||
|
kind: 'collectionType',
|
||||||
|
attributes: {
|
||||||
|
profilePicture: {
|
||||||
|
type: 'media',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
describe('Settings', () => {
|
||||||
|
beforeAll(async () => {
|
||||||
|
await builder.addContentType(dogModel).build();
|
||||||
|
strapi = await createStrapiInstance();
|
||||||
|
rq = await createAuthRequest({ strapi });
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await strapi.destroy();
|
||||||
|
await builder.cleanup();
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('GET /upload/settings => Get settings for an environment', () => {
|
||||||
|
test('Returns the settings', async () => {
|
||||||
|
const res = await rq({ method: 'GET', url: '/upload/settings' });
|
||||||
|
|
||||||
|
expect(res.statusCode).toBe(200);
|
||||||
|
expect(res.body).toEqual({
|
||||||
|
data: {
|
||||||
|
autoOrientation: false,
|
||||||
|
sizeOptimization: true,
|
||||||
|
responsiveDimensions: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('PUT /upload/settings/:environment', () => {
|
||||||
|
test('Updates an environment config correctly', async () => {
|
||||||
|
const updateRes = await rq({
|
||||||
|
method: 'PUT',
|
||||||
|
url: '/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({ method: 'GET', url: '/upload/settings' });
|
||||||
|
|
||||||
|
expect(getRes.statusCode).toBe(200);
|
||||||
|
expect(getRes.body).toEqual({
|
||||||
|
data: {
|
||||||
|
sizeOptimization: true,
|
||||||
|
responsiveDimensions: true,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -36,53 +36,7 @@ describe('Upload plugin end to end tests', () => {
|
|||||||
await builder.cleanup();
|
await builder.cleanup();
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('GET /upload/settings => Get settings for an environment', () => {
|
describe('Create', () => {
|
||||||
test('Returns the settings', async () => {
|
|
||||||
const res = await rq({ method: 'GET', url: '/upload/settings' });
|
|
||||||
|
|
||||||
expect(res.statusCode).toBe(200);
|
|
||||||
expect(res.body).toEqual({
|
|
||||||
data: {
|
|
||||||
autoOrientation: false,
|
|
||||||
sizeOptimization: true,
|
|
||||||
responsiveDimensions: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('PUT /upload/settings/:environment', () => {
|
|
||||||
test('Updates an environment config correctly', async () => {
|
|
||||||
const updateRes = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: '/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({ method: 'GET', url: '/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 () => {
|
test('Simple image upload', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
@ -158,31 +112,27 @@ describe('Upload plugin end to end tests', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
test('GET /upload/files => Find files', async () => {
|
describe('Read', () => {
|
||||||
const getRes = await rq({ method: 'GET', url: '/upload/files' });
|
test('Get files', async () => {
|
||||||
|
const getRes = await rq({ method: 'GET', url: '/upload/files' });
|
||||||
|
|
||||||
expect(getRes.statusCode).toBe(200);
|
expect(getRes.statusCode).toBe(200);
|
||||||
expect(getRes.body).toEqual({
|
expect(getRes.body).toEqual(
|
||||||
results: expect.arrayContaining([
|
expect.arrayContaining([
|
||||||
expect.objectContaining({
|
expect.objectContaining({
|
||||||
id: expect.anything(),
|
id: expect.anything(),
|
||||||
url: expect.any(String),
|
url: expect.any(String),
|
||||||
}),
|
}),
|
||||||
]),
|
])
|
||||||
pagination: {
|
);
|
||||||
page: expect.any(Number),
|
|
||||||
pageSize: expect.any(Number),
|
|
||||||
pageCount: expect.any(Number),
|
|
||||||
total: expect.any(Number),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('POST /api/:uid => Create an entity with a file', () => {
|
describe('Create an entity with a file', () => {
|
||||||
test('With an image', async () => {
|
test('With an image', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/api/dogs?populate=*',
|
url: '/dogs?populate=*',
|
||||||
formData: {
|
formData: {
|
||||||
data: '{}',
|
data: '{}',
|
||||||
'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
||||||
@ -210,7 +160,7 @@ describe('Upload plugin end to end tests', () => {
|
|||||||
test('With a pdf', async () => {
|
test('With a pdf', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/api/dogs?populate=*',
|
url: '/dogs?populate=*',
|
||||||
formData: {
|
formData: {
|
||||||
data: '{}',
|
data: '{}',
|
||||||
'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
'files.profilePicture': fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
||||||
@ -235,7 +185,4 @@ describe('Upload plugin end to end tests', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
test.todo('GET /upload/files/:id => Find one file');
|
|
||||||
test.todo('GET /upload/search/:id => Search files');
|
|
||||||
test.todo('DELETE /upload/files/:id => Delete a file');
|
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user