diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 52f7588..c639312 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -10,21 +10,6 @@ const port = 8010; const axios = require('axios'); const userServiceUrl = process.env.USER_SERVICE_URL || 'http://localhost:8001'; -/* -const User = require('../users/userservice/user-model'); -const mongoose = require('mongoose'); -const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/wiq-en1a-users'; -mongoose.connect(mongoUri); - -const mongo = mongoose.connection; - -// Check connection done correctly -mongo.on('error', console.error.bind(console, 'MongoDB connection error:')); -mongo.once('open', function () { - console.log('Connected to MongoDB successfully'); -}); -*/ - app.use(express.static('public')); app.use(express.json()); @@ -256,15 +241,4 @@ const server = app.listen(port, () => { console.log(`Questions service listening on http://localhost:${port}`); }); -module.exports = server - - -/** - * - * const { answer, username, category } =JSON.parse(req.body); - - if(correctImg==answer){ - await axios.post(userServiceUrl+'/addpoints', - {username: username, category: category, correct: "true" } ); - * - */ \ No newline at end of file +module.exports = server \ No newline at end of file diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js index 079f4f4..a416657 100644 --- a/questionservice/question-service.test.js +++ b/questionservice/question-service.test.js @@ -1,4 +1,5 @@ const request = require('supertest'); +const axios = require('axios'); const app = require('./question-service'); afterAll(async () => { @@ -7,6 +8,8 @@ afterAll(async () => { // Mock the fetch function global.fetch = jest.fn(); +// Mock axios +jest.mock('axios'); var imgsToAssociatesMap = new Map() @@ -32,6 +35,17 @@ jest.spyOn(global, 'fetch').mockImplementation(() => { }); describe('Question Service', () => { + axios.post.mockImplementation((url, data) => { + if (url.endsWith('/addpoints')) { + if(data.correct=="true"){ + return Promise.resolve({ response: "Correct answer handled" }); + } else if (data.correct=="false"){ + return Promise.resolve({ response: "Incorrect answer handled" }); + } + return Promise.resolve({ response: "Something went wrong" }); + } + }); + // Test /imgs/flags/question endpoint it('should return a flags question with 4 images as options', async () => { const response = await request(app).get('/imgs/flags/question'); @@ -94,7 +108,7 @@ describe('Question Service', () => { const responseAnswer = await request(app) .post("/imgs/answer") .set('Content-Type', 'application/json') - .send({answer:correctImage, question:question}) + .send({answer:correctImage, question:question, username:"username", category:"foods"}) expect(responseAnswer.body.correct).toBe("true") }); @@ -119,7 +133,7 @@ describe('Question Service', () => { const responseAnswer = await request(app) .post("/imgs/answer") .set('Content-Type', 'application/json') - .send({answer:incorrectImageAnswer, question:question}) + .send({answer:incorrectImageAnswer, question:question, username:"username", category:"foods"}) expect(responseAnswer.body.correct).toBe("false") expect(responseAnswer.body.associate).toBe(imgsToAssociatesMap.get(incorrectImageAnswer)) });