Skip to content

Commit

Permalink
Test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 committed Feb 23, 2024
1 parent b7aee22 commit 7b05f3c
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
2 changes: 2 additions & 0 deletions gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ app.post('/adduser', async (req, res) => {
});

app.get('/api/questions/create', async (req, res) => {
console.log('Create Question')
try {
// Forward the add user request to the user service
const userResponse = await axios.get(questionGenerationServiceUrl+'/api/questions/create');
console.log(userResponse.data);
res.json(userResponse.data);
} catch (error) {
res.status(error.response.status).json({ error: error.response.data.error });
Expand Down
31 changes: 26 additions & 5 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
const request = require('supertest');
const axios = require('axios');
const app = require('./gateway-service');

let app;
beforeAll(async () => {
app = require('./gateway-service');
});
afterAll(async () => {
app.close();
});
Expand All @@ -15,6 +17,25 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { token: 'mockedToken' } });
} else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
}else if (url.endsWith('/addquestion')) {
return Promise.resolve({
data: {
question: 'What is the capital of France?',
options: ['Paris', 'Berlin', 'Madrid', 'Rome'],
correctOptionIndex: 0
}
});
}
});
axios.get.mockImplementation((url) => {
if (url.endsWith('/api/questions/create')) {
return Promise.resolve({
data: {
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
}
});
}
});
// Test /health endpoint
Expand Down Expand Up @@ -50,13 +71,13 @@ describe('Gateway Service', () => {
it('should forward create question request to question generation service', async () => {
const response = await request(app)
.get('/api/questions/create');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('question');
expect(response.body).toHaveProperty('correct');
expect(response.body).toHaveProperty('incorrects');
},100000);

},10000);


// Test /addquestion endpoint
it('should add a new question', async () => {
const response = await request(app)
Expand Down
3 changes: 2 additions & 1 deletion question_generator/questionGenerationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ generalTemplate.loadData();

const server = app.listen(port, () => {
console.log(`Listening at http://localhost:${port}`);
});
});
module.exports = server;
22 changes: 21 additions & 1 deletion question_generator/questionGenerationService.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,25 @@ describe('Question generation service', () => {
expect(response.body).toHaveProperty('question');
expect(response.body).toHaveProperty('correct');
expect(response.body).toHaveProperty('incorrects');
},100000);
},10000);

it('should forward create question request to question generation service', async () => {
const response = await request(app)
.get('/api/questions/planets/create');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('question');
expect(response.body).toHaveProperty('correct');
expect(response.body).toHaveProperty('incorrects');
},10000);

it('should forward create question request to question generation service', async () => {
const response = await request(app)
.get('/api/questions/cities/create');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('question');
expect(response.body).toHaveProperty('correct');
expect(response.body).toHaveProperty('incorrects');
},10000);
});

0 comments on commit 7b05f3c

Please sign in to comment.