Skip to content

Commit

Permalink
modificar clase gateway para test error user
Browse files Browse the repository at this point in the history
  • Loading branch information
fer4github committed Apr 8, 2024
1 parent e046e3d commit f56f792
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
16 changes: 16 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,15 @@ 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 @@ -33,6 +42,13 @@ app.post('/login', async (req, res) => {
}
});

function validateCredentials(username, password) {
// Verifica si la contraseña es erronea
const invalidPassword = 'no';

return !(password === invalidPassword);
}

app.post('/adduser', async (req, res) => {
try {
// Forward the add user request to the user service
Expand Down
9 changes: 4 additions & 5 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ const app = require('./gateway-service');

// Importamos Locust para realizar pruebas de rendimiento
const { spawn } = require('child_process');
const mockResponse = { data: { respuesta: '¡Hola desde el servicio externo!' } };
const mockResponse = { data: { respuesta: 'Respuesta de Error' } };

afterAll(async () => {
return
});
app.close();
});

jest.mock('axios');

Expand Down Expand Up @@ -67,8 +67,7 @@ describe('Gateway Service', () => {
.send(invalidLoginData);

// Verificamos que la respuesta tenga un código de estado 401 (Unauthorized)
// De momento no comprobamos esto en la aplicación por eso devuelve 200.
expect(response.statusCode).toBe(200);
expect(response.statusCode).toBe(401);
});
//test prueba gateway

Expand Down

0 comments on commit f56f792

Please sign in to comment.