Skip to content

Commit

Permalink
Merge branch 'Develop' into FeatureQuestions
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 authored Feb 23, 2024
2 parents d134a7e + 36ecadb commit 0119bab
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
15 changes: 15 additions & 0 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ describe('Gateway Service', () => {
expect(response.body.userId).toBe('mockedUserId');
});

// Test /api/questions/create endpoint
it('should forward create question request to question generation service', async () => {
const response = await request(app)
.get('/api/questions/create');
Expand All @@ -47,4 +48,18 @@ describe('Gateway Service', () => {
expect(response.body).toHaveProperty('correct');
expect(response.body).toHaveProperty('incorrects');
});

// 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?');
});
});
9 changes: 9 additions & 0 deletions questionservice/question-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ const port = 8004; // 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 {
Expand Down
33 changes: 33 additions & 0 deletions questionservice/question-service.test.js
Original file line number Diff line number Diff line change
@@ -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?');
});

});
2 changes: 2 additions & 0 deletions webapp/e2e/test-environment-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ let mongoserver;
let userservice;
let authservice;
let gatewayservice;
let questionservice;

async function startServer() {
console.log('Starting MongoDB memory server...');
Expand All @@ -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();

0 comments on commit 0119bab

Please sign in to comment.