2019-09-18 12:07:59 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const fs = require('fs');
|
2020-10-27 11:27:17 +01:00
|
|
|
const path = require('path');
|
2019-09-18 12:07:59 +02:00
|
|
|
|
2022-03-22 18:19:46 +01:00
|
|
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
|
|
|
const { createAuthRequest } = require('../../../../../test/helpers/request');
|
2019-09-18 12:07:59 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
let strapi;
|
2019-09-18 12:07:59 +02:00
|
|
|
let rq;
|
|
|
|
|
2020-09-03 12:17:40 +02:00
|
|
|
const data = {};
|
|
|
|
|
2019-09-18 12:07:59 +02:00
|
|
|
describe('Upload plugin end to end tests', () => {
|
|
|
|
beforeAll(async () => {
|
2020-11-30 20:20:36 +01:00
|
|
|
strapi = await createStrapiInstance();
|
2020-11-17 15:38:41 +01:00
|
|
|
rq = await createAuthRequest({ strapi });
|
2021-03-26 20:15:38 +01:00
|
|
|
});
|
2019-09-18 12:07:59 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
});
|
|
|
|
|
2022-02-28 10:49:16 +01:00
|
|
|
test('Upload a single image', async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const formData = {
|
|
|
|
operations: JSON.stringify({
|
2019-09-18 12:07:59 +02:00
|
|
|
query: /* GraphQL */ `
|
2021-09-28 11:11:03 +02:00
|
|
|
mutation uploadFile($file: Upload!) {
|
2019-09-18 12:07:59 +02:00
|
|
|
upload(file: $file) {
|
2021-09-28 11:11:03 +02:00
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
mime
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 12:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
file: null,
|
|
|
|
},
|
2020-11-17 15:38:41 +01:00
|
|
|
}),
|
|
|
|
map: JSON.stringify({
|
|
|
|
nFile1: ['variables.file'],
|
|
|
|
}),
|
2022-03-30 16:26:09 +02:00
|
|
|
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
2020-11-17 15:38:41 +01:00
|
|
|
};
|
2019-09-18 12:07:59 +02:00
|
|
|
|
2020-11-17 15:38:41 +01:00
|
|
|
const res = await rq({ method: 'POST', url: '/graphql', formData });
|
2019-09-18 12:07:59 +02:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
upload: {
|
2021-09-28 11:11:03 +02:00
|
|
|
data: {
|
|
|
|
id: expect.anything(),
|
|
|
|
attributes: {
|
|
|
|
name: 'rec.jpg',
|
|
|
|
mime: 'image/jpeg',
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
2019-09-18 12:07:59 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
2020-09-03 12:17:40 +02:00
|
|
|
|
2021-09-28 11:11:03 +02:00
|
|
|
data.file = res.body.data.upload.data;
|
2019-09-18 12:07:59 +02:00
|
|
|
});
|
|
|
|
|
2022-02-28 10:49:16 +01:00
|
|
|
test('Upload multiple images', async () => {
|
2020-11-17 15:38:41 +01:00
|
|
|
const formData = {
|
|
|
|
operations: JSON.stringify({
|
2019-09-18 12:07:59 +02:00
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation uploadFiles($files: [Upload]!) {
|
|
|
|
multipleUpload(files: $files) {
|
2021-09-28 11:11:03 +02:00
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
mime
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
2019-09-18 12:07:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
files: [null, null],
|
|
|
|
},
|
2020-11-17 15:38:41 +01:00
|
|
|
}),
|
|
|
|
map: JSON.stringify({
|
|
|
|
nFile0: ['variables.files.0'],
|
|
|
|
nFile1: ['variables.files.1'],
|
|
|
|
}),
|
2022-03-30 16:26:09 +02:00
|
|
|
nFile0: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
|
|
|
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
2020-11-17 15:38:41 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({ method: 'POST', url: '/graphql', formData });
|
2019-09-18 12:07:59 +02:00
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
2021-09-28 11:11:03 +02:00
|
|
|
expect(res.body.data.multipleUpload).toHaveLength(2);
|
2019-09-18 12:07:59 +02:00
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
multipleUpload: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
2021-09-28 11:11:03 +02:00
|
|
|
data: {
|
|
|
|
id: expect.anything(),
|
|
|
|
attributes: {
|
|
|
|
name: 'rec.jpg',
|
|
|
|
mime: 'image/jpeg',
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
2019-09-18 12:07:59 +02:00
|
|
|
}),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2020-09-03 12:17:40 +02:00
|
|
|
|
2022-02-28 10:49:16 +01:00
|
|
|
test('Upload a single pdf', async () => {
|
|
|
|
const formData = {
|
|
|
|
operations: JSON.stringify({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation uploadFile($file: Upload!) {
|
|
|
|
upload(file: $file) {
|
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
mime
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
file: null,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
map: JSON.stringify({
|
|
|
|
nFile1: ['variables.file'],
|
|
|
|
}),
|
2022-03-30 16:26:09 +02:00
|
|
|
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
2022-02-28 10:49:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({ method: 'POST', url: '/graphql', formData });
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
upload: {
|
|
|
|
data: {
|
|
|
|
id: expect.anything(),
|
|
|
|
attributes: {
|
|
|
|
name: 'rec.pdf',
|
|
|
|
mime: 'application/pdf',
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
data.file = res.body.data.upload.data;
|
|
|
|
});
|
|
|
|
|
|
|
|
test('Upload multiple pdf', async () => {
|
|
|
|
const formData = {
|
|
|
|
operations: JSON.stringify({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation uploadFiles($files: [Upload]!) {
|
|
|
|
multipleUpload(files: $files) {
|
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
mime
|
|
|
|
url
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
files: [null, null],
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
map: JSON.stringify({
|
|
|
|
nFile0: ['variables.files.0'],
|
|
|
|
nFile1: ['variables.files.1'],
|
|
|
|
}),
|
2022-03-30 16:26:09 +02:00
|
|
|
nFile0: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
|
|
|
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
2022-02-28 10:49:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({ method: 'POST', url: '/graphql', formData });
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body.data.multipleUpload).toHaveLength(2);
|
|
|
|
expect(res.body).toEqual({
|
|
|
|
data: {
|
|
|
|
multipleUpload: expect.arrayContaining([
|
|
|
|
expect.objectContaining({
|
|
|
|
data: {
|
|
|
|
id: expect.anything(),
|
|
|
|
attributes: {
|
|
|
|
name: 'rec.pdf',
|
|
|
|
mime: 'application/pdf',
|
|
|
|
url: expect.any(String),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-09-03 12:17:40 +02:00
|
|
|
test('Update file information', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: '/graphql',
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation updateFileInfo($id: ID!, $info: FileInfoInput!) {
|
|
|
|
updateFileInfo(id: $id, info: $info) {
|
2021-09-28 11:11:03 +02:00
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
alternativeText
|
|
|
|
caption
|
|
|
|
}
|
|
|
|
}
|
2020-09-03 12:17:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
id: data.file.id,
|
|
|
|
info: {
|
|
|
|
name: 'test name',
|
|
|
|
alternativeText: 'alternative text test',
|
|
|
|
caption: 'caption test',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
updateFileInfo: {
|
2021-09-28 11:11:03 +02:00
|
|
|
data: {
|
|
|
|
id: data.file.id,
|
|
|
|
attributes: {
|
|
|
|
name: 'test name',
|
|
|
|
alternativeText: 'alternative text test',
|
|
|
|
caption: 'caption test',
|
|
|
|
},
|
|
|
|
},
|
2020-09-03 12:17:40 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2020-10-05 11:34:15 +02:00
|
|
|
|
|
|
|
test('Delete a file', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
url: '/graphql',
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation removeFile($id: ID!) {
|
2021-09-28 11:11:03 +02:00
|
|
|
removeFile(id: $id) {
|
|
|
|
data {
|
2020-10-05 11:34:15 +02:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
id: data.file.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
2021-09-28 11:11:03 +02:00
|
|
|
removeFile: {
|
|
|
|
data: {
|
2020-10-05 11:34:15 +02:00
|
|
|
id: data.file.id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-05-05 10:47:43 +02:00
|
|
|
test('Delete a file that does not exist', async () => {
|
2020-10-05 11:34:15 +02:00
|
|
|
const res = await rq({
|
|
|
|
url: '/graphql',
|
|
|
|
method: 'POST',
|
|
|
|
body: {
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation removeFile($id: ID!) {
|
2021-09-28 11:11:03 +02:00
|
|
|
removeFile(id: $id) {
|
|
|
|
data {
|
2020-10-05 11:34:15 +02:00
|
|
|
id
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
id: '404',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
2021-09-28 11:11:03 +02:00
|
|
|
removeFile: null,
|
2020-10-05 11:34:15 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2021-01-21 23:18:45 +09:00
|
|
|
|
2022-02-28 10:49:16 +01:00
|
|
|
test('Upload a single image with info', async () => {
|
2021-01-21 23:18:45 +09:00
|
|
|
const formData = {
|
|
|
|
operations: JSON.stringify({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation uploadFilesWithInfo($file: Upload!, $info: FileInfoInput) {
|
|
|
|
upload(file: $file, info: $info) {
|
2021-09-28 11:11:03 +02:00
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
alternativeText
|
|
|
|
caption
|
|
|
|
}
|
|
|
|
}
|
2021-01-21 23:18:45 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
file: null,
|
|
|
|
info: {
|
|
|
|
alternativeText: 'alternative text test',
|
|
|
|
caption: 'caption test',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
map: JSON.stringify({
|
|
|
|
nFile1: ['variables.file'],
|
|
|
|
}),
|
2022-03-30 16:26:09 +02:00
|
|
|
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
|
2021-01-21 23:18:45 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({ method: 'POST', url: '/graphql', formData });
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
upload: {
|
2021-09-28 11:11:03 +02:00
|
|
|
data: {
|
|
|
|
id: expect.anything(),
|
|
|
|
attributes: {
|
|
|
|
name: 'rec.jpg',
|
|
|
|
alternativeText: 'alternative text test',
|
|
|
|
caption: 'caption test',
|
|
|
|
},
|
|
|
|
},
|
2021-01-21 23:18:45 +09:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2022-02-28 10:49:16 +01:00
|
|
|
|
|
|
|
test('Upload a single pdf with info', async () => {
|
|
|
|
const formData = {
|
|
|
|
operations: JSON.stringify({
|
|
|
|
query: /* GraphQL */ `
|
|
|
|
mutation uploadFilesWithInfo($file: Upload!, $info: FileInfoInput) {
|
|
|
|
upload(file: $file, info: $info) {
|
|
|
|
data {
|
|
|
|
id
|
|
|
|
attributes {
|
|
|
|
name
|
|
|
|
alternativeText
|
|
|
|
caption
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
`,
|
|
|
|
variables: {
|
|
|
|
file: null,
|
|
|
|
info: {
|
|
|
|
alternativeText: 'alternative text test',
|
|
|
|
caption: 'caption test',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
map: JSON.stringify({
|
|
|
|
nFile1: ['variables.file'],
|
|
|
|
}),
|
2022-03-30 16:26:09 +02:00
|
|
|
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.pdf')),
|
2022-02-28 10:49:16 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const res = await rq({ method: 'POST', url: '/graphql', formData });
|
|
|
|
|
|
|
|
expect(res.statusCode).toBe(200);
|
|
|
|
expect(res.body).toMatchObject({
|
|
|
|
data: {
|
|
|
|
upload: {
|
|
|
|
data: {
|
|
|
|
id: expect.anything(),
|
|
|
|
attributes: {
|
|
|
|
name: 'rec.pdf',
|
|
|
|
alternativeText: 'alternative text test',
|
|
|
|
caption: 'caption test',
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|
|
|
|
});
|
2019-09-18 12:07:59 +02:00
|
|
|
});
|