From 82ba5f1d9c303198c28c5b80058d3470309124b1 Mon Sep 17 00:00:00 2001 From: hogaur Date: Wed, 9 Jul 2014 20:11:42 +0530 Subject: [PATCH] [Hari|Siddharth] Making connectionFactory, execute function in Database Model #42 Improving Controller Tests #19 --- app/models/database.server.model.js | 28 ++++++++- .../database.server.controller.test.js | 17 +++-- tests/models/database.server.model.test.js | 62 +++++++++++++++++-- 3 files changed, 92 insertions(+), 15 deletions(-) diff --git a/app/models/database.server.model.js b/app/models/database.server.model.js index 74df2d5..e28a284 100644 --- a/app/models/database.server.model.js +++ b/app/models/database.server.model.js @@ -56,12 +56,38 @@ function parseInfo (info) { return info; } +DatabaseSchema.methods.connectionFactory = function(callback){ + var client = redis.createClient(this.port, this.host); + client.on('error', function(error){ callback(error) }); + client.on('connect', function(){ callback(null, client); }); +}; + +function commandAllowed(command) { + if(/^flush*/.test(command)) + return new Error('You can not flush db!'); + else if(/^eval*/.test(command)) + return new Error('Eval is evil!'); + return null; +} + +DatabaseSchema.methods.execute = function(command, callback){ + var error = commandAllowed(command) + if(error) return callback(error, null); + command = command.split(' '); + this.connectionFactory(function(error, client){ + if(error) return callback(error); + client.send_command(command[0],command.splice(1),function(error, response){ + callback(error, response); + }); + client.quit(); + }); +} + DatabaseSchema.methods.fetchInfoFromClient = function(callback){ var _this = this; var client = redis.createClient(_this.port, _this.host); client.on('error', function(error){console.log(error); client.quit(); }); client.on('connect', function(){ - console.log('yay'); client.info(function(){ var info = new Info({ database: _this, diff --git a/tests/controllers/database.server.controller.test.js b/tests/controllers/database.server.controller.test.js index 6279989..acb743d 100644 --- a/tests/controllers/database.server.controller.test.js +++ b/tests/controllers/database.server.controller.test.js @@ -53,10 +53,8 @@ describe('Database Controller Tests:', function() { .send(database) .expect(200) .end(function(err,res){ - Database.findOne({ '_id' : res.body['_id']}, function(err, db){ - db.name.should.equal(database['name']); - done(); - }); + res.body['name'].should.equal(database['name']); + done(); }); }); }); @@ -159,7 +157,7 @@ describe('Database Controller Tests:', function() { .set('cookie',cookie) .expect(200) .end(function(err, res) { - if(res.body!==null) JSON.stringify(res.body).should.containDeep('keys'); + JSON.stringify(res.body).should.containDeep('keys'); done(); }); }); @@ -417,7 +415,7 @@ describe('Database Controller Tests:', function() { .delete('/databases/' + database._id) .set('cookie',cookie) .end(function(err, res){ - JSON.stringify(res.body._id).should.eql(databaseId); + JSON.stringify(res.body._id).should.equal(databaseId); done(); }); }); @@ -461,7 +459,7 @@ describe('Database Controller Tests:', function() { .end(function(err, res){ Info.find({database : database._id}).sort('-timestamp').limit(10) .exec(function(error, data){ - data.should.eql(res.body); + JSON.stringify(data).should.equal(JSON.stringify(res.body)); done(); }); }); @@ -558,8 +556,7 @@ describe('Database Controller Tests:', function() { .send({searchKeyword:'foo',selectedCollection:'db0'}) .set('cookie',cookie) .end(function(err, res){ - res.body.result.should.eql(["foo234567890","foo","foo123435366","foo34534253425234567890"]); - done(); + JSON.stringify(res.body).should.equal(JSON.stringify({result:["foo234567890","foo","foo123435366","foo34534253425234567890"]})); done(); }); }); }); @@ -573,7 +570,7 @@ describe('Database Controller Tests:', function() { .send({searchKeyword:'d', selectedCollection:'All Collections'}) .set('cookie', cookie) .end(function(err, res){ - res.body.result.should.eql(['dsafad-db0', 'dsafadf-db0', 'dsaf2-db2','dasfs-db2']); + JSON.stringify(res.body).should.equal(JSON.stringify({result:['dsafad-db0', 'dsafadf-db0', 'dsaf2-db2','dasfs-db2']})); done(); }); }); diff --git a/tests/models/database.server.model.test.js b/tests/models/database.server.model.test.js index 3bc235d..1b68d95 100644 --- a/tests/models/database.server.model.test.js +++ b/tests/models/database.server.model.test.js @@ -33,8 +33,8 @@ describe('Database Model Unit Tests:', function() { user.save(function() { database = new Database({ name: 'Database Name', - host:'host', - port: 1000, + host:'localhost', + port: 6379, user: user }); @@ -59,9 +59,8 @@ describe('Database Model Unit Tests:', function() { }); }); - it('should be able to show an error when try to save without host', function(done) { + it('should be able to show an error when try to save without host', function(done) { database.host = ''; - return database.save(function(err) { should.exist(err); done(); @@ -78,6 +77,61 @@ describe('Database Model Unit Tests:', function() { }); }); + describe('Method execute', function(){ + it('should return command result', function(done){ + database.save(); + database.execute('select 1', function(err, res){ + res.should.equal('OK'); + done(); + }); + }); + + it('should disable flush,eval commands', function(done){ + database.save(); + database.execute('flushdb',function(err, res){ + should.exist(err); + err.message.should.equal('You can not flush db!'); + }); + database.execute('eval', function(err, res){ + should.exist(err); + err.message.should.equal('Eval is evil!'); + done(); + }); + }); + + it('should give error for invalid commands', function(done){ + database.save(); + database.execute('dadsf',function(err, res){ + should.exist(err); + done(); + }); + }); + }); + + describe('Method connectionFactory',function () { + it('should create client', function(done){ + database.save(); + database.connectionFactory(function(error, client){ + should.exist(client); + done(); + }); + }); + + it('should give error when tries to connect to an unavailable redis', function(done){ + var db = new Database({ + name: 'New DB', + host: '1.1.1.1', + port: 1000, + user: user + }); + db.save(); + db.connectionFactory(function (error, client) { + should.exist(error); + done(); + }) + }); + + }); describe('#fetchInfoFromClient',function(){ it('should fetch info from redis and save as Info model', function(done){ var database1 = new Database({