Skip to content

Commit

Permalink
Merge pull request #330 from Arquisoft/laura
Browse files Browse the repository at this point in the history
aumento coverage en micros: auth, user y ranking y pequeñas ediciones para sonar
  • Loading branch information
uo287627 authored Apr 20, 2024
2 parents 1f7450a + 8a963da commit b33f134
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 2 deletions.
49 changes: 49 additions & 0 deletions users/authservice/auth-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const bcrypt = require('bcrypt');
const User = require('./auth-model');
const mongoose = require('mongoose');
const server = require('./auth-service');


let mongoServer;
let app;
Expand All @@ -10,6 +13,9 @@ let app;
const mockUsername = 'testuser';
const mockPassword = 'testpassword';

const mockUsername2 = 'invalidUser';
const mockPassword2 = 'invalidPassword';

const user = {
username: mockUsername,
password: mockPassword,
Expand Down Expand Up @@ -45,4 +51,47 @@ describe('Auth Service', () => {
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
});

// Test cubrir linea 25
test('POST /login without password field results in Internal Server Error', async () => {
const response = await request(app)
.post('/login')
.send({
username: mockUsername,
// password field is missing
});

expect(response.statusCode).toBe(500);
expect(response.body).toHaveProperty('error', 'Internal Server Error');
});

// Testlineas 48-51
test('POST /login with valid credentials returns token and user information', async () => {
const response = await request(app)
.post('/login')
.send({
username: mockUsername,
password: mockPassword,
});

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('token');
expect(response.body).toHaveProperty('username', mockUsername);
});

//test linea 48

test('POST /login with invalid credentials', async () => {
const response = await request(server)
.post('/login')
.send({
username: mockUsername2,
password: mockPassword2
});

expect(response.statusCode).toBe(401);
expect(response.body).toHaveProperty('error', 'Invalid credentials');
});


});
76 changes: 74 additions & 2 deletions users/rankingservice/ranking-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ describe('User Service', () => {
});
await existingUserRank.save();

// Datos para la solicitud POST de actualización del ranking de usuario
// Datos para la supdateRanking updates a user rankinglicitud POST de actualización del ranking de usuario
const updateData = {
username: 'existinguser',
preguntasCorrectas: 5,
Expand Down Expand Up @@ -159,5 +159,77 @@ describe('User Service', () => {
});

});


test('POST /createUserRank creates or resets a user ranking', async () => {
const username = 'testUser';

const response = await request(app)
.post('/createUserRank')
.send({ usernames: [username] });

expect(response.status).toBe(200);
expect(response.body.message).toBe('Rankings de usuarios creados o actualizados correctamente.');

const userRank = await UserRank.findOne({ username });
expect(userRank.preguntasCorrectas).toBe(0);
expect(userRank.preguntasFalladas).toBe(0);
expect(userRank.numPartidas).toBe(0);
});

test('GET /obtainRank gets all user rankings', async () => {
const response = await request(app).get('/obtainRank');

expect(response.status).toBe(200);
expect(Array.isArray(response.body)).toBe(true);
});

it('should reset an existing user rank', async () => {
// Arrange
const username = 'testUser';
const initialUserRank = new UserRank({
username,
porcentajeAciertos: 50,
preguntasCorrectas: 10,
preguntasFalladas: 10,
numPartidas: 1
});
await initialUserRank.save();

// Act
await request(app)
.post('/createUserRank')
.send({ usernames: [username] })
.expect(200);

// Assert
const updatedUserRank = await UserRank.findOne({ username });
expect(updatedUserRank.porcentajeAciertos).toBe(0);
expect(updatedUserRank.preguntasCorrectas).toBe(0);
expect(updatedUserRank.preguntasFalladas).toBe(0);
expect(updatedUserRank.numPartidas).toBe(0);
});

it('should return 400 if user does not exist', async () => {
// Arrange
const username = 'testUser';
const initialUserRank = new UserRank({
username,
porcentajeAciertos: 50,
preguntasCorrectas: 10,
preguntasFalladas: 10,
numPartidas: 1
});
await initialUserRank.save();

// Act
await request(app)
.post('/updateAllRanking')
.send({ usernames: ['anotherUser'] }) // username not included
.expect(400); // Expect 400 status code

// Assert
const deletedUserRank = await UserRank.findOne({ username });
expect(deletedUserRank).not.toBeNull(); // Expect the user rank to still exist
});

});
10 changes: 10 additions & 0 deletions users/userservice/user-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,14 @@ describe('User Service', () => {
expect(usernames).toContain('testuser2');

});

test('should throw an error when a required field is missing', async () => {
const response = await request(app)
.post('/adduser')
.send({ username: 'testuser' }); // password field is missing

expect(response.status).toBe(400);
expect(response.body.error).toBe('Missing required field: password');
});

});

0 comments on commit b33f134

Please sign in to comment.