Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tests to sonarcloud #140

Merged
merged 35 commits into from
May 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
5c86829
added tests to sonarcloud
carolbgmm May 1, 2024
7227341
removed from build conflicting test just for now
carolbgmm May 1, 2024
67b40da
small change
carolbgmm May 1, 2024
4dcd031
removed non-finishing tests
carolbgmm May 1, 2024
de31f7c
removed non-finishing tests
carolbgmm May 1, 2024
e62ed51
all tests again
carolbgmm May 1, 2024
993f8e1
fixed something
carolbgmm May 1, 2024
95c80dd
changed order
carolbgmm May 1, 2024
f21241f
tests for the game service
pelazas May 1, 2024
02d1892
Merge pull request #141 from Arquisoft/game-service-tests
pelazas May 1, 2024
4c6b259
changed order of tests
carolbgmm May 1, 2024
7dd2568
tests for create and join group
pelazas May 1, 2024
75fd513
group service tests
pelazas May 1, 2024
f56e49a
fix issue with test on game service
pelazas May 1, 2024
94f6b5c
Merge pull request #142 from Arquisoft/group-service-tests
pelazas May 1, 2024
ee35d87
trying test for qg
carolbgmm May 4, 2024
8659c11
Merge branch 'dev' of https://github.com/Arquisoft/wiq_en2a into dev
carolbgmm May 4, 2024
2c19325
changes in tests
carolbgmm May 4, 2024
ad88fea
imports fixed
carolbgmm May 4, 2024
3b48bf0
removed empty tests
carolbgmm May 4, 2024
e022dfc
added timeout
carolbgmm May 4, 2024
0169f95
commented some tests
carolbgmm May 4, 2024
fc63f13
fix mongo error on tests
pelazas May 6, 2024
526cd9e
qg tests base file
pelazas May 6, 2024
debacc2
remove multiplayer server tests
pelazas May 6, 2024
a5e5209
questions by ids tests
pelazas May 6, 2024
764a2ba
sparql queries test and more
pelazas May 6, 2024
91f1962
math questions
pelazas May 6, 2024
7ac97ae
binding results test
pelazas May 6, 2024
b9fc9c3
test for generating the questions and language
pelazas May 6, 2024
2b6c038
refactor duplicated code
pelazas May 6, 2024
cee357a
refactor duplicated code 2
pelazas May 6, 2024
3d59f29
refactor duplicated code 3
pelazas May 6, 2024
cdd13d4
Merge pull request #144 from Arquisoft/qgservice-tests
pelazas May 6, 2024
9eee00b
remove unnecessary test
pelazas May 6, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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