Skip to content

Commit

Permalink
Comprobar que la contraseña tiene al menos 8 caracteres en el registr…
Browse files Browse the repository at this point in the history
…o de usuarios
  • Loading branch information
larafmz committed Apr 24, 2024
1 parent 50b9657 commit 1693eb4
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 27 deletions.
25 changes: 15 additions & 10 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ app.get('/health', (_req, res) => {
});

app.post('/login', async (req, res) => {
const isValidUser = validateCredentials(req.body.username, req.body.password);

if (!isValidUser) {
// Si las credenciales son inválidas, devuelve un error 401
res.status(401).json({ error: 'Credenciales incorrectas' });
return; // Termina la ejecución de la función para evitar ejecutar el código restante
}

try {
// Forward the login request to the authentication service
const authResponse = await axios.post(authServiceUrl+'/login', req.body);
Expand All @@ -45,14 +37,26 @@ app.post('/login', async (req, res) => {
}
});


function validateCredentials(username, password) {
// Verifica si la contraseña es erronea
const invalidPassword = 'no';
if (password.length < 8) {
return false;
}

return !(password === invalidPassword);
return true;
}

app.post('/adduser', async (req, res) => {

const isValidUser = validateCredentials(req.body.username, req.body.password);

if (!isValidUser) {
// Si las credenciales son inválidas, devuelve un error 401
res.status(401).json({ error: 'Credenciales incorrectas. La contraseña debe contener al menos 8 caracteres' });
return; // Termina la ejecución de la función para evitar ejecutar el código restante
}

try {
// Forward the add user request to the user service
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
Expand All @@ -62,6 +66,7 @@ app.post('/adduser', async (req, res) => {
}
});


app.get('/pregunta', async (req, res) => {
try{
const questionResponse = await axios.get(questionServiceUrl+'/pregunta')
Expand Down
22 changes: 5 additions & 17 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,18 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
}
});

/*
// Test /login endpoint
it('deberia iniciar sesión correctamente', async () => {
const response = await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });
expect(response.statusCode).toBe(200);
expect(response.body.token).toBe('mockedToken');
});
*/

// Prueba de manejo de errores para el endpoint /login
it('deberia devolver error al iniciar sesion', async () => {
// Datos de prueba para iniciar sesión (incorrectos)
// Prueba de manejo de errores para el endpoint /adduser
it('deberia devolver error al registrate', async () => {
// Datos de prueba para registro (incorrectos)
const invalidLoginData = {
username: 'userInvalido',
password: 'no'
password: 'holaque'
};

// Realizamos una solicitud POST al endpoint /login con datos incorrectos
const response = await request(app)
.post('/login')
.post('/adduser')
.send(invalidLoginData);

// Verificamos que la respuesta tenga un código de estado 401 (Unauthorized)
Expand Down

0 comments on commit 1693eb4

Please sign in to comment.