Update unit tests

This commit is contained in:
Nick O'Leary 2015-01-10 22:09:37 +00:00
parent afb5e8cbce
commit a5afc258b1
10 changed files with 342 additions and 336 deletions

View File

@ -207,12 +207,12 @@ function diffNodeConfigs(oldNode,newNode) {
var subflowInstanceRE = /^subflow:(.+)$/; var subflowInstanceRE = /^subflow:(.+)$/;
function Flow(config) { function Flow(config) {
this.activeNodes = {}; this.activeNodes = {};
this.subflowInstanceNodes = {}; this.subflowInstanceNodes = {};
this.parseConfig(config); this.parseConfig(config);
} }
Flow.prototype.parseConfig = function(config) { Flow.prototype.parseConfig = function(config) {
@ -385,6 +385,14 @@ Flow.prototype.getFlow = function() {
return this.config; return this.config;
} }
Flow.prototype.eachNode = function(callback) {
for (var id in this.activeNodes) {
if (this.activeNodes.hasOwnProperty(id)) {
callback(this.activeNodes[id]);
}
}
}
Flow.prototype.applyConfig = function(config,type) { Flow.prototype.applyConfig = function(config,type) {
var diff = this.diffFlow(config); var diff = this.diffFlow(config);
//console.log(diff); //console.log(diff);

View File

@ -81,6 +81,10 @@ var flowNodes = module.exports = {
return activeFlow.getNode(i); return activeFlow.getNode(i);
}, },
eachNode: function(cb) {
activeFlow.eachNode(cb);
},
/** /**
* Stops all active nodes and clears the active set * Stops all active nodes and clears the active set
* @return a promise for the stopping of all active nodes * @return a promise for the stopping of all active nodes
@ -174,7 +178,7 @@ var flowNodes = module.exports = {
var missingTypes = activeFlow.getMissingTypes(); var missingTypes = activeFlow.getMissingTypes();
if (missingTypes.length > 0) { if (missingTypes.length > 0) {
util.log("[red] Waiting for missing types to be registered:"); util.log("[red] Waiting for missing types to be registered:");
for (i=0;i<missingTypes.length;i++) { for (var i=0;i<missingTypes.length;i++) {
util.log("[red] - "+missingTypes[i]); util.log("[red] - "+missingTypes[i]);
} }
} }

View File

@ -317,7 +317,11 @@ var registry = (function() {
}, },
getNodeConfig: function(id) { getNodeConfig: function(id) {
var config = moduleConfigs[getModule(id)].nodes[getNode(id)]; var config = moduleConfigs[getModule(id)];
if (!config) {
return null;
}
config = config.nodes[getNode(id)];
if (config) { if (config) {
var result = config.config; var result = config.config;
if (config.script) { if (config.script) {

View File

@ -102,7 +102,7 @@ module.exports = {
credentials: credentials, credentials: credentials,
clearFlows: function() { clearFlows: function() {
return flows.clear(); return flows.stopFlows();
}, },
request: function() { request: function() {

View File

@ -72,7 +72,7 @@ describe("flows api", function() {
}); });
it('returns error when set fails', function(done) { it('returns error when set fails', function(done) {
var setFlows = sinon.stub(redNodes,'setFlows', function() { var setFlows = sinon.stub(redNodes,'setFlows', function() {
return when.reject(new Error("test error")); return when.reject(new Error("expected error"));
}); });
request(app) request(app)
.post('/flows') .post('/flows')
@ -83,7 +83,7 @@ describe("flows api", function() {
if (err) { if (err) {
throw err; throw err;
} }
res.text.should.eql("test error"); res.text.should.eql("expected error");
done(); done();
}); });
}); });

View File

View File

@ -17,6 +17,8 @@
var should = require("should"); var should = require("should");
var sinon = require('sinon'); var sinon = require('sinon');
var RedNode = require("../../../red/nodes/Node"); var RedNode = require("../../../red/nodes/Node");
var flows = require("../../../red/nodes/flows");
var comms = require('../../../red/comms'); var comms = require('../../../red/comms');
describe('Node', function() { describe('Node', function() {
@ -90,12 +92,16 @@ describe('Node', function() {
it('emits a single message', function(done) { it('emits a single message', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]}); var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'}); var n2 = new RedNode({id:'n2',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
var message = {payload:"hello world"}; var message = {payload:"hello world"};
n2.on('input',function(msg) { n2.on('input',function(msg) {
// msg equals message, and is not a new copy // msg equals message, and is not a new copy
should.deepEqual(msg,message); should.deepEqual(msg,message);
should.strictEqual(msg,message); should.strictEqual(msg,message);
flowGet.restore();
done(); done();
}); });
@ -105,6 +111,9 @@ describe('Node', function() {
it('emits multiple messages on a single output', function(done) { it('emits multiple messages on a single output', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]}); var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'}); var n2 = new RedNode({id:'n2',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
var messages = [ var messages = [
{payload:"hello world"}, {payload:"hello world"},
@ -126,6 +135,7 @@ describe('Node', function() {
rcvdCount += 1; rcvdCount += 1;
if (rcvdCount === 2) { if (rcvdCount === 2) {
flowGet.restore();
done(); done();
} }
}); });
@ -138,6 +148,9 @@ describe('Node', function() {
var n3 = new RedNode({id:'n3',type:'abc'}); var n3 = new RedNode({id:'n3',type:'abc'});
var n4 = new RedNode({id:'n4',type:'abc'}); var n4 = new RedNode({id:'n4',type:'abc'});
var n5 = new RedNode({id:'n5',type:'abc'}); var n5 = new RedNode({id:'n5',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2,'n3':n3,'n4':n4,'n5':n5}[id];
});
var messages = [ var messages = [
{payload:"hello world"}, {payload:"hello world"},
@ -153,6 +166,7 @@ describe('Node', function() {
should.strictEqual(msg,messages[0]); should.strictEqual(msg,messages[0]);
rcvdCount += 1; rcvdCount += 1;
if (rcvdCount == 3) { if (rcvdCount == 3) {
flowGet.restore();
done(); done();
} }
}); });
@ -167,6 +181,7 @@ describe('Node', function() {
should.notStrictEqual(msg,messages[2]); should.notStrictEqual(msg,messages[2]);
rcvdCount += 1; rcvdCount += 1;
if (rcvdCount == 3) { if (rcvdCount == 3) {
flowGet.restore();
done(); done();
} }
}); });
@ -177,6 +192,7 @@ describe('Node', function() {
should.notStrictEqual(msg,messages[2]); should.notStrictEqual(msg,messages[2]);
rcvdCount += 1; rcvdCount += 1;
if (rcvdCount == 3) { if (rcvdCount == 3) {
flowGet.restore();
done(); done();
} }
}); });
@ -187,12 +203,16 @@ describe('Node', function() {
it('emits no messages', function(done) { it('emits no messages', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]}); var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'}); var n2 = new RedNode({id:'n2',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
n2.on('input',function(msg) { n2.on('input',function(msg) {
should.fail(null,null,"unexpected message"); should.fail(null,null,"unexpected message");
}); });
setTimeout(function() { setTimeout(function() {
flowGet.restore();
done(); done();
}, 200); }, 200);
@ -202,7 +222,10 @@ describe('Node', function() {
it('emits messages ignoring non-existent nodes', function(done) { it('emits messages ignoring non-existent nodes', function(done) {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n9'],['n2']]}); var n1 = new RedNode({id:'n1',type:'abc',wires:[['n9'],['n2']]});
var n2 = new RedNode({id:'n2',type:'abc'}); var n2 = new RedNode({id:'n2',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2}[id];
});
var messages = [ var messages = [
{payload:"hello world"}, {payload:"hello world"},
{payload:"hello world again"} {payload:"hello world again"}
@ -212,6 +235,7 @@ describe('Node', function() {
n2.on('input',function(msg) { n2.on('input',function(msg) {
should.deepEqual(msg,messages[1]); should.deepEqual(msg,messages[1]);
should.strictEqual(msg,messages[1]); should.strictEqual(msg,messages[1]);
flowGet.restore();
done(); done();
}); });
@ -222,6 +246,9 @@ describe('Node', function() {
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3']]}); var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3']]});
var n2 = new RedNode({id:'n2',type:'abc'}); var n2 = new RedNode({id:'n2',type:'abc'});
var n3 = new RedNode({id:'n3',type:'abc'}); var n3 = new RedNode({id:'n3',type:'abc'});
var flowGet = sinon.stub(flows,"get",function(id) {
return {'n1':n1,'n2':n2,'n3':n3}[id];
});
var req = {}; var req = {};
var res = {}; var res = {};
@ -234,6 +261,7 @@ describe('Node', function() {
msg.cloned.should.be.exactly(message.cloned); msg.cloned.should.be.exactly(message.cloned);
msg.req.should.be.exactly(message.req); msg.req.should.be.exactly(message.req);
msg.res.should.be.exactly(message.res); msg.res.should.be.exactly(message.res);
flowGet.restore();
done(); done();
}); });
@ -243,6 +271,7 @@ describe('Node', function() {
msg.cloned.should.not.be.exactly(message.cloned); msg.cloned.should.not.be.exactly(message.cloned);
msg.req.should.be.exactly(message.req); msg.req.should.be.exactly(message.req);
msg.res.should.be.exactly(message.res); msg.res.should.be.exactly(message.res);
flowGet.restore();
done(); done();
}); });

View File

@ -112,9 +112,7 @@ describe('Credentials', function() {
credentials.init(storage); credentials.init(storage);
credentials.load().then(function() { credentials.load().then(function() {
should.exist(credentials.get("a")); should.exist(credentials.get("a"));
credentials.clean(function() { credentials.clean([]);
return false;
});
storage.saveCredentials.callCount.should.be.exactly(1); storage.saveCredentials.callCount.should.be.exactly(1);
should.not.exist(credentials.get("a")); should.not.exist(credentials.get("a"));
storage.saveCredentials.restore(); storage.saveCredentials.restore();
@ -211,287 +209,287 @@ describe('Credentials', function() {
}); });
}); });
describe('extract and store credential updates in the provided node', function() { //describe('extract and store credential updates in the provided node', function() {
var path = require('path'); // var path = require('path');
var fs = require('fs-extra'); // var fs = require('fs-extra');
var http = require('http'); // var http = require('http');
var express = require('express'); // var express = require('express');
var server = require("../../../red/server"); // var server = require("../../../red/server");
var localfilesystem = require("../../../red/storage/localfilesystem"); // var localfilesystem = require("../../../red/storage/localfilesystem");
var app = express(); // var app = express();
var RED = require("../../../red/red.js"); // var RED = require("../../../red/red.js");
//
var userDir = path.join(__dirname,".testUserHome"); // var userDir = path.join(__dirname,".testUserHome");
before(function(done) { // before(function(done) {
fs.remove(userDir,function(err) { // fs.remove(userDir,function(err) {
fs.mkdir(userDir,function() { // fs.mkdir(userDir,function() {
sinon.stub(index, 'load', function() { // sinon.stub(index, 'load', function() {
return when.promise(function(resolve,reject){ // return when.promise(function(resolve,reject){
resolve([]); // resolve([]);
}); // });
}); // });
sinon.stub(localfilesystem, 'getCredentials', function() { // sinon.stub(localfilesystem, 'getCredentials', function() {
return when.promise(function(resolve,reject) { // return when.promise(function(resolve,reject) {
resolve({"tab1":{"foo": 2, "pswd":'sticks'}}); // resolve({"tab1":{"foo": 2, "pswd":'sticks'}});
}); // });
}) ; // }) ;
RED.init(http.createServer(function(req,res){app(req,res)}), // RED.init(http.createServer(function(req,res){app(req,res)}),
{userDir: userDir}); // {userDir: userDir});
server.start().then(function () { // server.start().then(function () {
done(); // done();
}); // });
}); // });
}); // });
}); // });
//
after(function(done) { // after(function(done) {
fs.remove(userDir,done); // fs.remove(userDir,done);
server.stop(); // server.stop();
index.load.restore(); // index.load.restore();
localfilesystem.getCredentials.restore(); // localfilesystem.getCredentials.restore();
}); // });
//
function TestNode(n) { // function TestNode(n) {
index.createNode(this, n); // index.createNode(this, n);
var node = this; // var node = this;
this.on("log", function() { // this.on("log", function() {
// do nothing // // do nothing
}); // });
} // }
//
it(': credential updated with good value', function(done) { // it(': credential updated with good value', function(done) {
index.registerType('test', TestNode, { // index.registerType('test', TestNode, {
credentials: { // credentials: {
foo: {type:"test"} // foo: {type:"test"}
} // }
}); // });
index.loadFlows().then(function() { // index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); // var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
credentials.extract(testnode); // credentials.extract(testnode);
should.exist(credentials.get('tab1')); // should.exist(credentials.get('tab1'));
credentials.get('tab1').should.have.property('foo',2); // credentials.get('tab1').should.have.property('foo',2);
//
// set credentials to be an updated value and checking this is extracted properly // // set credentials to be an updated value and checking this is extracted properly
testnode.credentials = {"foo": 3}; // testnode.credentials = {"foo": 3};
credentials.extract(testnode); // credentials.extract(testnode);
should.exist(credentials.get('tab1')); // should.exist(credentials.get('tab1'));
credentials.get('tab1').should.not.have.property('foo',2); // credentials.get('tab1').should.not.have.property('foo',2);
credentials.get('tab1').should.have.property('foo',3); // credentials.get('tab1').should.have.property('foo',3);
done(); // done();
}).otherwise(function(err){ // }).otherwise(function(err){
done(err); // done(err);
}); // });
}); // });
//
// it(': credential updated with empty value', function(done) {
// index.registerType('test', TestNode, {
// credentials: {
// foo: {type:"test"}
// }
// });
// index.loadFlows().then(function() {
// var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
// // setting value of "foo" credential to be empty removes foo as a property
// testnode.credentials = {"foo": ''};
// credentials.extract(testnode);
// should.exist(credentials.get('tab1'));
// credentials.get('tab1').should.not.have.property('foo',2);
// credentials.get('tab1').should.not.have.property('foo');
// done();
// }).otherwise(function(err){
// done(err);
// });
// });
//
// it(': undefined credential updated', function(done) {
// index.registerType('test', TestNode, {
// credentials: {
// foo: {type:"test"}
// }
// });
// index.loadFlows().then(function() {
// 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);
// });
// });
//
//})
it(': credential updated with empty value', function(done) { //describe('registerEndpoint', function() {
index.registerType('test', TestNode, { // var path = require('path');
credentials: { // var fs = require('fs-extra');
foo: {type:"test"} // var http = require('http');
} // var express = require('express');
}); // var request = require('supertest');
index.loadFlows().then(function() { //
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); // var server = require("../../../red/server");
// setting value of "foo" credential to be empty removes foo as a property // var localfilesystem = require("../../../red/storage/localfilesystem");
testnode.credentials = {"foo": ''}; // var app = express();
credentials.extract(testnode); // var RED = require("../../../red/red.js");
should.exist(credentials.get('tab1')); //
credentials.get('tab1').should.not.have.property('foo',2); // var userDir = path.join(__dirname,".testUserHome");
credentials.get('tab1').should.not.have.property('foo'); // before(function(done) {
done(); // fs.remove(userDir,function(err) {
}).otherwise(function(err){ // fs.mkdir(userDir,function() {
done(err); // sinon.stub(index, 'load', function() {
}); // return when.promise(function(resolve,reject){
}); // resolve([]);
// });
it(': undefined credential updated', function(done) { // });
index.registerType('test', TestNode, { // sinon.stub(localfilesystem, 'getCredentials', function() {
credentials: { // return when.promise(function(resolve,reject) {
foo: {type:"test"} // resolve({"tab1":{"foo": 2, "pswd":'sticks'}});
} // });
}); // }) ;
index.loadFlows().then(function() { // RED.init(http.createServer(function(req,res){app(req,res)}),
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'}); // {userDir: userDir});
// setting value of an undefined credential should not change anything // server.start().then(function () {
testnode.credentials = {"bar": 4}; // done();
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){ // after(function(done) {
done(err); // fs.remove(userDir,done);
}); // server.stop();
}); // index.load.restore();
// localfilesystem.getCredentials.restore();
it(': password credential updated', function(done) { // });
index.registerType('password', TestNode, { //
credentials: { // function TestNode(n) {
pswd: {type:"password"} // index.createNode(this, n);
} // var node = this;
}); // this.on("log", function() {
index.loadFlows().then(function() { // // do nothing
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); // it(': valid credential type', function(done) {
should.exist(credentials.get('tab1')); // index.registerType('test', TestNode, {
credentials.get('tab1').should.have.property('pswd','fiddle'); // credentials: {
credentials.get('tab1').should.not.have.property('pswd','sticks'); // foo: {type:"test"}
done(); // }
}).otherwise(function(err){ // });
done(err); // index.loadFlows().then(function() {
}); // var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
}); // request(RED.httpAdmin).get('/credentials/test/tab1').expect(200).end(function(err,res) {
// if (err) {
it(': password credential not updated', function(done) { // done(err);
index.registerType('password', TestNode, { // }
credentials: { // res.body.should.have.property('foo', 2);
pswd: {type:"password"} // done();
} // });
}); // }).otherwise(function(err){
index.loadFlows().then(function() { // done(err);
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); // it(': password credential type', function(done) {
should.exist(credentials.get('tab1')); // index.registerType('password', TestNode, {
credentials.get('tab1').should.have.property('pswd','sticks'); // credentials: {
credentials.get('tab1').should.not.have.property('pswd','__PWRD__'); // pswd: {type:"password"}
done(); // }
}).otherwise(function(err){ // });
done(err); // index.loadFlows().then(function() {
}); // var testnode = new TestNode({id:'tab1',type:'pswd',name:'barney'});
}); // request(RED.httpAdmin).get('/credentials/password/tab1').expect(200).end(function(err,res) {
// if (err) {
}) // done(err);
// }
describe('registerEndpoint', function() { // res.body.should.have.property('has_pswd', true);
var path = require('path'); // res.body.should.not.have.property('pswd');
var fs = require('fs-extra'); // done();
var http = require('http'); // });
var express = require('express'); // }).otherwise(function(err){
var request = require('supertest'); // done(err);
// });
var server = require("../../../red/server"); // });
var localfilesystem = require("../../../red/storage/localfilesystem"); //
var app = express(); // it(': returns 404 for undefined credential type', function(done) {
var RED = require("../../../red/red.js"); // index.registerType('test', TestNode, {
// credentials: {
var userDir = path.join(__dirname,".testUserHome"); // foo: {type:"test"}
before(function(done) { // }
fs.remove(userDir,function(err) { // });
fs.mkdir(userDir,function() { // index.loadFlows().then(function() {
sinon.stub(index, 'load', function() { // var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
return when.promise(function(resolve,reject){ // request(RED.httpAdmin).get('/credentials/unknownType/tab1').expect(404).end(done);
resolve([]); // }).otherwise(function(err){
}); // done(err);
}); // });
sinon.stub(localfilesystem, 'getCredentials', function() { // });
return when.promise(function(resolve,reject) { //
resolve({"tab1":{"foo": 2, "pswd":'sticks'}}); // it(': undefined nodeID', function(done) {
}); // index.registerType('test', TestNode, {
}) ; // credentials: {
RED.init(http.createServer(function(req,res){app(req,res)}), // foo: {type:"test"}
{userDir: userDir}); // }
server.start().then(function () { // });
done(); // index.loadFlows().then(function() {
}); // var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
}); // request(RED.httpAdmin).get('/credentials/test/unknownNode').expect(200).end(function(err,res) {
}); // if (err) {
}); // done(err);
// }
after(function(done) { // var b = res.body;
fs.remove(userDir,done); // res.body.should.not.have.property('foo');
server.stop(); // done();
index.load.restore(); // });
localfilesystem.getCredentials.restore(); // }).otherwise(function(err){
}); // done(err);
// });
function TestNode(n) { // });
index.createNode(this, n); //
var node = this; //})
this.on("log", function() {
// do nothing
});
}
it(': valid credential type', function(done) {
index.registerType('test', TestNode, {
credentials: {
foo: {type:"test"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
request(RED.httpAdmin).get('/credentials/test/tab1').expect(200).end(function(err,res) {
if (err) {
done(err);
}
res.body.should.have.property('foo', 2);
done();
});
}).otherwise(function(err){
done(err);
});
});
it(': password credential type', function(done) {
index.registerType('password', TestNode, {
credentials: {
pswd: {type:"password"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'pswd',name:'barney'});
request(RED.httpAdmin).get('/credentials/password/tab1').expect(200).end(function(err,res) {
if (err) {
done(err);
}
res.body.should.have.property('has_pswd', true);
res.body.should.not.have.property('pswd');
done();
});
}).otherwise(function(err){
done(err);
});
});
it(': returns 404 for undefined credential type', function(done) {
index.registerType('test', TestNode, {
credentials: {
foo: {type:"test"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
request(RED.httpAdmin).get('/credentials/unknownType/tab1').expect(404).end(done);
}).otherwise(function(err){
done(err);
});
});
it(': undefined nodeID', function(done) {
index.registerType('test', TestNode, {
credentials: {
foo: {type:"test"}
}
});
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'foo',name:'barney'});
request(RED.httpAdmin).get('/credentials/test/unknownNode').expect(200).end(function(err,res) {
if (err) {
done(err);
}
var b = res.body;
res.body.should.not.have.property('foo');
done();
});
}).otherwise(function(err){
done(err);
});
});
})
}) })

View File

@ -47,39 +47,13 @@ function loadFlows(testFlows, cb) {
describe('flows', function() { describe('flows', function() {
afterEach(function(done) { afterEach(function(done) {
flows.clear().then(function() { flows.stopFlows().then(function() {
loadFlows([],done); loadFlows([],done);
}); });
}); });
describe('#add',function() {
it('should be called by node constructor',function(done) {
var n = new RedNode({id:'123',type:'abc'});
should.deepEqual(n, flows.get("123"));
flows.clear().then(function() {
done();
});
});
});
describe('#each',function() {
it('should "visit" all nodes',function(done) {
var nodes = [
new RedNode({id:'n0'}),
new RedNode({id:'n1'})
];
var count = 0;
flows.each(function(node) {
should.deepEqual(nodes[count], node);
count += 1;
if (count == 2) {
done();
}
});
});
});
describe('#load',function() { describe('#load',function() {
it('should load nothing when storage is empty',function(done) { it('should load nothing when storage is empty',function(done) {
loadFlows([], done); loadFlows([], done);
}); });
@ -92,7 +66,7 @@ describe('flows', function() {
it('should load and start a registered node type', function(done) { it('should load and start a registered node type', function(done) {
RED.registerType('debug', function() {}); RED.registerType('debug', function() {});
var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) { var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) {
return function() {}; return RedNode;
}); });
loadFlows([{"id":"n1","type":"debug"}], function() { }); loadFlows([{"id":"n1","type":"debug"}], function() { });
events.once('nodes-started', function() { events.once('nodes-started', function() {
@ -104,8 +78,7 @@ describe('flows', function() {
it('should load and start when node type is registered', function(done) { it('should load and start when node type is registered', function(done) {
var typeRegistryGet = sinon.stub(typeRegistry,"get"); var typeRegistryGet = sinon.stub(typeRegistry,"get");
typeRegistryGet.onCall(0).returns(null); typeRegistryGet.onCall(0).returns(null);
typeRegistryGet.returns(function(){}); typeRegistryGet.returns(RedNode);
loadFlows([{"id":"n2","type":"inject"}], function() { loadFlows([{"id":"n2","type":"inject"}], function() {
events.emit('type-registered','inject'); events.emit('type-registered','inject');
}); });
@ -118,7 +91,7 @@ describe('flows', function() {
it('should not instantiate nodes of an unused subflow', function(done) { it('should not instantiate nodes of an unused subflow', function(done) {
RED.registerType('abc', function() {}); RED.registerType('abc', function() {});
var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) { var typeRegistryGet = sinon.stub(typeRegistry,"get",function(nt) {
return function() {}; return RedNode;
}); });
loadFlows([{"id":"n1","type":"subflow",inputs:[],outputs:[],wires:[]}, loadFlows([{"id":"n1","type":"subflow",inputs:[],outputs:[],wires:[]},
{"id":"n2","type":"abc","z":"n1",wires:[]} {"id":"n2","type":"abc","z":"n1",wires:[]}
@ -126,11 +99,10 @@ describe('flows', function() {
events.once('nodes-started', function() { events.once('nodes-started', function() {
(flows.get("n2") == null).should.be.true; (flows.get("n2") == null).should.be.true;
var ncount = 0 var ncount = 0
flows.each(function(n) { flows.eachNode(function(n) {
ncount++; ncount++;
}); });
ncount.should.equal(0); ncount.should.equal(0);
console.log(ncount);
typeRegistryGet.restore(); typeRegistryGet.restore();
done(); done();
}); });
@ -149,7 +121,7 @@ describe('flows', function() {
(flows.get("n2") == null).should.be.true; (flows.get("n2") == null).should.be.true;
var ncount = 0 var ncount = 0
var nodes = []; var nodes = [];
flows.each(function(n) { flows.eachNode(function(n) {
nodes.push(n); nodes.push(n);
}); });
nodes.should.have.lengthOf(2); nodes.should.have.lengthOf(2);

View File

@ -41,16 +41,6 @@ describe('NodeRegistry', function() {
var settings = stubSettings({},false); var settings = stubSettings({},false);
var settingsWithStorage = stubSettings({},true); var settingsWithStorage = stubSettings({},true);
it('automatically registers new nodes',function() {
var testNode = RedNodes.getNode('123');
should.not.exist(n);
var n = new RedNode({id:'123',type:'abc'});
var newNode = RedNodes.getNode('123');
should.strictEqual(n,newNode);
});
it('handles nodes that export a function', function(done) { it('handles nodes that export a function', function(done) {
typeRegistry.init(settings); typeRegistry.init(settings);
typeRegistry.load(resourcesDir + "TestNode1",true).then(function() { typeRegistry.load(resourcesDir + "TestNode1",true).then(function() {
@ -239,11 +229,12 @@ describe('NodeRegistry', function() {
}); });
}); });
it('returns nothing for an unregistered type config', function() { it('returns nothing for an unregistered type config', function(done) {
typeRegistry.init(settings); typeRegistry.init(settings);
typeRegistry.load("wontexist",true).then(function(){ typeRegistry.load("wontexist",true).then(function(){
var config = typeRegistry.getNodeConfig("imaginary-shark"); var config = typeRegistry.getNodeConfig("imaginary-shark");
(config === null).should.be.true; (config === null).should.be.true;
done();
}).catch(function(e) { }).catch(function(e) {
done(e); done(e);
}); });