Skip to content

Commit

Permalink
Merge pull request #126 from Arquisoft/servicesTests
Browse files Browse the repository at this point in the history
Test health, login error en gateway
  • Loading branch information
uo287998 authored Apr 7, 2024
2 parents 453f583 + 65b9af4 commit 5a8f3b7
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ describe('Gateway Service', () => {
// Mock responses from external services
axios.post.mockImplementation((url, data) => {
if (url.endsWith('/login')) {
return Promise.resolve({ data: { token: 'mockedToken' } });
if (data.username === 'test' && data.password === 'test') {
return Promise.reject({ response: { status: 401, data: { error: 'Error de autenticación' } } });
} else {
return Promise.resolve({ data: { token: 'mockedToken' } });
}
} else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
} else if (url.endsWith('/addStatistic')) {
Expand All @@ -34,7 +38,13 @@ describe('Gateway Service', () => {
}
});


// Test /health endpoint
it('debería devolver un estado de OK', async () => {
const response = await request(app).get('/health');

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

// Test /login endpoint
it('should forward login request to auth service', async () => {
Expand All @@ -46,6 +56,18 @@ describe('Gateway Service', () => {
expect(response.body.token).toBe('mockedToken');
});

it('debería manejar errores al intentar autenticar', async () => {
const mockErrorResponse = { error: 'Error de autenticación' };
const mockStatus = 401;

const response = await request(app)
.post('/login')
.send({ username: 'test', password: 'test' });

expect(response.status).toBe(mockStatus);
expect(response.body).toEqual(mockErrorResponse);
});

// Test /adduser endpoint
it('should forward add user request to user service', async () => {
const response = await request(app)
Expand Down

0 comments on commit 5a8f3b7

Please sign in to comment.