Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/Arquisoft/wiq_es2a
Browse files Browse the repository at this point in the history
  • Loading branch information
lauracc97 committed Apr 26, 2024
2 parents 2bc2ae5 + 870a772 commit 0a02c1a
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 1 deletion.
11 changes: 11 additions & 0 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
40 changes: 39 additions & 1 deletion webapp/src/components/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down Expand Up @@ -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('/');

});
});

0 comments on commit 0a02c1a

Please sign in to comment.