Skip to content

Commit

Permalink
New tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Mario committed Apr 25, 2024
1 parent d5d5059 commit 2fdd1e2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,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
47 changes: 43 additions & 4 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const request = require('supertest');
const axios = require('axios');
const jwt = require('jsonwebtoken');
const app = require('./gateway-service');
const { cat } = require('asciidoctor-emoji/dist/node/twemoji-map');

afterAll(async () => {
app.close();
Expand All @@ -13,8 +14,8 @@ jest.mock('jsonwebtoken');
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 @@ -146,7 +147,7 @@ describe('Gateway Service with token mock', () => {
});
});
});*/

function checkRecord(response){
expect(response.statusCode).toBe(200);
Expand All @@ -156,4 +157,42 @@ 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');
}
});

axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
throw new Error("Important information");
}
});

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 2fdd1e2

Please sign in to comment.