Add credential extract unit tests

This commit is contained in:
Nick O'Leary 2015-03-13 21:26:50 +00:00
parent f77dd06e65
commit 731efe1c01
4 changed files with 128 additions and 162 deletions

View File

@ -63,11 +63,9 @@ function registerEndpoint(type) {
module.exports = { module.exports = {
init: function (_storage) { init: function (_storage,_app) {
storage = _storage; storage = _storage;
// TODO: this should get passed in init function call rather than redApp = _app;
// required directly.
redApp = require("../server").app;
}, },
/** /**
@ -166,7 +164,6 @@ module.exports = {
var newCreds = node.credentials; var newCreds = node.credentials;
if (newCreds) { if (newCreds) {
var savedCredentials = credentialCache[nodeID] || {}; var savedCredentials = credentialCache[nodeID] || {};
var dashedType = nodeType.replace(/\s+/g, '-'); var dashedType = nodeType.replace(/\s+/g, '-');
var definition = credentialsDef[dashedType]; var definition = credentialsDef[dashedType];
if (!definition) { if (!definition) {

View File

@ -50,8 +50,8 @@ function createNode(node,def) {
} }
} }
function init(_settings,storage) { function init(_settings,storage,app) {
credentials.init(storage); credentials.init(storage,app);
flows.init(storage); flows.init(storage);
registry.init(_settings); registry.init(_settings);
} }

View File

@ -63,7 +63,7 @@ function start() {
} }
log.info("Node.js version: "+process.version); log.info("Node.js version: "+process.version);
log.info("Loading palette nodes"); log.info("Loading palette nodes");
redNodes.init(settings,storage); redNodes.init(settings,storage,app);
redNodes.load().then(function() { redNodes.load().then(function() {
var i; var i;
var nodes = redNodes.getNodeList(); var nodes = redNodes.getNodeList();

View File

@ -19,9 +19,14 @@ var sinon = require("sinon");
var when = require("when"); var when = require("when");
var util = require("util"); var util = require("util");
var express = require("express");
var request = require("supertest");
var index = require("../../../red/nodes/index"); var index = require("../../../red/nodes/index");
var credentials = require("../../../red/nodes/credentials"); var credentials = require("../../../red/nodes/credentials");
var log = require("../../../red/log"); var log = require("../../../red/log");
var auth = require("../../../red/api/auth");
describe('Credentials', function() { describe('Credentials', function() {
@ -50,8 +55,20 @@ describe('Credentials', function() {
}); });
}); });
it('saves to storage', function(done) { it('saves to storage', function(done) {
var storage = {
saveCredentials: function(creds) {
return when.resolve("saveCalled");
}
};
credentials.init(storage);
credentials.save().then(function(res) {
res.should.equal("saveCalled");
done();
});
});
it('saves to storage when new cred added', function(done) {
var storage = { var storage = {
getCredentials: function() { getCredentials: function() {
return when.promise(function(resolve,reject) { return when.promise(function(resolve,reject) {
@ -210,159 +227,111 @@ describe('Credentials', function() {
}); });
}); });
//describe('extract and store credential updates in the provided node', function() { it('extract and store credential updates in the provided node', function(done) {
// var path = require('path'); credentials.init({saveCredentials:function(){}},express());
// var fs = require('fs-extra'); credentials.register("test",{
// var http = require('http'); user1:{type:"text"},
// var express = require('express'); password1:{type:"password"},
// var server = require("../../../red/server"); user2:{type:"text"},
// var localfilesystem = require("../../../red/storage/localfilesystem"); password2:{type:"password"},
// var app = express(); user3:{type:"text"},
// var RED = require("../../../red/red.js"); password3:{type:"password"}
//
// var userDir = path.join(__dirname,".testUserHome"); });
// before(function(done) { credentials.add("node",{user1:"abc",password1:"123",user2:"def",password2:"456",user3:"ghi",password3:"789"});
// fs.remove(userDir,function(err) { var node = {id:"node",type:"test",credentials:{
// fs.mkdir(userDir,function() { // user1 unchanged
// sinon.stub(index, 'load', function() { password1:"__PWRD__",
// return when.promise(function(resolve,reject){ user2: "",
// resolve([]); password2:" ",
// }); user3:"newUser",
// }); password3:"newPassword"
// sinon.stub(localfilesystem, 'getCredentials', function() { }};
// return when.promise(function(resolve,reject) { credentials.extract(node);
// resolve({"tab1":{"foo": 2, "pswd":'sticks'}});
// }); node.should.not.have.a.property("credentials");
// }) ;
// RED.init(http.createServer(function(req,res){app(req,res)}), var newCreds = credentials.get("node");
// {userDir: userDir}); newCreds.should.have.a.property("user1","abc");
// server.start().then(function () { newCreds.should.have.a.property("password1","123");
// done(); newCreds.should.not.have.a.property("user2");
// }); newCreds.should.not.have.a.property("password2");
// }); newCreds.should.have.a.property("user3","newUser");
// }); newCreds.should.have.a.property("password3","newPassword");
// });
// done();
// after(function(done) { });
// fs.remove(userDir,done);
// server.stop(); describe('registerEndpoint',function() {
// index.load.restore(); it('returns empty credentials if none are stored',function(done) {
// localfilesystem.getCredentials.restore(); auth.init({});
// }); var app = express();
// credentials.init({saveCredentials:function(){}},app);
// function TestNode(n) { credentials.register("test",{
// index.createNode(this, n); user1:{type:"text"},
// var node = this; password1:{type:"password"},
// this.on("log", function() { user2:{type:"text"},
// // do nothing password2:{type:"password"},
// }); user3:{type:"text"},
// } password3:{type:"password"}
//
// it(': credential updated with good value', function(done) { });
// index.registerType('test', TestNode, { request(app)
// credentials: { .get("/credentials/test/123")
// foo: {type:"test"} .expect("Content-Type",/json/)
// } .end(function(err,res) {
// }); if (err) {
// index.loadFlows().then(function() { done(err);
// var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); } else {
// credentials.extract(testnode); try {
// should.exist(credentials.get('tab1')); res.body.should.eql({});
// credentials.get('tab1').should.have.property('foo',2); done();
// } catch(e) {
// // set credentials to be an updated value and checking this is extracted properly done(e);
// testnode.credentials = {"foo": 3}; }
// credentials.extract(testnode); }
// should.exist(credentials.get('tab1')); })
// credentials.get('tab1').should.not.have.property('foo',2); });
// credentials.get('tab1').should.have.property('foo',3); it('returns stored credentials',function(done) {
// done(); auth.init({});
// }).otherwise(function(err){ var app = express();
// done(err); credentials.init({saveCredentials:function(){}},app);
// }); credentials.register("test",{
// }); user1:{type:"text"},
// password1:{type:"password"},
// it(': credential updated with empty value', function(done) { user2:{type:"text"},
// index.registerType('test', TestNode, { password2:{type:"password"},
// credentials: { user3:{type:"text"},
// foo: {type:"test"} password3:{type:"password"}
// }
// }); });
// index.loadFlows().then(function() { credentials.add("node",{user1:"abc",password1:"123",user2:"def",password2:"456",user3:"ghi",password3:"789"});
// var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); request(app)
// // setting value of "foo" credential to be empty removes foo as a property .get("/credentials/test/node")
// testnode.credentials = {"foo": ''}; .expect("Content-Type",/json/)
// credentials.extract(testnode); .end(function(err,res) {
// should.exist(credentials.get('tab1')); if (err) {
// credentials.get('tab1').should.not.have.property('foo',2); done(err);
// credentials.get('tab1').should.not.have.property('foo'); } else {
// done(); try {
// }).otherwise(function(err){ res.body.should.have.a.property("user1","abc");
// done(err); res.body.should.have.a.property("user2","def");
// }); res.body.should.have.a.property("user3","ghi");
// }); res.body.should.have.a.property("has_password1",true);
// res.body.should.have.a.property("has_password2",true);
// it(': undefined credential updated', function(done) { res.body.should.have.a.property("has_password3",true);
// index.registerType('test', TestNode, { res.body.should.not.have.a.property("password1");
// credentials: { res.body.should.not.have.a.property("password2");
// foo: {type:"test"} res.body.should.not.have.a.property("password3");
// } done();
// }); } catch(e) {
// index.loadFlows().then(function() { done(e);
// var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); }
// // setting value of an undefined credential should not change anything }
// testnode.credentials = {"bar": 4}; })
// credentials.extract(testnode); });
// should.exist(credentials.get('tab1'));
// credentials.get('tab1').should.have.property('foo',2); });
// credentials.get('tab1').should.not.have.property('bar');
// done();
// }).otherwise(function(err){
// done(err);
// });
// });
//
// it(': password credential updated', function(done) {
// index.registerType('password', TestNode, {
// credentials: {
// pswd: {type:"password"}
// }
// });
// index.loadFlows().then(function() {
// var testnode = new TestNode({id:'tab1',type:'password',name:'barney'});
// // setting value of password credential should update password
// testnode.credentials = {"pswd": 'fiddle'};
// credentials.extract(testnode);
// should.exist(credentials.get('tab1'));
// credentials.get('tab1').should.have.property('pswd','fiddle');
// credentials.get('tab1').should.not.have.property('pswd','sticks');
// done();
// }).otherwise(function(err){
// done(err);
// });
// });
//
// it(': password credential not updated', function(done) {
// index.registerType('password', TestNode, {
// credentials: {
// pswd: {type:"password"}
// }
// });
// index.loadFlows().then(function() {
// var testnode = new TestNode({id:'tab1',type:'password',name:'barney'});
// // setting value of password credential should update password
// testnode.credentials = {"pswd": '__PWRD__'};
// credentials.extract(testnode);
// should.exist(credentials.get('tab1'));
// credentials.get('tab1').should.have.property('pswd','sticks');
// credentials.get('tab1').should.not.have.property('pswd','__PWRD__');
// done();
// }).otherwise(function(err){
// done(err);
// });
// });
//
//})
//describe('registerEndpoint', function() { //describe('registerEndpoint', function() {
// var path = require('path'); // var path = require('path');