Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ampliados tests gateway #359

Merged
merged 19 commits into from
Apr 23, 2024
299 changes: 174 additions & 125 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { generatedQuestionId: 'mockedGeneratedQuestionId' } });
}
});

axios.get.mockImplementation((url) => {
if (url.endsWith('/getAllGeneratedQuestions')) {
return Promise.resolve({ data: { questions: ['question1', 'question2'] } });
Expand All @@ -50,13 +49,19 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { count: 2 } });
}
});

axios.delete.mockImplementation((url) => {
if (url.endsWith('/deleteFirstQuestionGenerator')) {
return Promise.resolve({ data: { success: true } });
}
});

// Test /health endpoint
it('should return OK status', async () => {
const response = await request(app).get('/health');
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({ status: 'OK' });
});

// Test /login endpoint
it('should forward login request to auth service', async () => {
const mockUsername = 'testuser';
Expand Down Expand Up @@ -102,164 +107,208 @@ describe('Gateway Service', () => {
expect(response.body.recordId).toBe('mockedRecordId');
});

// Test /addQuestion endpoint
it('should add a question successfully', async () => {
const mockQuestion = {
questionBody: '¿Cual es la capital de Francia?',
typeQuestion: 'pais_capital'
};
// Test /addQuestion endpoint
it('should add a question successfully', async () => {
const mockQuestion = {
questionBody: '¿Cual es la capital de Francia?',
typeQuestion: 'pais_capital'
};

const response = await request(app)
.post('/addQuestion')
.send(mockQuestion);
const response = await request(app)
.post('/addQuestion')
.send(mockQuestion);

expect(response.statusCode).toBe(200);
expect(response.body.questionId).toBe('mockedQuestionId');
});
expect(response.statusCode).toBe(200);
expect(response.body.questionId).toBe('mockedQuestionId');
});

// Test /createUserRank endpoint
it('should create a user rank in ranking service', async () => {
const mockUsername = 'testuser';
// Test /createUserRank endpoint
it('should create a user rank in ranking service', async () => {
const mockUsername = 'testuser';

const response = await request(app)
.post('/createUserRank')
.send({ username: mockUsername });
const response = await request(app)
.post('/createUserRank')
.send({ username: mockUsername });

expect(response.statusCode).toBe(200);
expect(response.body.rankId).toBe('mockedRankId');
});
expect(response.statusCode).toBe(200);
expect(response.body.rankId).toBe('mockedRankId');
});

// Test /updateRanking endpoint
it('should update ranking for a user in ranking service', async () => {
const mockRanking = { username: 'testuser' };
// Test /updateRanking endpoint
it('should update ranking for a user in ranking service', async () => {
const mockRanking = { username: 'testuser' };

const response = await request(app)
.post('/updateRanking')
.send(mockRanking);
const response = await request(app)
.post('/updateRanking')
.send(mockRanking);

expect(response.statusCode).toBe(200);
expect(response.body.updatedRanking).toBe(true);
});
expect(response.statusCode).toBe(200);
expect(response.body.updatedRanking).toBe(true);
});

// Test /addGeneratedQuestion endpoint
it('should add a generated question successfully', async () => {
const mockGeneratedQuestion = {
generatedQuestionBody: '¿Cual es la capital de Francia?',
correctAnswer: 'Paris'
};
// Test /addGeneratedQuestion endpoint success
it('should add a generated question successfully', async () => {
const mockGeneratedQuestion = {
question: '¿Cuál es la capital de Francia?',
answer: 'París',
distractor: ['Londres', 'Madrid', 'Berlín']
};

const response = await request(app)
.post('/addGeneratedQuestion')
.send(mockGeneratedQuestion);
const response = await request(app).post('/addGeneratedQuestion').send(mockGeneratedQuestion);
expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('generatedQuestionId', 'mockedGeneratedQuestionId');
});

expect(response.statusCode).toBe(200);
expect(response.body.generatedQuestionId).toBe('mockedGeneratedQuestionId');
});
// Test /getAllGeneratedQuestions endpoint
it('should get all generated questions from generated question service', async () => {
const response = await request(app)
.get('/getAllGeneratedQuestions');

// Test /getAllGeneratedQuestions endpoint
it('should get all generated questions from generated question service', async () => {
const response = await request(app)
.get('/getAllGeneratedQuestions');
expect(response.statusCode).toBe(200);
expect(response.body.questions).toEqual(['question1', 'question2']);
});

expect(response.statusCode).toBe(200);
expect(response.body.questions).toEqual(['question1', 'question2']);
});
// Test /getRecords/:userId endpoint
it('should get all records for a user from record service', async () => {
const mockUserId = 'testuserid';
const mockRecords = [
{ recordId: 'record1', userId: mockUserId, score: 100 },
{ recordId: 'record2', userId: mockUserId, score: 200 },
];

// Test /getRecords/:userId endpoint
it('should get all records for a user from record service', async () => {
const mockUserId = 'testuserid';
const mockRecords = [
{ recordId: 'record1', userId: mockUserId, score: 100 },
{ recordId: 'record2', userId: mockUserId, score: 200 },
];

// Mock the axios.get implementation for this test
axios.get.mockImplementationOnce((url) => {
if (url.endsWith(`/getRecords/${mockUserId}`)) {
return Promise.resolve({ data: mockRecords });
}
});
// Mock the axios.get implementation for this test
axios.get.mockImplementationOnce((url) => {
if (url.endsWith(`/getRecords/${mockUserId}`)) {
return Promise.resolve({ data: mockRecords });
}
});

const response = await request(app).get(`/getRecords/${mockUserId}`);
const response = await request(app).get(`/getRecords/${mockUserId}`);

expect(response.statusCode).toBe(200);
expect(response.body).toEqual(mockRecords);
});
expect(response.statusCode).toBe(200);
expect(response.body).toEqual(mockRecords);
});

// Test /getAllUsers endpoint
it('should get all users from user service', async () => {
const response = await request(app)
.get('/getAllUsers');
// Test /getAllUsers endpoint
it('should get all users from user service', async () => {
const response = await request(app)
.get('/getAllUsers');

expect(response.statusCode).toBe(200);
expect(response.body.users).toEqual(['user1', 'user2']);
});
expect(response.statusCode).toBe(200);
expect(response.body.users).toEqual(['user1', 'user2']);
});

// Test /getFullQuestion endpoint
it('should get a full question from question service', async () => {
const response = await request(app)
.get('/getFullQuestion');
// Test /getFullQuestion endpoint
it('should get a full question from question service', async () => {
const response = await request(app)
.get('/getFullQuestion');

expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
});
expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
});

// Test /actRanking endpoint
it('should get a ranking from ranking service', async () => {
const response = await request(app)
.get('/actRanking');
// Test /actRanking endpoint
it('should get a ranking from ranking service', async () => {
const response = await request(app)
.get('/actRanking');

expect(response.statusCode).toBe(200);
expect(response.body.ranking).toBe('mockedRanking');
});
expect(response.statusCode).toBe(200);
expect(response.body.ranking).toBe('mockedRanking');
});

// Test /obtainRank endpoint
it('should get a rank from rank service', async () => {
const response = await request(app)
.get('/obtainRank');
// Test /obtainRank endpoint
it('should get a rank from rank service', async () => {
const response = await request(app)
.get('/obtainRank');

expect(response.statusCode).toBe(200);
expect(response.body.rank).toBe('mockedRank');
});
expect(response.statusCode).toBe(200);
expect(response.body.rank).toBe('mockedRank');
});

// Test /getRandomQuestionXXXXXX endpoints (themes)
const themes = ['Sports', 'Music', 'ImportantDates', 'Literature', 'Countries'];

// Test /getRandomQuestionXXXXXX endpoints (themes)
const themes = ['Sports', 'Music', 'ImportantDates', 'Literature', 'Countries'];
for (const theme of themes) {
it(`should get a random question from question generator service with theme "${theme}"`, async () => {
const response = await request(app).get(`/getRandomQuestion${theme}`);

for (const theme of themes) {
it(`should get a random question from question generator service with theme "${theme}"`, async () => {
const response = await request(app).get(`/getRandomQuestion${theme}`);
expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
});
}

// Test /getAllQuestionGenerator endpoint
it('should get all questions from question generator service', async () => {
const response = await request(app)
.get('/getAllQuestionGenerator');

expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
expect(response.body.questions).toEqual(['question1', 'question2']);
});
}

// Test /getAllQuestionGenerator endpoint
it('should get all questions from question generator service', async () => {
const response = await request(app)
.get('/getAllQuestionGenerator');
// Test /countQuestionGenerator endpoint
it('should count all questions from question generator service', async () => {
const response = await request(app)
.get('/countQuestionGenerator');

expect(response.statusCode).toBe(200);
expect(response.body.count).toBe(2);
});

expect(response.statusCode).toBe(200);
expect(response.body.questions).toEqual(['question1', 'question2']);
});
// Test /deleteFirstQuestionGenerator endpoint
it('should delete the first question from question generator service', async () => {
const response = await request(app)
.delete('/deleteFirstQuestionGenerator');

// Test /countQuestionGenerator endpoint
it('should count all questions from question generator service', async () => {
const response = await request(app)
.get('/countQuestionGenerator');
expect(response.statusCode).toBe(200);
expect(response.body.success).toBe(true);
});

expect(response.statusCode).toBe(200);
expect(response.body.count).toBe(2);
});

// Test /deleteFirstQuestionGenerator endpoint
it('should delete the first question from question generator service', async () => {
const response = await request(app)
.delete('/deleteFirstQuestionGenerator');
// Errors in external services
async function testEndpointErrorHandling(method, endpoint, data) {
const axiosMethod = axios[method];
axiosMethod.mockImplementationOnce(() => Promise.reject(new Error('Error interno del servidor')));
const response = await request(app)[method](endpoint).send(data);
expect(response.statusCode).toBe(500);
expect(response.body).toHaveProperty('error', 'Error interno del servidor');
}
const mockPassword = 'newpassword';

// Test /login endpoint error handling
it('should handle error in /login', async () => {
axios.post.mockImplementationOnce(() => Promise.reject({response: { status: 500, data: { error: 'Error interno del servidor' }}}));
const response = await request(app).post('/login').send({ username: 'testuser2', password: mockPassword });
expect(response.statusCode).toBe(500);
expect(response.body).toHaveProperty('error', 'Error interno del servidor');
});

// Test /addUser endpoint error handling
it('should handle error in /addUser', async () => {
axios.post.mockImplementationOnce(() => Promise.reject({response: { status: 500, data: { error: 'Error interno del servidor' }}}));
const response = await request(app).post('/addUser').send({ username: 'testuser2', password: mockPassword });
expect(response.statusCode).toBe(500);
expect(response.body).toHaveProperty('error', 'Error interno del servidor');
});

expect(response.statusCode).toBe(200);
expect(response.body.success).toBe(true);
});
const testCases = [
{ method: 'post', endpoint: '/addGeneratedQuestion', data: { question: '¿Cuál es la capital de Francia?', answer: 'París', distractor: ['Londres', 'Madrid', 'Berlín'] } },
{ method: 'get', endpoint: '/getAllGeneratedQuestions' },
{ method: 'get', endpoint: '/getRecords/:userId' },
{ method: 'get', endpoint: '/getAllUsers' },
{ method: 'get', endpoint: '/getFullQuestion' },
{ method: 'get', endpoint: '/actRanking' },
{ method: 'get', endpoint: '/obtainRank' },
{ method: 'get', endpoint: '/getAllQuestionGenerator' },
{ method: 'get', endpoint: '/countQuestionGenerator' },
{ method: 'delete', endpoint: '/deleteFirstQuestionGenerator' },
];

testCases.forEach(({ method, endpoint, data }) => {
it(`should handle error in ${endpoint}`, async () => {
await testEndpointErrorHandling(method, endpoint, data);
});
});

});