From f4fd92d2549ea934ea34ec999114850a32049625 Mon Sep 17 00:00:00 2001 From: andsandv Date: Wed, 22 Oct 2014 15:52:43 +0200 Subject: [PATCH] #13 Sending in a patch request when updating enabled flag --- unleash-server/lib/api.js | 30 ++++++++++++++--------------- unleash-server/public/js/unleash.js | 23 ++++++++++++---------- unleash-server/test/apiSpec.js | 25 ++++++++++++++++-------- 3 files changed, 45 insertions(+), 33 deletions(-) diff --git a/unleash-server/lib/api.js b/unleash-server/lib/api.js index c97d7fc694..6c780341a0 100644 --- a/unleash-server/lib/api.js +++ b/unleash-server/lib/api.js @@ -27,7 +27,7 @@ module.exports = function (app) { db.getFeature(newFeature.name).then(function (feature) { if (feature) { - res.status(500).end(); + res.status(403).end(); } else { db.addFeature(newFeature).then(function () { res.status(201).end(); @@ -36,20 +36,20 @@ module.exports = function (app) { }); }); - app.patch('/features/:id', function (req, res) { - var body = req.body; - body.data.name = req.params.id; - var event = {}; - event.user = req.connection.remoteAddress; - event.comment = body.comment; - event.data = body.data; - - // console.log(event); - - // db.save(event).then(function () { - res.status(204).end(); - // }); - + app.patch('/features/:featureName', function (req, res) { + var featureName = req.params.featureName; + db.getFeature(featureName).then(function (feature) { + if (feature) { + var changeRequest = req.body; + var event = {}; + event.type = 'feature-update'; + event.user = req.connection.remoteAddress; + event.data = changeRequest; + res.status(202).end(); + } else { + res.status(404).end(); + } + }); }); }; diff --git a/unleash-server/public/js/unleash.js b/unleash-server/public/js/unleash.js index dd99726ce6..cdf75ed521 100644 --- a/unleash-server/public/js/unleash.js +++ b/unleash-server/public/js/unleash.js @@ -29,19 +29,20 @@ var FeatureList = React.createClass({ this.setState({features: data.features}); }, - updateFeature: function (feature) { + updateFeature: function (changeRequest) { var newFeatures = this.state.features; newFeatures.forEach(function(f){ - if(f.name === feature.name) { - f = feature; + if(f.name === changeRequest.name) { + f[changeRequest.field] = changeRequest.value; } }); - + console.log(changeRequest); reqwest({ - url: 'features/' + feature.name, - method: 'post', + url: 'features/' + changeRequest.name, + method: 'patch', type: 'json', - data: feature + contentType: 'application/json', + data: JSON.stringify(changeRequest) }).then(function() { this.setState({features: newFeatures}); }.bind(this), function() { @@ -78,9 +79,11 @@ var Feature = React.createClass({ */ handleEnableChange: function(event) { var feature = this.props.feature; - - feature.enabled = event.target.checked; - this.props.updateFeature(feature); + this.props.updateFeature({ + name: feature.name, + field: "enabled", + value: event.target.checked + }); }, render: function () { diff --git a/unleash-server/test/apiSpec.js b/unleash-server/test/apiSpec.js index 6d565748d8..8072d5a88c 100644 --- a/unleash-server/test/apiSpec.js +++ b/unleash-server/test/apiSpec.js @@ -22,22 +22,31 @@ describe('The api', function () { it('creates new feature toggle', function (done) { request .post('/features') - .send({name: 'com.test.feature', 'status': 'off'}) + .send({name: 'com.test.feature', 'enabled': false}) .set('Content-Type', 'application/json') .expect(201, done); }); - it('change status of feature toggle', function (done) { + it('can not change status of feature toggle that dose not exsist', function (done) { request - .patch('/features/com.test.feature') + .patch('/features/shouldNotExsist') .send({ - 'comment': 'patch test of com.test.feature', - data: { - 'status': 'on' - } + 'field': 'enabled', + 'value': true }) .set('Content-Type', 'application/json') - .expect(204, done); + .expect(404, done); + }); + + it('can change status of feature toggle that dose exsist', function (done) { + request + .patch('/features/featureY') + .send({ + 'field': 'enabled', + 'value': true + }) + .set('Content-Type', 'application/json') + .expect(202, done); }); }); \ No newline at end of file