strapi/api-tests/core/upload/content-api/graphql-upload-automatic-folder.test.api.js
Ben Irvin 13a2f8b246
feat: Upgrade to Apollo v4
* feat: update and make build work

BREAKING CHANGE: Update from 'apollo-server-koa' to '@apollo/server' and '@as-integrations/koa'

* chore: fix comments

* chore: upgrade graphql-upload package

* chore: fix for body type unknown

* chore: remove old comment

* chore: clean up error handling

* chore: fix comment

* fix: http status codes for input validation errors

* fix: remove unused import

* fix: remove accidental bodyparser

* fix: add new required header to tests

* chore: standardize directive key names to be kebab-case

* test: add some extra message validation

* chore: remove devdep for koa-cors typings

* fix: add unknown error name

* fix: yarn.lock

* fix: add typings

* fix: typings

* fix: typings again

* fix: remove unused imports

* chore: remove unused import

* chore: move playground check to a service

* fix: package imports and versions

* chore: fix yarn.lock

* chore: fix types

* chore: clean up koa typings

* chore: koa typing cleanup

* chore: cleanup koa typings

* chore: more koa type cleanup

* chore: revert missing imports

* chore: cleanup koa typings

* chore: update yarn.lock
2024-01-15 14:54:58 +01:00

320 lines
8.0 KiB
JavaScript

'use strict';
const fs = require('fs');
const path = require('path');
const { createStrapiInstance } = require('api-tests/strapi');
const { createAuthRequest } = require('api-tests/request');
let strapi;
let rq;
let rqAdmin;
let uploadFolder;
describe('Uploads folder (GraphQL)', () => {
beforeAll(async () => {
strapi = await createStrapiInstance();
rq = await createAuthRequest({
strapi,
// header required for multipart requests
state: { headers: { 'x-apollo-operation-name': 'graphql-upload' } },
});
rqAdmin = await createAuthRequest({ strapi });
});
afterAll(async () => {
// delete all folders
const res = await rqAdmin({
method: 'GET',
url: '/upload/folders',
});
await rqAdmin({
method: 'POST',
url: '/upload/actions/bulk-delete',
body: {
folderIds: res.body.data.map((f) => f.id),
},
});
await strapi.destroy();
});
describe('uploadFile', () => {
test('Uploaded file goes into a specific folder', async () => {
const formData = {
operations: JSON.stringify({
query: /* GraphQL */ `
mutation uploadFile($file: Upload!) {
upload(file: $file) {
data {
id
}
}
}
`,
}),
map: JSON.stringify({ nFile1: ['variables.file'] }),
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
};
const res = await rq({ method: 'POST', url: '/graphql', formData });
expect(res.statusCode).toBe(200);
const { body: file } = await rqAdmin({
method: 'GET',
url: `/upload/files/${res.body.data.upload.data.id}`,
});
expect(file).toMatchObject({
folder: {
name: 'API Uploads',
pathId: expect.any(Number),
},
folderPath: `/${file.folder.pathId}`,
});
uploadFolder = file.folder;
});
test('Uploads folder is recreated if deleted', async () => {
await rqAdmin({
method: 'POST',
url: '/upload/actions/bulk-delete',
body: {
folderIds: [uploadFolder.id],
},
});
const formData = {
operations: JSON.stringify({
query: /* GraphQL */ `
mutation uploadFile($file: Upload!) {
upload(file: $file) {
data {
id
}
}
}
`,
}),
map: JSON.stringify({ nFile1: ['variables.file'] }),
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
};
const res = await rq({ method: 'POST', url: '/graphql', formData });
expect(res.statusCode).toBe(200);
const { body: file } = await rqAdmin({
method: 'GET',
url: `/upload/files/${res.body.data.upload.data.id}`,
});
expect(file).toMatchObject({
folder: {
name: 'API Uploads',
pathId: expect.any(Number),
},
folderPath: `/${file.folder.pathId}`,
});
uploadFolder = file.folder;
});
test('Uploads folder is recreated if deleted (handle duplicates)', async () => {
await rqAdmin({
method: 'POST',
url: '/upload/actions/bulk-delete',
body: {
folderIds: [uploadFolder.id],
},
});
await rqAdmin({
method: 'POST',
url: '/upload/folders',
body: {
name: 'API Uploads',
parent: null,
},
});
const formData = {
operations: JSON.stringify({
query: /* GraphQL */ `
mutation uploadFile($file: Upload!) {
upload(file: $file) {
data {
id
}
}
}
`,
}),
map: JSON.stringify({ nFile1: ['variables.file'] }),
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
};
const res = await rq({ method: 'POST', url: '/graphql', formData });
expect(res.statusCode).toBe(200);
const { body: file } = await rqAdmin({
method: 'GET',
url: `/upload/files/${res.body.data.upload.data.id}`,
});
expect(file).toMatchObject({
folder: {
name: 'API Uploads (1)',
pathId: expect.any(Number),
},
folderPath: `/${file.folder.pathId}`,
});
uploadFolder = file.folder;
});
});
describe('multipleUploadFile', () => {
test('Uploaded file goes into a specific folder', async () => {
const formData = {
operations: JSON.stringify({
query: /* GraphQL */ `
mutation multipleUploadFile($files: [Upload]!) {
multipleUpload(files: $files) {
data {
id
}
}
}
`,
}),
map: JSON.stringify({ nFile1: ['variables.files'] }),
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
};
const res = await rq({ method: 'POST', url: '/graphql', formData });
expect(res.statusCode).toBe(200);
const { body: file } = await rqAdmin({
method: 'GET',
url: `/upload/files/${res.body.data.multipleUpload[0].data.id}`,
});
expect(file).toMatchObject({
folder: {
name: 'API Uploads (1)',
pathId: expect.any(Number),
},
folderPath: `/${file.folder.pathId}`,
});
uploadFolder = file.folder;
});
test('Uploads folder is recreated if deleted', async () => {
await rqAdmin({
method: 'POST',
url: '/upload/actions/bulk-delete',
body: {
folderIds: [uploadFolder.id],
},
});
const formData = {
operations: JSON.stringify({
query: /* GraphQL */ `
mutation multipleUploadFile($files: [Upload]!) {
multipleUpload(files: $files) {
data {
id
}
}
}
`,
}),
map: JSON.stringify({ nFile1: ['variables.files'] }),
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
};
const res = await rq({ method: 'POST', url: '/graphql', formData });
expect(res.statusCode).toBe(200);
const { body: file } = await rqAdmin({
method: 'GET',
url: `/upload/files/${res.body.data.multipleUpload[0].data.id}`,
});
expect(file).toMatchObject({
folder: {
name: 'API Uploads (1)',
pathId: expect.any(Number),
},
folderPath: `/${file.folder.pathId}`,
});
uploadFolder = file.folder;
});
test('Uploads folder is recreated if deleted (handle duplicates)', async () => {
await rqAdmin({
method: 'POST',
url: '/upload/actions/bulk-delete',
body: {
folderIds: [uploadFolder.id],
},
});
await rqAdmin({
method: 'POST',
url: '/upload/folders',
body: {
name: 'API Uploads (1)',
parent: null,
},
});
const formData = {
operations: JSON.stringify({
query: /* GraphQL */ `
mutation multipleUploadFile($files: [Upload]!) {
multipleUpload(files: $files) {
data {
id
}
}
}
`,
}),
map: JSON.stringify({ nFile1: ['variables.files'] }),
nFile1: fs.createReadStream(path.join(__dirname, '../utils/rec.jpg')),
};
const res = await rq({ method: 'POST', url: '/graphql', formData });
expect(res.statusCode).toBe(200);
const { body: file } = await rqAdmin({
method: 'GET',
url: `/upload/files/${res.body.data.multipleUpload[0].data.id}`,
});
expect(file).toMatchObject({
folder: {
name: 'API Uploads (2)',
pathId: expect.any(Number),
},
folderPath: `/${file.folder.pathId}`,
});
uploadFolder = file.folder;
});
});
});