2014-07-17 09:34:26 +02:00
|
|
|
/**
|
2017-01-11 16:24:33 +01:00
|
|
|
* Copyright JS Foundation and other contributors, http://js.foundation
|
2014-07-17 09:34:26 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
2014-07-29 15:58:49 +02:00
|
|
|
|
2014-07-24 10:41:47 +02:00
|
|
|
var should = require("should");
|
|
|
|
var fs = require('fs-extra');
|
|
|
|
var path = require('path');
|
|
|
|
var when = require("when");
|
2014-08-04 18:12:54 +02:00
|
|
|
var sinon = require('sinon');
|
2018-04-26 13:32:05 +02:00
|
|
|
var inherits = require("util").inherits;
|
|
|
|
|
2015-11-12 08:56:23 +01:00
|
|
|
var index = require("../../../../red/runtime/nodes/index");
|
|
|
|
var flows = require("../../../../red/runtime/nodes/flows");
|
2018-04-26 13:32:05 +02:00
|
|
|
var registry = require("../../../../red/runtime-registry");
|
|
|
|
var Node = require("../../../../red/runtime/nodes/Node");
|
2014-07-17 09:34:26 +02:00
|
|
|
|
|
|
|
describe("red/nodes/index", function() {
|
2015-11-02 16:38:16 +01:00
|
|
|
before(function() {
|
2017-11-17 18:29:33 +01:00
|
|
|
sinon.stub(index,"startFlows");
|
2015-11-12 08:56:23 +01:00
|
|
|
process.env.NODE_RED_HOME = path.resolve(path.join(__dirname,"..","..","..",".."))
|
2015-11-02 16:38:16 +01:00
|
|
|
});
|
|
|
|
after(function() {
|
2017-11-17 18:29:33 +01:00
|
|
|
index.startFlows.restore();
|
2015-11-12 08:56:23 +01:00
|
|
|
delete process.env.NODE_RED_HOME;
|
2015-11-02 16:38:16 +01:00
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
afterEach(function() {
|
|
|
|
index.clearRegistry();
|
|
|
|
});
|
|
|
|
|
2016-11-16 22:45:11 +01:00
|
|
|
process.env.foo="bar";
|
2014-07-29 15:58:49 +02:00
|
|
|
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
|
2016-11-16 22:45:11 +01:00
|
|
|
var testCredentials = {"tab1":{"b":1, "c":"2", "d":"$(foo)"}};
|
2014-07-29 15:58:49 +02:00
|
|
|
var storage = {
|
2016-09-21 22:58:50 +02:00
|
|
|
getFlows: function() {
|
2016-10-09 23:02:24 +02:00
|
|
|
return when({red:123,flows:testFlows,credentials:testCredentials});
|
2016-09-21 22:58:50 +02:00
|
|
|
},
|
|
|
|
saveFlows: function(conf) {
|
|
|
|
should.deepEqual(testFlows, conf.flows);
|
2016-10-09 23:02:24 +02:00
|
|
|
return when.resolve(123);
|
2016-09-21 22:58:50 +02:00
|
|
|
}
|
2014-08-28 01:35:07 +02:00
|
|
|
};
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
var settings = {
|
2016-09-23 11:38:30 +02:00
|
|
|
available: function() { return false },
|
|
|
|
get: function() { return false }
|
2014-08-28 01:35:07 +02:00
|
|
|
};
|
2014-07-24 10:41:47 +02:00
|
|
|
|
2017-02-15 23:54:32 +01:00
|
|
|
var EventEmitter = require('events').EventEmitter;
|
2015-11-17 22:12:43 +01:00
|
|
|
var runtime = {
|
|
|
|
settings: settings,
|
2016-09-23 11:38:30 +02:00
|
|
|
storage: storage,
|
2017-02-15 23:54:32 +01:00
|
|
|
log: {debug:function() {}, warn:function() {}},
|
|
|
|
events: new EventEmitter()
|
2015-11-17 22:12:43 +01:00
|
|
|
};
|
|
|
|
|
2014-07-29 15:58:49 +02:00
|
|
|
function TestNode(n) {
|
|
|
|
index.createNode(this, n);
|
|
|
|
var node = this;
|
|
|
|
this.on("log", function() {
|
|
|
|
// do nothing
|
|
|
|
});
|
|
|
|
}
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
it('nodes are initialised with credentials',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2017-03-09 22:06:49 +01:00
|
|
|
index.registerType('test-node-set','test', TestNode);
|
2014-07-24 10:41:47 +02:00
|
|
|
index.loadFlows().then(function() {
|
2014-11-21 16:15:24 +01:00
|
|
|
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
|
2014-07-24 10:41:47 +02:00
|
|
|
testnode.credentials.should.have.property('b',1);
|
2016-11-16 22:45:11 +01:00
|
|
|
testnode.credentials.should.have.property('c',"2");
|
|
|
|
testnode.credentials.should.have.property('d',"bar");
|
2014-07-24 10:41:47 +02:00
|
|
|
done();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-07-24 10:41:47 +02:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
it('flows should be initialised',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2014-07-24 10:41:47 +02:00
|
|
|
index.loadFlows().then(function() {
|
2017-03-09 22:06:49 +01:00
|
|
|
// console.log(testFlows);
|
|
|
|
// console.log(index.getFlows());
|
2016-10-09 23:02:24 +02:00
|
|
|
should.deepEqual(testFlows, index.getFlows().flows);
|
2014-07-24 10:41:47 +02:00
|
|
|
done();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-07-24 10:41:47 +02:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2017-03-09 22:06:49 +01:00
|
|
|
describe("registerType", function() {
|
|
|
|
describe("logs deprecated usage", function() {
|
|
|
|
before(function() {
|
|
|
|
sinon.stub(registry,"registerType");
|
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
registry.registerType.restore();
|
|
|
|
});
|
|
|
|
it("called without node-set name", function() {
|
|
|
|
var runtime = {
|
|
|
|
settings: settings,
|
|
|
|
storage: storage,
|
|
|
|
log: {debug:function() {}, warn:sinon.spy()},
|
|
|
|
events: new EventEmitter()
|
|
|
|
}
|
|
|
|
index.init(runtime);
|
|
|
|
|
|
|
|
index.registerType(/*'test-node-set',*/'test', TestNode, {});
|
|
|
|
runtime.log.warn.called.should.be.true();
|
|
|
|
registry.registerType.called.should.be.true();
|
|
|
|
registry.registerType.firstCall.args[0].should.eql('');
|
|
|
|
registry.registerType.firstCall.args[1].should.eql('test');
|
|
|
|
registry.registerType.firstCall.args[2].should.eql(TestNode);
|
|
|
|
});
|
|
|
|
});
|
2018-04-26 13:32:05 +02:00
|
|
|
describe("extends constructor with Node constructor", function() {
|
|
|
|
var TestNodeConstructor;
|
|
|
|
before(function() {
|
|
|
|
sinon.stub(registry,"registerType");
|
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
registry.registerType.restore();
|
|
|
|
});
|
|
|
|
beforeEach(function() {
|
|
|
|
TestNodeConstructor = function TestNodeConstructor() {};
|
|
|
|
var runtime = {
|
|
|
|
settings: settings,
|
|
|
|
storage: storage,
|
|
|
|
log: {debug:function() {}, warn:sinon.spy()},
|
|
|
|
events: new EventEmitter()
|
|
|
|
}
|
|
|
|
index.init(runtime);
|
|
|
|
})
|
|
|
|
it('extends a constructor with the Node constructor', function() {
|
|
|
|
TestNodeConstructor.prototype.should.not.be.an.instanceOf(Node);
|
|
|
|
index.registerType('node-set','node-type',TestNodeConstructor);
|
|
|
|
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
|
|
|
});
|
|
|
|
it('does not override a constructor prototype', function() {
|
|
|
|
function Foo(){};
|
|
|
|
inherits(TestNodeConstructor,Foo);
|
|
|
|
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
|
|
|
TestNodeConstructor.prototype.should.not.be.an.instanceOf(Node);
|
|
|
|
|
|
|
|
index.registerType('node-set','node-type',TestNodeConstructor);
|
|
|
|
|
|
|
|
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
|
|
|
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2018-04-26 13:32:05 +02:00
|
|
|
index.registerType('node-set','node-type2',TestNodeConstructor);
|
|
|
|
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
|
|
|
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
|
|
|
});
|
|
|
|
});
|
2017-03-09 22:06:49 +01:00
|
|
|
describe("register credentials definition", function() {
|
|
|
|
var http = require('http');
|
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
|
|
|
var runtime = require("../../../../red/runtime");
|
|
|
|
var credentials = require("../../../../red/runtime/nodes/credentials");
|
|
|
|
var localfilesystem = require("../../../../red/runtime/storage/localfilesystem");
|
2018-04-23 15:24:51 +02:00
|
|
|
var log = require("../../../../red/util/log");
|
2017-03-09 22:06:49 +01:00
|
|
|
var RED = require("../../../../red/red.js");
|
|
|
|
|
|
|
|
var userDir = path.join(__dirname,".testUserHome");
|
|
|
|
before(function(done) {
|
2018-01-16 17:18:18 +01:00
|
|
|
sinon.stub(log,"log",function(){});
|
2017-03-09 22:06:49 +01:00
|
|
|
fs.remove(userDir,function(err) {
|
|
|
|
fs.mkdir(userDir,function() {
|
|
|
|
sinon.stub(index, 'load', function() {
|
|
|
|
return when.promise(function(resolve,reject){
|
|
|
|
resolve([]);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
sinon.stub(localfilesystem, 'getCredentials', function() {
|
|
|
|
return when.promise(function(resolve,reject) {
|
|
|
|
resolve({"tab1":{"b":1,"c":2}});
|
|
|
|
});
|
|
|
|
}) ;
|
|
|
|
RED.init(http.createServer(function(req,res){app(req,res)}),
|
|
|
|
{userDir: userDir});
|
|
|
|
runtime.start().then(function () {
|
|
|
|
done();
|
2014-07-29 15:58:49 +02:00
|
|
|
});
|
|
|
|
});
|
2017-03-09 22:06:49 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
2018-01-16 17:18:18 +01:00
|
|
|
fs.remove(userDir,function() {
|
2017-03-09 22:06:49 +01:00
|
|
|
runtime.stop().then(function() {
|
|
|
|
index.load.restore();
|
|
|
|
localfilesystem.getCredentials.restore();
|
|
|
|
log.log.restore();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('definition defined',function() {
|
|
|
|
index.registerType('test-node-set','test', TestNode, {
|
|
|
|
credentials: {
|
|
|
|
foo: {type:"test"}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
var testnode = new TestNode({id:'tab1',type:'test',name:'barney', '_alias':'tab1'});
|
|
|
|
index.getCredentialDefinition("test").should.have.property('foo');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe("register settings definition", function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
sinon.stub(registry,"registerType");
|
|
|
|
})
|
|
|
|
afterEach(function() {
|
|
|
|
registry.registerType.restore();
|
|
|
|
})
|
|
|
|
it('registers valid settings',function() {
|
|
|
|
var runtime = {
|
|
|
|
settings: settings,
|
|
|
|
storage: storage,
|
|
|
|
log: {debug:function() {}, warn:function() {}},
|
|
|
|
events: new EventEmitter()
|
|
|
|
}
|
|
|
|
runtime.settings.registerNodeSettings = sinon.spy();
|
|
|
|
index.init(runtime);
|
|
|
|
|
|
|
|
index.registerType('test-node-set','test', TestNode, {
|
|
|
|
settings: {
|
|
|
|
testOne: {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
runtime.settings.registerNodeSettings.called.should.be.true();
|
|
|
|
runtime.settings.registerNodeSettings.firstCall.args[0].should.eql('test');
|
|
|
|
runtime.settings.registerNodeSettings.firstCall.args[1].should.eql({testOne: {}});
|
|
|
|
});
|
|
|
|
it('logs invalid settings',function() {
|
|
|
|
var runtime = {
|
|
|
|
settings: settings,
|
|
|
|
storage: storage,
|
|
|
|
log: {debug:function() {}, warn:sinon.spy()},
|
|
|
|
events: new EventEmitter()
|
|
|
|
}
|
|
|
|
runtime.settings.registerNodeSettings = function() { throw new Error("pass");}
|
|
|
|
index.init(runtime);
|
|
|
|
|
|
|
|
index.registerType('test-node-set','test', TestNode, {
|
|
|
|
settings: {
|
|
|
|
testOne: {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
runtime.log.warn.called.should.be.true();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('allows nodes to be added/removed/enabled/disabled from the registry', function() {
|
|
|
|
var randomNodeInfo = {id:"5678",types:["random"]};
|
|
|
|
|
|
|
|
beforeEach(function() {
|
|
|
|
sinon.stub(registry,"getNodeInfo",function(id) {
|
|
|
|
if (id == "test") {
|
|
|
|
return {id:"1234",types:["test"]};
|
|
|
|
} else if (id == "doesnotexist") {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return randomNodeInfo;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sinon.stub(registry,"disableNode",function(id) {
|
2018-02-28 12:23:25 +01:00
|
|
|
return when.resolve(randomNodeInfo);
|
2017-03-09 22:06:49 +01:00
|
|
|
});
|
|
|
|
});
|
|
|
|
afterEach(function() {
|
|
|
|
registry.getNodeInfo.restore();
|
|
|
|
registry.disableNode.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('allows an unused node type to be disabled',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2017-03-09 22:06:49 +01:00
|
|
|
index.registerType('test-node-set','test', TestNode);
|
2014-08-28 01:35:07 +02:00
|
|
|
index.loadFlows().then(function() {
|
2018-02-28 12:23:25 +01:00
|
|
|
return index.disableNode("5678").then(function(info) {
|
|
|
|
registry.disableNode.calledOnce.should.be.true();
|
|
|
|
registry.disableNode.calledWith("5678").should.be.true();
|
|
|
|
info.should.eql(randomNodeInfo);
|
|
|
|
done();
|
|
|
|
});
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-08-28 01:35:07 +02:00
|
|
|
done(err);
|
|
|
|
});
|
2017-03-09 22:06:49 +01:00
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
it('prevents disabling a node type that is in use',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2017-03-09 22:06:49 +01:00
|
|
|
index.registerType('test-node-set','test', TestNode);
|
2014-08-28 01:35:07 +02:00
|
|
|
index.loadFlows().then(function() {
|
|
|
|
/*jshint immed: false */
|
|
|
|
(function() {
|
|
|
|
index.disabledNode("test");
|
2014-11-21 16:15:24 +01:00
|
|
|
}).should.throw();
|
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
done();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-08-28 01:35:07 +02:00
|
|
|
done(err);
|
|
|
|
});
|
2017-03-09 22:06:49 +01:00
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
it('prevents disabling a node type that is unknown',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2017-03-09 22:06:49 +01:00
|
|
|
index.registerType('test-node-set','test', TestNode);
|
2014-08-28 01:35:07 +02:00
|
|
|
index.loadFlows().then(function() {
|
|
|
|
/*jshint immed: false */
|
|
|
|
(function() {
|
|
|
|
index.disableNode("doesnotexist");
|
2014-11-21 16:15:24 +01:00
|
|
|
}).should.throw();
|
|
|
|
|
2014-08-07 23:20:06 +02:00
|
|
|
done();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-08-07 23:20:06 +02:00
|
|
|
done(err);
|
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
});
|
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
describe('allows modules to be removed from the registry', function() {
|
|
|
|
var randomNodeInfo = {id:"5678",types:["random"]};
|
|
|
|
var randomModuleInfo = {
|
|
|
|
name:"random",
|
|
|
|
nodes: [randomNodeInfo]
|
2014-11-21 16:15:24 +01:00
|
|
|
};
|
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
before(function() {
|
|
|
|
sinon.stub(registry,"getNodeInfo",function(id) {
|
|
|
|
if (id == "node-red/foo") {
|
|
|
|
return {id:"1234",types:["test"]};
|
|
|
|
} else if (id == "doesnotexist") {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return randomNodeInfo;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sinon.stub(registry,"getModuleInfo",function(module) {
|
|
|
|
if (module == "node-red") {
|
|
|
|
return {nodes:[{name:"foo"}]};
|
|
|
|
} else if (module == "doesnotexist") {
|
|
|
|
return null;
|
|
|
|
} else {
|
|
|
|
return randomModuleInfo;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
sinon.stub(registry,"removeModule",function(id) {
|
|
|
|
return randomModuleInfo;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
registry.getNodeInfo.restore();
|
|
|
|
registry.getModuleInfo.restore();
|
|
|
|
registry.removeModule.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('prevents removing a module that is in use',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2017-03-09 22:06:49 +01:00
|
|
|
index.registerType('test-node-set','test', TestNode);
|
2014-11-21 16:15:24 +01:00
|
|
|
index.loadFlows().then(function() {
|
|
|
|
/*jshint immed: false */
|
|
|
|
(function() {
|
|
|
|
index.removeModule("node-red");
|
|
|
|
}).should.throw();
|
2014-08-07 23:20:06 +02:00
|
|
|
|
2014-11-21 16:15:24 +01:00
|
|
|
done();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-11-21 16:15:24 +01:00
|
|
|
done(err);
|
|
|
|
});
|
2017-03-09 22:06:49 +01:00
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2017-03-09 22:06:49 +01:00
|
|
|
it('prevents removing a module that is unknown',function(done) {
|
2015-11-17 22:12:43 +01:00
|
|
|
index.init(runtime);
|
2017-03-09 22:06:49 +01:00
|
|
|
index.registerType('test-node-set','test', TestNode);
|
2014-11-21 16:15:24 +01:00
|
|
|
index.loadFlows().then(function() {
|
|
|
|
/*jshint immed: false */
|
|
|
|
(function() {
|
|
|
|
index.removeModule("doesnotexist");
|
|
|
|
}).should.throw();
|
|
|
|
|
|
|
|
done();
|
2018-04-24 16:01:49 +02:00
|
|
|
}).catch(function(err) {
|
2014-11-21 16:15:24 +01:00
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
});
|
2014-07-17 09:34:26 +02:00
|
|
|
});
|