Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prueba conjunto #61

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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('/');

});
});