2022-03-22 18:19:46 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
// Test a simple default API with no relations
|
|
|
|
|
2022-04-05 17:36:09 +02:00
|
|
|
const { omit, pick } = require('lodash/fp');
|
2022-03-22 18:19:46 +01:00
|
|
|
|
|
|
|
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: [],
|
|
|
|
};
|
|
|
|
|
2022-04-05 17:36:09 +02:00
|
|
|
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;
|
2022-04-12 16:32:05 +02:00
|
|
|
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 getFolderPathRegex = uid =>
|
2022-04-05 17:36:09 +02:00
|
|
|
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}$',
|
|
|
|
'i'
|
|
|
|
);
|
|
|
|
|
2022-03-22 18:19:46 +01:00
|
|
|
describe('Folder', () => {
|
|
|
|
const builder = createTestBuilder();
|
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
strapi = await createStrapiInstance();
|
|
|
|
rq = await createAuthRequest({ strapi });
|
|
|
|
});
|
|
|
|
|
|
|
|
afterAll(async () => {
|
|
|
|
await strapi.destroy();
|
|
|
|
await builder.cleanup();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('create', () => {
|
|
|
|
test('Can create a folder at root level', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders?populate=parent',
|
|
|
|
body: {
|
2022-03-28 16:51:11 +02:00
|
|
|
name: 'folder 1',
|
2022-03-22 18:19:46 +01:00
|
|
|
parent: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2022-04-04 14:32:08 +02:00
|
|
|
expect(res.status).toBe(200);
|
2022-03-22 18:19:46 +01:00
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
id: expect.anything(),
|
2022-03-28 16:51:11 +02:00
|
|
|
name: 'folder 1',
|
2022-04-05 17:36:09 +02:00
|
|
|
uid: expect.stringMatching(uuidRegex),
|
2022-04-12 16:32:05 +02:00
|
|
|
path: expect.stringMatching(rootPathRegex),
|
2022-03-22 18:19:46 +01:00
|
|
|
createdAt: expect.anything(),
|
|
|
|
updatedAt: expect.anything(),
|
|
|
|
parent: null,
|
|
|
|
});
|
2022-04-12 16:32:05 +02:00
|
|
|
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
|
2022-03-22 18:19:46 +01:00
|
|
|
|
|
|
|
data.folders.push(omit('parent', res.body.data));
|
|
|
|
});
|
|
|
|
|
2022-04-04 14:32:08 +02:00
|
|
|
test('Can create a folder inside another folder', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders?populate=parent',
|
|
|
|
body: {
|
|
|
|
name: 'folder-2',
|
|
|
|
parent: data.folders[0].id,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.body.data).toMatchObject({
|
|
|
|
id: expect.anything(),
|
|
|
|
name: 'folder-2',
|
2022-04-05 17:36:09 +02:00
|
|
|
uid: expect.stringMatching(uuidRegex),
|
2022-04-12 16:32:05 +02:00
|
|
|
path: expect.stringMatching(getFolderPathRegex(data.folders[0].uid)),
|
2022-04-04 14:32:08 +02:00
|
|
|
createdAt: expect.anything(),
|
|
|
|
updatedAt: expect.anything(),
|
|
|
|
parent: data.folders[0],
|
|
|
|
});
|
2022-04-12 16:32:05 +02:00
|
|
|
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
|
2022-04-04 14:32:08 +02:00
|
|
|
|
|
|
|
data.folders.push(omit('parent', res.body.data));
|
|
|
|
});
|
|
|
|
|
2022-03-22 18:19:46 +01:00
|
|
|
test('Cannot create a folder with duplicated name at root level', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders?populate=parent',
|
|
|
|
body: {
|
2022-03-28 16:51:11 +02:00
|
|
|
name: 'folder 1',
|
2022-03-22 18:19:46 +01:00
|
|
|
parent: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.status).toBe(400);
|
|
|
|
expect(res.body.error.message).toBe('name already taken');
|
|
|
|
});
|
|
|
|
|
2022-04-04 14:32:08 +02:00
|
|
|
test('Cannot create a folder with duplicated name inside a folder', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders?populate=parent',
|
|
|
|
body: {
|
|
|
|
name: 'folder-2',
|
|
|
|
parent: data.folders[0],
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.status).toBe(400);
|
|
|
|
expect(res.body.error.message).toBe('name already taken');
|
|
|
|
});
|
|
|
|
|
2022-03-28 16:51:11 +02:00
|
|
|
test('Cannot create a folder with name containing a slash', async () => {
|
2022-03-22 18:19:46 +01:00
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders?populate=parent',
|
|
|
|
body: {
|
2022-03-28 16:51:11 +02:00
|
|
|
name: 'folder 1/2',
|
2022-03-22 18:19:46 +01:00
|
|
|
parent: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.status).toBe(400);
|
|
|
|
expect(res.body.error.message).toBe('name cannot contain slashes');
|
|
|
|
});
|
|
|
|
|
2022-03-28 16:51:11 +02:00
|
|
|
test.each([[' abc'], [' abc '], ['abc '], [' abc '], [' abc ']])(
|
2022-04-04 14:32:08 +02:00
|
|
|
'Cannot create a folder with name starting or ending with a whitespace (%p)',
|
2022-03-28 16:51:11 +02:00
|
|
|
async name => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders?populate=parent',
|
|
|
|
body: {
|
|
|
|
name,
|
|
|
|
parent: null,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.status).toBe(400);
|
|
|
|
expect(res.body.error.message).toBe('name cannot start or end with a whitespace');
|
|
|
|
}
|
|
|
|
);
|
2022-03-22 18:19:46 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('read', () => {
|
|
|
|
test('Can read folders', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'GET',
|
|
|
|
url: '/upload/folders',
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.body.pagination).toMatchObject({
|
|
|
|
page: 1,
|
|
|
|
pageCount: 1,
|
|
|
|
pageSize: 10,
|
|
|
|
total: 2,
|
|
|
|
});
|
|
|
|
expect(res.body.results).toEqual(
|
|
|
|
expect.arrayContaining([
|
|
|
|
{
|
2022-04-05 17:36:09 +02:00
|
|
|
...data.folders[0],
|
2022-03-22 18:19:46 +01:00
|
|
|
children: { count: 1 },
|
|
|
|
createdBy: {
|
2022-03-22 18:57:50 +01:00
|
|
|
firstname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
id: expect.anything(),
|
2022-03-22 18:57:50 +01:00
|
|
|
lastname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
username: null,
|
|
|
|
},
|
|
|
|
files: { count: 0 },
|
|
|
|
parent: null,
|
|
|
|
updatedBy: {
|
2022-03-22 18:57:50 +01:00
|
|
|
firstname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
id: expect.anything(),
|
2022-03-22 18:57:50 +01:00
|
|
|
lastname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
username: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2022-04-05 17:36:09 +02:00
|
|
|
...data.folders[1],
|
2022-03-22 18:19:46 +01:00
|
|
|
children: { count: 0 },
|
|
|
|
createdBy: {
|
2022-03-22 18:57:50 +01:00
|
|
|
firstname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
id: expect.anything(),
|
2022-03-22 18:57:50 +01:00
|
|
|
lastname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
username: null,
|
|
|
|
},
|
2022-03-22 18:57:50 +01:00
|
|
|
files: { count: 0 },
|
2022-04-12 16:32:05 +02:00
|
|
|
parent: pick(['createdAt', 'id', 'name', 'path', 'uid', 'updatedAt'], data.folders[0]),
|
2022-03-22 18:19:46 +01:00
|
|
|
updatedBy: {
|
2022-03-22 18:57:50 +01:00
|
|
|
firstname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
id: expect.anything(),
|
2022-03-22 18:57:50 +01:00
|
|
|
lastname: expect.anything(),
|
2022-03-22 18:19:46 +01:00
|
|
|
username: null,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-04-04 14:32:08 +02:00
|
|
|
|
|
|
|
describe('delete', () => {
|
|
|
|
test('Can delete folders', async () => {
|
|
|
|
const res = await rq({
|
|
|
|
method: 'POST',
|
|
|
|
url: '/upload/folders/batch-delete',
|
|
|
|
body: {
|
|
|
|
ids: data.folders.map(f => f.id),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(res.body.data).toEqual(
|
|
|
|
expect.arrayContaining([
|
2022-04-12 16:32:05 +02:00
|
|
|
pick(['id', 'name', 'path', 'uid', 'updatedAt', 'createdAt'])(data.folders[0]),
|
|
|
|
pick(['id', 'name', 'path', 'uid', 'updatedAt', 'createdAt'])(data.folders[1]),
|
2022-04-04 14:32:08 +02:00
|
|
|
])
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2022-03-22 18:19:46 +01:00
|
|
|
});
|