diff --git a/users/recordservice/record-service.js b/users/recordservice/record-service.js index d49c2909..aa0ace2b 100644 --- a/users/recordservice/record-service.js +++ b/users/recordservice/record-service.js @@ -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) => { @@ -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(); } }); diff --git a/users/recordservice/record-service.test.js b/users/recordservice/record-service.test.js index a364e61e..d2c2ca6e 100644 --- a/users/recordservice/record-service.test.js +++ b/users/recordservice/record-service.test.js @@ -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",