From ba45cc2929ad08583b4fe9c8d1373ddcd7251536 Mon Sep 17 00:00:00 2001
From: uo283182 <uo283182@uniovi.es>
Date: Thu, 25 Apr 2024 19:08:41 +0200
Subject: [PATCH 1/2] =?UTF-8?q?Test=20de=20cerrar=20sesi=C3=B3n?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 webapp/src/components/Login.test.js | 40 ++++++++++++++++++++++++++++-
 1 file changed, 39 insertions(+), 1 deletion(-)

diff --git a/webapp/src/components/Login.test.js b/webapp/src/components/Login.test.js
index 854b18b7..a12ed777 100644
--- a/webapp/src/components/Login.test.js
+++ b/webapp/src/components/Login.test.js
@@ -4,7 +4,7 @@ import { BrowserRouter} from 'react-router-dom';
 import axios from 'axios';
 import MockAdapter from 'axios-mock-adapter';
 import Login from './Login';
-
+import App from '../App';
 const mockAxios = new MockAdapter(axios);
 
 describe('Login component', () => {
@@ -79,4 +79,42 @@ describe('Login component', () => {
     // Verificar que no hay token en localStorage
     expect(localStorage.getItem('token')).toBeNull();
   });
+
+  //TEST 3 - Test de cerrar sesión
+  it('should logout successfully', async () => {
+    render( 
+    <BrowserRouter>
+      <App />
+    </BrowserRouter>
+    );
+    const button = screen.getByRole('button', { name: /Login/i });
+    fireEvent.click(button);
+    const logoutButton = screen.queryByRole('button', { name: /Cerrar sesión/i });
+    expect(logoutButton).toBeNull();
+    const usernameInput = screen.getByLabelText(/Username/i);
+    const passwordInput = screen.getByLabelText(/Password/i);
+    const loginButton = screen.getByRole('button', { name: /Login/i });
+
+    mockAxios.onPost('http://localhost:8000/login').reply(200, { token: 'testToken' });
+
+    fireEvent.change(usernameInput, { target: { value: 'testUser' } });
+    fireEvent.change(passwordInput, { target: { value: 'testPassword' } });
+
+    // Click en el botón de login	
+    fireEvent.click(loginButton);
+    let logoutButton2;
+    await waitFor(() => {
+      logoutButton2 = screen.getByRole('button', { name: /Cerrar sesión/i });
+      expect(logoutButton2).toBeInTheDocument();
+    });
+    
+    logoutButton2.click();
+
+     // Verificar que el token ha sido borrado del localStorage
+     expect(localStorage.getItem('token')).toBeNull();
+
+     // Verificar que se ha redirigido a /
+     expect(window.location.pathname).toBe('/');
+  
+  });
 });
\ No newline at end of file

From 4663ea18cd6cad2b71fea412cd6cec9109ae1c07 Mon Sep 17 00:00:00 2001
From: uo283182 <uo283182@uniovi.es>
Date: Thu, 25 Apr 2024 20:35:45 +0200
Subject: [PATCH 2/2] Test de nombre de usuario existente

---
 users/userservice/user-service.test.js | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js
index 04a0465a..417605b8 100644
--- a/users/userservice/user-service.test.js
+++ b/users/userservice/user-service.test.js
@@ -60,6 +60,17 @@ describe('User Service', () => {
     expect(response.body).toHaveProperty('error', 'Missing required field: password');
   });
 
+  it('should return 400 if the username already exists', async () => {
+    const userError3 = {
+      username: 'testuser',
+      password: 'test'
+    };
+
+    const response = await request(app).post('/adduser').send(userError3);
+    expect(response.status).toBe(400);
+    expect(response.body).toHaveProperty('error', 'Este nombre de usuario está en uso');
+  });
+
   //For getting all users
   it('should GET all the users', async () => {
     const response = await request(app).get('/getAllUsers');