From 190269f737ac35ee7396cf6bb33fe51ffca0c012 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Sun, 7 Apr 2024 13:12:28 +0200 Subject: [PATCH 1/3] Question service tests fixed to include new answer handling --- questionservice/question-service.js | 28 +----------------------- questionservice/question-service.test.js | 18 +++++++++++++-- 2 files changed, 17 insertions(+), 29 deletions(-) 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)) }); From 7d57ea5f1b133f990ba29811d1b6ef6af0b24666 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Sun, 7 Apr 2024 13:37:25 +0200 Subject: [PATCH 2/3] User service test fixed --- users/userservice/user-service.js | 3 ++- users/userservice/user-service.test.js | 9 +++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 7cdf1c3..2e0e67e 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -122,8 +122,9 @@ app.post('/adduser', async (req, res) => { }); await newUser.save(); - res.json(newUser); + res.status(200).json(newUser); } catch (error) { + console.log(error) res.status(400).json({ error: error.message }); }}); diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index 8dd8ea1..89fc9d9 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -20,11 +20,16 @@ describe('User Service', () => { it('should add a new user on POST /adduser', async () => { const newUser = { username: 'testuser', + email: 'testemail', password: 'testpassword', }; - const response = await request(app).post('/adduser').send(newUser); + 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', newUser.username); + expect(response.body).toHaveProperty('email', newUser.email); + expect(response.body.password).not.toBe(newUser.password); }); }); From 21a3574f46393b63a73b54453f4d4c726b9bd916 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Fern=C3=A1ndez=20Noriega?= Date: Sun, 7 Apr 2024 14:48:20 +0200 Subject: [PATCH 3/3] Revert "User service test fixed" This reverts commit 7d57ea5f1b133f990ba29811d1b6ef6af0b24666. --- users/userservice/user-service.js | 3 +-- users/userservice/user-service.test.js | 9 ++------- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/users/userservice/user-service.js b/users/userservice/user-service.js index 2e0e67e..7cdf1c3 100644 --- a/users/userservice/user-service.js +++ b/users/userservice/user-service.js @@ -122,9 +122,8 @@ app.post('/adduser', async (req, res) => { }); await newUser.save(); - res.status(200).json(newUser); + res.json(newUser); } catch (error) { - console.log(error) res.status(400).json({ error: error.message }); }}); diff --git a/users/userservice/user-service.test.js b/users/userservice/user-service.test.js index 89fc9d9..8dd8ea1 100644 --- a/users/userservice/user-service.test.js +++ b/users/userservice/user-service.test.js @@ -20,16 +20,11 @@ describe('User Service', () => { it('should add a new user on POST /adduser', async () => { const newUser = { username: 'testuser', - email: 'testemail', password: 'testpassword', }; - const response = await request(app) - .post('/adduser') - .send(newUser); + const response = await request(app).post('/adduser').send(newUser); expect(response.status).toBe(200); - expect(response.body).toHaveProperty('username', newUser.username); - expect(response.body).toHaveProperty('email', newUser.email); - expect(response.body.password).not.toBe(newUser.password); + expect(response.body).toHaveProperty('username', 'testuser'); }); });