Merge pull request #2316 from automatisch/update-folders
feat: Implement update folder API endpoint
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { renderObject } from '../../../../helpers/renderer.js';
|
||||
|
||||
export default async (request, response) => {
|
||||
const folder = await request.currentUser
|
||||
.$relatedQuery('folders')
|
||||
.findOne({
|
||||
id: request.params.folderId,
|
||||
})
|
||||
.throwIfNotFound();
|
||||
|
||||
await folder.$query().patchAndFetch({
|
||||
name: request.body.name,
|
||||
});
|
||||
|
||||
renderObject(response, folder);
|
||||
};
|
||||
@@ -0,0 +1,99 @@
|
||||
import { describe, it, expect, beforeEach } from 'vitest';
|
||||
import request from 'supertest';
|
||||
import Crypto from 'crypto';
|
||||
import app from '../../../../app.js';
|
||||
import createAuthTokenByUserId from '../../../../helpers/create-auth-token-by-user-id.js';
|
||||
import { createUser } from '../../../../../test/factories/user.js';
|
||||
import { createFolder } from '../../../../../test/factories/folder.js';
|
||||
import { createPermission } from '../../../../../test/factories/permission.js';
|
||||
import updateFolderMock from '../../../../../test/mocks/rest/api/v1/folders/update-folder.js';
|
||||
|
||||
describe('PATCH /api/v1/folders/:folderId', () => {
|
||||
let currentUser, currentUserRole, token;
|
||||
|
||||
beforeEach(async () => {
|
||||
currentUser = await createUser();
|
||||
currentUserRole = await currentUser.$relatedQuery('role');
|
||||
|
||||
token = await createAuthTokenByUserId(currentUser.id);
|
||||
});
|
||||
|
||||
it('should return the updated folder data of current user', async () => {
|
||||
const currentUserFolder = await createFolder({ userId: currentUser.id });
|
||||
|
||||
await createPermission({
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
roleId: currentUserRole.id,
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.patch(`/api/v1/folders/${currentUserFolder.id}`)
|
||||
.set('Authorization', token)
|
||||
.send({
|
||||
name: 'Updated folder name',
|
||||
})
|
||||
.expect(200);
|
||||
|
||||
const refetchedCurrentUserFolder = await currentUserFolder.$query();
|
||||
|
||||
const expectedPayload = await updateFolderMock({
|
||||
...refetchedCurrentUserFolder,
|
||||
name: 'Updated folder name',
|
||||
});
|
||||
|
||||
expect(response.body).toStrictEqual(expectedPayload);
|
||||
});
|
||||
|
||||
it('should return not found response for not existing folder UUID', async () => {
|
||||
await createPermission({
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
roleId: currentUserRole.id,
|
||||
});
|
||||
|
||||
const notExistingFolderUUID = Crypto.randomUUID();
|
||||
|
||||
await request(app)
|
||||
.patch(`/api/v1/folders/${notExistingFolderUUID}`)
|
||||
.set('Authorization', token)
|
||||
.expect(404);
|
||||
});
|
||||
|
||||
it('should return bad request response for invalid UUID', async () => {
|
||||
await createPermission({
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
roleId: currentUserRole.id,
|
||||
});
|
||||
|
||||
await request(app)
|
||||
.patch('/api/v1/folders/invalidFolderUUID')
|
||||
.set('Authorization', token)
|
||||
.expect(400);
|
||||
});
|
||||
|
||||
it('should return unprocessable entity response for invalid data', async () => {
|
||||
const currentUserFolder = await createFolder({ userId: currentUser.id });
|
||||
|
||||
await createPermission({
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
roleId: currentUserRole.id,
|
||||
});
|
||||
|
||||
const response = await request(app)
|
||||
.patch(`/api/v1/folders/${currentUserFolder.id}`)
|
||||
.set('Authorization', token)
|
||||
.send({
|
||||
name: null,
|
||||
})
|
||||
.expect(422);
|
||||
|
||||
expect(response.body.errors).toStrictEqual({
|
||||
name: ['must be string'],
|
||||
});
|
||||
|
||||
expect(response.body.meta.type).toStrictEqual('ModelValidation');
|
||||
});
|
||||
});
|
||||
@@ -137,6 +137,10 @@ const authorizationList = {
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
},
|
||||
'PATCH /api/v1/folders/:folderId': {
|
||||
action: 'create',
|
||||
subject: 'Flow',
|
||||
},
|
||||
};
|
||||
|
||||
export const authorizeUser = async (request, response, next) => {
|
||||
|
||||
@@ -2,9 +2,11 @@ import { Router } from 'express';
|
||||
import { authenticateUser } from '../../../helpers/authentication.js';
|
||||
import { authorizeUser } from '../../../helpers/authorization.js';
|
||||
import createFolderAction from '../../../controllers/api/v1/folders/create-folder.js';
|
||||
import updateFolderAction from '../../../controllers/api/v1/folders/update-folder.js';
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.post('/', authenticateUser, authorizeUser, createFolderAction);
|
||||
router.patch('/:folderId', authenticateUser, authorizeUser, updateFolderAction);
|
||||
|
||||
export default router;
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
const updateFolderMock = async (flow) => {
|
||||
const data = {
|
||||
id: flow.id,
|
||||
name: flow.name,
|
||||
createdAt: flow.createdAt.getTime(),
|
||||
updatedAt: flow.updatedAt.getTime(),
|
||||
};
|
||||
|
||||
return {
|
||||
data: data,
|
||||
meta: {
|
||||
count: 1,
|
||||
currentPage: null,
|
||||
isArray: false,
|
||||
totalPages: null,
|
||||
type: 'Folder',
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
export default updateFolderMock;
|
||||
Reference in New Issue
Block a user