Skip to content

Commit

Permalink
Merge pull request #108 from Arquisoft/servicesTests
Browse files Browse the repository at this point in the history
Gateway-service tests ampliados
  • Loading branch information
uo287998 authored Apr 3, 2024
2 parents 3995993 + 0c9904f commit a360da2
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { token: 'mockedToken' } });
} else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
}
} else if (url.endsWith('/addStatistic')) {
return Promise.resolve({ data: { statisticId: 'mockedStatisticId' } });
}
});

axios.get.mockImplementation((url) => {
if (url.endsWith('/generate-question')) {
return Promise.resolve({ data: { question: 'mockedQuestion' } });
Expand All @@ -26,6 +29,8 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { gamesPlayed: 'mockedGamesPlayed' ,
rigthAnswers: 'mockedRigthAnswers',
wrongAnswers:'mockedWrongAnswers' }});
} else if (url.endsWith('/users')) {
return Promise.resolve({ data: { users: ['mockedUser1', 'mockedUser2'] } });
}
});

Expand All @@ -50,19 +55,19 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
});
// Test /generate-question endpoint
it('should forward generate question request to question service', async () => {
const response = await request(app)
.get('/generate-question')
.set('Authorization', 'Bearer some-token');

if (response.statusCode !== 200) {
console.error(response.body.error);
}

expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
});
// Test /generate-question endpoint
it('should forward generate question request to question service', async () => {
const response = await request(app)
.get('/generate-question')
.set('Authorization', 'Bearer some-token');

if (response.statusCode !== 200) {
console.error(response.body.error);
}

expect(response.statusCode).toBe(200);
expect(response.body.question).toBe('mockedQuestion');
});
// Test /questions endpoint
it('should forward get questions request to question service', async () => {
const response = await request(app)
Expand All @@ -83,4 +88,23 @@ it('should forward generate question request to question service', async () => {
expect(response.body.wrongAnswers).toBe('mockedWrongAnswers');
});

// Test /addStatistic endpoint
it('should forward add statistic request to statistic service', async () => {
const response = await request(app)
.post('/addStatistic')
.send({ userId: 'testuser', gamesPlayed: 10, rightAnswers: 5, wrongAnswers: 5 });

expect(response.statusCode).toBe(200);
expect(response.body.statisticId).toBe('mockedStatisticId');
});

// Test /users endpoint
it('should forward get users request to user service', async () => {
const response = await request(app)
.get('/users');

expect(response.statusCode).toBe(200);
expect(response.body.users).toEqual(['mockedUser1', 'mockedUser2']);
});

});

0 comments on commit a360da2

Please sign in to comment.