diff --git a/QualityControl/lib/api.js b/QualityControl/lib/api.js index 4d6356691..959272b11 100644 --- a/QualityControl/lib/api.js +++ b/QualityControl/lib/api.js @@ -18,7 +18,6 @@ import { UserRole } from './../common/library/userRole.enum.js'; import { layoutOwnerMiddleware } from './middleware/layouts/layoutOwner.middleware.js'; import { layoutIdMiddleware } from './middleware/layouts/layoutId.middleware.js'; import { layoutServiceMiddleware } from './middleware/layouts/layoutService.middleware.js'; -import { requestBodyMiddleware } from './middleware/requestBody.middleware.js'; /** * Adds paths and binds websocket to instance of HttpServer passed @@ -48,7 +47,6 @@ export const setup = (http, ws) => { '/layout/:id', layoutServiceMiddleware(jsonDb), layoutIdMiddleware(jsonDb), - requestBodyMiddleware, layoutOwnerMiddleware(jsonDb), layoutService.putLayoutHandler.bind(layoutService), ); diff --git a/QualityControl/lib/controllers/LayoutController.js b/QualityControl/lib/controllers/LayoutController.js index e7c60aaf0..7d57f6874 100644 --- a/QualityControl/lib/controllers/LayoutController.js +++ b/QualityControl/lib/controllers/LayoutController.js @@ -126,11 +126,18 @@ export class LayoutController { try { let layoutProposed = {}; try { + if (!req.body) { + updateAndSendExpressResponseFromNativeError( + res, + new InvalidInputError('Missing request body to update layout'), + ); + return; + } layoutProposed = await LayoutDto.validateAsync(req.body); } catch (error) { updateAndSendExpressResponseFromNativeError( res, - new Error(`Failed to update layout ${error?.details?.[0]?.message || ''}`), + new InvalidInputError(`Failed to update layout ${error?.details?.[0]?.message || ''}`), ); return; } diff --git a/QualityControl/lib/middleware/requestBody.middleware.js b/QualityControl/lib/middleware/requestBody.middleware.js deleted file mode 100644 index f51549ec3..000000000 --- a/QualityControl/lib/middleware/requestBody.middleware.js +++ /dev/null @@ -1,33 +0,0 @@ -/** - * @license - * Copyright 2019-2020 CERN and copyright holders of ALICE O2. - * See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. - * All rights not expressly granted are reserved. - * - * This software is distributed under the terms of the GNU General Public - * License v3 (GPL Version 3), copied verbatim in the file "COPYING". - * - * In applying this license CERN does not waive the privileges and immunities - * granted to it by virtue of its status as an Intergovernmental Organization - * or submit itself to any jurisdiction. - */ - -import { InvalidInputError, updateAndSendExpressResponseFromNativeError } from '@aliceo2/web-ui'; - -/** - * Middleware that checks if the body of the request is not empty - * @param {Express.Request} req - HTTP Request - * @param {Express.Response} res - HTTP Response - * @param {Express.Next} next - HTTP Next (check pass) - */ -export const requestBodyMiddleware = (req, res, next) => { - try { - if (!req.body) { - throw new InvalidInputError('Missing body content in request'); - } - next(); - } catch (error) { - updateAndSendExpressResponseFromNativeError(res, error); - return; - } -}; diff --git a/QualityControl/test/lib/controllers/LayoutController.test.js b/QualityControl/test/lib/controllers/LayoutController.test.js index 89e1c316f..eb459cca3 100644 --- a/QualityControl/test/lib/controllers/LayoutController.test.js +++ b/QualityControl/test/lib/controllers/LayoutController.test.js @@ -201,6 +201,18 @@ export const layoutControllerTestSuite = async () => { }; }); + test('should respond with 400 error if request did not contain body id', async () => { + const req = { params: { id: 'someid' } }; + const layoutConnector = new LayoutController({}); + await layoutConnector.putLayoutHandler(req, res); + ok(res.status.calledWith(400), 'Response status was not 400'); + ok(res.json.calledWith({ + message: 'Missing request body to update layout', + status: 400, + title: 'Invalid Input', + }), 'Error message was incorrect'); + }); + test('should successfully return the id of the updated layout', async () => { const expectedMockWithDefaults = { id: 'mylayout', diff --git a/QualityControl/test/lib/middlewares/requestBody.middleware.test.js b/QualityControl/test/lib/middlewares/requestBody.middleware.test.js deleted file mode 100644 index d535ac876..000000000 --- a/QualityControl/test/lib/middlewares/requestBody.middleware.test.js +++ /dev/null @@ -1,52 +0,0 @@ -/** - * @license - * Copyright 2019-2020 CERN and copyright holders of ALICE O2. - * See http://alice-o2.web.cern.ch/copyright for details of the copyright holders. - * All rights not expressly granted are reserved. - * - * This software is distributed under the terms of the GNU General Public - * License v3 (GPL Version 3), copied verbatim in the file "COPYING". - * - * In applying this license CERN does not waive the privileges and immunities - * granted to it by virtue of its status as an Intergovernmental Organization - * or submit itself to any jurisdiction. - */ - -import { suite, test } from 'node:test'; -import { ok } from 'node:assert'; -import sinon from 'sinon'; -import { requestBodyMiddleware } from '../../../lib/middleware/requestBody.middleware.js'; - -/** - * Test suite for the middleware that checks if the request has a body - */ -export const requestBodyMiddlewareTest = () => { - suite('Request body middleware', () => { - test('should return an "Invalid input" error if the body is not provided in the request', () => { - const req = {}; - const res = { - status: sinon.stub().returnsThis(), - json: sinon.stub().returns(), - }; - const next = sinon.stub().returns(); - requestBodyMiddleware(req, res, next); - ok(res.status.calledWith(400), 'The status code should be 400'); - ok(res.json.calledWith({ - message: 'Missing body content in request', - status: 400, - title: 'Invalid Input', - })); - }); - - test('should successfully pass the check if the request contains a body', async () => { - const req = { - body: { - test: [], - }, - }; - const next = sinon.stub().returns(); - await requestBodyMiddleware(req, {}, next); - ok(next.called, 'It should call the next middleware'); - }); - }); -}; diff --git a/QualityControl/test/test-index.js b/QualityControl/test/test-index.js index 49db28a80..810fcaa6c 100644 --- a/QualityControl/test/test-index.js +++ b/QualityControl/test/test-index.js @@ -59,7 +59,6 @@ import { commonLibraryUtilsDateTimeTestSuite } from './common/library/utils/date import { layoutIdMiddlewareTest } from './lib/middlewares/layouts/layoutId.middleware.test.js'; import { layoutOwnerMiddlewareTest } from './lib/middlewares/layouts/layoutOwner.middleware.test.js'; import { layoutServiceMiddlewareTest } from './lib/middlewares/layouts/layoutService.middleware.test.js'; -import { requestBodyMiddlewareTest } from './lib/middlewares/requestBody.middleware.test.js'; const FRONT_END_PER_TEST_TIMEOUT = 5000; // each front-end test is allowed this timeout // remaining tests are based on the number of individual tests in each suite @@ -166,7 +165,6 @@ suite('All Tests - QCG', { timeout: FRONT_END_TIMEOUT + BACK_END_TIMEOUT }, asyn suite('LayoutServiceMiddleware test suite', async () => layoutServiceMiddlewareTest()); suite('LayoutIdMiddleware test suite', async () => layoutIdMiddlewareTest()); suite('LayoutOwnerMiddleware test suite', async () => layoutOwnerMiddlewareTest()); - suite('RequestBodyMiddleware test suite', async () => requestBodyMiddlewareTest()); }); suite('Controllers - Test Suite', async () => {