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

Mejora de coverage en nuevo codigo #95

Merged
merged 4 commits into from
Apr 28, 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
90 changes: 89 additions & 1 deletion gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const request = require('supertest');
const axios = require('axios');
const app = require('./gateway-service');

const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001';
afterAll(async () => {
app.close();
});
Expand Down Expand Up @@ -275,7 +275,95 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(404);
expect(response.body.error).toBe('User not found');
});
});
describe('User API Endpoints', () => {
beforeEach(() => {
jest.resetAllMocks(); // Clear previous mocks between tests
});

// Test for GET /user
it('should retrieve user data successfully', async () => {
const mockUserData = [{ id: 'user1', name: 'John Doe' }];
axios.get.mockResolvedValueOnce({ data: mockUserData });

const response = await request(app).get('/user');

expect(response.statusCode).toBe(200);
expect(response.body).toEqual(mockUserData);
expect(axios.get).toHaveBeenCalledWith(`${userServiceUrl}/user`);
});

it('should handle failure when retrieving user data', async () => {
axios.get.mockRejectedValueOnce({ response: { status: 404, data: { error: 'User not found' } } });

const response = await request(app).get('/user');

expect(response.statusCode).toBe(404);
expect(response.body.error).toBe('User not found');
});

// Test for POST /user
it('should create a user successfully', async () => {
const newUser = { username: 'newuser', password: 'password123' };
axios.post.mockResolvedValueOnce({ data: { userId: '12345' } });

const response = await request(app).post('/user').send(newUser);

expect(response.statusCode).toBe(201);
expect(response.body.userId).toBe('12345');
expect(axios.post).toHaveBeenCalledWith(`${userServiceUrl}/adduser`, newUser);
});

it('should handle failure when creating a user', async () => {
const newUser = { username: 'newuser', password: 'password123' };
axios.post.mockRejectedValueOnce({ response: { status: 500, data: { error: 'Error creating the user' } } });

const response = await request(app).post('/user').send(newUser);

expect(response.statusCode).toBe(500);
expect(response.body.error).toBe('Error creating the user');
});

// Test for PATCH /user/:id
it('should update user data successfully', async () => {
const updates = { name: 'Jane Doe' };
axios.patch.mockResolvedValueOnce({ data: { status: 'OK' } });

const response = await request(app).patch('/user/1').send(updates);

expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('OK');
expect(axios.patch).toHaveBeenCalledWith(`${userServiceUrl}/user/1`, updates);
});

it('should handle failure when updating user data', async () => {
const updates = { name: 'Jane Doe' };
axios.patch.mockRejectedValueOnce({ response: { status: 404, data: { error: 'User not found' } } });

const response = await request(app).patch('/user/1').send(updates);

expect(response.statusCode).toBe(404);
expect(response.body.error).toBe('User not found');
});

// Test for DELETE /user/:id
it('should delete a user successfully', async () => {
axios.delete.mockResolvedValueOnce({ data: { status: 'OK' } });

const response = await request(app).delete('/user/1');

expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('OK');
expect(axios.delete).toHaveBeenCalledWith(`${userServiceUrl}/user/1`, {});
});

it('should handle failure when deleting a user', async () => {
axios.delete.mockRejectedValueOnce({ response: { status: 404, data: { error: 'User not found' } } });

const response = await request(app).delete('/user/1');

expect(response.statusCode).toBe(404);
expect(response.body.error).toBe('User not found');
});
});

59 changes: 44 additions & 15 deletions webapp/src/components/pages/AboutUS.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,30 +4,59 @@ import '@testing-library/jest-dom';
import AboutUS from './AboutUS';
import {BrowserRouter as Router} from "react-router-dom";

// Describimos la suite de pruebas para el componente AboutUS
describe('AboutUS Component', () => {

test('renders the header text', () => {

beforeEach(() => {
render(
<Router>
<AboutUS />
</Router>
);
// Verificamos que el texto del encabezado esté presente en el documento
expect(screen.getByText(/Conoce las áreas clave manejadas por los estudiantes en el proyecto WIQ/i)).toBeInTheDocument();
});

// Test para verificar si el componente renderiza los elementos CardItem correctamente
test('renders the correct number of CardItem elements', () => {
render(
<Router>
<AboutUS />
</Router>
);
// Usamos queryAllByTestId para seleccionar todos los elementos con el data-testid "card-item"
test('renders the header text and the card items', () => {
expect(screen.getByText(/Conoce las áreas clave manejadas por los estudiantes en el proyecto WIQ/i)).toBeInTheDocument();
const cardItems = screen.queryAllByTestId('card-item');
// Verificamos que el número de elementos encontrados sea el esperado
expect(cardItems).toHaveLength(5); // Cambia el número esperado según cuántos CardItem esperas renderizar
expect(cardItems).toHaveLength(5);
});

test('links point to the correct paths', () => {
const creditosLink = screen.getByText(/Créditos de la Aplicación/i).closest('a');
expect(creditosLink).toHaveAttribute('href', '/creditos');

const githubLink = screen.getByText(/Explora nuestro código en GitHub y contribuye al desarrollo del proyecto WIQ./i).closest('a');
expect(githubLink).toHaveAttribute('href', 'https://github.com/Arquisoft/wiq_es04d');

const databaseLink = screen.getByText(/Implementación de soluciones de bases de datos para gestionar eficientemente la información./i).closest('a');
expect(databaseLink).toHaveAttribute('href', 'https://github.com/Arquisoft/wiq_es04d/wiki/ADR-4-%E2%80%90-Base-de-datos');

const documentationLink = screen.getByText(/Creación de documentación detallada para facilitar la comprensión y el uso del proyecto./i).closest('a');
expect(documentationLink).toHaveAttribute('href', 'https://arquisoft.github.io/wiq_es04d/');

const apiLink = screen.getByText(/Desarrollo de la lógica de integración con la API de Wikidata para enriquecer nuestro proyecto./i).closest('a');
expect(apiLink).toHaveAttribute('href', `${process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000'}/api-doc`);
});
test('each CardItem has the correct label', () => {
// Asegúrate de que ya has renderizado el componente en un beforeEach o directamente en el test
const figures = document.querySelectorAll('figure[data-category]');
const expectedLabels = ['Créditos', 'GitHub WIQ4D', 'Base de Datos', 'Documentación', 'API de Wikidata'];

expect(figures.length).toBe(expectedLabels.length); // Verifica que el número de elementos coincide
figures.forEach((figure, index) => {
expect(figure.getAttribute('data-category')).toBe(expectedLabels[index]);
});
});
test('each CardItem has the correct src', () => {
const images = document.querySelectorAll('img[src]'); // Correctamente selecciona las imágenes
const expectedSrcs = ['images/creditos.jpg', 'images/github.jpg', 'images/database.jpg', 'images/documentation.jpg', 'images/api-logic.jpg'];

expect(images.length).toBe(expectedSrcs.length);
images.forEach((img, index) => {
expect(img.src).toContain(expectedSrcs[index]);
});
});




});
127 changes: 71 additions & 56 deletions webapp/src/components/pages/AddUser.test.js
Original file line number Diff line number Diff line change
@@ -1,72 +1,87 @@
import React from 'react';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { render, screen, fireEvent, waitFor } from '@testing-library/react';
import AddUser from './AddUser';
import { BrowserRouter as Router } from 'react-router-dom';
import {AuthProvider} from "../../AuthContext";
const mockAxios = new MockAdapter(axios);

describe('AddUser component', () => {
beforeEach(() => {
mockAxios.reset();
});

it('should add user successfully', async () => {
render(
<Router>
<AuthProvider>
<AddUser />
</AuthProvider>
</Router>
import { BrowserRouter } from 'react-router-dom';
import { AuthContext } from '../../AuthContext';
import axios from 'axios';

jest.mock('axios');
const mockHandleLogin = jest.fn();

function renderAddUser() {
return render(
<BrowserRouter>
<AuthContext.Provider value={{ handleLogin: mockHandleLogin }}>
<AddUser />
</AuthContext.Provider>
</BrowserRouter>
);
}

const usernameInput = screen.getByLabelText(/Nombre de Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const addUserButton = screen.getByRole('button', { name: /Registrarse/i });
function fillAndSubmitForm(username, password) {
fireEvent.change(screen.getByLabelText(/nombre de usuario/i), { target: { value: username } });
fireEvent.change(screen.getByLabelText(/contraseña/i), { target: { value: password } });
fireEvent.click(screen.getByRole('button', { name: /registrarse/i }));
}

// Mock the axios.post request to simulate a successful response
mockAxios.onPost('http://localhost:8000/adduser').reply(200);
test('renderiza el formulario de registro', () => {
renderAddUser();
expect(screen.getByLabelText(/nombre de usuario/i)).toBeInTheDocument();
expect(screen.getByLabelText(/contraseña/i)).toBeInTheDocument();
expect(screen.getByRole('button', { name: /registrarse/i })).toBeInTheDocument();
});

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword2' } });
test('Errores de validacion al crear una nueva cuenta', async () => {
renderAddUser();
fillAndSubmitForm('abc', '12345678');

// Trigger the add user button click
fireEvent.click(addUserButton);
await waitFor(() => {
expect(screen.getByText(/el nombre de usuario debe tener al menos 4 caracteres/i)).toBeInTheDocument();
});
});

test('La contraseña debe tener al menos 8 caracteres', async () => {
renderAddUser();
fillAndSubmitForm('testuser', 'abc');

// Wait for the Snackbar to be open
await waitFor(() => {
expect(screen.getByText(/Usuario añadido correctamente/i)).toBeInTheDocument();
expect(screen.getByText(/la contraseña debe tener al menos 8 caracteres/i)).toBeInTheDocument();
});
});

it('should handle error when adding user', async () => {
render(
<Router>
<AuthProvider>
<AddUser />
</AuthProvider>
</Router>
);
});

const usernameInput = screen.getByLabelText(/Nombre de Usuario/i);
const passwordInput = screen.getByLabelText(/Contraseña/i);
const addUserButton = screen.getByRole('button', { name: /Registrarse/i });
test('La contraseña debe contener al menos una letra mayúscula', async () => {
renderAddUser();
fillAndSubmitForm('testuser', 'abcdefgh');

// Mock the axios.post request to simulate an error response
mockAxios.onPost('http://localhost:8000/adduser').reply(500, { error: 'Internal Server Error' });
await waitFor(() => {
expect(screen.getByText(/la contraseña debe contener al menos una letra mayúscula/i)).toBeInTheDocument();
});
});

test('La contraseña debe contener al menos un número', async () => {
renderAddUser();
fillAndSubmitForm('testuser', 'Abcdefgh');

await waitFor(() => {
expect(screen.getByText(/la contraseña debe contener al menos un número/i)).toBeInTheDocument();
});
});

// Simulate user input
fireEvent.change(usernameInput, { target: { value: 'testUser' } });
fireEvent.change(passwordInput, { target: { value: 'testPassword2' } });
test('Registro exitoso de usuario', async () => {
axios.post.mockImplementation((url) => {
if (url.includes('/adduser')) {
return Promise.resolve({ status: 200 });
} else if (url.includes('/login')) {
return Promise.resolve({ data: { token: 'fakeToken123' } });
}
});

// Trigger the add user button click
fireEvent.click(addUserButton);
renderAddUser();
fillAndSubmitForm('validUser', 'Valid1234');

// Wait for the error Snackbar to be open
await waitFor(() => {
expect(screen.getByText(/Error: Internal Server Error/i)).toBeInTheDocument();
expect(screen.getByText('Usuario añadido correctamente')).toBeInTheDocument();
});
});
});

// Verificar redireccionamiento a la página inicial
expect(window.location.pathname).toBe('/');
});