Skip to content

Commit

Permalink
Added a new test case
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Mario committed Apr 2, 2024
1 parent 919092e commit b1d38fc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 19 deletions.
40 changes: 21 additions & 19 deletions users/recordservice/record-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,27 @@ mongoose.connect(mongoUri);
app.post('/record', async (req, res) => {
const user = req.body.user;
const game = req.body.game;
let record = await Record.findOne({ user : user });
console.log(record)
if(record){ //If it exits
record.games.push(game);
}
else{ //Lo creamos
record = new Record({
user:user,
games:[game]
});
}

try {
const savedRecord = await record.save();
res.json({user:savedRecord.user});
} catch (err) {
console.log(err);
res.status(500).json({error : 'Hubo un error en el servidor'});
if(user && game){
let record = await Record.findOne({ user : user });
if(record){ //If it exits
record.games.push(game);
}
else{ //Lo creamos
record = new Record({
user:user,
games:[game]
});
}

try {
const savedRecord = await record.save();
res.json({user:savedRecord.user});
} catch (err) {
console.log(err);
res.status(500).send();
}
}
res.status(400).send();
});

app.get('/record/:user', async (req, res) => {
Expand All @@ -48,7 +50,7 @@ app.get('/record/:user', async (req, res) => {
res.json({record : recordFound});
}
} catch (err) {
res.status(500).json({ error: "Hubo un error en el servidor" });
res.status(500).send();
}
});

Expand Down
83 changes: 83 additions & 0 deletions users/recordservice/record-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,89 @@ afterAll(async () => {
});

describe('Record Service', () => {
it('should give a 400 with a bad requests on POST /record', async () => {
//Does not have neither user nor game
let newUser = {
bad:"testuser",
bad2:
{
"questions": [
{
"question": "¿Cuál es el río más largo del mundo?",
"answers": ["Nilo", "Amazonas", "Yangtsé", "Misisipi"],
"answerGiven": "Amazonas",
"correctAnswer": "Amazonas"
},
{
"question": "¿Cuál es el elemento más abundante en la corteza terrestre?",
"answers": ["Hierro", "Oxígeno", "Silicio", "Aluminio"],
"answerGiven": "Oxígeno",
"correctAnswer": "Oxígeno"
}
],
"points": 2500,
"date": "02/02/24"
}
};

let response = await request(app).post('/record').send(newUser);
expect(response.status).toBe(400);

//Does not have game
newUser = {
user:"testuser",
bad:
{
"questions": [
{
"question": "¿Cuál es el río más largo del mundo?",
"answers": ["Nilo", "Amazonas", "Yangtsé", "Misisipi"],
"answerGiven": "Amazonas",
"correctAnswer": "Amazonas"
},
{
"question": "¿Cuál es el elemento más abundante en la corteza terrestre?",
"answers": ["Hierro", "Oxígeno", "Silicio", "Aluminio"],
"answerGiven": "Oxígeno",
"correctAnswer": "Oxígeno"
}
],
"points": 2500,
"date": "02/02/24"
}
};

response = await request(app).post('/record').send(newUser);
expect(response.status).toBe(400);

//Does not have user
newUser = {
bad:"testuser",
game:
{
"questions": [
{
"question": "¿Cuál es el río más largo del mundo?",
"answers": ["Nilo", "Amazonas", "Yangtsé", "Misisipi"],
"answerGiven": "Amazonas",
"correctAnswer": "Amazonas"
},
{
"question": "¿Cuál es el elemento más abundante en la corteza terrestre?",
"answers": ["Hierro", "Oxígeno", "Silicio", "Aluminio"],
"answerGiven": "Oxígeno",
"correctAnswer": "Oxígeno"
}
],
"points": 2500,
"date": "02/02/24"
}
};

response = await request(app).post('/record').send(newUser);
expect(response.status).toBe(400);

});
it('should add a new record on POST /record', async () => {
const newUser = {
user:"testuser",
Expand Down

0 comments on commit b1d38fc

Please sign in to comment.