2014-07-17 09:34:26 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2014 IBM Corp.
|
|
|
|
*
|
|
|
|
* 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');
|
2015-11-08 15:06:36 +01:00
|
|
|
var child_process = require('child_process');
|
2014-07-24 10:41:47 +02:00
|
|
|
|
2014-07-29 15:58:49 +02:00
|
|
|
var index = require("../../../red/nodes/index");
|
2015-11-02 16:38:16 +01:00
|
|
|
var flows = require("../../../red/nodes/flows");
|
2015-11-08 15:06:36 +01:00
|
|
|
var registry = require("../../../red/nodes/registry");
|
2014-07-17 09:34:26 +02:00
|
|
|
|
|
|
|
describe("red/nodes/index", function() {
|
2015-11-02 16:38:16 +01:00
|
|
|
before(function() {
|
|
|
|
sinon.stub(flows,"startFlows");
|
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
flows.startFlows.restore();
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-08-01 23:05:49 +02:00
|
|
|
afterEach(function() {
|
|
|
|
index.clearRegistry();
|
|
|
|
});
|
|
|
|
|
2014-07-29 15:58:49 +02:00
|
|
|
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
|
|
|
|
var storage = {
|
|
|
|
getFlows: function() {
|
2014-08-04 18:12:54 +02:00
|
|
|
return when(testFlows);
|
2014-07-29 15:58:49 +02:00
|
|
|
},
|
|
|
|
getCredentials: function() {
|
2014-08-04 18:12:54 +02:00
|
|
|
return when({"tab1":{"b":1,"c":2}});
|
2014-07-29 15:58:49 +02:00
|
|
|
},
|
|
|
|
saveFlows: function(conf) {
|
|
|
|
should.deepEqual(testFlows, conf);
|
2014-08-04 18:12:54 +02:00
|
|
|
return when();
|
2014-07-29 15:58:49 +02:00
|
|
|
},
|
|
|
|
saveCredentials: function(creds) {
|
|
|
|
return when(true);
|
|
|
|
}
|
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 = {
|
|
|
|
available: function() { return false }
|
|
|
|
};
|
2014-07-24 10:41:47 +02: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
|
|
|
|
|
|
|
it('nodes are initialised with credentials',function(done) {
|
2014-08-28 01:35:07 +02:00
|
|
|
index.init(settings, storage);
|
2014-11-21 16:15:24 +01:00
|
|
|
index.registerType('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);
|
|
|
|
testnode.credentials.should.have.property('c',2);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
|
|
|
it('flows should be initialised',function(done) {
|
2014-08-28 01:35:07 +02:00
|
|
|
index.init(settings, storage);
|
2014-07-24 10:41:47 +02:00
|
|
|
index.loadFlows().then(function() {
|
|
|
|
should.deepEqual(testFlows, index.getFlows());
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-07-29 15:58:49 +02:00
|
|
|
describe("registerType should register credentials definition", function() {
|
|
|
|
var http = require('http');
|
|
|
|
var express = require('express');
|
|
|
|
var app = express();
|
|
|
|
var server = require("../../../red/server");
|
|
|
|
var credentials = require("../../../red/nodes/credentials");
|
|
|
|
var localfilesystem = require("../../../red/storage/localfilesystem");
|
|
|
|
var RED = require("../../../red/red.js");
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-07-29 15:58:49 +02:00
|
|
|
var userDir = path.join(__dirname,".testUserHome");
|
|
|
|
before(function(done) {
|
|
|
|
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});
|
|
|
|
server.start().then(function () {
|
2014-11-21 16:15:24 +01:00
|
|
|
done();
|
2014-07-29 15:58:49 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
after(function(done) {
|
|
|
|
fs.remove(userDir,done);
|
|
|
|
server.stop();
|
|
|
|
index.load.restore();
|
|
|
|
localfilesystem.getCredentials.restore();
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
|
|
|
it(': definition defined',function(done) {
|
2014-07-29 15:58:49 +02:00
|
|
|
index.registerType('test', TestNode, {
|
|
|
|
credentials: {
|
|
|
|
foo: {type:"test"}
|
2014-11-21 16:15:24 +01:00
|
|
|
}
|
|
|
|
});
|
2015-03-06 23:58:30 +01:00
|
|
|
var testnode = new TestNode({id:'tab1',type:'test',name:'barney', '_alias':'tab1'});
|
2015-11-04 12:13:02 +01:00
|
|
|
index.getCredentialDefinition("test").should.have.property('foo');
|
2014-07-29 15:58:49 +02:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
|
|
|
describe('allows nodes to be added/removed/enabled/disabled from the registry', function() {
|
2014-08-04 18:12:54 +02:00
|
|
|
var registry = require("../../../red/nodes/registry");
|
|
|
|
var randomNodeInfo = {id:"5678",types:["random"]};
|
2014-11-21 16:15:24 +01:00
|
|
|
|
2014-08-04 18:12:54 +02:00
|
|
|
before(function() {
|
|
|
|
sinon.stub(registry,"getNodeInfo",function(id) {
|
|
|
|
if (id == "test") {
|
|
|
|
return {id:"1234",types:["test"]};
|
2014-08-07 23:20:06 +02:00
|
|
|
} else if (id == "doesnotexist") {
|
|
|
|
return null;
|
2014-08-04 18:12:54 +02:00
|
|
|
} else {
|
|
|
|
return randomNodeInfo;
|
|
|
|
}
|
|
|
|
});
|
2014-08-28 01:35:07 +02:00
|
|
|
sinon.stub(registry,"disableNode",function(id) {
|
|
|
|
return randomNodeInfo;
|
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
registry.getNodeInfo.restore();
|
2014-08-28 01:35:07 +02:00
|
|
|
registry.disableNode.restore();
|
2014-08-04 18:12:54 +02:00
|
|
|
});
|
|
|
|
|
2014-11-21 16:15:24 +01:00
|
|
|
it(': allows an unused node type to be disabled',function(done) {
|
2014-08-28 01:35:07 +02:00
|
|
|
index.init(settings, storage);
|
2014-11-21 16:15:24 +01:00
|
|
|
index.registerType('test', TestNode);
|
2014-08-28 01:35:07 +02:00
|
|
|
index.loadFlows().then(function() {
|
|
|
|
var info = index.disableNode("5678");
|
|
|
|
registry.disableNode.calledOnce.should.be.true;
|
|
|
|
registry.disableNode.calledWith("5678").should.be.true;
|
|
|
|
info.should.eql(randomNodeInfo);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
|
2014-08-28 01:35:07 +02:00
|
|
|
it(': prevents disabling a node type that is in use',function(done) {
|
|
|
|
index.init(settings, storage);
|
2014-11-21 16:15:24 +01:00
|
|
|
index.registerType('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();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
|
|
|
|
it(': prevents disabling a node type that is unknown',function(done) {
|
2014-08-28 01:35:07 +02:00
|
|
|
index.init(settings, storage);
|
2014-11-21 16:15:24 +01:00
|
|
|
index.registerType('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();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
});
|
2014-11-21 16:15:24 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
describe('allows modules to be removed from the registry', function() {
|
|
|
|
var registry = require("../../../red/nodes/registry");
|
|
|
|
var randomNodeInfo = {id:"5678",types:["random"]};
|
|
|
|
var randomModuleInfo = {
|
|
|
|
name:"random",
|
|
|
|
nodes: [randomNodeInfo]
|
|
|
|
};
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2015-03-30 22:49:20 +02:00
|
|
|
sinon.stub(registry,"getModuleInfo",function(module) {
|
2014-11-21 16:15:24 +01:00
|
|
|
if (module == "node-red") {
|
2015-03-30 22:49:20 +02:00
|
|
|
return {nodes:[{name:"foo"}]};
|
2014-11-21 16:15:24 +01:00
|
|
|
} else if (module == "doesnotexist") {
|
|
|
|
return null;
|
|
|
|
} else {
|
2015-03-30 22:49:20 +02:00
|
|
|
return randomModuleInfo;
|
2014-11-21 16:15:24 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
sinon.stub(registry,"removeModule",function(id) {
|
|
|
|
return randomModuleInfo;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
after(function() {
|
|
|
|
registry.getNodeInfo.restore();
|
2015-03-30 22:49:20 +02:00
|
|
|
registry.getModuleInfo.restore();
|
2014-11-21 16:15:24 +01:00
|
|
|
registry.removeModule.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
it(': prevents removing a module that is in use',function(done) {
|
|
|
|
index.init(settings, storage);
|
|
|
|
index.registerType('test', TestNode);
|
|
|
|
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();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it(': prevents removing a module that is unknown',function(done) {
|
|
|
|
index.init(settings, storage);
|
|
|
|
index.registerType('test', TestNode);
|
|
|
|
index.loadFlows().then(function() {
|
|
|
|
/*jshint immed: false */
|
|
|
|
(function() {
|
|
|
|
index.removeModule("doesnotexist");
|
|
|
|
}).should.throw();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
});
|
|
|
|
});
|
2014-08-04 18:12:54 +02:00
|
|
|
});
|
2015-11-08 15:06:36 +01:00
|
|
|
|
|
|
|
describe("installs module", function() {
|
|
|
|
it("rejects invalid module names", function(done) {
|
|
|
|
var promises = [];
|
|
|
|
promises.push(index.installModule("this_wont_exist "));
|
|
|
|
promises.push(index.installModule("this_wont_exist;no_it_really_wont"));
|
|
|
|
when.settle(promises).then(function(results) {
|
|
|
|
results[0].state.should.be.eql("rejected");
|
|
|
|
results[1].state.should.be.eql("rejected");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("rejects when npm returns a 404", function(done) {
|
2015-11-08 15:26:11 +01:00
|
|
|
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-08 15:06:36 +01:00
|
|
|
cb(new Error(),""," 404 this_wont_exist");
|
|
|
|
});
|
|
|
|
|
|
|
|
index.installModule("this_wont_exist").otherwise(function(err) {
|
|
|
|
err.code.should.be.eql(404);
|
|
|
|
done();
|
|
|
|
}).finally(function() {
|
|
|
|
exec.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("rejects with generic error", function(done) {
|
2015-11-08 15:26:11 +01:00
|
|
|
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-08 15:06:36 +01:00
|
|
|
cb(new Error("test_error"),"","");
|
|
|
|
});
|
|
|
|
|
|
|
|
index.installModule("this_wont_exist").then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done();
|
|
|
|
}).finally(function() {
|
|
|
|
exec.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when module is found", function(done) {
|
|
|
|
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
2015-11-08 15:26:11 +01:00
|
|
|
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-08 15:06:36 +01:00
|
|
|
cb(null,"","");
|
|
|
|
});
|
|
|
|
var addModule = sinon.stub(registry,"addModule",function(md) {
|
|
|
|
return when.resolve(nodeInfo);
|
|
|
|
});
|
|
|
|
|
|
|
|
index.installModule("this_wont_exist").then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
// commsMessages.should.have.length(1);
|
|
|
|
// commsMessages[0].topic.should.equal("node/added");
|
|
|
|
// commsMessages[0].msg.should.eql(nodeInfo.nodes);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
}).finally(function() {
|
|
|
|
exec.restore();
|
|
|
|
addModule.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
describe("uninstalls module", function() {
|
|
|
|
it("rejects invalid module names", function(done) {
|
|
|
|
var promises = [];
|
|
|
|
promises.push(index.uninstallModule("this_wont_exist "));
|
|
|
|
promises.push(index.uninstallModule("this_wont_exist;no_it_really_wont"));
|
|
|
|
when.settle(promises).then(function(results) {
|
|
|
|
results[0].state.should.be.eql("rejected");
|
|
|
|
results[1].state.should.be.eql("rejected");
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it("rejects with generic error", function(done) {
|
|
|
|
var nodeInfo = [{module:"foo",types:["a"]}];
|
|
|
|
var removeModule = sinon.stub(registry,"removeModule",function(md) {
|
|
|
|
return when.resolve(nodeInfo);
|
|
|
|
});
|
2015-11-08 15:26:11 +01:00
|
|
|
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-08 15:06:36 +01:00
|
|
|
cb(new Error("test_error"),"","");
|
|
|
|
});
|
|
|
|
|
|
|
|
index.uninstallModule("this_wont_exist").then(function() {
|
|
|
|
done(new Error("Unexpected success"));
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done();
|
|
|
|
}).finally(function() {
|
|
|
|
exec.restore();
|
|
|
|
removeModule.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
it("succeeds when module is found", function(done) {
|
|
|
|
var nodeInfo = [{module:"foo",types:["a"]}];
|
|
|
|
var removeModule = sinon.stub(registry,"removeModule",function(md) {
|
|
|
|
return nodeInfo;
|
|
|
|
});
|
|
|
|
var getModuleInfo = sinon.stub(registry,"getModuleInfo",function(md) {
|
|
|
|
return {nodes:[]};
|
|
|
|
});
|
2015-11-08 15:26:11 +01:00
|
|
|
var exec = sinon.stub(child_process,"execFile",function(cmd,args,opt,cb) {
|
2015-11-08 15:06:36 +01:00
|
|
|
cb(null,"","");
|
|
|
|
});
|
|
|
|
|
|
|
|
var exists = sinon.stub(require('fs'),"existsSync", function(fn) { return true; });
|
|
|
|
|
|
|
|
index.uninstallModule("this_wont_exist").then(function(info) {
|
|
|
|
info.should.eql(nodeInfo);
|
|
|
|
// commsMessages.should.have.length(1);
|
|
|
|
// commsMessages[0].topic.should.equal("node/removed");
|
|
|
|
// commsMessages[0].msg.should.eql(nodeInfo);
|
|
|
|
done();
|
|
|
|
}).otherwise(function(err) {
|
|
|
|
done(err);
|
|
|
|
}).finally(function() {
|
|
|
|
exec.restore();
|
|
|
|
removeModule.restore();
|
|
|
|
exists.restore();
|
|
|
|
getModuleInfo.restore();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-07-17 09:34:26 +02:00
|
|
|
});
|