mirror of
https://github.com/strapi/strapi.git
synced 2025-11-07 05:38:13 +00:00
fix tests
This commit is contained in:
parent
5402209c9f
commit
103e47a9a4
@ -3,7 +3,7 @@
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
|
|
||||||
const { omit, pick, map } = require('lodash/fp');
|
const { pick, map } = require('lodash/fp');
|
||||||
|
|
||||||
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
const { createTestBuilder } = require('../../../../../test/helpers/builder');
|
||||||
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
|
||||||
@ -69,7 +69,7 @@ describe('Folder', () => {
|
|||||||
test('Can create a folder at root level', async () => {
|
test('Can create a folder at root level', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name: 'folder 1',
|
name: 'folder 1',
|
||||||
parent: null,
|
parent: null,
|
||||||
@ -84,17 +84,16 @@ describe('Folder', () => {
|
|||||||
path: expect.stringMatching(rootPathRegex),
|
path: expect.stringMatching(rootPathRegex),
|
||||||
createdAt: expect.anything(),
|
createdAt: expect.anything(),
|
||||||
updatedAt: expect.anything(),
|
updatedAt: expect.anything(),
|
||||||
parent: null,
|
|
||||||
});
|
});
|
||||||
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
|
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
|
||||||
|
|
||||||
data.folders.push(omit('parent', res.body.data));
|
data.folders.push(res.body.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Can create a folder inside another folder', async () => {
|
test('Can create a folder inside another folder', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name: 'folder-2',
|
name: 'folder-2',
|
||||||
parent: data.folders[0].id,
|
parent: data.folders[0].id,
|
||||||
@ -108,17 +107,16 @@ describe('Folder', () => {
|
|||||||
path: expect.stringMatching(getFolderPathRegex(data.folders[0].uid)),
|
path: expect.stringMatching(getFolderPathRegex(data.folders[0].uid)),
|
||||||
createdAt: expect.anything(),
|
createdAt: expect.anything(),
|
||||||
updatedAt: expect.anything(),
|
updatedAt: expect.anything(),
|
||||||
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.path.split('/').pop());
|
||||||
|
|
||||||
data.folders.push(omit('parent', res.body.data));
|
data.folders.push(res.body.data);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('Cannot create a folder with duplicated name at root level', async () => {
|
test('Cannot create a folder with duplicated name at root level', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name: 'folder 1',
|
name: 'folder 1',
|
||||||
parent: null,
|
parent: null,
|
||||||
@ -132,7 +130,7 @@ describe('Folder', () => {
|
|||||||
test('Cannot create a folder with duplicated name inside a folder', async () => {
|
test('Cannot create a folder with duplicated name inside a folder', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name: 'folder-2',
|
name: 'folder-2',
|
||||||
parent: data.folders[0].id,
|
parent: data.folders[0].id,
|
||||||
@ -146,7 +144,7 @@ describe('Folder', () => {
|
|||||||
test('Cannot create a folder inside a folder that does not exist', async () => {
|
test('Cannot create a folder inside a folder that does not exist', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name: 'folder-3',
|
name: 'folder-3',
|
||||||
parent: 99999,
|
parent: 99999,
|
||||||
@ -160,7 +158,7 @@ describe('Folder', () => {
|
|||||||
test('Cannot create a folder with name containing a slash', async () => {
|
test('Cannot create a folder with name containing a slash', async () => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name: 'folder 1/2',
|
name: 'folder 1/2',
|
||||||
parent: null,
|
parent: null,
|
||||||
@ -176,7 +174,7 @@ describe('Folder', () => {
|
|||||||
async name => {
|
async name => {
|
||||||
const res = await rq({
|
const res = await rq({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/folders?populate=parent',
|
url: '/upload/folders',
|
||||||
body: {
|
body: {
|
||||||
name,
|
name,
|
||||||
parent: null,
|
parent: null,
|
||||||
@ -450,208 +448,4 @@ describe('Folder', () => {
|
|||||||
data.folders.push(...resFolders.body.results);
|
data.folders.push(...resFolders.body.results);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('update', () => {
|
|
||||||
test('rename a folder', async () => {
|
|
||||||
const folder = await createFolder('folder-name', null);
|
|
||||||
|
|
||||||
const res = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: `/upload/folders/${folder.id}`,
|
|
||||||
body: {
|
|
||||||
name: 'new name',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.body.data).toMatchObject({
|
|
||||||
name: 'new name',
|
|
||||||
path: folder.path,
|
|
||||||
});
|
|
||||||
data.folders.push(res.body.data);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('cannot rename a folder if duplicated (with name)', async () => {
|
|
||||||
const folder0 = await createFolder('folder-a-0', null);
|
|
||||||
const folder1 = await createFolder('folder-a-1', null);
|
|
||||||
await createFolder('folder-a-00', folder0.id);
|
|
||||||
|
|
||||||
const res = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: `/upload/folders/${folder1.id}`,
|
|
||||||
body: {
|
|
||||||
name: 'folder-a-00',
|
|
||||||
parent: folder0.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body.error.message).toBe('folder already exists');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('cannot rename a folder if duplicated (without name)', async () => {
|
|
||||||
const folder0 = await createFolder('folder-b-0', null);
|
|
||||||
const folder1 = await createFolder('folder-b-samename', null);
|
|
||||||
await createFolder('folder-b-samename', folder0.id);
|
|
||||||
data.folders.push(folder0, folder1);
|
|
||||||
|
|
||||||
const res = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: `/upload/folders/${folder1.id}`,
|
|
||||||
body: {
|
|
||||||
parent: folder0.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body.error.message).toBe('folder already exists');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('cannot move a folder to a folder that does not exist', async () => {
|
|
||||||
const folder = await createFolder('folder-c-0', null);
|
|
||||||
|
|
||||||
const res = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: `/upload/folders/${folder.id}`,
|
|
||||||
body: {
|
|
||||||
parent: 9999,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.status).toBe(400);
|
|
||||||
expect(res.body.error.message).toBe('parent folder does not exist');
|
|
||||||
});
|
|
||||||
|
|
||||||
test('move a folder inside another folder', async () => {
|
|
||||||
const folder0 = await createFolder('folder-0', null);
|
|
||||||
const folder00 = await createFolder('folder-00', folder0.id);
|
|
||||||
const folder01 = await createFolder('folder-01', folder0.id);
|
|
||||||
const folder02 = await createFolder('folder-02', folder0.id);
|
|
||||||
const folder000 = await createFolder('folder-000', folder00.id);
|
|
||||||
const file000 = await createAFile(folder000.id);
|
|
||||||
const file02 = await createAFile(folder02.id);
|
|
||||||
|
|
||||||
// moving folder00 in folder01
|
|
||||||
const res = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: `/upload/folders/${folder00.id}`,
|
|
||||||
body: {
|
|
||||||
name: 'folder-00-new',
|
|
||||||
parent: folder01.id,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.body.data).toMatchObject({
|
|
||||||
name: 'folder-00-new',
|
|
||||||
path: `${folder01.path}/${folder00.uid}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
const resFolders = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: '/upload/folders',
|
|
||||||
qs: {
|
|
||||||
filters: { id: { $in: map('id', [folder0, folder00, folder01, folder02, folder000]) } },
|
|
||||||
sort: 'id:asc',
|
|
||||||
populate: 'parent',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(resFolders.body.results[0]).toMatchObject({ path: folder0.path, parent: null });
|
|
||||||
expect(resFolders.body.results[1]).toMatchObject({
|
|
||||||
path: `${folder01.path}/${folder00.uid}`,
|
|
||||||
parent: { id: folder01.id },
|
|
||||||
});
|
|
||||||
expect(resFolders.body.results[2]).toMatchObject({
|
|
||||||
path: folder01.path,
|
|
||||||
parent: { id: folder0.id },
|
|
||||||
});
|
|
||||||
expect(resFolders.body.results[3]).toMatchObject({
|
|
||||||
path: folder02.path,
|
|
||||||
parent: { id: folder0.id },
|
|
||||||
});
|
|
||||||
expect(resFolders.body.results[4]).toMatchObject({
|
|
||||||
path: `${folder01.path}/${folder00.uid}/${folder000.uid}`,
|
|
||||||
parent: { id: folder00.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const resFiles = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: '/upload/files',
|
|
||||||
qs: {
|
|
||||||
filters: { id: { $in: [file000.id, file02.id] } },
|
|
||||||
sort: 'id:asc',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(resFiles.body.results[0]).toMatchObject({
|
|
||||||
folderPath: `${folder01.path}/${folder00.uid}/${folder000.uid}`,
|
|
||||||
});
|
|
||||||
expect(resFiles.body.results[1]).toMatchObject({ folderPath: file02.folderPath });
|
|
||||||
|
|
||||||
data.folders.push(...resFolders.body.results);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('move a folder to root level', async () => {
|
|
||||||
const folder0 = await createFolder('folder-test-0', null);
|
|
||||||
const folder00 = await createFolder('folder-test-00', folder0.id);
|
|
||||||
const folder02 = await createFolder('folder-test-02', folder0.id);
|
|
||||||
const folder000 = await createFolder('folder-test-000', folder00.id);
|
|
||||||
const file000 = await createAFile(folder000.id);
|
|
||||||
const file02 = await createAFile(folder02.id);
|
|
||||||
|
|
||||||
// moving folder00 in folder01
|
|
||||||
const res = await rq({
|
|
||||||
method: 'PUT',
|
|
||||||
url: `/upload/folders/${folder00.id}`,
|
|
||||||
body: {
|
|
||||||
name: 'folder-test-00-new',
|
|
||||||
parent: null,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(res.body.data).toMatchObject({
|
|
||||||
name: 'folder-test-00-new',
|
|
||||||
path: `/${folder00.uid}`,
|
|
||||||
});
|
|
||||||
|
|
||||||
const resFolders = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: '/upload/folders',
|
|
||||||
qs: {
|
|
||||||
filters: { id: { $in: map('id', [folder0, folder00, folder02, folder000]) } },
|
|
||||||
sort: 'id:asc',
|
|
||||||
populate: 'parent',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(resFolders.body.results[0]).toMatchObject({ path: folder0.path, parent: null });
|
|
||||||
expect(resFolders.body.results[1]).toMatchObject({
|
|
||||||
path: `/${folder00.uid}`,
|
|
||||||
parent: null,
|
|
||||||
});
|
|
||||||
expect(resFolders.body.results[2]).toMatchObject({
|
|
||||||
path: folder02.path,
|
|
||||||
parent: { id: folder0.id },
|
|
||||||
});
|
|
||||||
expect(resFolders.body.results[3]).toMatchObject({
|
|
||||||
path: `/${folder00.uid}/${folder000.uid}`,
|
|
||||||
parent: { id: folder00.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const resFiles = await rq({
|
|
||||||
method: 'GET',
|
|
||||||
url: '/upload/files',
|
|
||||||
qs: {
|
|
||||||
filters: { id: { $in: [file000.id, file02.id] } },
|
|
||||||
sort: 'id:asc',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(resFiles.body.results[0]).toMatchObject({
|
|
||||||
folderPath: `/${folder00.uid}/${folder000.uid}`,
|
|
||||||
});
|
|
||||||
expect(resFiles.body.results[1]).toMatchObject({ folderPath: file02.folderPath });
|
|
||||||
|
|
||||||
data.folders.push(...resFolders.body.results);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
@ -42,10 +42,11 @@ describe('Uploads folder', () => {
|
|||||||
|
|
||||||
afterAll(async () => {
|
afterAll(async () => {
|
||||||
// delete all folders
|
// delete all folders
|
||||||
const res = await rq({
|
const res = await rqAdmin({
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
url: '/upload/folders?pagination[pageSize]=100',
|
url: '/upload/folders?pagination[pageSize]=100',
|
||||||
});
|
});
|
||||||
|
|
||||||
await rqAdmin({
|
await rqAdmin({
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
url: '/upload/actions/bulk-delete',
|
url: '/upload/actions/bulk-delete',
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user