Skip to content

Commit

Permalink
Merge pull request #115 from Arquisoft/LaraFMz
Browse files Browse the repository at this point in the history
Lara f mz
  • Loading branch information
UO277938 authored Apr 25, 2024
2 parents f384bf4 + 5f61e1b commit 7af02ca
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 34 deletions.
24 changes: 14 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 Down
26 changes: 5 additions & 21 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,15 @@ 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)
const invalidLoginData = {
username: 'userInvalido',
password: 'no'
};
// Prueba de manejo de errores para el endpoint /adduser
it('deberia devolver error al registrate', async () => {

// Realizamos una solicitud POST al endpoint /login con datos incorrectos
const response = await request(app)
.post('/login')
.send(invalidLoginData);
.post('/adduser')
.send({ username: 'userInvalido', password: 'no' });


// Verificamos que la respuesta tenga un código de estado 401 (Unauthorized)
expect(response.statusCode).toBe(401);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defineFeature(feature, test => {

given('An unregistered user', async () => {
username = "[email protected]"
password = "pabloasw"
password = "pabloasw1"
await expect(page).toClick("button", { text: "REGÍSTRATE" });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defineFeature(feature, test => {

given('A user that is logged in the application', async () => {
username = "[email protected]"
password = "pabloasw"
password = "pabloasw1"
await expect(page).toClick("button", { text: "INICIA SESIÓN" });
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ defineFeature(feature, test => {

given('A user that is logged in the application', async () => {
username = "[email protected]"
password = "pabloasw"
password = "pabloasw1"
await expect(page).toClick("button", { text: "INICIA SESIÓN" });
});

Expand Down

0 comments on commit 7af02ca

Please sign in to comment.