From a3941e6b49efc8a1d48f0f73d1034cac1da2289f Mon Sep 17 00:00:00 2001 From: AnderGI Date: Sat, 15 Feb 2025 14:22:34 +0100 Subject: [PATCH] Client BDD test for put request with valid path param and invalid request json bpdy types --- .../routes/register-client/register.route.ts | 40 ++++++++++++++++++- .../backend/features/register-clients.feature | 40 ++++++++++++++++++- 2 files changed, 76 insertions(+), 4 deletions(-) diff --git a/src/apps/backoffice/backend/routes/register-client/register.route.ts b/src/apps/backoffice/backend/routes/register-client/register.route.ts index 35b8874..8e29500 100644 --- a/src/apps/backoffice/backend/routes/register-client/register.route.ts +++ b/src/apps/backoffice/backend/routes/register-client/register.route.ts @@ -1,9 +1,45 @@ import { NextFunction, Request, Response, Router } from 'express'; -import { param, validationResult } from 'express-validator'; +import { body, param, validationResult } from 'express-validator'; import httpStatus from 'http-status'; -const requestSchema = [param('id').isUUID().withMessage('The id param must be a valid UUID')]; +const requestSchema = [ + param('id').isUUID().withMessage('The id param must be a valid UUID'), + // Validar el campo 'name' + body('name') + .notEmpty() + .withMessage('Name is required') + .isString() + .withMessage('Name must be a string') + .isLength({ min: 1 }) + .withMessage('Name cannot be empty'), + + // Validar el campo 'email' + body('email').isEmail().withMessage('Must be a valid email address').normalizeEmail(), // Para normalizar el correo (convertirlo en minúsculas) + + // Validar el campo 'phone' para que sea exactamente 9 dígitos + body('phone') + .notEmpty() + .withMessage('Phone number is required') + .isString() + .withMessage('Phone number must be a string') + .matches(/^\d{9}$/) + .withMessage('Phone number must be exactly 9 digits'), + + // Validar el campo 'company' + body('company') + .notEmpty() + .withMessage('Company name is required') + .isString() + .withMessage('Company must be a string'), + + // Validar el campo 'position' + body('position') + .notEmpty() + .withMessage('Position is required') + .isString() + .withMessage('Position must be a string') +]; export const register = (router: Router): void => { router.put( '/clients/:id', diff --git a/tests/apps/backoffice/backend/features/register-clients.feature b/tests/apps/backoffice/backend/features/register-clients.feature index 3c16620..011ca4f 100644 --- a/tests/apps/backoffice/backend/features/register-clients.feature +++ b/tests/apps/backoffice/backend/features/register-clients.feature @@ -7,7 +7,7 @@ Feature: Register Clients { "name": "John Doe", "email": "johndoe@example.com", - "phone": "+123456789", + "phone": "123456789", "company": "Acme Corp", "position": "Sales Manager" } @@ -21,7 +21,7 @@ Feature: Register Clients { "name": "John Doe", "email": "johndoe@example.com", - "phone": "+123456789", + "phone": "123456789", "company": "Acme Corp", "position": "Sales Manager" } @@ -37,4 +37,40 @@ Feature: Register Clients } ] } + """ + + Scenario: Invalid JSON request body types. + Given I send a PUT request to "/clients/2e8456c2-e22d-41de-99c5-f61475b44bc1" with JSON request body: + """ + { + "name": "John Doe", + "email": "jjajjaj", + "phone": 2344, + "company": "Acme Corp", + "position": true + } + """ + Then the response status code should be 400 + Then the response body should be: + """ + { + "errors": [ + { + "msg": "Must be a valid email address", + "param": "email" + }, + { + "msg": "Phone number must be a string", + "param": "phone" + }, + { + "msg": "Phone number must be exactly 9 digits", + "param": "phone" + }, + { + "msg": "Position must be a string", + "param": "position" + } + ] + } """ \ No newline at end of file