Skip to content

Commit

Permalink
tests historial
Browse files Browse the repository at this point in the history
  • Loading branch information
yagonavajas committed Mar 28, 2024
1 parent f77a056 commit f05e9d8
Showing 1 changed file with 94 additions and 35 deletions.
129 changes: 94 additions & 35 deletions users/historyservice/history-service.test.js
Original file line number Diff line number Diff line change
@@ -1,48 +1,107 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const bcrypt = require('bcrypt');
const User = require('./auth-model');
const mongoose = require('mongoose');
const app = require('./history-service');
const History = require('./history-model');

let mongoServer;
let app;
const { MongoMemoryServer } = require('mongodb-memory-server');
const mongodb = new MongoMemoryServer();

beforeAll(async () => {
const uri = await mongodb.getUri();
await mongoose.connect(uri, { useNewUrlParser: true, useUnifiedTopology: true });
});

afterAll(async () => {
await mongoose.disconnect();
await mongodb.stop();
});

beforeEach(async () => {
await History.deleteMany({});
});

describe('POST /savehistory', () => {
it('should save history entry for a new user that plays a game', async () => {
const userData = {
username: 'testuser',
NumPreguntasJugadas: 5,
NumAcertadas: 3
};

//test user
const user = {
username: 'testuser',
password: 'testpassword',
};
const response = await request(app)
.post('/savehistory')
.send(userData)
.expect(200);

async function addUser(user){
const hashedPassword = await bcrypt.hash(user.password, 10);
const newUser = new User({
username: user.username,
password: hashedPassword,
expect(response.body.username).toBe(userData.username);
expect(response.body.NumJugadas).toBe(1);
expect(response.body.NumPreguntasJugadas).toBe(userData.NumPreguntasJugadas);
expect(response.body.NumAcertadas).toBe(userData.NumAcertadas);
expect(response.body.NumFalladas).toBe(userData.NumPreguntasJugadas - userData.NumAcertadas);
});

await newUser.save();
}
it('should update history entry for an existing user', async () => {
// Crear una entrada de historial existente en la base de datos
await History.create({
username: 'existinguser',
NumJugadas: 1,
NumPreguntasJugadas: 5,
NumAcertadas: 3,
NumFalladas: 2
});

beforeAll(async () => {
mongoServer = await MongoMemoryServer.create();
const mongoUri = mongoServer.getUri();
process.env.MONGODB_URI = mongoUri;
app = require('./auth-service');
//Load database with initial conditions
await addUser(user);
await request(app).post('/login').send(user);
});
const userData = {
username: 'existinguser',
NumPreguntasJugadas:5,
NumAcertadas: 2
};

afterAll(async () => {
app.close();
await mongoServer.stop();
const response = await request(app)
.post('/savehistory')
.send(userData)
.expect(200);

expect(response.body.username).toBe(userData.username);
expect(response.body.NumJugadas).toBe(2); // Verificar que se ha incrementado el número de jugadas
expect(response.body.NumPreguntasJugadas).toBe(10); // Verificar que se han añadido las nuevas preguntas jugadas
expect(response.body.NumAcertadas).toBe(5); // Verificar que se han añadido las nuevas preguntas acertadas
expect(response.body.NumFalladas).toBe(5); // Verificar que se han añadido las nuevas preguntas falladas
});
});

describe('History Service', () => {
it('Should history /gethistory', async () => {
const response = await request(app).get('/gethistory');
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('username', 'testuser');
expect(response.body).toHaveProperty('NumJugadas', '0');
describe('GET /gethistory', () => {
it('should get history entry for an existing user', async () => {
// Crear una entrada de historial existente en la base de datos
await History.create({
username: 'existinguser',
NumJugadas: 1,
NumPreguntasJugadas: 5,
NumAcertadas: 3,
NumFalladas: 2
});

const response = await request(app)
.get('/gethistory')
.query({ username: 'existinguser' })
.expect(200);

expect(response.body.username).toBe('existinguser');
expect(response.body.NumJugadas).toBe(1);
expect(response.body.NumPreguntasJugadas).toBe(5);
expect(response.body.NumAcertadas).toBe(3);
expect(response.body.NumFalladas).toBe(2);
});

it('should create new history entry for a non-existing user', async () => {
const response = await request(app)
.get('/gethistory')
.query({ username: 'nonexistinguser' })
.expect(200);

expect(response.body.username).toBe('nonexistinguser');
expect(response.body.NumJugadas).toBe(0);
expect(response.body.NumPreguntasJugadas).toBe(0);
expect(response.body.NumAcertadas).toBe(0);
expect(response.body.NumFalladas).toBe(0);
});
});

0 comments on commit f05e9d8

Please sign in to comment.