Skip to content

Commit

Permalink
Creo que ya está
Browse files Browse the repository at this point in the history
  • Loading branch information
uo287627 committed Apr 28, 2024
1 parent d024293 commit b6b0183
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 55 deletions.
1 change: 0 additions & 1 deletion gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ describe('Gateway Service', () => {
// Mock responses from external services
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
console.error("en gateway tests estamos"); // borrar
return Promise.resolve({ data: { token: 'mockedToken' } });
} else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
Expand Down
3 changes: 1 addition & 2 deletions webapp/src/components/Login.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ const Login = ({ setLogged }) => {
const loginUser = async () => {
try {
const response = await axios.post(`${apiEndpoint}/login`, { username, password });
console.error("RESPONSE... "+response);
const { createdAt: userCreatedAt } = response.data;
await axios.get(`${apiEndpoint}/getAllUsers`);
// const users = usersResponse.data;
Expand All @@ -64,7 +63,7 @@ const Login = ({ setLogged }) => {
} else if (error.request) {
setError('No response from server. Please try again later.');
} else {
setError('An unexpected error occurred...' + error.message);
setError('An unexpected error occurred');
}
}
};
Expand Down
183 changes: 131 additions & 52 deletions webapp/src/components/Login.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ describe('Login Component', () => {
expect(passwordInput).toBeInTheDocument();

await act(async () => {
// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'admin' } });
fireEvent.change(passwordInput, { target: { value: 'admin' } });
fireEvent.change(usernameInput, { target: { value: 'normalUser' } });
fireEvent.change(passwordInput, { target: { value: 'test123' } });

// Trigger the add user button click
fireEvent.click(loginButton);
});

// Wait for the Snackbar to be open
await waitFor(() => {
expect(setLogged).toHaveBeenCalledTimes(0);
expect(setLogged).toHaveBeenCalledTimes(1);

expect(screen.getByText(/Jugar/i)).toBeInTheDocument();
expect(screen.getByText(/Historial de jugadas/i)).toBeInTheDocument();
Expand All @@ -77,76 +74,158 @@ describe('Login Component', () => {
});
});

/*
test('login with valid admin credentials', async () => {
await act(async () => {
const setLogged = jest.fn();
render(<Login setLogged={setLogged}/>);
const setLogged = jest.fn();

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });
// Mock para la petición POST de login exitosa
axios.post.mockResolvedValueOnce({
data: {
createdAt: new Date().toISOString()
}
});

// Mock para la petición GET de obtener todos los usuarios
axios.get.mockResolvedValueOnce({
data: [] // Puedes ajustar esto según lo que necesites en tu test
});

// Mock the axios.post & axios.get requests to simulate successful responses
mockAxios.onPost('http://localhost:8000/login').reply(200);
mockAxios.onGet('http://localhost:8000/getAllUsers').reply(200);
mockAxios.onPost('http://localhost:8000/createUserRank').reply(200);
mockAxios.onGet('http://localhost:8000/actRanking').reply(200);
mockAxios.onPost('http://localhost:8000/updateAllRanking').reply(200);
// Mock para la petición POST de createUserRank exitosa
axios.post.mockResolvedValueOnce({
data: {} // Puedes ajustar esto según lo que necesites en tu test
});

await act(async () => {
render(<Login setLogged={setLogged}/>);
});

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });

// Simulate user input
await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'admin' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });

// Trigger the add user button click
fireEvent.click(loginButton);
});

await waitFor(() => {
expect(setLogged).toHaveBeenCalledTimes(1);

expect(screen.getByText(/Jugar/i)).toBeInTheDocument();
// only for admin
expect(screen.getByText(/Historial de Usuarios/i)).toBeInTheDocument();
expect(screen.getByText(/Historial de Preguntas Generadas/i)).toBeInTheDocument();
// end only for admin
expect(screen.getByText(/Historial de jugadas/i)).toBeInTheDocument();
expect(screen.getByText(/Ranking/i)).toBeInTheDocument();
expect(screen.getByText(/Ajustes de partida/i)).toBeInTheDocument();

// Wait for the Snackbar to be open
await waitFor(() => {
expect(setLogged).toHaveBeenCalledTimes(0);
expect(screen.getByText(/Jugar/i)).toBeInTheDocument();
// only for admin
expect(screen.getByText(/Historial de Usuarios/i)).toBeInTheDocument();
expect(screen.getByText(/Historial de Preguntas Generadas/i)).toBeInTheDocument();
// end only for admin
expect(screen.getByText(/Historial de jugadas/i)).toBeInTheDocument();
expect(screen.getByText(/Ranking/i)).toBeInTheDocument();
expect(screen.getByText(/Ajustes de partida/i)).toBeInTheDocument();
expect(screen.getByText(/Hola testUser!/i)).toBeInTheDocument();
expect(screen.getByText(/Tu cuenta fue creada el/i)).toBeInTheDocument();
expect(screen.getByText(/Comenzar a jugar/i)).toBeInTheDocument();
});
expect(screen.getByText(/Hola testUser!/i)).toBeInTheDocument();
expect(screen.getByText(/Tu cuenta fue creada el/i)).toBeInTheDocument();
expect(screen.getByText(/Comenzar a jugar/i)).toBeInTheDocument();
});
});

test('login fails on post /login and error is handled ', async () => {
const setLogged = jest.fn();

// Mock para la petición POST de login fallada
axios.post.mockRejectedValueOnce({ response: { status: 500, data: { error: 'Internal Server Error' } } });

await act(async () => {
render(<Login setLogged={setLogged}/>);
});

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });

await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });

fireEvent.click(loginButton);
});

await waitFor(() => {
expect(setLogged).toHaveBeenCalled(0);
expect(screen.getByText(/Internal Server Error/i)).toBeInTheDocument();
expect(screen.queryByText(/Comenzar a jugar/i)).not.toBeInTheDocument();
});
});

test('login fails on get /getAllUsers and error is handled ', async () => {
const setLogged = jest.fn();

// Mock para la petición POST de login exitosa
axios.post.mockResolvedValueOnce({
data: {
createdAt: new Date().toISOString()
}
});
// Mock para la petición get de login fallada
axios.get.mockRejectedValueOnce({ response: { status: 500} });

await act(async () => {
const setLogged = jest.fn();
render(<Login setLogged={setLogged}/>);
});

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });

await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });

fireEvent.click(loginButton);
});

await waitFor(() => {
expect(setLogged).toHaveBeenCalled(0);
expect(screen.getByText(/Error interno del servidor/i)).toBeInTheDocument();
expect(screen.queryByText(/Comenzar a jugar/i)).not.toBeInTheDocument();
});
});

test('login fails on post /createUserRank and error is handled ', async () => {
const setLogged = jest.fn();

// Mock para la petición POST de login exitosa
axios.post.mockResolvedValueOnce({
data: {
createdAt: new Date().toISOString()
}
});

// Mock para la petición GET de obtener todos los usuarios
axios.get.mockResolvedValueOnce({
data: [] // Puedes ajustar esto según lo que necesites en tu test
});

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });
// Mock para la petición POST de login fallada
axios.post.mockRejectedValueOnce({ response: { status: 500} });

// Mock the axios.post & axios.get requests to simulate successful responses
mockAxios.onPost('http://localhost:8000/login').reply(500);
await act(async () => {
render(<Login setLogged={setLogged}/>);
});

const usernameInput = screen.getByLabelText(/Username/i);
const passwordInput = screen.getByLabelText(/Password/i);
const loginButton = screen.getByRole('button', { name: /Iniciar sesión/i });

// Simulate user input
await act(async () => {
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword' } });

// Trigger the add user button click
fireEvent.click(loginButton);
});

// Wait for the Snackbar to be open
await waitFor(() => {
expect(setLogged).toHaveBeenCalled();
expect(screen.getByText(/Login successful/i)).toBeInTheDocument();
});
await waitFor(() => {
expect(setLogged).toHaveBeenCalled(0);
expect(screen.getByText(/Error interno del servidor/i)).toBeInTheDocument();
expect(screen.queryByText(/Comenzar a jugar/i)).not.toBeInTheDocument();
});
});
*/
});

0 comments on commit b6b0183

Please sign in to comment.