Skip to content

Commit

Permalink
Merge pull request #131 from Arquisoft/129-add-more-tests-to-gateway-…
Browse files Browse the repository at this point in the history
…service-to-increase-code-coverage

New tests added
  • Loading branch information
Mister-Mario authored Apr 25, 2024
2 parents d5d5059 + 2b4fc3e commit b1289a4
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
3 changes: 2 additions & 1 deletion gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ app.post('/adduser', async (req, res) => {
try {
// Forward the add user request to the user service
const userResponse = await axios.post(userServiceUrl+'/adduser', req.body);
console.log(userResponse)
res.json(userResponse.data);
} catch (error) {
manageError(res, error);
Expand Down Expand Up @@ -231,7 +232,7 @@ function manageError(res, error){
if(error.response) //Some microservice responded with an error
res.status(error.response.status).json({ error: error.response.data.error });
else //Some other error
res.status(500).json({error : "Interanl server error"})
res.status(500).json({error : "Internal server error"})
}

module.exports = server
36 changes: 34 additions & 2 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jest.mock('axios');



describe('Gateway Service with token mock', () => {
describe('Gateway Service with mocked micro services', () => {

// Mock responses from external services
axios.post.mockImplementation((url, data) => {
Expand Down Expand Up @@ -156,4 +156,36 @@ function checkRecord(response){
function checkQuestion(response){
expect(response.statusCode).toBe(200);
expect(response.body[0]).toHaveProperty('question', "¿Cuál es la población de Oviedo?");
}
}

describe('Gateway Service without mocked micro services', () => {

it('should not forward login request and give 500', async () => {
try{
await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });

} catch(error){
expect(error.response.status).toBe(500);
expect(error.response.data.error).toBe('Internal server error');
}
});

it('should not forward login request and give 500', async () => {
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
throw new Error("Important information");
}
});
try{
await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });

} catch(error){
expect(error.response.status).toBe(500);
expect(error.response.data.error).toBe('Internal server error');
}
});
});

0 comments on commit b1289a4

Please sign in to comment.