diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index eff727e..2a60464 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -27,16 +27,49 @@ describe('Gateway Service', () => { expect(response.statusCode).toBe(200); expect(response.body.token).toBe('mockedToken'); }); - - // Test /adduser endpoint +// Test /adduser endpoint it('should forward add user request to user service', async () => { const response = await request(app) - .post('/adduser') - .send({ username: 'newuser', password: 'newpassword' }); + .post('/adduser') + .send({ username: 'newuser', password: 'newpassword' }); expect(response.statusCode).toBe(200); expect(response.body.userId).toBe('mockedUserId'); }); + + it('should handle errors from the auth service on login', async () => { + // Simula una respuesta de error del servicio de autenticación + axios.post.mockRejectedValue({ response: { status: 401, data: { error: 'Unauthorized' } } }); + + const response = await request(app) + .post('/login') + .send({ username: 'testuser', password: 'wrongpassword' }); + + expect(response.statusCode).toBe(401); + expect(response.body.error).toBe('Unauthorized'); + }); + + it('should validate a token successfully', async () => { + // Simula una respuesta exitosa del servicio de autenticación + axios.get.mockResolvedValue({ data: { valid: true } }); + + const response = await request(app) + .get('/validate/mockedToken'); + + expect(response.statusCode).toBe(200); + expect(response.body.valid).toBe(true); + }); + it('should handle validation error', async () => { + // Simula una respuesta de error del servicio de autenticación + axios.get.mockRejectedValue({ response: { status: 401, data: { error: 'Invalid token' } } }); + + const response = await request(app) + .get('/validate/invalidToken'); + + expect(response.statusCode).toBe(401); + expect(response.body.error).toBe('Invalid token'); + }); + // Test for /getquestions endpoint it('should forward get questions request to generate service', async () => { axios.get.mockResolvedValue({ data: [{ question: 'What is 2+2?' }] }); diff --git a/gatewayservice/package-lock.json b/gatewayservice/package-lock.json index 430cbe9..3f35bf5 100644 --- a/gatewayservice/package-lock.json +++ b/gatewayservice/package-lock.json @@ -1516,12 +1516,12 @@ "peer": true }, "node_modules/body-parser": { - "version": "1.20.1", - "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.1.tgz", - "integrity": "sha512-jWi7abTbYwajOytWCQc37VulmWiRae5RyTpaCyDcS5/lMdtwSz5lOpDE67srw/HYe35f1z3fDQw+3txg7gNtWw==", + "version": "1.20.2", + "resolved": "https://registry.npmjs.org/body-parser/-/body-parser-1.20.2.tgz", + "integrity": "sha512-ml9pReCu3M61kGlqoTm2umSXTlRTuGTx0bfYj+uIUKKYycG5NtSbeetV3faSU6R7ajOPw0g/J1PvK4qNy7s5bA==", "dependencies": { "bytes": "3.1.2", - "content-type": "~1.0.4", + "content-type": "~1.0.5", "debug": "2.6.9", "depd": "2.0.0", "destroy": "1.2.0", @@ -1529,7 +1529,7 @@ "iconv-lite": "0.4.24", "on-finished": "2.4.1", "qs": "6.11.0", - "raw-body": "2.5.1", + "raw-body": "2.5.2", "type-is": "~1.6.18", "unpipe": "1.0.0" }, @@ -1810,9 +1810,9 @@ "dev": true }, "node_modules/cookie": { - "version": "0.5.0", - "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.5.0.tgz", - "integrity": "sha512-YZ3GUyn/o8gfKJlnlX7g7xq4gyO6OSuhGPKaaGssGB2qgDUS0gPgtTvoyZLTt9Ab6dC4hfc9dV5arkvc/OCmrw==", + "version": "0.6.0", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.6.0.tgz", + "integrity": "sha512-U71cyTamuh1CRNCfpGY6to28lxvNwPG4Guz/EVjgf3Jmzv0vlDp1atT9eS5dDjMYHucpHbWns6Lwf3BKz6svdw==", "engines": { "node": ">= 0.6" } @@ -2118,16 +2118,16 @@ } }, "node_modules/express": { - "version": "4.18.2", - "resolved": "https://registry.npmjs.org/express/-/express-4.18.2.tgz", - "integrity": "sha512-5/PsL6iGPdfQ/lKM1UuielYgv3BUoJfz1aUwU9vHZ+J7gyvwdQXFEBIEIaxeGf0GIcreATNyBExtalisDbuMqQ==", + "version": "4.19.2", + "resolved": "https://registry.npmjs.org/express/-/express-4.19.2.tgz", + "integrity": "sha512-5T6nhjsT+EOMzuck8JjBHARTHfMht0POzlA60WV2pMD3gyXw2LZnZ+ueGdNxG+0calOJcWKbpFcuzLZ91YWq9Q==", "dependencies": { "accepts": "~1.3.8", "array-flatten": "1.1.1", - "body-parser": "1.20.1", + "body-parser": "1.20.2", "content-disposition": "0.5.4", "content-type": "~1.0.4", - "cookie": "0.5.0", + "cookie": "0.6.0", "cookie-signature": "1.0.6", "debug": "2.6.9", "depd": "2.0.0", @@ -2259,9 +2259,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", - "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "funding": [ { "type": "individual", @@ -4097,9 +4097,9 @@ } }, "node_modules/raw-body": { - "version": "2.5.1", - "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.1.tgz", - "integrity": "sha512-qqJBtEyVgS0ZmPGdCFPWJ3FreoqvG4MVQln/kCgF7Olq95IbOp0/BWyMwbdtn4VTvkM8Y7khCQ2Xgk/tcrCXig==", + "version": "2.5.2", + "resolved": "https://registry.npmjs.org/raw-body/-/raw-body-2.5.2.tgz", + "integrity": "sha512-8zGqypfENjCIqGhgXToC8aB2r7YrBX+AQAfIPs/Mlk+BtPTztOvTS01NRW/3Eh60J+a48lt8qsCzirQ6loCVfA==", "dependencies": { "bytes": "3.1.2", "http-errors": "2.0.0",