2022-04-06 11:36:13 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
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
|
|
|
|
|
|
|
let strapi;
|
|
|
|
let rq;
|
2022-08-08 15:50:34 +02:00
|
|
|
const data = {
|
2022-04-06 11:36:13 +02:00
|
|
|
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',
|
2022-04-26 11:23:07 +02:00
|
|
|
url: '/upload/actions/bulk-delete',
|
2022-04-06 11:36:13 +02:00
|
|
|
body: {
|
2022-08-08 23:33:39 +02:00
|
|
|
folderIds: data.folders.map((f) => f.id),
|
2022-04-06 11:36:13 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
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,
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: '/',
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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 },
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: data.folders[0].path,
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2022-04-12 16:32:05 +02:00
|
|
|
expect(res.body.error.message).toBe('the folder does not exist');
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
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 },
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: data.folders[1].path,
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
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 },
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: data.folders[0].path,
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
data.files[1] = file;
|
|
|
|
});
|
|
|
|
});
|
2022-09-30 11:02:59 +01:00
|
|
|
|
2022-04-06 11:36:13 +02:00
|
|
|
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 },
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: data.folders[0].path,
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
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 },
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: data.folders[1].path,
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
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,
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: '/',
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
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,
|
2022-04-12 16:32:05 +02:00
|
|
|
folderPath: '/',
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
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);
|
2022-04-12 16:32:05 +02:00
|
|
|
expect(res.body.error.message).toBe('the folder does not exist');
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
test('whithout replacing the file', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: `/upload?id=${data.files[1].id}`,
|
|
|
|
formData: {
|
|
|
|
fileInfo: JSON.stringify({
|
2022-04-12 16:32:05 +02:00
|
|
|
folder: '1234', // id that does not exist
|
2022-04-06 11:36:13 +02:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.status).toBe(400);
|
2022-04-12 16:32:05 +02:00
|
|
|
expect(res.body.error.message).toBe('the folder does not exist');
|
2022-04-06 11:36:13 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|