1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Finalise nodeSettings and update tlsConfigDisableLocalFiles

- increase test coverage around registerType
This commit is contained in:
Nick O'Leary 2017-03-09 21:06:49 +00:00
parent f45a2643f2
commit 5356373681
No known key found for this signature in database
GPG Key ID: 4F2157149161A6C9
6 changed files with 225 additions and 142 deletions

View File

@ -82,7 +82,7 @@ module.exports = function(RED) {
settings: {
tlsConfigDisableLocalFiles: {
value: false,
exportable: false
exportable: true
}
}
});

View File

@ -43,10 +43,6 @@ module.exports = {
safeSettings.flowFilePretty = settings.flowFilePretty;
}
if (settings.tlsDisableLocalFiles) {
safeSettings.tlsDisableLocalFiles = settings.tlsDisableLocalFiles;
}
if (!runtime.nodes.paletteEditorEnabled()) {
safeSettings.editorTheme = safeSettings.editorTheme || {};
safeSettings.editorTheme.palette = safeSettings.editorTheme.palette || {};

View File

@ -24,7 +24,7 @@ var flows = require("./flows");
var flowUtil = require("./flows/util")
var context = require("./context");
var Node = require("./Node");
var log = require("../log");
var log = null;
var library = require("./library");
var events = require("../events");
@ -55,7 +55,11 @@ function registerType(nodeSet,type,constructor,opts) {
credentials.register(type,opts.credentials);
}
if (opts.settings) {
try {
settings.registerNodeSettings(type,opts.settings);
} catch(err) {
log.warn("["+type+"] "+err.message);
}
}
}
registry.registerType(nodeSet,type,constructor);
@ -90,6 +94,7 @@ function createNode(node,def) {
function init(runtime) {
settings = runtime.settings;
log = runtime.log;
credentials.init(runtime);
flows.init(runtime);
registry.init(runtime);

View File

@ -106,19 +106,15 @@ var persistentSettings = {
storage = null;
},
registerNodeSettings: function(type, opts) {
try {
var normalisedType = util.normaliseNodeTypeName(type);
for (var property in opts) {
if (opts.hasOwnProperty(property)) {
var normalisedType = util.normaliseNodeTypeName(type);
if (!property.startsWith(normalisedType)) {
throw new Error("The name of node setting property " + property + " must start with \"" + normalisedType + "\" (case sensitive).");
throw new Error("Registered invalid property name '"+property+"'. Properties for this node must start with '"+normalisedType+"'");
}
}
}
nodeSettings[type] = opts;
} catch (err) {
console.log(err.toString());
}
},
exportNodeSettings: function(safeSettings) {
safeSettings["nodeSettings"] = {};
@ -131,7 +127,7 @@ var persistentSettings = {
if (setting.exportable) {
if (userSettings.hasOwnProperty(property)) {
safeSettings["nodeSettings"][property] = userSettings[property];
} else {
} else if (setting.hasOwnProperty('value')) {
safeSettings["nodeSettings"][property] = setting.value;
}
}

View File

@ -74,7 +74,7 @@ describe("red/nodes/index", function() {
it('nodes are initialised with credentials',function(done) {
index.init(runtime);
index.registerType('test', TestNode);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
var testnode = new TestNode({id:'tab1',type:'test',name:'barney'});
testnode.credentials.should.have.property('b',1);
@ -89,8 +89,8 @@ describe("red/nodes/index", function() {
it('flows should be initialised',function(done) {
index.init(runtime);
index.loadFlows().then(function() {
console.log(testFlows);
console.log(index.getFlows());
// console.log(testFlows);
// console.log(index.getFlows());
should.deepEqual(testFlows, index.getFlows().flows);
done();
}).otherwise(function(err) {
@ -98,18 +98,45 @@ describe("red/nodes/index", function() {
});
});
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);
describe("registerType should register credentials definition", function() {
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);
});
});
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");
var log = require("../../../../red/runtime/log");
var RED = require("../../../../red/red.js");
var userDir = path.join(__dirname,".testUserHome");
before(function(done) {
sinon.stub(log,"log");
fs.remove(userDir,function(err) {
fs.mkdir(userDir,function() {
sinon.stub(index, 'load', function() {
@ -136,13 +163,14 @@ describe("red/nodes/index", function() {
runtime.stop().then(function() {
index.load.restore();
localfilesystem.getCredentials.restore();
log.log.restore();
done();
});
});
});
it(': definition defined',function() {
index.registerType('test', TestNode, {
it('definition defined',function() {
index.registerType('test-node-set','test', TestNode, {
credentials: {
foo: {type:"test"}
}
@ -150,7 +178,52 @@ describe("red/nodes/index", function() {
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() {
@ -175,9 +248,9 @@ describe("red/nodes/index", function() {
registry.disableNode.restore();
});
it(': allows an unused node type to be disabled',function(done) {
it('allows an unused node type to be disabled',function(done) {
index.init(runtime);
index.registerType('test', TestNode);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
var info = index.disableNode("5678");
registry.disableNode.calledOnce.should.be.true();
@ -189,9 +262,9 @@ describe("red/nodes/index", function() {
});
});
it(': prevents disabling a node type that is in use',function(done) {
it('prevents disabling a node type that is in use',function(done) {
index.init(runtime);
index.registerType('test', TestNode);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
@ -204,9 +277,9 @@ describe("red/nodes/index", function() {
});
});
it(': prevents disabling a node type that is unknown',function(done) {
it('prevents disabling a node type that is unknown',function(done) {
index.init(runtime);
index.registerType('test', TestNode);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
@ -257,9 +330,9 @@ describe("red/nodes/index", function() {
registry.removeModule.restore();
});
it(': prevents removing a module that is in use',function(done) {
it('prevents removing a module that is in use',function(done) {
index.init(runtime);
index.registerType('test', TestNode);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {
@ -272,9 +345,9 @@ describe("red/nodes/index", function() {
});
});
it(': prevents removing a module that is unknown',function(done) {
it('prevents removing a module that is unknown',function(done) {
index.init(runtime);
index.registerType('test', TestNode);
index.registerType('test-node-set','test', TestNode);
index.loadFlows().then(function() {
/*jshint immed: false */
(function() {

View File

@ -153,6 +153,8 @@ describe("red/settings", function() {
settings.registerNodeSettings("_http_request_", {httpRequest3:{value:"a3", exportable:true}} );
settings.registerNodeSettings("mQtT", {mQtTColor:{value:"purple", exportable:true}} );
settings.registerNodeSettings("abc123", {abc123:{value:"def456", exportable:true}} );
settings.registerNodeSettings("noValue", {noValueHasValue:{value:"123", exportable:true}, noValueNoValue:{exportable:true}} );
var safeSettings = {};
settings.exportNodeSettings(safeSettings);
safeSettings["nodeSettings"].should.have.property("injectColor", "red");
@ -164,15 +166,26 @@ describe("red/settings", function() {
safeSettings["nodeSettings"].should.have.property("httpRequest3", "a3");
safeSettings["nodeSettings"].should.have.property("mQtTColor", "purple");
safeSettings["nodeSettings"].should.have.property("abc123", "def456");
safeSettings["nodeSettings"].should.have.property("noValueHasValue", "123");
safeSettings["nodeSettings"].should.not.have.property("noValueNoValue");
});
it('prohibits registering the property whose name do not start with type name', function() {
var userSettings = {};
settings.init(userSettings);
(function() {
settings.registerNodeSettings("inject", {color:{value:"red", exportable:true}} );
}).should.throw();
(function() {
settings.registerNodeSettings("_a_b_1_", {ab1Color:{value:"red", exportable:true}} );
}).should.throw();
(function() {
settings.registerNodeSettings("AB2", {AB2Color:{value:"red", exportable:true}} );
}).should.throw();
(function() {
settings.registerNodeSettings("abcDef", {abcColor:{value:"red", exportable:true}} );
}).should.throw();
var safeSettings = {};
settings.exportNodeSettings(safeSettings);
safeSettings["nodeSettings"].should.not.have.property("color");