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

83 navigability in the UI #94

Merged
merged 43 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
b9efe75
Added favicon and title to webapp
uo289267 Apr 14, 2024
690c90a
Added one back button
uo289267 Apr 14, 2024
1710458
Added back Buttons to GameMenu and Mocks
uo289267 Apr 14, 2024
4153efc
Added Game configuration View to configurate customized games and com…
uo289267 Apr 14, 2024
6b48eac
Added new endpoints for taking different sizes of questions
Mister-Mario Apr 14, 2024
aa34cf9
Added new endpoint to get an specific amount of questions of a specif…
Mister-Mario Apr 15, 2024
581feba
Added Game Configurator logic
uo289267 Apr 15, 2024
759076b
Added style to game configurator
uo289267 Apr 15, 2024
d31b5ab
Added enpoints for getting the ranking information of the competitive…
Mister-Mario Apr 15, 2024
a2b2f6a
Removed console log
Mister-Mario Apr 15, 2024
9451f87
Commented mocks in order to check sonar cloud evaluation
Mister-Mario Apr 15, 2024
ccfd483
Fixed a bug that was already solved in another branch
Mister-Mario Apr 15, 2024
18636fe
Removed console log
Mister-Mario Apr 15, 2024
ab9b42c
Refactored the gateway-service tests for removing duplicated code
Mister-Mario Apr 15, 2024
88c100d
Removed unused hook for e2e tests
Mister-Mario Apr 15, 2024
6a49ed9
Added Ranking View need of style
uo289267 Apr 16, 2024
c178367
Added ranking View
uo289267 Apr 17, 2024
9942584
Added back button to menu in ranking
uo289267 Apr 17, 2024
8c179b9
Added test for BackButton and RankingView (need finishing up)
uo289267 Apr 17, 2024
ee00ba2
Added Game Configurator tests
uo289267 Apr 17, 2024
7a715c0
Finished Ranking jest tests
uo289267 Apr 17, 2024
c51a18d
Modified sonar cloud properties to not check code duplication on test…
Mister-Mario Apr 18, 2024
4c54efc
Resolved jest tests problems
uo289267 Apr 18, 2024
70a03a1
Added user for userContext in tests
uo289267 Apr 18, 2024
50b0781
Added new style for ranking
uo289267 Apr 18, 2024
8c0b47f
Added gameMenu tests to e2e
uo289267 Apr 18, 2024
6d29ff7
Removed mock from ranking
uo289267 Apr 18, 2024
8b72579
Fixing tests
uo289267 Apr 18, 2024
cd62261
Corrected Game Menu e2e
uo289267 Apr 20, 2024
34945b4
GameMenu e2e
uo289267 Apr 20, 2024
c48eb0b
Merge branch 'master' into 83-navigability-in-the-ui
uo289267 Apr 20, 2024
a503257
e2e fixes
uo289267 Apr 20, 2024
504232b
Added e2e test
uo289267 Apr 20, 2024
b7249ce
Added enpoint functionalities to return the ranking position of each …
Mister-Mario Apr 20, 2024
2894658
Fixed e2e
uo289267 Apr 20, 2024
417d0b9
Merge branch '83-navigability-in-the-ui' of https://github.com/Arquis…
uo289267 Apr 20, 2024
489a687
Removed weird import, probably VS Code stuff
Mister-Mario Apr 20, 2024
2f4e6a3
Made view work with the new endpoint
Mister-Mario Apr 21, 2024
e497488
Added search in Ranking
uo289267 Apr 21, 2024
3a0be80
Merge branch '83-navigability-in-the-ui' of https://github.com/Arquis…
uo289267 Apr 21, 2024
3801d71
Tested that the search user in ranking works
Mister-Mario Apr 21, 2024
e407b7b
Made the default search value the username and fixed bad mock on tests
Mister-Mario Apr 21, 2024
25d65b9
Added test case to increase test coverage
Mister-Mario Apr 21, 2024
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
53 changes: 52 additions & 1 deletion gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,39 @@
}
});

app.get('/questions/:lang/:amount/:type', async (req, res) => {
try {
const lang = req.params.lang.toString();
const amount = req.params.amount.toString();
const type = req.params.type.toString();
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount + '/' + type);

res.json(questionResponse.data);
} catch (error) {

res.status(error.response.status).json({ error: error.response.data.error });
}
});


app.get('/questions/:lang/:amount', async (req, res) => {
try {
const lang = req.params.lang.toString();
const amount = req.params.amount.toString();
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang + '/' + amount);

res.json(questionResponse.data);
} catch (error) {

res.status(error.response.status).json({ error: error.response.data.error });
}
});

app.get('/questions/:lang', async (req, res) => {
try {
const lang = req.params.lang;
const lang = req.params.lang.toString();
// Forward the question request to the quetion service
const questionResponse = await axios.get(questionServiceUrl+'/questions/' + lang);

Expand All @@ -81,6 +111,27 @@
}
});

app.get('/record/ranking/top10', async(req, res)=>{
try {
// Forward the record request to the record service
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/top10');
res.json(recordResponse.data);
} catch (error) {
res.send(error);
}
});

app.get('/record/ranking/:user', async(req, res)=>{
try {
const user = req.params.user;
// Forward the record request to the record service
const recordResponse = await axios.get(recordServiceUrl + '/record/ranking/' + user);
res.json(recordResponse.data);
} catch (error) {
res.send(error);
}
});

app.get('/record/:user', async(req, res)=>{
try {
const user = req.params.user;
Expand Down
77 changes: 64 additions & 13 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@ describe('Gateway Service', () => {
}
});

const question = { data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] };

//Dont need to check a good record just that it redirects the call
const record = {data : {record:'undefined'}};

axios.get.mockImplementation((url, data) => {
if (url.endsWith('/questions')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
return Promise.resolve(question);
} else if (url.endsWith('/questions/es/1/CAPITAL')){
return Promise.resolve(question);
} else if (url.endsWith('/questions/es/1')){
return Promise.resolve(question);
} else if (url.endsWith('/questions/es')){
return Promise.resolve({ data: [{question: "¿Cuál es la población de Oviedo?",
answers: ["225089","272357","267855","231841"]}] });
return Promise.resolve(question);

} else if(url.endsWith('/record/testuser')){
//Dont need to check a good record just that it redirects the call
return Promise.resolve({data : {record:'undefined'}})
return Promise.resolve(record)
} else if(url.endsWith('/record/ranking/top10')){
return Promise.resolve(record)
} else if(url.endsWith('/record/ranking/testuser')){
return Promise.resolve(record)
}
});

Expand Down Expand Up @@ -59,17 +71,31 @@ describe('Gateway Service', () => {
const response = await request(app)
.get('/questions');

expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
checkQuestion(response);
});

// Test /questions/:lang endpoint
it('should forward questions request to question service', async () => {
const response = await request(app)
.get('/questions/es');

expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
checkQuestion(response);
});

// Test /questions/:lang/:amount endpoint
it('should forward questions request to question service', async () => {
const response = await request(app)
.get('/questions/es/1');

checkQuestion(response);
});

// Test /questions/:lang/:amount/:type endpoint
it('should forward questions request to question service', async () => {
const response = await request(app)
.get('/questions/es/1/CAPITAL');

checkQuestion(response);
});

// Test /record endpoint
Expand All @@ -86,7 +112,32 @@ describe('Gateway Service', () => {
const response = await request(app)
.get('/record/testuser');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('record', "undefined");
checkRecord(response);
});

// Test /record/ranking/:user endpoint
it('should forward record request to record service', async () => {
const response = await request(app)
.get('/record/ranking/testuser');

checkRecord(response);
});
});

// Test /record/ranking/top10 endpoint
it('should forward record request to record service', async () => {
const response = await request(app)
.get('/record/ranking/top10');
checkRecord(response);

});
});

function checkRecord(response){
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('record', "undefined");
}

function checkQuestion(response){
expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
}
59 changes: 59 additions & 0 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,60 @@ app.get('/questions', async (req, res) => {
}
});

app.get('/questions/:lang/:amount/:type', async (req, res) => {
try {
const lang = req.params.lang.toString();
let amount = checkAmount(parseInt(req.params.amount));
const type = req.params.type.toString();

if(amount > 20 || amount < 1)
amount = 5;

const questions = await Question.aggregate([
{$match: {language : lang, type: type}}, //Condition
{$sample: {size:amount}}
]);

let jsonResult = {};
for (let i = 0; i < questions.length; i++) {
const question = questions[i];
jsonResult[i] = {
question : question.question,
answers : question.answers
}
}
res.json(jsonResult);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

app.get('/questions/:lang/:amount', async (req, res) => {
try {
const lang = req.params.lang;
let amount = checkAmount(parseInt(req.params.amount));



const questions = await Question.aggregate([
{$match: {language : lang}}, //Condition
{$sample: {size:amount}}
]);

let jsonResult = {};
for (let i = 0; i < questions.length; i++) {
const question = questions[i];
jsonResult[i] = {
question : question.question,
answers : question.answers
}
}
res.json(jsonResult);
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
});

app.get('/questions/:lang', async (req, res) => {
try {
const lang = req.params.lang;
Expand All @@ -53,6 +107,11 @@ app.get('/questions/:lang', async (req, res) => {
}
});

function checkAmount(amount){
if(amount > 20 || amount < 1)
return 5;
return amount;
}

const server = app.listen(port, () => {
console.log(`Question Service listening at http://localhost:${port}`);
Expand Down
50 changes: 49 additions & 1 deletion questionservice/question-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ beforeAll(async () => {
app = require('./question-service');

//Populate db
for(let i = 0; i < 6 ; i++){
for(let i = 0; i < 21 ; i++){
const question = new Question( {
question: "¿Cuál es la población de Oviedo?",
answers: [
Expand Down Expand Up @@ -60,4 +60,52 @@ describe('Question Service', () => {
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(5);
});


it('Should give 20 questions /questions/es/20', async () => {

let response = await request(app).get('/questions/es/20');
expect(response.status).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(20);
});

it('Should give 1 questions /questions/es/1', async () => {

let response = await request(app).get('/questions/es/20');
expect(response.status).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(20);
});

it('Should give 5 questions as the max is 20 /questions/es/21', async () => {

let response = await request(app).get('/questions/es/21');
expect(response.status).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(5);
});

it('Should give 5 questions as the min is 1 /questions/es/0', async () => {

let response = await request(app).get('/questions/es/0');
expect(response.status).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(5);
});

it('Should give 10 questions /questions/es/10/POPULATION', async () => {

let response = await request(app).get('/questions/es/10/POPULATION');
expect(response.status).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
expect(Object.keys(response.body).length).toBe(10);
});

it('Should give 0 questions /questions/es/10/CAPITAL', async () => {

let response = await request(app).get('/questions/es/10/CAPITAL');
expect(response.status).toBe(200);
expect(Object.keys(response.body).length).toBe(0);
});
});
3 changes: 2 additions & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ sonar.coverage.exclusions=**/*.test.js
sonar.sources=webapp/src/components,users/authservice,users/userservice,gatewayservice
sonar.sourceEncoding=UTF-8
sonar.exclusions=node_modules/**
sonar.javascript.lcov.reportPaths=**/coverage/lcov.info
sonar.javascript.lcov.reportPaths=**/coverage/lcov.info
sonar.cpd.exclusions=**/*.test.js,**/*steps.js,**/*Tests.java
18 changes: 10 additions & 8 deletions users/recordservice/record-model.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
const mongoose = require('mongoose');
const { Schema } = mongoose;

const recordSchema = new mongoose.Schema({
user: String,
const recordSchema = new Schema({
user: { type: String, required: true },
games: [{
questions: [{
question: String,
answers: [String],
answerGiven: String,
correctAnswer: String
question: { type: String, required: true },
answers: { type: [String], required: true },
answerGiven: { type: String, required: true },
correctAnswer: { type: String, required: true }
}],
points: Number,
date: String
points: { type: Number, required: true },
date: { type: String, required: true },
competitive: { type: Boolean, required: true }
}]
});
const Record = mongoose.model('Record', recordSchema);
Expand Down
Loading