diff --git a/lib/application/authentication/index.ts b/lib/application/authentication/index.ts index e5df2a3..967d9e4 100644 --- a/lib/application/authentication/index.ts +++ b/lib/application/authentication/index.ts @@ -1,4 +1,6 @@ import type { Server } from '@hapi/hapi'; +import Joi from 'joi'; + import { authenticate } from './authentication.js'; const register = async function (server: Server) { @@ -8,11 +10,18 @@ const register = async function (server: Server) { path: '/token', options: { auth: false, + validate: { + payload: Joi.object({ + user: Joi.string().required(), + password: Joi.string().required(), + }).label('AuthenticationPayload'), + }, handler: authenticate, + tags: ['api', 'authentication'], }, }, ]); }; const name = 'authentication-api'; -export { register, name }; +export { name, register }; diff --git a/lib/application/query/index.ts b/lib/application/query/index.ts index 5df9382..175fb3a 100644 --- a/lib/application/query/index.ts +++ b/lib/application/query/index.ts @@ -1,4 +1,6 @@ import type { Server } from '@hapi/hapi'; +import Joi from 'joi'; + import { execute } from './query.js'; const register = async function (server: Server) { @@ -7,11 +9,26 @@ const register = async function (server: Server) { method: 'POST', path: '/query', options: { + validate: { + payload: Joi.object({ + queryId: Joi.string().uuid().required(), + params: Joi.array() + .items( + Joi.object({ + name: Joi.string().required(), + value: Joi.any().required(), + }).label('QueryParameter'), + ) + .required() + .label('QueryParameters'), + }).label('QueryPayload'), + }, handler: execute, + tags: ['api', 'query'], }, }, ]); }; const name = 'query-api'; -export { register, name }; +export { name, register }; diff --git a/lib/application/query/query.ts b/lib/application/query/query.ts index 77e947f..5d3519b 100644 --- a/lib/application/query/query.ts +++ b/lib/application/query/query.ts @@ -1,4 +1,5 @@ import type { Request, ResponseObject, ResponseToolkit } from '@hapi/hapi'; + import { UserCommand } from '../../domain/commands/UserCommand.js'; import type { Result } from '../../domain/models/Result.js'; import { executeQueryUseCase } from '../../domain/usecases/ExecuteQueryUsecase.js'; diff --git a/tests/acceptance/application/query_test.ts b/tests/acceptance/application/query_test.ts index a65819e..3c66659 100644 --- a/tests/acceptance/application/query_test.ts +++ b/tests/acceptance/application/query_test.ts @@ -47,11 +47,9 @@ describe('Acceptance | query', function () { // then expect(response.statusCode).to.equal(400); expect(JSON.parse(response.payload)).to.deep.equal({ - status: 'failure', - messages: [ - 'unknown attribute: "queryIdddddddd"', - '"queryId" is mandatory', - ], + statusCode: 400, + error: 'Bad Request', + message: 'Invalid request payload input', }); }); }); @@ -136,7 +134,7 @@ describe('Acceptance | query', function () { it('should return a proper error response with status code 422', async function () { // given const queryId = '26f6efcc-ce13-4b20-b6ea-5bebae6115af'; - const otherQueryId = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee'; + const otherQueryId = '11a1aaaa-aa11-1a11-a1aa-1aaaaa1111aa'; await knexAPI('catalog_queries').insert({ id: otherQueryId, sql_query: 'SELECT COUNT(*) FROM public.data_ref_academies',