Skip to content

Commit

Permalink
correccion errores test gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
fer4github committed Apr 8, 2024
1 parent 896e172 commit bae4225
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 20 deletions.
1 change: 0 additions & 1 deletion gatewayservice/gateway-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ app.get('/health', (_req, res) => {
});

app.post('/login', async (req, res) => {

const isValidUser = validateCredentials(req.body.username, req.body.password);

if (!isValidUser) {
Expand Down
35 changes: 16 additions & 19 deletions gatewayservice/gateway-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('Gateway Service', () => {
});

// Test /login endpoint
it('should forward login request to auth service', async () => {
it('deberia iniciar sesión correctamente', async () => {
const response = await request(app)
.post('/login')
.send({ username: 'testuser', password: 'testpassword' });
Expand All @@ -32,21 +32,8 @@ describe('Gateway Service', () => {
expect(response.body.token).toBe('mockedToken');
});

// Test /adduser endpoint
it('should forward add user request to user service', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: 'newuser', password: 'newpassword' });

// Verificamos que la respuesta tenga un código de estado 200 y un ID de usuario
expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
});

// TODO: Si comrpobamos user cambiar 200 por 401

// Prueba de manejo de errores para el endpoint /login
it('should handle authentication errors gracefully', async () => {
it('deberia devolver error al iniciar sesion', async () => {
// Datos de prueba para iniciar sesión (incorrectos)
const invalidLoginData = {
username: 'userInvalido',
Expand All @@ -55,14 +42,24 @@ describe('Gateway Service', () => {

// Realizamos una solicitud POST al endpoint /login con datos incorrectos
const response = await request(app)
.post('/adduser')
.send({ username: 'newuser', password: 'newpassword' });
.post('/login')
.send(invalidLoginData);

// Verificamos que la respuesta tenga un código de estado 401 (Unauthorized)
expect(response.statusCode).toBe(401);
});
//test prueba gateway

// Test /adduser endpoint
it('deberia añadir usuario correctamente', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: 'newuser', password: 'newpassword' });

// Verificamos que la respuesta tenga un código de estado 200 y un ID de usuario
expect(response.statusCode).toBe(200);
expect(response.body.userId).toBe('mockedUserId');
});

// Probamos con una pregunta errónea
it('debería devolver error con esa pregunta', async () => {
const response = await request(app).get('/pregunta');
Expand All @@ -73,7 +70,7 @@ describe('Gateway Service', () => {
});

// Test para pregunta correcta
it('should return question data with status 200', async () => {
it('deberia devolver 200 la pregunta porque es correcta', async () => {
// Configurar el mock de axios para devolver una respuesta exitosa
const mockData = { question: 'What is the capital of France?' };
require('axios').get.mockResolvedValue({ data: mockData });
Expand Down

0 comments on commit bae4225

Please sign in to comment.