From 36ecadbdee7d142fcb148f2aae62023858a06256 Mon Sep 17 00:00:00 2001 From: Alfonso Date: Fri, 23 Feb 2024 00:03:53 +0100 Subject: [PATCH] question service funcionando por primera vez. Primera version de sus tests --- gatewayservice/gateway-service.test.js | 14 ++++++++++ questionservice/question-service.js | 9 +++++++ questionservice/question-service.test.js | 33 ++++++++++++++++++++++++ webapp/e2e/test-environment-setup.js | 2 ++ 4 files changed, 58 insertions(+) create mode 100644 questionservice/question-service.test.js diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 56eeb124..0a39e530 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -37,4 +37,18 @@ describe('Gateway Service', () => { expect(response.statusCode).toBe(200); expect(response.body.userId).toBe('mockedUserId'); }); + + // Test /addquestion endpoint + it('should add a new question', async () => { + const response = await request(app) + .post('/addquestion') + .send({ + question: 'What is the capital of France?', + options: ['Paris', 'Berlin', 'Madrid', 'Rome'], + correctOptionIndex: 0, + }); + + expect(response.statusCode).toBe(200); + expect(response.body).toHaveProperty('question', 'What is the capital of France?'); + }); }); \ No newline at end of file diff --git a/questionservice/question-service.js b/questionservice/question-service.js index 88c53e91..8f18dcc3 100644 --- a/questionservice/question-service.js +++ b/questionservice/question-service.js @@ -7,6 +7,15 @@ const port = 8003; // Puerto para el servicio de preguntas app.use(express.json()); +// FunciĆ³n para validar campos requeridos +const validateRequiredFields = (req, fields) => { + for (const field of fields) { + if (!(field in req.body)) { + throw new Error(`Field '${field}' is required.`); + } + } +}; + // Ruta para agregar una nueva pregunta app.post('/addquestion', async (req, res) => { try { diff --git a/questionservice/question-service.test.js b/questionservice/question-service.test.js new file mode 100644 index 00000000..06af2ee5 --- /dev/null +++ b/questionservice/question-service.test.js @@ -0,0 +1,33 @@ +const request = require('supertest'); +const { MongoMemoryServer } = require('mongodb-memory-server'); +const mongoose = require('mongoose'); +const app = require('./question-service'); + +let mongoServer; + +beforeAll(async () => { + mongoServer = await MongoMemoryServer.create(); + const mongoUri = mongoServer.getUri(); + process.env.MONGODB_QUESTIONS_URI = mongoUri; + mongoose.connect(mongoUri); +}); + +afterAll(async () => { + await mongoose.disconnect(); + await mongoServer.stop(); +}); + +describe('Question Service', () => { + it('should add a new question on POST /addquestion', async () => { + const newQuestion = { + question: 'What is the capital of France?', + options: ['Paris', 'Berlin', 'Madrid', 'Rome'], + correctOptionIndex: 0, + }; + + const response = await request(app).post('/addquestion').send(newQuestion); + expect(response.status).toBe(200); + expect(response.body).toHaveProperty('question', 'What is the capital of France?'); + }); + +}); diff --git a/webapp/e2e/test-environment-setup.js b/webapp/e2e/test-environment-setup.js index 7b7ed511..287a4aca 100644 --- a/webapp/e2e/test-environment-setup.js +++ b/webapp/e2e/test-environment-setup.js @@ -5,6 +5,7 @@ let mongoserver; let userservice; let authservice; let gatewayservice; +let questionservice; async function startServer() { console.log('Starting MongoDB memory server...'); @@ -14,6 +15,7 @@ async function startServer() { userservice = await require("../../users/userservice/user-service"); authservice = await require("../../users/authservice/auth-service"); gatewayservice = await require("../../gatewayservice/gateway-service"); + questionservice = await require("../../questionservice/question-service"); } startServer();