Skip to content

Commit

Permalink
Tests de gateway service
Browse files Browse the repository at this point in the history
  • Loading branch information
uo283182 committed Apr 24, 2024
1 parent dc6cf20 commit 92e0788
Showing 1 changed file with 81 additions and 0 deletions.
81 changes: 81 additions & 0 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,41 @@ describe('Gateway Service', () => {
return Promise.resolve({ data: { token: 'mockedToken' } });
} else if (url.endsWith('/adduser')) {
return Promise.resolve({ data: { userId: 'mockedUserId' } });
} else if (url.endsWith('/addRecord')) {
return Promise.resolve({ data: { recordId: 'mockedRecordId' } });
} else if (url.endsWith('/getRecords')) {
return Promise.resolve({
data: [{ user_id: "user",
correctQuestions: 10,
totalQuestions: 20,
totalTime: 200 } ]});
} else if (url.endsWith('/questions')) {
return Promise.resolve({ data: {
pregunta: "¿Cuál es la capital de España?",
correcta: "España",
incorrectas: ["Londres", "París", "Berlín"]
} });
}
});
axios.get.mockImplementation((url, data) => {
if (url.endsWith('/getAllUsers')) {
return Promise.resolve({ data:
[{ username: "user",
createdAt: "2022-01-01T00:00:00Z"
} ,
{ username: "user2",
createdAt: "2022-02-01T00:00:00Z"
}] });
}
});

// Test /health endpoint
it('should forward health request', async () => {
const response = await request(app)
.get('/health');
expect(response.statusCode).toBe(200);
expect(response.body.status).toBe('OK');
});

// Test /login endpoint
it('should forward login request to auth service', async () => {
Expand All @@ -37,4 +70,52 @@ describe('Gateway Service', () => {
expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
});

// Test /addRecord endpoint
it('should forward add record request to record service', async () => {
const response = await request(app)
.post('/addRecord')
.send({ user_id: "user",
correctQuestions: 10,
totalQuestions: 20,
totalTime: 200 });

expect(response.statusCode).toBe(200);
expect(response.body.recordId).toBe('mockedRecordId');
});

// Test /getRecords endpoint
it('should forward get records request to record service', async () => {
const response = await request(app)
.post('/getRecords')
.send({ username: "user" });

expect(response.statusCode).toBe(200);
expect(response.body).toEqual([{ user_id: "user",
correctQuestions: 10,
totalQuestions: 20,
totalTime: 200 } ]);
});

// Test /questions endpoint
it('should forward get questions request to questions service', async () => {
const response = await request(app)
.post('/questions');
expect(response.statusCode).toBe(200);
expect(response.body).toEqual({
pregunta: "¿Cuál es la capital de España?",
correcta: "España",
incorrectas: ["Londres", "París", "Berlín"]
});
});

// Test /getAllUsers endpoint
it('should forward get all users request to users service', async () => {
const response = await request(app)
.get('/getAllUsers');
expect(response.statusCode).toBe(200);
expect(response.body).toEqual([{ username: "user", createdAt: "2022-01-01T00:00:00Z"} ,
{ username: "user2", createdAt: "2022-02-01T00:00:00Z"}]);
});

});

0 comments on commit 92e0788

Please sign in to comment.