Skip to content

Commit

Permalink
Merge pull request #140 from Arquisoft/dev
Browse files Browse the repository at this point in the history
missing tests
  • Loading branch information
pelazas authored May 6, 2024
2 parents 867d3f6 + 9eee00b commit d400cd1
Show file tree
Hide file tree
Showing 17 changed files with 1,027 additions and 221 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ jobs:
- run: npm --prefix users/userservice ci
- run: npm --prefix gatewayservice ci
- run: npm --prefix webapp ci
- run: npm --prefix game/gameservice ci
- run: npm --prefix game/groupservice ci
- run: npm --prefix game/qgservice ci
- run: npm --prefix multiplayerservice ci
- run: npm --prefix users/authservice test -- --coverage
- run: npm --prefix users/userservice test -- --coverage
- run: npm --prefix gatewayservice test -- --coverage
- run: npm --prefix webapp test -- --coverage
- run: npm --prefix game/gameservice test -- --coverage
- run: npm --prefix game/groupservice test -- --coverage
- run: npm --prefix game/qgservice test -- --coverage
- name: Analyze with SonarCloud
uses: sonarsource/sonarcloud-github-action@master
env:
Expand Down
21 changes: 21 additions & 0 deletions docs/src/14_usability_test.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ifndef::imagesdir[:imagesdir: ../images]

[[section-load_test]]
== Anex II: Usability test
The tests were carried out with a sample of 10 users with different backgrounds. Being 3 of them students from Software Engineering.

=== Effectiveness

* 100% of users completed the tasks on the first try.
* 80% of users did not need extra help to navigate the web.

=== Efficiency

* All users did each task in less than 10 seconds, including answering the questions of the game.
* 80% of users did not need extra help to navigate the web.

=== Satisfaction

* 1 user though the questions should be harder.
* 2 users found a bug that is now fixed: when you play, close session, login again and go to profile, the last questions do not appear.
* 6 users tried to login with google, but it only works with test users.
8 changes: 3 additions & 5 deletions game/gameservice/GameController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
let Game = require('./game-model');
const { createGame } = require('./queries/CreateGame');
const mongoose = require('mongoose');

let GameController = {
/* HACER EN USER - GET LAST GAME BY USER
Expand All @@ -11,10 +10,9 @@ let GameController = {
},*/
create: async (req, res) => {
try{
const { questions, players } = req.body;
console.log(questions, players)
const game = await createGame(questions, players);
res.json(game);
const { questions, players } = req.body;
const game = await createGame(questions, players);
res.json(game);
} catch(error){
res.status(500).json({ message: error.message });
}
Expand Down
104 changes: 91 additions & 13 deletions game/gameservice/game-service.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const request = require('supertest');
const { MongoMemoryServer } = require('mongodb-memory-server');
const Game = require('./game-model');
const { createGame } = require('./queries/CreateGame');

let mongoServer;
let app;
Expand All @@ -18,23 +20,99 @@ afterAll(async () => {

describe('Game service', () => {

// mock data
const questions = [{ _id: 'question1_id' }, { _id: 'question2_id' }];
const users = [{ _id: 'user1_id' }, { _id: 'user2_id' }];

it('should create a game and update user lastGame field', async () => {
const response = await request(app).post('/creategame').send({questions,users});

const questions = [{ uuid: 'question1_id' }, { uuid: 'question2_id' }];
const players = [{ uuid: 'user1_id' }, { uuid: 'user2_id' }];

const response = await request(app).post('/creategame').send({questions,players});
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('_id');

const gameId = response.body._id;
const gameFromDB = await mongoose.model('Game').findById(gameId);
const user1FromDB = await mongoose.model('User').findById('user1_id');
const user2FromDB = await mongoose.model('User').findById('user2_id');
const gameUUId = response.body.uuid;

const gameInDB = await Game.findOne({ uuid: gameUUId });
// expect the game to exist
expect(gameInDB).toBeTruthy();

// Expect the game to have the correct questions
expect(gameInDB.questions.length).toBe(questions.length);
for (let i = 0; i < questions.length; i++) {
expect(gameInDB.questions[i]).toBe(questions[i].uuid);
}

// Expect the game to have the correct players
expect(gameInDB.players.length).toBe(players.length);
for (let i = 0; i < players.length; i++) {
expect(gameInDB.players[i]).toBe(players[i].uuid);
}

// Assertions for the database state
expect(gameFromDB).toBeTruthy();
expect(user1FromDB.lastGame.toString()).toBe(gameId);
expect(user2FromDB.lastGame.toString()).toBe(gameId);
});

it('should return status 500 when sending invalid data', async () => {
const invalidData = {}; // Sending empty object as invalid data

const response = await request(app).post('/creategame').send(invalidData);

expect(response.status).toBe(500);
expect(response.body).toHaveProperty('message');
});

it('should delete a game', async () => {
// Create a game to be deleted
const newGame = await Game.create({ uuid: 'game_to_delete_id' });

// Send request to delete the game
const response = await request(app).delete(`/deletegame/${newGame.uuid}`);

// Expect response status to be 200
expect(response.status).toBe(200);
expect(response.body).toHaveProperty('message', 'Game deleted');

// Verify that the game is deleted from the database
const deletedGame = await Game.findOne({ uuid: newGame.uuid });
expect(deletedGame).toBeNull(); // Expect deletedGame to be null, indicating it doesn't exist in the database
});

it('should return a game by its ID', async () => {
// Create a game to retrieve
const newGame = await Game.create({ uuid: 'game_to_retrieve_id' });

// Send request to retrieve the game by its ID
const response = await request(app).get(`/getgame/${newGame.uuid}`);

// Expect response status to be 200
expect(response.status).toBe(200);
expect(response.body).toHaveLength(1); // Assuming the endpoint always returns an array of games
expect(response.body[0]).toHaveProperty('uuid', newGame.uuid); // Assuming the game object contains a UUID property
});

it('should return a JSON response with "hi" message', async () => {
const response = await request(app).get('/');

// Expect response status to be 200
expect(response.status).toBe(200);

// Expect response body to contain the expected JSON object
expect(response.body).toEqual({ hi: 'game service' });
});

it('should throw an error if no players are found', async () => {
// Prepare test data with empty players array
const questions = [{ uuid: 'question1_id' }, { uuid: 'question2_id' }];
const players = [];

// Expect createGame function to throw an error when called with empty players array
await expect(createGame(questions, players)).rejects.toThrow('No players found');
});

it('should throw an error if player UUID is null or undefined', async () => {
// Prepare test data with null or undefined player UUID
const questions = [{ uuid: 'question1_id' }, { uuid: 'question2_id' }];
const players = [{ uuid: null }, { uuid: undefined }];

// Expect createGame function to throw an error when called with player UUID null or undefined
await expect(createGame(questions, players)).rejects.toThrow('No players found');
});

});
10 changes: 6 additions & 4 deletions game/gameservice/gameservice.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
// gameservice.js
const express = require('express');
const axios = require('axios');
const mongoose = require('mongoose');
const { createGame } = require('./queries/CreateGame');
const bodyParser = require('body-parser');
const GameController = require('./GameController');

const app = express();
const port = 8004;

// app.use(bodyParser.json());
app.use(express.json());
app.use(bodyParser.json());

const mongoUri = process.env.MONGODB_URI || 'mongodb://localhost:27017/userdb';
mongoose.connect(mongoUri);
Expand All @@ -29,4 +27,8 @@ const server = app.listen(port, () => {
console.log(`Question generator Service listening at http://localhost:${port}`);
});

server.on('close', () => {
mongoose.connection.close();
});

module.exports = server;
5 changes: 2 additions & 3 deletions game/gameservice/queries/CreateGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ const uuid = require('uuid')
async function createGame(questions, players) {
try {
// Create a new Game instance
console.log(players)
if(players.length == 0){
if(players.length === 0){
throw new Error('No players found')
}
if(players[0].uuid == null || players[0].uuid == undefined){
if(players[0].uuid === null || players[0].uuid === undefined){
throw new Error('No players found')
}
const game = new Game({
Expand Down
52 changes: 22 additions & 30 deletions game/groupservice/GroupController.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,17 @@ let GroupController = {
res.json(response);

}catch(error){
console.log(error)
res.status(400).json({error: error.message})
res.status(500).json({error: error.message})
}

},
leaveGroup: async (req,res) => {
try{
console.log(req.body)
requiredFields = ['expelledUUID','groupName', 'adminUUID']
validateRequiredFields(req, requiredFields);
const group = await getGroupByName(req.body.groupName);
console.log(req.body.adminUUID +" - "+ req.body.expelledUUID)

if(req.body.adminUUID != group.admin && req.body.adminUUID != req.body.expelledUUID){
console.log("entra en la condicion")
res.json({ message: 'User is unable to perform this operation' });
return;
}
Expand Down Expand Up @@ -76,12 +73,11 @@ let GroupController = {
createGroup: async (req,res) =>{
try{

requiredFields =['groupName','creatorUUID','description','isPublic']
let requiredFields =['groupName','creatorUUID','description','isPublic']
validateRequiredFields(req,requiredFields);

let newGroup;
if(req.body.isPublic){

if(req.body.isPublic){
newGroup = new Group({
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
Expand All @@ -92,25 +88,22 @@ let GroupController = {
groupName: req.body.groupName,
uuid: uuid.v4(),
})
await newGroup.save();

} else {
const joinCode = generateJoinCode();

newGroup = new Group({
groupName: req.body.groupName,
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
maxNumUsers: maxNumUsers,
description: req.body.description,
isPublic: false,
joinCode: joinCode,
creationDate: Date(),
uuid: uuid.v4(),
});
await newGroup.save();
}
res.json(newGroup);
} else {
const joinCode = generateJoinCode();
newGroup = new Group({
groupName: req.body.groupName,
admin: req.body.creatorUUID,
members: [req.body.creatorUUID],
maxNumUsers: maxNumUsers,
description: req.body.description,
isPublic: false,
joinCode: joinCode,
creationDate: Date(),
uuid: uuid.v4(),
});
}
const savedGroup = await newGroup.save()
res.json(savedGroup);

} catch(error){
res.status(500).json({error: error.message})
Expand Down Expand Up @@ -144,9 +137,8 @@ let GroupController = {
async function getGroupByName(name) {
try {
const group = await Group.findOne({ groupName: name });

if (!group) {
throw new Error('This group does not exist');
throw new Error('This group does not exist');
}

return group;
Expand All @@ -164,4 +156,4 @@ function validateRequiredFields(req, requiredFields) {
}
}

module.exports = GroupController;
module.exports = {GroupController, getGroupByName};
2 changes: 1 addition & 1 deletion game/groupservice/group-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const express = require('express');
const mongoose = require('mongoose');
const GroupController = require('./GroupController');
const {GroupController} = require('./GroupController');

const app = express();
const port = 8005;
Expand Down
Loading

0 comments on commit d400cd1

Please sign in to comment.