diff --git a/src/libs/routes/simulation.js b/src/libs/routes/simulation.js index 30aa6b5ac499767ea2e0213c50166344eea812b5..961f6173315bf795ab490b50da79ed6898783839 100644 --- a/src/libs/routes/simulation.js +++ b/src/libs/routes/simulation.js @@ -64,7 +64,7 @@ simulationApp.get('/:id', (req, res) => { }); simulationApp.post('/:id', (req, res, next) => { - if (!req.body) { + if (!Object.keys(req.body).length) { res.send({ success: false, msg: 'No field informed to update' }); } else { next(); diff --git a/src/test/test.js b/src/test/test.js index c25c218b0ecdb168196835520de3846b9aae111d..a5fc30a6b704d67cea513c543e6da73854e42053 100644 --- a/src/test/test.js +++ b/src/test/test.js @@ -492,6 +492,7 @@ describe('Requires a simulation', () => { Simulation.findById(res.body.id, (err, simulation) => { simulation.should.have.property('name'); simulation.name.should.be.a('string'); + simulation.name.should.equal('test'); simulation.should.have.property('location'); simulation.location.should.be.a('number'); simulation.location.should.equal(5); @@ -500,6 +501,63 @@ describe('Requires a simulation', () => { }); }); }); + it('should update multiple fields on a single request', (done) => { + newSimulation = new Simulation(); + newSimulation.name = 'test'; + newSimulation.save((err, sim) => { + let id = sim._id; + chai.request(server) + .post(`/api/v1/simulation/${id}`) + .send({ + location: 5, + time: 3, + failure_rate: [0.1, 0.2, 0.3], + goals_care: [0.3, 0.2, 0.1], + goals_inclusion: [0.8, 0.9, 1] + }) + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('id'); + res.body.id.should.be.a('string'); + Simulation.findById(res.body.id, (err, simulation) => { + simulation.should.have.property('name'); + simulation.name.should.be.a('string'); + simulation.name.should.equal('test'); + simulation.should.have.property('location'); + simulation.location.should.be.a('number'); + simulation.location.should.equal(5); + simulation.should.have.property('time'); + simulation.time.should.be.a('number'); + simulation.time.should.equal(3); + simulation.should.have.property('failure_rate'); + simulation.failure_rate.should.be.a('array'); + simulation.failure_rate.length.should.equal(3); + simulation.should.have.property('goals_care'); + simulation.goals_care.should.be.a('array'); + simulation.goals_care.length.should.equal(3); + simulation.should.have.property('goals_inclusion'); + simulation.goals_inclusion.should.be.a('array'); + simulation.goals_inclusion.length.should.equal(3); + }); + done(); + }); + }); + }); + it('should not update an unexisting simulation', (done) => { + newSimulation = new Simulation(); + let id = newSimulation._id; + chai.request(server) + .post(`/api/v1/simulation/${id}`) + .send({ location: 5 }) + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + done(); + }); + }); it('should update an existing simulation\'s time', (done) => { newSimulation = new Simulation(); newSimulation.name = 'test'; @@ -524,4 +582,22 @@ describe('Requires a simulation', () => { }); }); }); + it('should not change results for empty post requests', (done) => { + newSimulation = new Simulation(); + newSimulation.name = 'test'; + newSimulation.save((err, sim) => { + let id = sim._id; + chai.request(server) + .post(`/api/v1/simulation/${id}`) + .end((err, res) => { + res.should.have.status(200); + res.should.be.json; + res.body.should.have.property('success'); + res.body.success.should.equal(false); + done(); + }); + }); + + }); + });