From 5bde656cad2a358de2733095e9b50d21cc8a8129 Mon Sep 17 00:00:00 2001 From: Kevin Nderitu Date: Fri, 22 Jan 2021 12:23:42 +0300 Subject: [PATCH 1/3] Added ID validation on search_term --- app/api/search/deprecatedRoutes.js | 17 ++++++++++++---- .../specs/__snapshots__/routes.spec.js.snap | 20 +------------------ 2 files changed, 14 insertions(+), 23 deletions(-) diff --git a/app/api/search/deprecatedRoutes.js b/app/api/search/deprecatedRoutes.js index ef9d6c3d67..3e3ad5d18b 100644 --- a/app/api/search/deprecatedRoutes.js +++ b/app/api/search/deprecatedRoutes.js @@ -40,10 +40,19 @@ export default app => { app.get( '/api/search_snippets', validation.validateRequest( - Joi.object().keys({ - searchTerm: Joi.string().allow(''), - id: Joi.string(), - }), + { + required: ['query'], + properties: { + query: { + type: 'object', + required: ['id'], + properties: { + searchTerm: { type: 'string', default: '' }, + id: { type: 'string' }, + }, + }, + }, + }, 'query' ), (req, res, next) => diff --git a/app/api/search/specs/__snapshots__/routes.spec.js.snap b/app/api/search/specs/__snapshots__/routes.spec.js.snap index e12421f0cd..ad31291a2e 100644 --- a/app/api/search/specs/__snapshots__/routes.spec.js.snap +++ b/app/api/search/specs/__snapshots__/routes.spec.js.snap @@ -20,22 +20,4 @@ Object { } `; -exports[`search routes /api/search_snippets should have a validation schema 1`] = ` -Object { - "children": Object { - "id": Object { - "invalids": Array [ - "", - ], - "type": "string", - }, - "searchTerm": Object { - "type": "string", - "valids": Array [ - "", - ], - }, - }, - "type": "object", -} -`; +exports[`search routes /api/search_snippets should have a validation schema 1`] = `undefined`; From 259f615dbf3c3b21727e54134bb710ce37201120 Mon Sep 17 00:00:00 2001 From: Kevin Nderitu Date: Tue, 26 Jan 2021 22:52:52 +0300 Subject: [PATCH 2/3] Updated validation tests --- .../specs/__snapshots__/routes.spec.js.snap | 2 -- app/api/search/specs/routes.spec.js | 19 +++++++++++++++++-- app/api/utils/testingRoutes.ts | 2 +- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/app/api/search/specs/__snapshots__/routes.spec.js.snap b/app/api/search/specs/__snapshots__/routes.spec.js.snap index ad31291a2e..8f16948578 100644 --- a/app/api/search/specs/__snapshots__/routes.spec.js.snap +++ b/app/api/search/specs/__snapshots__/routes.spec.js.snap @@ -19,5 +19,3 @@ Object { "type": "object", } `; - -exports[`search routes /api/search_snippets should have a validation schema 1`] = `undefined`; diff --git a/app/api/search/specs/routes.spec.js b/app/api/search/specs/routes.spec.js index 11be551e69..8bb1021373 100644 --- a/app/api/search/specs/routes.spec.js +++ b/app/api/search/specs/routes.spec.js @@ -1,5 +1,8 @@ +import request from 'supertest'; import entities from 'api/entities'; import { catchErrors } from 'api/utils/jasmineHelpers'; +import { setUpApp } from 'api/utils/testingRoutes'; +import { testingDB } from 'api/utils/testing_db'; import searchRoutes from '../deprecatedRoutes.js'; import instrumentRoutes from '../../utils/instrumentRoutes'; import search from '../search'; @@ -87,8 +90,20 @@ describe('search routes', () => { }); describe('/api/search_snippets', () => { - it('should have a validation schema', () => { - expect(routes.get.validation('/api/search_snippets')).toMatchSnapshot(); + const app = setUpApp(searchRoutes); + + it('should have a validation schema', async () => { + await testingDB.clearAllAndLoad({ + settings: [ + { + languages: [{ key: 'es', default: true }, { key: 'pt' }, { key: 'en' }], + }, + ], + }); + const response = await request(app) + .get('/api/search_snippets?searchTerm=test') + .send({}); + expect(response.text).toMatch(/should have required property 'id'/); }); it('should search', done => { diff --git a/app/api/utils/testingRoutes.ts b/app/api/utils/testingRoutes.ts index c244d18661..bc7492a301 100644 --- a/app/api/utils/testingRoutes.ts +++ b/app/api/utils/testingRoutes.ts @@ -13,13 +13,13 @@ const setUpApp = ( ): Application => { const app: Application = express(); app.use(bodyParser.json()); + customMiddleware.forEach(middlewareElement => app.use(middlewareElement)); app.use((req: Request, _res: Response, next: NextFunction) => { req.emitToSessionSocket = (event: string, ...args: any[]) => iosocket.emit(event, ...args); next(); }); app.use(languageMiddleware); - customMiddleware.forEach(middlewareElement => app.use(middlewareElement)); route(app); app.use(errorHandlingMiddleware); From 6590dee15bd7028aa122c5b430ab0e25a885088b Mon Sep 17 00:00:00 2001 From: Kevin Nderitu Date: Tue, 26 Jan 2021 23:07:15 +0300 Subject: [PATCH 3/3] Reverted setupapp middleware order --- app/api/utils/testingRoutes.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/api/utils/testingRoutes.ts b/app/api/utils/testingRoutes.ts index bc7492a301..c244d18661 100644 --- a/app/api/utils/testingRoutes.ts +++ b/app/api/utils/testingRoutes.ts @@ -13,13 +13,13 @@ const setUpApp = ( ): Application => { const app: Application = express(); app.use(bodyParser.json()); - customMiddleware.forEach(middlewareElement => app.use(middlewareElement)); app.use((req: Request, _res: Response, next: NextFunction) => { req.emitToSessionSocket = (event: string, ...args: any[]) => iosocket.emit(event, ...args); next(); }); app.use(languageMiddleware); + customMiddleware.forEach(middlewareElement => app.use(middlewareElement)); route(app); app.use(errorHandlingMiddleware);