Merge branch '0.15.0'

This commit is contained in:
Nick O'Leary
2016-10-09 23:00:28 +01:00
75 changed files with 5930 additions and 952 deletions

View File

@@ -34,12 +34,12 @@ describe("flows api", function() {
app.post("/flows",flows.post);
});
it('returns flow', function(done) {
it('returns flow - v1', function(done) {
flows.init({
settings: {},
log:{warn:function(){},_:function(){},audit:function(){}},
nodes:{
getFlows: function() { return [1,2,3]; }
getFlows: function() { return {rev:"123",flows:[1,2,3]}; }
}
});
request(app)
@@ -50,13 +50,60 @@ describe("flows api", function() {
if (err) {
return done(err);
}
res.body.should.be.an.Array;
res.body.should.have.lengthOf(3);
done();
try {
res.body.should.have.lengthOf(3);
done();
} catch(e) {
return done(e);
}
});
});
it('sets flows - default', function(done) {
it('returns flow - v2', function(done) {
flows.init({
settings: {},
log:{warn:function(){},_:function(){},audit:function(){}},
nodes:{
getFlows: function() { return {rev:"123",flows:[1,2,3]}; }
}
});
request(app)
.get('/flows')
.set('Accept', 'application/json')
.set('Node-RED-API-Version','v2')
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
try {
res.body.should.have.a.property('rev','123');
res.body.should.have.a.property('flows');
res.body.flows.should.have.lengthOf(3);
done();
} catch(e) {
return done(e);
}
});
});
it('returns flow - bad version', function(done) {
request(app)
.get('/flows')
.set('Accept', 'application/json')
.set('Node-RED-API-Version','xxx')
.expect(400)
.end(function(err,res) {
if (err) {
return done(err);
}
try {
res.body.should.have.a.property('error','bad_api_version');
done();
} catch(e) {
return done(e);
}
});
});
it('sets flows - default - v1', function(done) {
var setFlows = sinon.spy(function() { return when.resolve();});
flows.init({
log:{warn:function(){},_:function(){},audit:function(){}},
@@ -77,7 +124,7 @@ describe("flows api", function() {
done();
});
});
it('sets flows - non-default', function(done) {
it('sets flows - non-default - v1', function(done) {
var setFlows = sinon.spy(function() { return when.resolve();});
flows.init({
log:{warn:function(){},_:function(){},audit:function(){}},
@@ -100,6 +147,96 @@ describe("flows api", function() {
});
});
it('set flows - rejects mismatched revision - v2', function(done) {
var setFlows = sinon.spy(function() { return when.resolve();});
var getFlows = sinon.spy(function() { return {rev:123,flows:[1,2,3]}});
flows.init({
log:{warn:function(){},_:function(){},audit:function(){}},
nodes:{
setFlows: setFlows,
getFlows: getFlows
}
});
request(app)
.post('/flows')
.set('Accept', 'application/json')
.set('Node-RED-API-Version','v2')
.send({rev:456,flows:[4,5,6]})
.expect(409)
.end(function(err,res) {
if (err) {
return done(err);
}
res.body.should.have.property("error","version_mismatch");
done();
});
});
it('set flows - rev provided - v2', function(done) {
var setFlows = sinon.spy(function() { return when.resolve(456);});
var getFlows = sinon.spy(function() { return {rev:123,flows:[1,2,3]}});
flows.init({
log:{warn:function(){},_:function(){},audit:function(){}},
nodes:{
setFlows: setFlows,
getFlows: getFlows
}
});
request(app)
.post('/flows')
.set('Accept', 'application/json')
.set('Node-RED-API-Version','v2')
.send({rev:123,flows:[4,5,6]})
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
res.body.should.have.property("rev",456);
done();
});
});
it('set flows - no rev provided - v2', function(done) {
var setFlows = sinon.spy(function() { return when.resolve(456);});
var getFlows = sinon.spy(function() { return {rev:123,flows:[1,2,3]}});
flows.init({
log:{warn:function(){},_:function(){},audit:function(){}},
nodes:{
setFlows: setFlows,
getFlows: getFlows
}
});
request(app)
.post('/flows')
.set('Accept', 'application/json')
.set('Node-RED-API-Version','v2')
.send({flows:[4,5,6]})
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
res.body.should.have.property("rev",456);
done();
});
});
it('sets flow - bad version', function(done) {
request(app)
.post('/flows')
.set('Accept', 'application/json')
.set('Node-RED-API-Version','xxx')
.expect(400)
.end(function(err,res) {
if (err) {
return done(err);
}
try {
res.body.should.have.a.property('error','bad_api_version');
done();
} catch(e) {
return done(e);
}
});
});
it('reloads flows', function(done) {
var loadFlows = sinon.spy(function() { return when.resolve(); });
flows.init({

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2014, 2015 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -19,34 +19,30 @@ var sinon = require("sinon");
var when = require("when");
var util = require("util");
var express = require("express");
var request = require("supertest");
var index = require("../../../../red/runtime/nodes/index");
var credentials = require("../../../../red/runtime/nodes/credentials");
var log = require("../../../../red/runtime/log");
var auth = require("../../../../red/api/auth");
describe('Credentials', function() {
describe('red/runtime/nodes/credentials', function() {
var encryptionDisabledSettings = {
get: function(key) {
return false;
}
}
afterEach(function() {
index.clearRegistry();
});
it('loads from storage',function(done) {
it('loads provided credentials',function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
}
};
credentials.init(storage);
credentials.load().then(function() {
credentials.load({"a":{"b":1,"c":2}}).then(function() {
credentials.get("a").should.have.property('b',1);
credentials.get("a").should.have.property('c',2);
@@ -54,182 +50,120 @@ describe('Credentials', 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('adds a new credential',function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
});
it('saves to storage when new cred added', function(done) {
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
},
saveCredentials: function(creds) {
return when(true);
}
};
sinon.spy(storage,"saveCredentials");
credentials.init(storage);
credentials.load().then(function() {
should.not.exist(credentials.get("b"))
credentials.add('b',{"d":3});
storage.saveCredentials.callCount.should.be.exactly(1);
credentials.get("b").should.have.property('d',3);
storage.saveCredentials.restore();
done();
});
});
it('deletes from storage', function(done) {
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
},
saveCredentials: function(creds) {
return when(true);
}
};
sinon.spy(storage,"saveCredentials");
credentials.init(storage);
credentials.load().then(function() {
should.exist(credentials.get("a"))
credentials.delete('a');
storage.saveCredentials.callCount.should.be.exactly(1);
should.not.exist(credentials.get("a"));
storage.saveCredentials.restore();
done();
});
});
it('clean up from storage', function(done) {
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"a":{"b":1,"c":2}});
});
},
saveCredentials: function(creds) {
return when(true);
}
};
sinon.spy(storage,"saveCredentials");
credentials.init(storage);
credentials.load().then(function() {
should.exist(credentials.get("a"));
credentials.clean([]);
storage.saveCredentials.callCount.should.be.exactly(1);
should.not.exist(credentials.get("a"));
storage.saveCredentials.restore();
done();
});
});
it('handle error loading from storage', function(done) {
var storage = {
getCredentials: function() {
return when.promise(function(resolve,reject) {
reject("test forcing failure");
});
},
saveCredentials: function(creds) {
return when(true);
}
};
var logmsg = 'nothing logged yet';
sinon.stub(log, 'warn', function(msg) {
logmsg = msg;
});
credentials.init(storage);
credentials.load().then(function() {
log.warn.calledOnce.should.be.true;
log.warn.restore();
done();
}).otherwise(function(err){
log.warn.restore();
done(err);
});
});
it('credential type is not registered when extract', function(done) {
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
var storage = {
getFlows: function() {
var defer = when.defer();
defer.resolve(testFlows);
return defer.promise;
},
getCredentials: function() {
return when.promise(function(resolve,reject) {
resolve({"tab1":{"b":1,"c":2}});
});
},
saveFlows: function(conf) {
var defer = when.defer();
defer.resolve();
should.deepEqual(testFlows, conf);
return defer.promise;
},
saveCredentials: function(creds) {
return when(true);
},
getSettings: function() {
return when({});
},
saveSettings: function(s) {
return when();
}
};
function TestNode(n) {
index.createNode(this, n);
this.id = 'tab1';
this.type = 'test';
this.name = 'barney';
var node = this;
this.on("log", function() {
// do nothing
credentials.load({"a":{"b":1,"c":2}}).then(function() {
credentials.dirty().should.be.false();
should.not.exist(credentials.get("b"));
credentials.add("b",{"foo":"bar"}).then(function() {
credentials.get("b").should.have.property("foo","bar");
credentials.dirty().should.be.true();
done();
});
}
var logmsg = 'nothing logged yet';
sinon.stub(log, 'warn', function(msg) {
logmsg = msg;
});
var settings = {
available: function() { return false;}
}
index.init({settings:settings, storage:storage});
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
credentials.extract(testnode);
log.warn.calledOnce.should.be.true;
log.warn.restore();
});
it('deletes an existing credential',function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
credentials.load({"a":{"b":1,"c":2}}).then(function() {
credentials.dirty().should.be.false();
credentials.delete("a");
should.not.exist(credentials.get("a"));
credentials.dirty().should.be.true();
done();
}).otherwise(function(err){
log.warn.restore();
done(err);
});
});
it('extract and store credential updates in the provided node', function(done) {
credentials.init({saveCredentials:function(){}},express());
credentials.register("test",{
it('exports the credentials, clearing dirty flag', function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
var creds = {"a":{"b":1,"c":2}};
credentials.load(creds).then(function() {
credentials.add("b",{"foo":"bar"}).then(function() {
credentials.dirty().should.be.true();
credentials.export().then(function(exported) {
exported.should.eql(creds);
credentials.dirty().should.be.false();
done();
})
});
});
})
describe("#clean",function() {
it("removes credentials of unknown nodes",function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
var creds = {"a":{"b":1,"c":2},"b":{"d":3}};
credentials.load(creds).then(function() {
credentials.dirty().should.be.false();
should.exist(credentials.get("a"));
should.exist(credentials.get("b"));
credentials.clean([{id:"b"}]).then(function() {
credentials.dirty().should.be.true();
should.not.exist(credentials.get("a"));
should.exist(credentials.get("b"));
done();
});
});
});
it("extracts credentials of known nodes",function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
credentials.register("testNode",{"b":"text","c":"password"})
var creds = {"a":{"b":1,"c":2}};
var newConfig = [{id:"a",type:"testNode",credentials:{"b":"newBValue","c":"newCValue"}}];
credentials.load(creds).then(function() {
credentials.dirty().should.be.false();
credentials.clean(newConfig).then(function() {
credentials.dirty().should.be.true();
credentials.get("a").should.have.property('b',"newBValue");
credentials.get("a").should.have.property('c',"newCValue");
should.not.exist(newConfig[0].credentials);
done();
});
});
});
});
it('warns if a node has no credential definition', function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
credentials.load({}).then(function() {
var node = {id:"node",type:"test",credentials:{
user1:"newUser",
password1:"newPassword"
}};
sinon.spy(log,"warn");
credentials.extract(node);
log.warn.called.should.be.true();
should.not.exist(node.credentials);
log.warn.restore();
done();
});
})
it('extract credential updates in the provided node', function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
var defintion = {
user1:{type:"text"},
password1:{type:"password"},
user2:{type:"text"},
@@ -237,28 +171,301 @@ describe('Credentials', function() {
user3:{type:"text"},
password3:{type:"password"}
};
credentials.register("test",defintion);
var def = credentials.getDefinition("test");
defintion.should.eql(def);
credentials.load({"node":{user1:"abc",password1:"123",user2:"def",password2:"456",user3:"ghi",password3:"789"}}).then(function() {
var node = {id:"node",type:"test",credentials:{
// user1 unchanged
password1:"__PWRD__",
user2: "",
password2:" ",
user3:"newUser",
password3:"newPassword"
}};
credentials.dirty().should.be.false();
credentials.extract(node);
node.should.not.have.a.property("credentials");
credentials.dirty().should.be.true();
var newCreds = credentials.get("node");
newCreds.should.have.a.property("user1","abc");
newCreds.should.have.a.property("password1","123");
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();
});
credentials.add("node",{user1:"abc",password1:"123",user2:"def",password2:"456",user3:"ghi",password3:"789"});
var node = {id:"node",type:"test",credentials:{
// user1 unchanged
password1:"__PWRD__",
user2: "",
password2:" ",
user3:"newUser",
password3:"newPassword"
}};
credentials.extract(node);
node.should.not.have.a.property("credentials");
var newCreds = credentials.get("node");
newCreds.should.have.a.property("user1","abc");
newCreds.should.have.a.property("password1","123");
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();
});
it('extract ignores node without credentials', function(done) {
credentials.init({
log: log,
settings: encryptionDisabledSettings
});
credentials.load({"node":{user1:"abc",password1:"123"}}).then(function() {
var node = {id:"node",type:"test"};
credentials.dirty().should.be.false();
credentials.extract(node);
credentials.dirty().should.be.false();
done();
});
});
describe("encryption",function() {
var settings = {};
var runtime = {
log: log,
settings: {
get: function(key) {
return settings[key];
},
set: function(key,value) {
settings[key] = value;
return when.resolve();
},
delete: function(key) {
delete settings[key];
return when.resolve();
}
}
}
it('migrates to encrypted and generates default key', function(done) {
settings = {};
credentials.init(runtime);
credentials.load({"node":{user1:"abc",password1:"123"}}).then(function() {
settings.should.have.a.property("_credentialSecret");
settings._credentialSecret.should.have.a.length(64);
credentials.dirty().should.be.true();
credentials.export().then(function(result) {
result.should.have.a.property("$");
// reset everything - but with _credentialSecret still set
credentials.init(runtime);
// load the freshly encrypted version
credentials.load(result).then(function() {
should.exist(credentials.get("node"));
done();
})
});
});
});
it('uses default key', function(done) {
settings = {
_credentialSecret: "e3a36f47f005bf2aaa51ce3fc6fcaafd79da8d03f2b1a9281f8fb0a285e6255a"
};
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
should.exist(credentials.get("node"));
credentials.dirty().should.be.false();
credentials.add("node",{user1:"def",password1:"456"});
credentials.export().then(function(result) {
result.should.have.a.property("$");
// reset everything - but with _credentialSecret still set
credentials.init(runtime);
// load the freshly encrypted version
credentials.load(result).then(function() {
should.exist(credentials.get("node"));
credentials.get("node").should.have.a.property("user1","def");
credentials.get("node").should.have.a.property("password1","456");
done();
})
});
});
});
it('uses user key', function(done) {
settings = {
credentialSecret: "e3a36f47f005bf2aaa51ce3fc6fcaafd79da8d03f2b1a9281f8fb0a285e6255a"
};
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
credentials.dirty().should.be.false();
should.exist(credentials.get("node"));
credentials.add("node",{user1:"def",password1:"456"});
credentials.export().then(function(result) {
result.should.have.a.property("$");
// reset everything - but with _credentialSecret still set
credentials.init(runtime);
// load the freshly encrypted version
credentials.load(result).then(function() {
should.exist(credentials.get("node"));
credentials.get("node").should.have.a.property("user1","def");
credentials.get("node").should.have.a.property("password1","456");
done();
})
});
});
});
it('uses user key - when settings are otherwise unavailable', function(done) {
var runtime = {
log: log,
settings: {
get: function(key) {
if (key === 'credentialSecret') {
return "e3a36f47f005bf2aaa51ce3fc6fcaafd79da8d03f2b1a9281f8fb0a285e6255a";
}
throw new Error();
},
set: function(key,value) {
throw new Error();
}
}
}
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
should.exist(credentials.get("node"));
credentials.add("node",{user1:"def",password1:"456"});
credentials.export().then(function(result) {
result.should.have.a.property("$");
// reset everything - but with _credentialSecret still set
credentials.init(runtime);
// load the freshly encrypted version
credentials.load(result).then(function() {
should.exist(credentials.get("node"));
credentials.get("node").should.have.a.property("user1","def");
credentials.get("node").should.have.a.property("password1","456");
done();
})
});
});
});
it('migrates from default key to user key', function(done) {
settings = {
_credentialSecret: "e3a36f47f005bf2aaa51ce3fc6fcaafd79da8d03f2b1a9281f8fb0a285e6255a",
credentialSecret: "aaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbcccccccccccccddddddddddddeeeee"
};
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
credentials.dirty().should.be.true();
should.exist(credentials.get("node"));
credentials.export().then(function(result) {
result.should.have.a.property("$");
settings.should.not.have.a.property("_credentialSecret");
// reset everything - but with _credentialSecret still set
credentials.init(runtime);
// load the freshly encrypted version
credentials.load(result).then(function() {
should.exist(credentials.get("node"));
credentials.get("node").should.have.a.property("user1","abc");
credentials.get("node").should.have.a.property("password1","123");
done();
})
});
});
});
it('migrates from default key to user key - unencrypted original', function(done) {
settings = {
_credentialSecret: "e3a36f47f005bf2aaa51ce3fc6fcaafd79da8d03f2b1a9281f8fb0a285e6255a",
credentialSecret: "aaaaaaaaaaaaaaaaabbbbbbbbbbbbbbbbbcccccccccccccddddddddddddeeeee"
};
// {"node":{user1:"abc",password1:"123"}}
var unencryptedFlows = {"node":{user1:"abc",password1:"123"}};
credentials.init(runtime);
credentials.load(unencryptedFlows).then(function() {
credentials.dirty().should.be.true();
should.exist(credentials.get("node"));
credentials.export().then(function(result) {
result.should.have.a.property("$");
settings.should.not.have.a.property("_credentialSecret");
// reset everything - but with _credentialSecret still set
credentials.init(runtime);
// load the freshly encrypted version
credentials.load(result).then(function() {
should.exist(credentials.get("node"));
credentials.get("node").should.have.a.property("user1","abc");
credentials.get("node").should.have.a.property("password1","123");
done();
})
});
});
});
it('migrates from default key to unencrypted', function(done) {
settings = {
_credentialSecret: "e3a36f47f005bf2aaa51ce3fc6fcaafd79da8d03f2b1a9281f8fb0a285e6255a",
credentialSecret: false
};
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
credentials.dirty().should.be.true();
should.exist(credentials.get("node"));
credentials.export().then(function(result) {
result.should.not.have.a.property("$");
settings.should.not.have.a.property("_credentialSecret");
result.should.eql({"node":{user1:"abc",password1:"123"}});
done();
});
});
});
it('handles bad default key - resets credentials', function(done) {
settings = {
_credentialSecret: "badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb"
};
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
credentials.dirty().should.be.true();
should.not.exist(credentials.get("node"));
done();
});
});
it('handles bad user key - resets credentials', function(done) {
settings = {
credentialSecret: "badbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadbadb"
};
// {"node":{user1:"abc",password1:"123"}}
var cryptedFlows = {"$":"5b89d8209b5158a3c313675561b1a5b5phN1gDBe81Zv98KqS/hVDmc9EKvaKqRIvcyXYvBlFNzzzJtvN7qfw06i"};
credentials.init(runtime);
credentials.load(cryptedFlows).then(function() {
credentials.dirty().should.be.true();
should.not.exist(credentials.get("node"));
done();
});
});
it('handles unavailable settings - leaves creds unencrypted', function(done) {
var runtime = {
log: log,
settings: {
get: function(key) {
throw new Error();
},
set: function(key,value) {
throw new Error();
}
}
}
// {"node":{user1:"abc",password1:"123"}}
credentials.init(runtime);
credentials.load({"node":{user1:"abc",password1:"123"}}).then(function() {
credentials.dirty().should.be.false();
should.exist(credentials.get("node"));
credentials.export().then(function(result) {
result.should.not.have.a.property("$");
result.should.have.a.property("node");
done();
});
});
});
})
})

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2014, 2015 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,8 +30,6 @@ describe('flows/index', function() {
var storage;
var eventsOn;
var credentialsExtract;
var credentialsSave;
var credentialsClean;
var credentialsLoad;
@@ -50,13 +48,10 @@ describe('flows/index', function() {
beforeEach(function() {
eventsOn = sinon.spy(events,"on");
credentialsExtract = sinon.stub(credentials,"extract",function(conf) {
delete conf.credentials;
});
credentialsSave = sinon.stub(credentials,"save",function() {
return when.resolve();
});
credentialsClean = sinon.stub(credentials,"clean",function(conf) {
conf.forEach(function(n) {
delete n.credentials;
});
return when.resolve();
});
credentialsLoad = sinon.stub(credentials,"load",function() {
@@ -97,8 +92,6 @@ describe('flows/index', function() {
afterEach(function(done) {
eventsOn.restore();
credentialsExtract.restore();
credentialsSave.restore();
credentialsClean.restore();
credentialsLoad.restore();
flowCreate.restore();
@@ -119,28 +112,35 @@ describe('flows/index', function() {
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
credentialsExtract.called.should.be.false;
credentialsClean.called.should.be.true;
storage.hasOwnProperty('conf').should.be.true;
flows.getFlows().should.eql(originalConfig);
flows.getFlows().flows.should.eql(originalConfig);
done();
});
});
it('sets the full flow for type load', function(done) {
it('loads the full flow for type load', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({},storage);
var loadStorage = {
saveFlows: function(conf) {
loadStorage.conf = conf;
return when.resolve(456);
},
getFlows: function() {
return when.resolve({flows:originalConfig,rev:123})
}
}
flows.init({settings:{},storage:loadStorage});
flows.setFlows(originalConfig,"load").then(function() {
credentialsExtract.called.should.be.false;
credentialsClean.called.should.be.true;
credentialsClean.called.should.be.false;
// 'load' type does not trigger a save
storage.hasOwnProperty('conf').should.be.false;
flows.getFlows().should.eql(originalConfig);
loadStorage.hasOwnProperty('conf').should.be.false;
flows.getFlows().flows.should.eql(originalConfig);
done();
});
@@ -148,19 +148,18 @@ describe('flows/index', function() {
it('extracts credentials from the full flow', function(done) {
var originalConfig = [
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[],credentials:{}},
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[],credentials:{"a":1}},
{id:"t1",type:"tab"}
];
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
credentialsExtract.called.should.be.true;
credentialsClean.called.should.be.true;
storage.hasOwnProperty('conf').should.be.true;
var cleanedFlows = flows.getFlows();
storage.conf.should.eql(cleanedFlows);
cleanedFlows.should.not.eql(originalConfig);
cleanedFlows[0].credentials = {};
cleanedFlows.should.eql(originalConfig);
storage.conf.flows.should.eql(cleanedFlows.flows);
cleanedFlows.flows.should.not.eql(originalConfig);
cleanedFlows.flows[0].credentials = {"a":1};
cleanedFlows.flows.should.eql(originalConfig);
done();
});
});
@@ -175,12 +174,12 @@ describe('flows/index', function() {
newConfig.push({id:"t2",type:"tab"});
newConfig.push({id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]});
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
flows.setFlows(newConfig,"nodes").then(function() {
flows.getFlows().should.eql(newConfig);
flows.getFlows().flows.should.eql(newConfig);
flowCreate.flows['t1'].update.called.should.be.true;
flowCreate.flows['t2'].start.called.should.be.true;
flowCreate.flows['_GLOBAL_'].update.called.should.be.true;
@@ -188,7 +187,7 @@ describe('flows/index', function() {
})
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -204,12 +203,12 @@ describe('flows/index', function() {
newConfig.push({id:"t2",type:"tab"});
newConfig.push({id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]});
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
flows.setFlows(newConfig,"nodes").then(function() {
flows.getFlows().should.eql(newConfig);
flows.getFlows().flows.should.eql(newConfig);
flowCreate.flows['t1'].update.called.should.be.true;
flowCreate.flows['t2'].start.called.should.be.true;
flowCreate.flows['_GLOBAL_'].update.called.should.be.true;
@@ -217,7 +216,7 @@ describe('flows/index', function() {
})
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -232,16 +231,14 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
credentialsExtract.called.should.be.false;
credentialsLoad.called.should.be.true;
credentialsClean.called.should.be.true;
// 'load' type does not trigger a save
storage.hasOwnProperty('conf').should.be.false;
flows.getFlows().should.eql(originalConfig);
flows.getFlows().flows.should.eql(originalConfig);
done();
});
});
@@ -254,7 +251,7 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
@@ -262,7 +259,7 @@ describe('flows/index', function() {
done();
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -274,10 +271,10 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
flowCreate.called.should.be.false;
@@ -293,9 +290,9 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
flowCreate.called.should.be.false;
@@ -316,7 +313,7 @@ describe('flows/index', function() {
});
describe('#get',function() {
describe.skip('#get',function() {
});
@@ -327,9 +324,9 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
var c = 0;
flows.eachNode(function(node) {
@@ -351,7 +348,7 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
@@ -360,7 +357,7 @@ describe('flows/index', function() {
done();
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -376,7 +373,7 @@ describe('flows/index', function() {
{id:"t3-1",x:10,y:10,z:"t3",type:"test",config:"configNode",wires:[]}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
@@ -391,7 +388,7 @@ describe('flows/index', function() {
}
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -404,7 +401,7 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
@@ -413,7 +410,7 @@ describe('flows/index', function() {
done();
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -430,7 +427,7 @@ describe('flows/index', function() {
{id:"t3-1",x:10,y:10,z:"t3",type:"test",config:"configNode",wires:[]}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
events.once('nodes-started',function() {
@@ -445,7 +442,7 @@ describe('flows/index', function() {
}
});
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.startFlows();
});
@@ -473,7 +470,7 @@ describe('flows/index', function() {
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
flows.checkTypeInUse("unused-module");
done();
@@ -484,7 +481,7 @@ describe('flows/index', function() {
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
{id:"t1",type:"tab"}
];
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.setFlows(originalConfig).then(function() {
/*jshint immed: false */
try {
@@ -505,9 +502,9 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
flows.addFlow({
label:'new flow',
@@ -529,12 +526,12 @@ describe('flows/index', function() {
{id:"t1",type:"tab"}
];
storage.getFlows = function() {
return when.resolve(originalConfig);
return when.resolve({flows:originalConfig});
}
storage.setFlows = function() {
return when.resolve();
}
flows.init({},storage);
flows.init({settings:{},storage:storage});
flows.load().then(function() {
return flows.startFlows();
}).then(function() {
@@ -547,7 +544,7 @@ describe('flows/index', function() {
{id:"t2-3",z:"t1",type:"test"}
]
}).then(function(id) {
flows.getFlows().should.have.lengthOf(6);
flows.getFlows().flows.should.have.lengthOf(6);
var createdFlows = Object.keys(flowCreate.flows);
createdFlows.should.have.lengthOf(3);
createdFlows[2].should.eql(id);

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2014 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -39,29 +39,26 @@ describe("red/nodes/index", function() {
});
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
var testCredentials = {"tab1":{"b":1,"c":2}};
var storage = {
getFlows: function() {
return when(testFlows);
},
getCredentials: function() {
return when({"tab1":{"b":1,"c":2}});
},
saveFlows: function(conf) {
should.deepEqual(testFlows, conf);
return when();
},
saveCredentials: function(creds) {
return when(true);
}
getFlows: function() {
return when({red:123,flows:testFlows,credentials:testCredentials});
},
saveFlows: function(conf) {
should.deepEqual(testFlows, conf.flows);
return when.resolve(123);
}
};
var settings = {
available: function() { return false }
available: function() { return false },
get: function() { return false }
};
var runtime = {
settings: settings,
storage: storage
storage: storage,
log: {debug:function(){},warn:function(){}}
};
function TestNode(n) {
@@ -88,7 +85,9 @@ describe("red/nodes/index", function() {
it('flows should be initialised',function(done) {
index.init(runtime);
index.loadFlows().then(function() {
should.deepEqual(testFlows, index.getFlows());
console.log(testFlows);
console.log(index.getFlows());
should.deepEqual(testFlows, index.getFlows().flows);
done();
}).otherwise(function(err) {
done(err);
@@ -177,8 +176,8 @@ describe("red/nodes/index", function() {
index.registerType('test', TestNode);
index.loadFlows().then(function() {
var info = index.disableNode("5678");
registry.disableNode.calledOnce.should.be.true;
registry.disableNode.calledWith("5678").should.be.true;
registry.disableNode.calledOnce.should.be.true();
registry.disableNode.calledWith("5678").should.be.true();
info.should.eql(randomNodeInfo);
done();
}).otherwise(function(err) {

View File

@@ -1,5 +1,5 @@
/**
* Copyright 2014, 2015 IBM Corp.
* Copyright 2014, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,7 +22,9 @@ describe("red/storage/index", function() {
it('rejects the promise when settings suggest loading a bad module', function(done) {
var wrongModule = {
settings:{
storageModule : "thisaintloading"
}
};
storage.init(wrongModule).then( function() {
@@ -42,13 +44,15 @@ describe("red/storage/index", function() {
var initSetsMeToTrue = false;
var moduleWithBooleanSettingInit = {
init : function() {
initSetsMeToTrue = true;
}
init : function() {
initSetsMeToTrue = true;
}
};
var setsBooleanModule = {
settings: {
storageModule : moduleWithBooleanSettingInit
}
};
storage.init(setsBooleanModule);
@@ -71,12 +75,15 @@ describe("red/storage/index", function() {
},
getFlows : function() {
calledFlagGetFlows = true;
return when.resolve([]);
},
saveFlows : function (flows) {
flows.should.be.true;
return when.resolve("");
},
getCredentials : function() {
calledFlagGetCredentials = true;
return when.resolve({});
},
saveCredentials : function(credentials) {
credentials.should.be.true;
@@ -116,14 +123,14 @@ describe("red/storage/index", function() {
};
var moduleToLoad = {
storageModule : interfaceCheckerModule
settings: {
storageModule : interfaceCheckerModule
}
};
storage.init(moduleToLoad);
storage.getFlows();
storage.saveFlows(true);
storage.getCredentials();
storage.saveCredentials(true);
storage.saveFlows({flows:[],credentials:{}});
storage.getSettings();
storage.saveSettings(true);
storage.getSessions();
@@ -172,7 +179,9 @@ describe("red/storage/index", function() {
};
var moduleToLoad = {
storageModule : interfaceCheckerModule
settings: {
storageModule : interfaceCheckerModule
}
};
before(function() {
storage.init(moduleToLoad);
@@ -220,7 +229,7 @@ describe("red/storage/index", function() {
var interfaceCheckerModule = {
init : function () {}
};
storage.init({storageModule: interfaceCheckerModule});
storage.init({settings:{storageModule: interfaceCheckerModule}});
});
it('defaults missing getSettings',function(done) {