Permalink
Browse files

Add PUT and POST api routes

  • Loading branch information...
1 parent b7845f2 commit 5af3fafe365df704add88abf85e4827bccfa5196 @PopDaph PopDaph committed Nov 4, 2013
Showing with 172 additions and 6 deletions.
  1. +8 −4 app.js
  2. +32 −1 app/api/routes/check.js
  3. +27 −0 models/check.js
  4. +2 −1 package.json
  5. +48 −0 test/functional/check.js
  6. +55 −0 test/unit/check.js
View
@@ -135,11 +135,15 @@ fs.exists('./plugins/index.js', function(exists) {
}
});
-var port = process.env.PORT || config.server.port;
-server.listen(port);
-console.log("Express server listening on port %d in %s mode", port, app.settings.env);
+module.exports = app;
+if (!module.parent) {
+ var port = process.env.PORT || config.server.port;
+ server.listen(port, function(){
+ console.log("Express server listening on port %d in %s mode", port, app.settings.env);
+ });
+}
// monitor
if (config.autoStartMonitor) {
require('./monitor');
-}
+}
@@ -111,4 +111,35 @@ module.exports = function(app) {
});
});
-};
+ app.put('/checks', function(req, res, next) {
+ var check = new Check();
+ try {
+ check.populateFromDirtyCheck(req.body, app.get('pollerCollection'));
+ } catch (checkException) {
+ return next(checkException);
+ }
+ check.save(function(saveError) {
+ if(saveError) return next(saveError);
+
+ res.json(check);
+ });
+ });
+
+ app.post('/checks/:id', function(req, res, next) {
+ Check.findOne({ _id: req.params.id }, function(err, check) {
+ if (err) return next(err);
+ if (!check) return next('Check not found');
+
+ try {
+ check.populateFromDirtyCheck(req.body, app.get('pollerCollection'));
+ } catch (checkException) {
+ return next(checkException);
+ }
+ check.save(function(saveError) {
+ if(saveError) return next(saveError);
+
+ res.json(check);
+ });
+ });
+ });
+};
View
@@ -314,6 +314,33 @@ Check.methods.getSingleStatForPeriod = function(period, date, callback) {
});
};
+Check.methods.populateFromDirtyCheck = function(dirtyCheck, pollerCollection) {
+ this.url = dirtyCheck.url || this.url;
+ this.maxTime = dirtyCheck.maxTime || this.maxTime;
+ this.isPaused = dirtyCheck.isPaused || this.isPaused;
+ this.alertTreshold = dirtyCheck.alertTreshold || this.alertTreshold;
+ this.interval = dirtyCheck.interval * 1000 || this.interval;
+
+ if(typeof(dirtyCheck.name) != 'undefined') {
+ this.name = dirtyCheck.name;
+ } else if(typeof(this.name) == 'undefined') {
+ this.name = dirtyCheck.url;
+ }
+
+ if(typeof(dirtyCheck.tags) != 'undefined') {
+ this.tags = this.constructor.convertTags(dirtyCheck.tags);
+ }
+
+ if (dirtyCheck.type) {
+ if (!pollerCollection.getForType(dirtyCheck.type).validateTarget(this.url)) {
+ throw new Error('URL ' + this.url + ' and poller type ' + dirtyCheck.type + ' mismatch');
+ }
+ this.type = dirtyCheck.type;
+ } else {
+ this.type = pollerCollection.guessTypeForUrl(this.url);
+ }
+};
+
Check.statics.getAllTags = function(callback) {
this.aggregate(
{ $unwind: "$tags" },
View
@@ -21,7 +21,8 @@
},
"devDependencies": {
"mocha": "1.7.x",
- "should": "1.1.0"
+ "should": "1.1.0",
+ "request": "2.27.0"
},
"keywords": ["uptime", "monitoring", "api", "check"],
"repository": "https://github.com/fzaninotto/uptime",
@@ -0,0 +1,48 @@
+process.env.NODE_ENV = 'test';
+var mongoose = require('../../bootstrap');
+var Check = require('../../models/check');
+var app = require('../../app');
+var assert = require('assert');
+var http = require('http');
+var request = require('request');
+
+describe('GET /checks', function() {
+
+ var check1, check2, pollerCollection; // fixtures
+
+ before(function() {
+ this.server = app.listen(3000);
+ pollerCollection = app.get('pollerCollection');
+ });
+
+ before(function() {
+ check1 = new Check();
+ check1.url = 'http://www.url1.fr';
+ check1.name = 'name1';
+ check1.isPaused = false;
+ check1.save();
+
+ check2 = new Check();
+ check2.url = 'http://www.url2.fr';
+ check2.isPaused = false;
+ check2.save();
+ });
+
+ it('should fetch all elements', function(done) {
+ request('http://127.0.0.1:3000/api/checks', function(err, resp, body){
+ assert(!err);
+ content = JSON.parse(body);
+ assert.equal(content.length, 2);
+ done();
+ });
+ });
+
+ after(function(done) {
+ Check.remove({}, done);
+ });
+
+ after(function(done) {
+ this.server.close(done);
+ });
+
+});
View
@@ -0,0 +1,55 @@
+var Check = require('../../models/check');
+var PollerCollection = require('../../lib/pollers/pollerCollection');
+var assert = require('assert');
+
+
+describe('populateFromDirtyCheck', function() {
+
+ var checkWithName, checkWithoutName, pollerCollection; // fixtures
+
+ before(function() {
+ pollerCollection = new PollerCollection();
+ });
+
+ beforeEach(function() {
+ checkWithName = new Check();
+ checkWithName.url = 'http://www.url1.fr';
+ checkWithName.name = 'name1';
+ checkWithName.isPaused = false;
+
+ checkWithoutName = new Check();
+ checkWithoutName.url = 'http://www.url2.fr';
+ checkWithoutName.isPaused = false;
+ });
+
+ it('should update object', function() {
+ checkWithName.populateFromDirtyCheck({url:'http://www.new-url.fr', name:'new-name'}, pollerCollection);
+ assert.equal(checkWithName.name, 'new-name');
+ assert.equal(checkWithName.url, 'http://www.new-url.fr');
+ });
+
+ it('should not erase empty values', function() {
+ checkWithName.populateFromDirtyCheck({url:'http://www.new-url.fr'}, pollerCollection);
+ assert.equal(checkWithName.name, 'name1');
+ assert.equal(checkWithName.url, 'http://www.new-url.fr');
+ });
+
+ it('should use given url as name if check has no name and no name is given', function() {
+ checkWithoutName.populateFromDirtyCheck({url:'http://www.new-url.fr'}, pollerCollection);
+ assert.equal(checkWithoutName.name, 'http://www.new-url.fr');
+ assert.equal(checkWithoutName.url, 'http://www.new-url.fr');
+ });
+
+ it('should not use given url if check has name and no name is given', function() {
+ checkWithName.populateFromDirtyCheck({url:'http://www.new-url.fr'}, pollerCollection);
+ assert.equal(checkWithName.name, 'name1');
+ assert.equal(checkWithName.url, 'http://www.new-url.fr');
+ });
+
+ it('should use given name if check has no name name is given', function() {
+ checkWithoutName.populateFromDirtyCheck({name:'new-name'}, pollerCollection);
+ assert.equal(checkWithoutName.name, 'new-name');
+ assert.equal(checkWithoutName.url, 'http://www.url2.fr');
+ });
+
+});

0 comments on commit 5af3faf

Please sign in to comment.