Skip to content

Commit

Permalink
Merge branch 'develop' into ray
Browse files Browse the repository at this point in the history
  • Loading branch information
UO290054 committed Apr 27, 2024
2 parents 19da8d8 + 74e846e commit aea0c61
Show file tree
Hide file tree
Showing 7 changed files with 227 additions and 80 deletions.
6 changes: 4 additions & 2 deletions questions/creationservice/creation-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ var answerOptions = [];

var randomQuerySelector;
// Array of the possible queries
var queries = ['SELECT DISTINCT ?questionObject ?questionObjectLabel ?answer ?answerLabel WHERE { ?questionObject wdt:P31 wd:Q6256. ?questionObject wdt:P36 ?answer. SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es".}}'];
var queries = ['SELECT DISTINCT ?questionObject ?questionObjectLabel ?answer ?answerLabel WHERE { ?questionObject wdt:P31 wd:Q6256. ?questionObject wdt:P36 ?answer. SERVICE wikibase:label {bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es".}}',
'SELECT DISTINCT ?questionObject ?questionObjectLabel ?answer ?answerLabel WHERE { ?questionObject wdt:P31 wd:Q11344; wdt:P1086 ?answer. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es".}}',
'SELECT ?questionObject ?questionObjectLabel ?answer ?answerLabel WHERE { ?questionObject wdt:P31 wd:Q6256; wdt:P1082 ?answer. SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],es".}}'];
// Array of the possible questions
var questions = ["¿Cuál es la capital de "];
var questions = ["¿Cuál es la capital de ","¿Cuál es el número atómico del ", "¿Cuántos habitantes tiene "];

// Recieves the information of the query and select wich data use on the question
function getQuestionInfo(info){
Expand Down
21 changes: 21 additions & 0 deletions users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ const user = {
password: 'testpassword',
};

const user2 = {
username: 'testuser',
password: 'wrongpass',
};

async function addUser(user){
const hashedPassword = await bcrypt.hash(user.password, 10);
const newUser = new User({
Expand Down Expand Up @@ -42,4 +47,20 @@ describe('Auth Service', () => {
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
});

it('Should perform a fail login operation du to incorrect answer /login', async () => {
const response = await request(app).post('/login').send(user2);
expect(response.status).toBe(401);
expect(response.body).toHaveProperty('error', 'Invalid credentials');
});

it('Should perform a server error due to missing value /login', async () => {
const response = await request(app).post('/login')
.send(user3 = {
username: 'testuser',
});
expect(response.status).toBe(500);
expect(response.body).toHaveProperty('error', 'Internal Server Error');
});

});
48 changes: 45 additions & 3 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const bcrypt = require('bcrypt');
const User = require('./user-model');

let mongoServer;
let app;

const user = {
username: 'testuser',
password: 'testpassword',
};

async function addUser(user){
const hashedPassword = await bcrypt.hash(user.password, 10);
const newUser = new User({
username: user.username,
password: hashedPassword,
});

await newUser.save();
}

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
app = require('./user-service');
await addUser(user);
});

afterAll(async () => {
Expand All @@ -17,14 +35,38 @@ afterAll(async () => {
});

describe('User Service', () => {

//
it('should get the registered users on /getregisteredusers', async () => {

const response = await request(app).get('/getregisteredusers');
expect(response.status).toBe(200);
expect(response.body.length).toBe(1);
expect(Array.from(response.body[0])[0]).toBe('testuser');
});

//
it('should add a new user on POST /adduser', async () => {
const newUser = {
username: 'testuser',
password: 'testpassword',
username: 'testuser2',
password: 'testpassword2',
};

const response = await request(app).post('/adduser').send(newUser);
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
expect(response.body).toHaveProperty('username', 'testuser2');
});

//
it('trying to add a user without username', async () => {
const newUser = {
password: 'testpassword',
};

const response = (await request(app).post('/adduser').send(newUser));
expect(response.status).toBe(400);
expect(response.body).toHaveProperty( "error" , "Missing required field: username" );
});


});
85 changes: 47 additions & 38 deletions webapp/src/components/HistoricalData.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,45 +42,54 @@ const HistoricalData = () => {
<Typography component="h2" style={{ marginTop: '1rem', marginBottom: '1rem' }} className='fs-2 main-title animate__animated animate__backInLeft' variant="h2" sx={{ textAlign: 'center' }}>
Historial de preguntas
</Typography>
<Container component="main" className='contenedor containerTable' >
<TableContainer>
<Table sx={{ minWidth: 650 }} aria-label="customized table">
<TableHead>
<TableRow className='custom-td'>
<TableCell>Pregunta</TableCell>
<TableCell>Opción correcta</TableCell>
<TableCell>Opción incorrecta 1</TableCell>
<TableCell>Opción incorrecta 2</TableCell>
<TableCell>Opción incorrecta 3</TableCell>

<Container component="main" className='contenedor containerTable' >
<div>
<TableContainer>
<Table sx={{ minWidth: 650 }} aria-label="customized table">
<TableHead>
<TableRow className='custom-td'>
<TableCell>Pregunta</TableCell>
<TableCell>Opción correcta</TableCell>
<TableCell>Opción incorrecta 1</TableCell>
<TableCell>Opción incorrecta 2</TableCell>
<TableCell>Opción incorrecta 3</TableCell>
</TableRow>
</TableHead>
<TableBody>
{/* {paginatedData.map((row, rowIndex) => (
<TableRow key={rowIndex}>
{row.map((cell, cellIndex) => (
<TableCell key={cellIndex}>{cell}</TableCell>
))}
</TableRow>
</TableHead>
<TableBody>
{paginatedData.map((row, rowIndex) => (
<TableRow key={rowIndex}>
{row.map((cell, cellIndex) => (
<TableCell key={cellIndex}>{cell}</TableCell>
))}
</TableRow>
))}
{emptyRows > 0 && (
<TableRow style={{ height: 53 * emptyRows }}>
<TableCell colSpan={5} />
</TableRow>
)}
</TableBody>
</Table>
</TableContainer>
<TablePagination
rowsPerPageOptions={[5, 10, 15, 20]} // Opciones de filas por página
component="div"
count={questionsHistory.length} // Total de filas
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
/>
</Container>
</>
))} */}
{paginatedData.map((row, rowIndex) => (
<TableRow key={rowIndex}>
<TableCell>{row[0]}</TableCell>
<TableCell>{row[1]}</TableCell>
<TableCell>{row[2]}</TableCell>
<TableCell>{row[3]}</TableCell>
<TableCell>{row[4]}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</TableContainer>
<TablePagination
component="div"
count={questionsHistory.length}
rowsPerPage={rowsPerPage}
page={page}
onPageChange={handleChangePage}
onRowsPerPageChange={handleChangeRowsPerPage}
rowsPerPageOptions={[5, 10, 15, 20, 25]}
/>
</div>
</Container>

</>

);
};

Expand Down
44 changes: 44 additions & 0 deletions webapp/src/components/HistoricalData.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import HistoricalData from './HistoricalData';
import { BrowserRouter as Router } from 'react-router-dom';

const mockAxios = new MockAdapter(axios);

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

it('muestra la página con el histórico de preguntas generadas', async () => {

const question1 = ['¿Cual es la capital de Venezuela?', 'Caracas', 'Doha', 'Barcelona', 'Nasáu'];
const question2 = ['¿Cual es la capital de Francia?', 'París', 'Londres', 'Madrid', 'Roma'];
const mockUsers = [question1, question2];
mockAxios.onGet("http://localhost:8000/getquestionshistory").reply(200, mockUsers);

render(<Router>
<HistoricalData />
</Router>);

await waitFor(() => {

expect(screen.getByText('¿Cual es la capital de Venezuela?')).toBeInTheDocument();
expect(screen.getByText('Caracas')).toBeInTheDocument();
expect(screen.getByText('Doha')).toBeInTheDocument();
expect(screen.getByText('Barcelona')).toBeInTheDocument();
expect(screen.getByText('Nasáu')).toBeInTheDocument();

expect(screen.getByText('¿Cual es la capital de Francia?')).toBeInTheDocument();
expect(screen.getByText('París')).toBeInTheDocument();
expect(screen.getByText('Londres')).toBeInTheDocument();
expect(screen.getByText('Madrid')).toBeInTheDocument();
expect(screen.getByText('Roma')).toBeInTheDocument();

});
});
});


71 changes: 34 additions & 37 deletions webapp/src/components/RegisteredUsers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,44 +37,41 @@ const RegisteredUsers = () => {

return (
<>
<Navbar />
<Container component="main" maxWidth="xs" sx={{ marginTop: 4 }} className='contenedor' >
<div>
<Typography component="h2" style={{ marginTop: '1rem', marginBottom: '1rem' }} className='fs-2 main-title animate__animated animate__backInLeft' variant="h2" sx={{ textAlign: 'center' }}>
Usuarios registrados
</Typography>
<TableContainer>
<Table>
<TableHead>
<TableRow>
<TableCell className='text-center custom-td'>Nombre de usuario</TableCell>
<TableCell className='text-center custom-td'>Fecha de registro</TableCell>
</TableRow>
</TableHead>
<TableBody>
{paginatedRegisteredUsers.map((row, rowIndex) => (
<TableRow key={rowIndex}>
{row.map((cell, cellIndex) => (
<TableCell key={cellIndex}>{cell}</TableCell>
))}
</TableRow>
))}
</TableBody>
<Navbar />
<Container component="main" maxWidth="xs" sx={{ marginTop: 4 }} className='contenedor' >

<div>
<Typography component="h2" style={{ marginTop: '1rem', marginBottom: '1rem' }} className='fs-2 main-title animate__animated animate__backInLeft' variant="h2" sx={{ textAlign: 'center' }}>
Usuarios registrados
</Typography>
<table>
<thead>
<tr>
<th className='text-center custom-td'>Nombre de usuario</th>
<th className='text-center custom-td'>Fecha de registro</th>
</tr>
</thead>
<tbody>
{/* {registeredUsers.map((row, rowIndex) => (
<tr key={rowIndex}>
{row.map((cell, cellIndex) => (
<td key={cellIndex}>{cell}</td>
))}
</tr>
))} */}
{registeredUsers.map((row, rowIndex) => (
<tr key={rowIndex}>
<td>{row[0]}</td>
<td>{row[1]}</td>
</tr>
))}

</tbody>
</table>
</div>
</Container>
</>

</Table>
</TableContainer>
<TablePagination
component="div"
count={registeredUsers.length}
page={page}
onPageChange={handleChangePage}
rowsPerPage={rowsPerPage}
onRowsPerPageChange={handleChangeRowsPerPage}
rowsPerPageOptions={[5, 10, 15]}
/>
</div>
</Container>
</>
);
};

Expand Down
32 changes: 32 additions & 0 deletions webapp/src/components/RegisteredUsers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import {BrowserRouter as Router} from "react-router-dom";
import RegisteredUsers from './RegisteredUsers';

const mockAxios = new MockAdapter(axios);

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

it('muestra todos los usuarios registrados', async () => {
const user1 = ['pablo', '4/23/2024'];
const mockUsers = [user1];
mockAxios.onGet("http://localhost:8000/getregisteredusers").reply(200, mockUsers);

render(
<Router>
<RegisteredUsers />
</Router>);

await waitFor(() => {

expect(screen.getByText('pablo')).toBeInTheDocument();
expect(screen.getByText('4/23/2024')).toBeInTheDocument();
});

});
});

0 comments on commit aea0c61

Please sign in to comment.