Skip to content

Commit

Permalink
Merge branch 'develop' into service_question_generator
Browse files Browse the repository at this point in the history
  • Loading branch information
AbelMH1 authored Apr 29, 2024
2 parents f3b4bfd + 6205aba commit 38288c2
Show file tree
Hide file tree
Showing 45 changed files with 9,620 additions and 2,329 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
21 changes: 19 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,23 @@ services:
MONGODB_URI: mongodb://mongodb:27017/userdb
restart: always

apisgatewayservice:
container_name: apisgatewayservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es6c/apisgatewayservice:latest
profiles: ["dev", "prod"]
build: ./apisgatewayservice
depends_on:
- mongodb
ports:
- "8100:8100"
networks:
- mynetwork
environment:
STORE_QUESTION_SERVICE_URL: http://localhost:8004
USER_STATS_SERVICE_URL: http://localhost:8003
USER_SERVICE_URL: http://localhost:8001
restart: always

gameservice:
container_name: gameservice-${teamname:-defaultASW}
image: ghcr.io/arquisoft/wiq_es6c/gameservice:latest
Expand Down Expand Up @@ -145,7 +162,7 @@ services:
prometheus:
image: prom/prometheus
container_name: prometheus-${teamname:-defaultASW}
profiles: ["dev"]
profiles: ["dev", "prod"]
networks:
- mynetwork
volumes:
Expand All @@ -159,7 +176,7 @@ services:
grafana:
image: grafana/grafana
container_name: grafana-${teamname:-defaultASW}
profiles: ["dev"]
profiles: ["dev", "prod"]
networks:
- mynetwork
volumes:
Expand Down
24 changes: 14 additions & 10 deletions gameservice/game-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,19 @@ app.use(cors());

var gameId = 0;

app.get('/generateGameUnlimitedQuestions', async (req, res) => {
try {
app.get('/generateGame', async (req, res) => {
console.log("Llegamos a crear un id del juego")
var gameId = generateAleatoryString()
res.json(gameId)
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' })
}
})


// Route for getting questions
app.get('/gameUnlimitedQuestions', async (req, res) => {
app.get('/questions', async (req, res) => {
try {
// TODO: Implement logic to fetch questions from MongoDB and send response
// const questions = await Question.find()
console.log("Llegamos a pedir preguntas")
const questionGenerated = await axios.get(`${questionService}/questions?n_preguntas=${1}`);
console.log("Pedimos las preguntas")
const questionGenerated = await axios.get(questionService + req.url);
res.json(questionGenerated.data);
} catch (error) {
// res.status(500).json({ message: error.message })
Expand All @@ -60,15 +54,25 @@ 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) {
res.status(500).json({ error: 'Internal Server Error' });
}
})

app.get('/topics', async (req, res) => {
try {
const topics = await axios.get(`${questionService}/topics`)
res.json(topics.data)
} catch (error) {
res.status(500).json({ error: 'Internal Server Error' });
}
})

app.use((err, req, res, next) => {
console.error(`An error occurred: ${err}`);
res.status(500).send(`An error occurred: ${err.message}`);
Expand Down
92 changes: 92 additions & 0 deletions gameservice/game-service.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const request = require('supertest');
const axios = require('axios');
const app = require('./game-service');

afterAll(async () => {
app.close();
});

jest.mock('axios');

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

it('should return a game id', async () => {
const response = await request(app)
.get('/generateGame');
expect(response.statusCode).toBe(200);
expect(response.body).toHaveLength(24);
})

it('should fail', async () => {
const response = await request(app)
.get('/questions?n_preguntas=2&n_respuestas=5&tema=capital&tema=lenguaje')
expect(response.statusCode).toBe(500)
})
})

describe('Test the geting topics', () => {
axios.get.mockImplementation((url, data) => {
if (url.endsWith('/topics')) {
return Promise.resolve(["paises", "capitales"])
} else if (url.endsWith('/questions?n_preguntas=1&n_respuestas=4&tema=capital')) {
return Promise.resolve({data: {
pregunta: '¿Cuál es la capital Italia?',
respuesta_correcta: 'Roma',
respuestas_incorrectas: ['Nápoles', 'Florencia', 'Milán'],
}})
}
})

it('should return the topics paises, capitales', async () => {

const response = await request(app)
.get('/topics')
expect(response.statusCode).toBe(200)
})

it('should return a number o questions of a diferent types of topics', async () => {
const response = await request(app)
.get('/questions?n_preguntas=1&n_respuestas=4&tema=capital')
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")

})


})



//Revisar este test por algun motivo no lo está mockeando bien la llamada
describe('Test the store game', () => {
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/history/game')) {
return Promise.resolve("Example string")
}
})

it('should store the data of a game', 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)
})
})
Loading

0 comments on commit 38288c2

Please sign in to comment.