Skip to content

Commit

Permalink
Client BDD test for put request with valid path param and invalid req…
Browse files Browse the repository at this point in the history
…uest json bpdy types
  • Loading branch information
AnderGI committed Feb 15, 2025
1 parent 768e555 commit a3941e6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
40 changes: 38 additions & 2 deletions tests/apps/backoffice/backend/features/register-clients.feature
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Feature: Register Clients
{
"name": "John Doe",
"email": "johndoe@example.com",
"phone": "+123456789",
"phone": "123456789",
"company": "Acme Corp",
"position": "Sales Manager"
}
Expand All @@ -21,7 +21,7 @@ Feature: Register Clients
{
"name": "John Doe",
"email": "johndoe@example.com",
"phone": "+123456789",
"phone": "123456789",
"company": "Acme Corp",
"position": "Sales Manager"
}
Expand All @@ -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"
}
]
}
"""

0 comments on commit a3941e6

Please sign in to comment.