replace names by uids in folder path

This commit is contained in:
Pierre Noël 2022-04-05 17:36:09 +02:00
parent a1c441e8bf
commit 7689a340ea
3 changed files with 34 additions and 48 deletions

View File

@ -2,22 +2,28 @@
const { setPathAndUID } = require('../folder');
const folderUID = '9bc2352b-e29b-4ba3-810f-7b91033222de';
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;
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 folderPathRegex = new RegExp(
'^/' + folderUID + '/[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$',
'i'
);
describe('folder', () => {
describe('setPathAndUID', () => {
beforeAll(() => {
global.strapi = {
entityService: {
findOne: jest.fn(() => ({ path: '/parent-path' })),
findOne: jest.fn(() => ({ uid: `/${folderUID}` })),
},
};
});
test.each([
[{ parent: 1, name: 'myFile.txt' }, '/parent-path/myFile.txt'],
[{ name: 'myFile.txt' }, '/myFile.txt'],
[{ parent: null, name: 'myFile.txt' }, '/myFile.txt'],
[{ parent: 1 }, folderPathRegex],
[{}, rootPathRegex],
[{ parent: null }, rootPathRegex],
])('inputs %s', async (folder, expectedPath) => {
const clonedFolder = { ...folder };
const result = await setPathAndUID(clonedFolder);
@ -26,7 +32,7 @@ describe('folder', () => {
expect(result).toMatchObject({
...folder,
uid: expect.stringMatching(uuidRegex),
path: expectedPath,
path: expect.stringMatching(expectedPath),
});
});
});

View File

@ -24,6 +24,7 @@ const joinBy = (joint, ...args) => {
const generateUID = () => uuid();
const setPathAndUID = async folder => {
const uid = generateUID();
let parentPath = '/';
if (folder.parent) {
const parentFolder = await strapi.entityService.findOne(folderModel, folder.parent);
@ -31,8 +32,8 @@ const setPathAndUID = async folder => {
}
return Object.assign(folder, {
uid: generateUID(),
path: joinBy('/', parentPath, folder.name),
uid,
path: joinBy('/', parentPath, uid),
});
};

View File

@ -2,7 +2,7 @@
// Test a simple default API with no relations
const { omit } = require('lodash/fp');
const { omit, pick } = require('lodash/fp');
const { createTestBuilder } = require('../../../../../test/helpers/builder');
const { createStrapiInstance } = require('../../../../../test/helpers/strapi');
@ -14,6 +14,14 @@ let data = {
folders: [],
};
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;
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 =>
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'
);
describe('Folder', () => {
const builder = createTestBuilder();
@ -42,12 +50,13 @@ describe('Folder', () => {
expect(res.body.data).toMatchObject({
id: expect.anything(),
name: 'folder 1',
uid: expect.anything(),
path: '/folder 1',
uid: expect.stringMatching(uuidRegex),
path: expect.stringMatching(rootPathRegex),
createdAt: expect.anything(),
updatedAt: expect.anything(),
parent: null,
});
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
data.folders.push(omit('parent', res.body.data));
});
@ -65,12 +74,13 @@ describe('Folder', () => {
expect(res.body.data).toMatchObject({
id: expect.anything(),
name: 'folder-2',
uid: expect.anything(),
path: '/folder 1/folder-2',
uid: expect.stringMatching(uuidRegex),
path: expect.stringMatching(getFolderPathRegex(data.folders[0].uid)),
createdAt: expect.anything(),
updatedAt: expect.anything(),
parent: data.folders[0],
});
expect(res.body.data.uid).toBe(res.body.data.path.split('/').pop());
data.folders.push(omit('parent', res.body.data));
});
@ -151,8 +161,8 @@ describe('Folder', () => {
expect(res.body.results).toEqual(
expect.arrayContaining([
{
...data.folders[0],
children: { count: 1 },
createdAt: expect.anything(),
createdBy: {
firstname: expect.anything(),
id: expect.anything(),
@ -160,12 +170,7 @@ describe('Folder', () => {
username: null,
},
files: { count: 0 },
id: expect.anything(),
name: 'folder 1',
parent: null,
path: '/folder 1',
uid: expect.anything(),
updatedAt: expect.anything(),
updatedBy: {
firstname: expect.anything(),
id: expect.anything(),
@ -174,8 +179,8 @@ describe('Folder', () => {
},
},
{
...data.folders[1],
children: { count: 0 },
createdAt: expect.anything(),
createdBy: {
firstname: expect.anything(),
id: expect.anything(),
@ -183,19 +188,7 @@ describe('Folder', () => {
username: null,
},
files: { count: 0 },
id: expect.anything(),
name: 'folder-2',
parent: {
createdAt: expect.anything(),
id: expect.anything(),
name: 'folder 1',
path: '/folder 1',
uid: expect.anything(),
updatedAt: expect.anything(),
},
path: '/folder 1/folder-2',
uid: expect.anything(),
updatedAt: expect.anything(),
parent: pick(['createdAt', 'id', 'name', 'path', 'uid', 'updatedAt'], data.folders[0]),
updatedBy: {
firstname: expect.anything(),
id: expect.anything(),
@ -220,22 +213,8 @@ describe('Folder', () => {
expect(res.body.data).toEqual(
expect.arrayContaining([
{
createdAt: expect.anything(),
id: expect.anything(),
name: 'folder 1',
path: '/folder 1',
uid: expect.anything(),
updatedAt: expect.anything(),
},
{
createdAt: expect.anything(),
id: expect.anything(),
name: 'folder-2',
path: '/folder 1/folder-2',
uid: expect.anything(),
updatedAt: expect.anything(),
},
pick(['id', 'name', 'path', 'uid', 'updatedAt', 'createdAt'])(data.folders[0]),
pick(['id', 'name', 'path', 'uid', 'updatedAt', 'createdAt'])(data.folders[1]),
])
);
});