Skip to content

Commit

Permalink
coverage increased
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 committed Apr 7, 2024
1 parent 5167797 commit f8ac409
Showing 1 changed file with 34 additions and 33 deletions.
67 changes: 34 additions & 33 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ let app;
beforeAll(async () => {
app = require('./gateway-service');
});
const fs = require('fs');
const YAML = require('yaml');
const swaggerUi = require('swagger-ui-express');

afterAll(async () => {
app.close();
Expand All @@ -30,15 +27,7 @@ describe('Gateway Service', () => {
}
});
axios.get.mockImplementation((url) => {
if (url.endsWith('/api/questions/create')) {
return Promise.resolve({
data: {
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
}
});
}else if(url.endsWith('/api/questions/create?lang=es&category=sports')){
if(url.endsWith('/api/questions/create?lang=es&category=sports')){
return Promise.resolve({
data: {
question: 'Mocked Question',
Expand Down Expand Up @@ -72,29 +61,12 @@ describe('Gateway Service', () => {
}
});


it('should return 401 Unauthorized if authorization token is missing', async () => {
const response = await request(app).post('/addgame');
expect(response.statusCode).toBe(401);
expect(response.body).toEqual({ error: 'Unauthorized' });
});

it('should return 401 Unauthorized if authorization token is invalid', async () => {
const mockedToken = 'invalidToken';
axios.get.mockRejectedValueOnce({ response: { status: 401 } });
const response = await request(app)
.post('/addgame')
.set('Authorization', `Bearer ${mockedToken}`);
expect(response.statusCode).toBe(401);
expect(response.body).toEqual({ error: 'Unauthorized' });
});
// Test /health endpoint
it('should give information of the status', async () => {
const response = await request(app)
.get('/health');

expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('OK');
});

// Test /login endpoint
Expand All @@ -105,8 +77,8 @@ describe('Gateway Service', () => {
.send({ username: 'testuser', password: 'testpassword' });

expect(response.statusCode).toBe(200);
expect(response.body.token).toBe('mockedToken');
});

it('should return an error when the user service is down', async () => {
jest.spyOn(axios, 'post').mockRejectedValueOnce({ response: { status: 500, data: { error: 'Service down' } } });
const response = await request(app)
Expand Down Expand Up @@ -134,7 +106,7 @@ describe('Gateway Service', () => {
// Test /verify endpoint when auth service is down
it('should return an error when the auth service is down', async () => {
const mockedToken = 'mockedToken';
axios.get.mockRejectedValueOnce({ response: { status: 500, data: { error: 'Service down' } } });
axios.get.mockImplementation({ response: { status: 500, data: { error: 'Service down' } } });
const response = await request(app)
.get('/verify')
.set('Authorization', `Bearer ${mockedToken}`);
Expand Down Expand Up @@ -177,12 +149,13 @@ describe('Gateway Service', () => {

// Test /api/info/questions endpoint
it('should forward info request with id to question service', async () => {
axios.get.mockResolvedValueOnce({ data: { question: 'Mocked Question', correct: 'Mocked Correct Answer', incorrects: ['Mocked Option 1', 'Mocked Option 2'] } });
const response = await request(app)
.get('/api/info/questions');
expect(response.statusCode).toBe(200);
expect(response.headers['content-type']).toContain('application/json');
expect(response.body).toEqual(expect.any(Array));
});
expect(response.body).toEqual({ question: 'Mocked Question', correct: 'Mocked Correct Answer', incorrects: ['Mocked Option 1', 'Mocked Option 2'] });
});

// Test /api/info/questions endpoint when service is down
it('should return an error when the question service is down', async () => {
Expand All @@ -199,6 +172,15 @@ describe('Gateway Service', () => {

// Test /api/questions/create endpoint
it('should forward create question request to question generation service', async () => {
axios.get.mockImplementation(() => {
return Promise.resolve({
data: {
question: 'Mocked Question',
correct: 'Mocked Correct Answer',
incorrects: ['Mocked Option 1', 'Mocked Option 2']
}
});
});
const response = await request(app)
.get('/api/questions/create');
expect(response.statusCode).toBe(200);
Expand Down Expand Up @@ -323,4 +305,23 @@ describe('GET /api/info/games', () => {
expect(response.statusCode).toBe(500);
expect(response.body).toEqual(mockedError);
});
});
describe('addGame', () =>{
it('should return 401 Unauthorized if authorization token is missing', async () => {
const response = await request(app).post('/addgame');
expect(response.statusCode).toBe(401);
expect(response.body).toEqual({ error: 'Unauthorized' });
});

it('should return 401 Unauthorized if authorization token is invalid', async () => {
const mockedToken = 'invalidToken';
axios.get.mockRejectedValueOnce({ response: { status: 401 } });
const response = await request(app)
.post('/addgame')
.set('Authorization', `Bearer ${mockedToken}`);
expect(response.statusCode).toBe(401);
expect(response.body).toEqual({ error: 'Unauthorized' });
});


});

0 comments on commit f8ac409

Please sign in to comment.