diff --git a/gatewayservice/gateway-service.test.js b/gatewayservice/gateway-service.test.js index e7e8f107..2ab6e6d2 100644 --- a/gatewayservice/gateway-service.test.js +++ b/gatewayservice/gateway-service.test.js @@ -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(); @@ -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', @@ -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 @@ -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) @@ -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}`); @@ -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 () => { @@ -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); @@ -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' }); + }); + + }); \ No newline at end of file