From 2fdd1e2dc28d17691e6186f30a64b53cf67b35f3 Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Thu, 25 Apr 2024 20:59:00 +0200 Subject: [PATCH 1/3] New tests added --- gatewayservice/gateway-service.js | 2 +- gatewayservice/gateway-service.test.js | 47 +++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 9fe898b..9e2e935 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -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 diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 550c402..ecb355a 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -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(); @@ -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) => { @@ -146,7 +147,7 @@ describe('Gateway Service with token mock', () => { }); -}); +});*/ function checkRecord(response){ expect(response.statusCode).toBe(200); @@ -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?"); -} \ No newline at end of file +} + +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'); + } + }); +}); From ada02ec53a22b0190a70aae143b08e1f41d87fe6 Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Thu, 25 Apr 2024 21:02:27 +0200 Subject: [PATCH 2/3] Removed some comments --- gatewayservice/gateway-service.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index ecb355a..9629a44 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -14,7 +14,7 @@ jest.mock('jsonwebtoken'); jest.mock('axios'); -/* + describe('Gateway Service with mocked micro services', () => { // Mock responses from external services @@ -147,7 +147,7 @@ describe('Gateway Service with mocked micro services', () => { }); -});*/ +}); function checkRecord(response){ expect(response.statusCode).toBe(200); From 2b4fc3e23cdbe10f06d13483a67d0ee56fc440ab Mon Sep 17 00:00:00 2001 From: Mister-Mario Date: Thu, 25 Apr 2024 21:23:39 +0200 Subject: [PATCH 3/3] Removed an extra mock --- gatewayservice/gateway-service.js | 1 + gatewayservice/gateway-service.test.js | 7 ------- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/gatewayservice/gateway-service.js b/gatewayservice/gateway-service.js index 9e2e935..49b59c0 100644 --- a/gatewayservice/gateway-service.js +++ b/gatewayservice/gateway-service.js @@ -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); diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 9629a44..a51f5f1 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -2,7 +2,6 @@ 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(); @@ -173,12 +172,6 @@ describe('Gateway Service without mocked micro services', () => { } }); - 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')) {