Skip to content

Commit

Permalink
Merge branch 'webapp_interface' of https://github.com/Arquisoft/wiq_es6c
Browse files Browse the repository at this point in the history
 into webapp_interface
  • Loading branch information
uo288574 committed Apr 29, 2024
2 parents e2c79d8 + 4beba06 commit 67d4454
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ jobs:
- run: npm --prefix storeQuestionService ci
- run: npm --prefix userStatsService ci
- run: npm --prefix gameservice ci
- run: npm --prefix gameservice ci
- run: npm --prefix users/authservice test -- --coverage
- run: npm --prefix users/userservice test -- --coverage
- run: npm --prefix gatewayservice test -- --coverage
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix storeQuestionService test -- --coverage
- run: npm --prefix userStatsService test -- --coverage
- run: npm --prefix gameservice test -- --coverage
# - run: npm --prefix gameservice test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix storeQuestionService test -- --coverage
- run: npm --prefix userStatsService test -- --coverage
#- run: npm --prefix gameservice test -- --coverage
- run: npm --prefix gameservice test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
Expand Down
3 changes: 2 additions & 1 deletion gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ app.post('/storeGame', async (req, res) => {
var username = req.body.username
var points = req.body.points
var questions = req.body.questions
var avgtime = req.body.avgtime
console.log("Vamos a guardar resultado")
const store = await axios.post(`${userStatsService}/history/game`, {id, username, points, questions})
const store = await axios.post(`${userStatsService}/history/game`, {id, points, username, questions, avgtime})
console.log("Guardamos resultado")
res.json(store.data)
} catch (error) {
Expand Down
11 changes: 4 additions & 7 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,8 @@ function catchAction(error, res) {
else if('response' in error && 'status' in error.response){
res.status(error.response.status).json({ error: 'Unknown error' });
} else {
console.log("Unknown error: " + error);
}
// } else {
// res.status(500).json({ error: 'Internal server error' });
// }
res.status(500).json({ error: 'Internal server error' });
}
}

app.use(metricsMiddleware);
Expand Down Expand Up @@ -116,9 +113,9 @@ app.post('/storeGame', async (req, res) => {
var username = req.body.username
var points = req.body.points
var questions = req.body.questions
console.log(questions)
var avgtime = req.body.avgtime
console.log("Hacemos la llamada al guardar preguntas")
const post = await axios.post(gameService + `/storeGame`, {id, username, points, questions})
const post = await axios.post(gameService + `/storeGame`, {id, username, points, questions, avgtime})
console.log("Devuelve la llamada")
res.json(post.data)
} catch (error) {
Expand Down
186 changes: 172 additions & 14 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@ afterAll(async () => {

jest.mock('axios');

describe('Gateway Service', () => {
describe('Gateway Service with errors', () => {
// Mock responses from external services
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
return Promise.resolve({ data: { token: 'mockedToken' } });
} else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
}
});

// Test /login endpoint
it('should forward login request to auth service', async () => {
it('should fail', async () => {
const response = await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });

expect(response.statusCode).toBe(200);
expect(response.body.token).toBe('mockedToken');
expect(response.statusCode).toBe(500);
});

// Test /adduser endpoint
Expand All @@ -34,7 +26,173 @@ describe('Gateway Service', () => {
.post('/adduser')
.send({ username: 'newuser', password: 'newpassword' });

expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
expect(response.statusCode).toBe(500);
});
});

it('should return the data of the user', async () => {
const response = await request(app)
.get('/history/games/asdf')

console.log(response)
expect(response.statusCode).toBe(500);
})

it('should return all the questions stored in the database', async () => {
const response = await request(app)
.get('/history/questions')

expect(response.statusCode).toBe(500)
})

it('should generate the id of a game', async () => {
const response = await request(app)
.get('/generateGame')

expect(response.statusCode).toBe(500)
})

it('should return the number of questions and answes solicitated 1 question and 4 answers is the default', async () => {
const response = await request(app)
.get('/questions')

expect(response.statusCode).toBe(500)
})

it('should return the number of questions and answes solicitated 1 question and 4 answers is the default', async () => {
const response = await request(app)
.post('/storeGame')
.send("Example data")

expect(response.statusCode).toBe(500)
})

it('should return the posible topics for the aplication', async () => {
const response = await request(app)
.get('/topics')

expect(response.statusCode).toBe(500)
})
});


// describe('Gateway Service', () => {
// // Mock responses from external services
// axios.post.mockImplementation((url, data) => {
// if (url.endsWith('/login')) {
// return Promise.resolve({ data: { token: 'mockedToken' } });
// } else if (url.endsWith('/adduser')) {
// return Promise.resolve({ data: { userId: 'mockedUserId' } });
// } else if (url.endsWith("/storeGame")) {
// return Promise.resolve({data: {status: "Correct call"}})
// }
// });

// axios.get.mockImplementation((url, data) => {
// if (url.endsWith('/history/games/asdf')) {
// return Promise.resolve({data: {status: "Correct call"}})
// } else if (url.endsWith('/history/questions')) {
// return Promise.resolve({data: {status: "Correct call"}})
// } else if (url.endsWith("/questions")) {
// return Promise.resolve({data: {status: "Correct call"}})
// } else if (url.endsWith("/generateGame")) {
// return Promise.resolve({data: {status: "Correct call"}})
// } else if (url.endsWith('/topics')) {
// return Promise.resolve({data: {
// pregunta: '¿Cuál es la capital Italia?',
// respuesta_correcta: 'Roma',
// respuestas_incorrectas: ['Nápoles', 'Florencia', 'Milán'],
// }})
// }
// });

// // Test /login endpoint
// it('should forward login request to auth service', async () => {
// const response = await request(app)
// .post('/login')
// .send({ username: 'testuser', password: 'testpassword' });

// expect(response.statusCode).toBe(200);
// expect(response.body.token).toBe('mockedToken');
// });

// // Test /adduser endpoint
// it('should forward add user request to user service', async () => {
// const response = await request(app)
// .post('/adduser')
// .send({ username: 'newuser', password: 'newpassword' });

// expect(response.statusCode).toBe(200);
// expect(response.body.userId).toBe('mockedUserId');
// });

// it('should return the data of the user', async () => {
// const response = await request(app)
// .get('/history/games/asdf')

// console.log(response)
// expect(response.statusCode).toBe(200);
// expect(response.body.status).toBe("Correct call")
// })

// it('should return all the questions stored in the database', async () => {
// const response = await request(app)
// .get('/history/questions')

// expect(response.statusCode).toBe(200)
// expect(response.body.status).toBe("Correct call")
// })

// it('should generate the id of a game', async () => {
// const response = await request(app)
// .get('/generateGame')

// expect(response.statusCode).toBe(200)
// expect(response.body.status).toBe("Correct call")
// })

// it('should return the number of questions and answes solicitated 1 question and 4 answers is the default', async () => {
// const response = await request(app)
// .get('/questions')

// expect(response.statusCode).toBe(200)
// expect(response.body.status).toBe("Correct call")
// })

// it('should return the number of questions and answes solicitated 1 question and 4 answers is the default', async () => {
// const newGame = {
// id: "1",
// username: 'testuser',
// points: 100,
// questions: [{
// title: 'Question 1',
// answers: ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4'],
// ansIndex: [1, 2]
// }, {
// title: 'Question 2',
// answers: ['Answer 1', 'Answer 2', 'Answer 3', 'Answer 4'],
// ansIndex: [1, 1]
// }]
// };
// const response = await request(app)
// .post('/storeGame')
// .send(newGame)

// expect(response.statusCode).toBe(200)
// expect(response.body.status).toBe("Correct call")
// })

// it('should return the posible topics for the aplication', async () => {
// const response = await request(app)
// .get('/topics')

// expect(response.statusCode).toBe(200)
// expect(response.body.pregunta).toBe('¿Cuál es la capital Italia?')
// expect(response.body.respuesta_correcta).toBe('Roma')
// expect(response.body.respuestas_incorrectas[0]).toBe("Nápoles")
// expect(response.body.respuestas_incorrectas[1]).toBe("Florencia")
// expect(response.body.respuestas_incorrectas[2]).toBe("Milán")
// })
// });



30 changes: 22 additions & 8 deletions webapp/src/components/FirstGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ const Quiz = () => {
// const [currentQuestionIndex, setCurrentQuestionIndex] = useState(storedInt);
// const [isCorrect, setIsCorrect] = useState(false);
const [remTime, setRemTime] = useState(0);
const [totalTime, setTotalTime] = useState(0);

useEffect(() => {
const time = setInterval(() => {
setRemTime((progress) => {
if(progress === 100){
// checkAnswer(-1);
checkAnswer(-1);
console.log("Antes", totalTime)
setTotalTime(totalTime + progress/10)
console.log("Despues", totalTime)
return 0;
}
const diff = 4;
Expand All @@ -53,7 +57,6 @@ const Quiz = () => {
};

function changeButtons(param) {
console.log("Entramos aqui")
var borders = document.getElementsByClassName("border");;
for(var i = 0; i < Math.min(borders.length, allQuestions[0].options.length); i++) {
borders[i].setAttribute("data-disabled", param)
Expand All @@ -65,7 +68,10 @@ const Quiz = () => {
var username = localStorage.getItem("username")
console.log(username)
console.log(questions)
const response = await axios.post(`${apiEndpoint}/storeGame`, { id, username, points, questions});
console.log(totalTime)
var avgtime = totalTime/questions.length
console.log(avgtime)
const response = await axios.post(`${apiEndpoint}/storeGame`, { id, username, points, questions, avgtime});
questions = []
points = 0
console.log(response)
Expand All @@ -80,7 +86,6 @@ const Quiz = () => {
}
load = false


console.log("Todas las preguntas", allQuestions)
isCorrect = (option === allQuestions[currentQuestionIndex].correctAnswer);

Expand All @@ -102,14 +107,20 @@ const Quiz = () => {
currentQuestionIndex = (currentQuestionIndex + 1);
botonCorrecta.style.backgroundColor = previousBackgroundColor
changeButtons("false")
await gameStore()

haveFailedQuestion = false;
load = true
navigator('/menu')
haveEnter = false
console.log("Calbo")

if (currentQuestionIndex === allQuestions.length ) {
console.log("Entramos aqui")
gameStore()
navigator('/menu')
}
return
}


}
const botonIncorrecta = document.getElementById('option-' + allQuestions[currentQuestionIndex].options.indexOf(option))

if (!isCorrect) {
Expand Down Expand Up @@ -140,6 +151,9 @@ const Quiz = () => {
}

isCorrect = (false)
console.log("Durante antes", totalTime)
await setTotalTime(totalTime + remTime/10)
console.log("Durante despues", totalTime)
setRemTime(0)

load = true
Expand Down

0 comments on commit 67d4454

Please sign in to comment.