From c5ad8c0c97190718851979d497d1e8eacb40a93e Mon Sep 17 00:00:00 2001 From: DavidAlvrz Date: Wed, 10 Apr 2024 14:14:21 +0200 Subject: [PATCH] Gateway - auth service tests added --- gatewayservice/gateway-service.test.js | 65 +++++++++++++++++--------- 1 file changed, 43 insertions(+), 22 deletions(-) diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index 34619fa7..8e7581aa 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -1,6 +1,7 @@ const request = require('supertest'); const axios = require('axios'); const app = require('./gateway-service'); +const e = require('express'); afterAll(async () => { app.close(); @@ -10,17 +11,15 @@ jest.mock('axios'); /*User service tests*/ -describe('[Gateway Service] - /login', () => { - beforeEach(() => { - // Mock responses from external services - axios.post.mockImplementation((url, data) => { +describe('[Gateway Service] - Auth', () => { + // Login + it('should repsond with 200 after a successful login', async () => { + // Mock responses from external service + axios.post.mockImplementation((url, data) => { return Promise.resolve({ data: { token: 'mockedToken', username: "testuser", userId: "1234" } }); }); - }) - // Test /login endpoint - it('should forward login request to auth service', async () => { const response = await request(app) .post('/login') .send({ username: 'testuser', password: 'testpassword' }); @@ -30,25 +29,47 @@ describe('[Gateway Service] - /login', () => { expect(response.body).toHaveProperty("username", 'testuser'); expect(response.body).toHaveProperty("userId", '1234'); }); -}); -describe("[Gateway Service] - /adduser", () => { - beforeEach(() => { + it('should repsond with 500 after a invalid login', async () => { + // Mock responses from external service axios.post.mockImplementation((url, data) => { - return Promise.resolve({ data: { userId: 'mockedUserId', token: "userToken", username: "newuser", message: "User created successfully" } }); + return Promise.reject({ error: "Invalid credentials" }); }); - }) - // Mock responses from external services - // Test /adduser endpoint - it('should forward add user request to user service', async () => { + const response = await request(app) - .post('/adduser') - .send({ username: 'newuser', password: 'newPassword123_' }); + .post('/login') + .send({ username: 'testuser', password: 'testpassword' }); + + expect(response.statusCode).toBe(500); + }); + + //Token validation + it('should respond with 200 after a successful token validation', async () => { + + // Mock responses from external service + axios.get.mockImplementation((url, data) => { + return Promise.resolve({ data: { valid: true } }); + }); + + const response = await request(app).get('/validate/faketoken'); expect(response.statusCode).toBe(200); - expect(response.body).toHaveProperty("userId", 'mockedUserId'); - expect(response.body).toHaveProperty("token", 'userToken'); - expect(response.body).toHaveProperty("username", 'newuser'); - expect(response.body).toHaveProperty("message", 'User created successfully'); + expect(response.body).toHaveProperty("valid", true); }); -}) \ No newline at end of file + + it('should respond with 500 due to an invalid token validation', async () => { + // Mock responses from external service with a rejected value containing specific values + const errorResponse = { valid:false }; + axios.get.mockImplementation((url, data) => { + return Promise.reject(errorResponse); + }); + + const response = await request(app).get('/validate/faketoken'); + console.log(response) + + // Assertions + expect(response.statusCode).toBe(500); + }); + + +}); \ No newline at end of file