Skip to content

Commit

Permalink
Gateway - auth service tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidAlvrz committed Apr 10, 2024
1 parent 38e9b18 commit c5ad8c0
Showing 1 changed file with 43 additions and 22 deletions.
65 changes: 43 additions & 22 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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' });
Expand All @@ -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);
});
})

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);
});


});

0 comments on commit c5ad8c0

Please sign in to comment.