mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Merge branch 'dev' into pr_1789
This commit is contained in:
237
test/unit/@node-red/editor-api/lib/admin/context_spec.js
Normal file
237
test/unit/@node-red/editor-api/lib/admin/context_spec.js
Normal file
@@ -0,0 +1,237 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var sinon = require('sinon');
|
||||
var when = require('when');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var context = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/context");
|
||||
// var Context = require("../../../../red/runtime/nodes/context");
|
||||
// var Util = require("../../../../red/runtime/util");
|
||||
|
||||
describe("api/admin/context", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
});
|
||||
/*
|
||||
var app = undefined;
|
||||
|
||||
before(function (done) {
|
||||
var node_context = undefined;
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.get("/context/:scope(global)", context.get);
|
||||
app.get("/context/:scope(global)/*", context.get);
|
||||
app.get("/context/:scope(node|flow)/:id", context.get);
|
||||
app.get("/context/:scope(node|flow)/:id/*", context.get);
|
||||
|
||||
context.init({
|
||||
settings: {
|
||||
},
|
||||
log:{warn:function(){},_:function(){},audit:function(){}},
|
||||
nodes: {
|
||||
listContextStores: Context.listStores,
|
||||
getContext: Context.get,
|
||||
getNode: function(id) {
|
||||
if (id === 'NID') {
|
||||
return {
|
||||
id: 'NID',
|
||||
context: function () {
|
||||
return node_context;
|
||||
}
|
||||
};
|
||||
}
|
||||
return null;
|
||||
}
|
||||
},
|
||||
util: Util
|
||||
});
|
||||
|
||||
Context.init({
|
||||
contextStorage: {
|
||||
memory0: {
|
||||
module: "memory"
|
||||
},
|
||||
memory1: {
|
||||
module: "memory"
|
||||
}
|
||||
}
|
||||
});
|
||||
Context.load().then(function () {
|
||||
var ctx = Context.get("NID", "FID");
|
||||
node_context = ctx;
|
||||
ctx.set("foo", "n_v00", "memory0");
|
||||
ctx.set("bar", "n_v01", "memory0");
|
||||
ctx.set("baz", "n_v10", "memory1");
|
||||
ctx.set("bar", "n_v11", "memory1");
|
||||
ctx.flow.set("foo", "f_v00", "memory0");
|
||||
ctx.flow.set("bar", "f_v01", "memory0");
|
||||
ctx.flow.set("baz", "f_v10", "memory1");
|
||||
ctx.flow.set("bar", "f_v11", "memory1");
|
||||
ctx.global.set("foo", "g_v00", "memory0");
|
||||
ctx.global.set("bar", "g_v01", "memory0");
|
||||
ctx.global.set("baz", "g_v10", "memory1");
|
||||
ctx.global.set("bar", "g_v11", "memory1");
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
after(function () {
|
||||
Context.clean({allNodes:{}});
|
||||
Context.close();
|
||||
});
|
||||
|
||||
function check_mem(body, mem, name, val) {
|
||||
var mem0 = body[mem];
|
||||
mem0.should.have.property(name);
|
||||
mem0[name].should.deepEqual(val);
|
||||
}
|
||||
|
||||
function check_scope(scope, prefix, id) {
|
||||
describe('# '+scope, function () {
|
||||
var xid = id ? ("/"+id) : "";
|
||||
|
||||
it('should return '+scope+' contexts', function (done) {
|
||||
request(app)
|
||||
.get('/context/'+scope+xid)
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
var body = res.body;
|
||||
body.should.have.key('memory0', 'memory1');
|
||||
check_mem(body, 'memory0',
|
||||
'foo', {msg:prefix+'_v00', format:'string[5]'});
|
||||
check_mem(body, 'memory0',
|
||||
'bar', {msg:prefix+'_v01', format:'string[5]'});
|
||||
check_mem(body, 'memory1',
|
||||
'baz', {msg:prefix+'_v10', format:'string[5]'});
|
||||
check_mem(body, 'memory1',
|
||||
'bar', {msg:prefix+'_v11', format:'string[5]'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a value from default '+scope+' context', function (done) {
|
||||
request(app)
|
||||
.get('/context/'+scope+xid+'/foo')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
var body = res.body;
|
||||
body.should.deepEqual({msg: prefix+'_v00', format: 'string[5]'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a value from specified '+scope+' context', function (done) {
|
||||
request(app)
|
||||
.get('/context/'+scope+xid+'/bar?store=memory1')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
var body = res.body;
|
||||
body.should.deepEqual({msg: prefix+'_v11', format: 'string[5]', store: 'memory1'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return specified '+scope+' store', function (done) {
|
||||
request(app)
|
||||
.get('/context/'+scope+xid+'?store=memory1')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
var body = res.body;
|
||||
body.should.deepEqual({
|
||||
memory1: {
|
||||
baz: { msg: prefix+'_v10', format: 'string[5]' },
|
||||
bar: { msg: prefix+'_v11', format: 'string[5]' }
|
||||
}
|
||||
});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should return undefined for unknown key of default '+scope+' store', function (done) {
|
||||
request(app)
|
||||
.get('/context/'+scope+xid+'/unknown')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
var body = res.body;
|
||||
body.should.deepEqual({msg:'(undefined)', format:'undefined'});
|
||||
done();
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
it('should cause error for unknown '+scope+' store', function (done) {
|
||||
request(app)
|
||||
.get('/context/'+scope+xid+'?store=unknown')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done();
|
||||
}
|
||||
done("unexpected");
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
check_scope("global", "g", undefined);
|
||||
check_scope("node", "n", "NID");
|
||||
check_scope("flow", "f", "FID");
|
||||
|
||||
describe("# errors", function () {
|
||||
it('should cause error for unknown scope', function (done) {
|
||||
request(app)
|
||||
.get('/context/scope')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function (err, res) {
|
||||
if (err) {
|
||||
return done();
|
||||
}
|
||||
done("unexpected");
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
*/
|
||||
249
test/unit/@node-red/editor-api/lib/admin/flow_spec.js
Normal file
249
test/unit/@node-red/editor-api/lib/admin/flow_spec.js
Normal file
@@ -0,0 +1,249 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var sinon = require('sinon');
|
||||
var when = require('when');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var flow = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/flow");
|
||||
|
||||
describe("api/admin/flow", function() {
|
||||
|
||||
var app;
|
||||
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.get("/flow/:id",flow.get);
|
||||
app.post("/flow",flow.post);
|
||||
app.put("/flow/:id",flow.put);
|
||||
app.delete("/flow/:id",flow.delete);
|
||||
});
|
||||
|
||||
describe("get", function() {
|
||||
before(function() {
|
||||
var opts;
|
||||
flow.init({
|
||||
flows: {
|
||||
getFlow: function(_opts) {
|
||||
opts = _opts;
|
||||
if (opts.id === '123') {
|
||||
return Promise.resolve({id:'123'});
|
||||
} else {
|
||||
var err = new Error("message");
|
||||
err.code = "not_found";
|
||||
err.status = 404;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
it('gets a known flow', function(done) {
|
||||
request(app)
|
||||
.get('/flow/123')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.has.a.property('id','123');
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('404s an unknown flow', function(done) {
|
||||
request(app)
|
||||
.get('/flow/456')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(404)
|
||||
.end(done);
|
||||
})
|
||||
});
|
||||
|
||||
describe("add", function() {
|
||||
var opts;
|
||||
before(function() {
|
||||
flow.init({
|
||||
flows: {
|
||||
addFlow: function(_opts) {
|
||||
opts = _opts;
|
||||
if (opts.flow.id === "123") {
|
||||
return Promise.resolve('123')
|
||||
} else {
|
||||
var err = new Error("random error");
|
||||
err.code = "random_error";
|
||||
err.status = 400;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
it('adds a new flow', function(done) {
|
||||
request(app)
|
||||
.post('/flow')
|
||||
.set('Accept', 'application/json')
|
||||
.send({id:'123'})
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.has.a.property('id','123');
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('400 an invalid flow', function(done) {
|
||||
request(app)
|
||||
.post('/flow')
|
||||
.set('Accept', 'application/json')
|
||||
.send({id:'error'})
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.has.a.property('code','random_error');
|
||||
res.body.should.has.a.property('message','random error');
|
||||
|
||||
done();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe("update", function() {
|
||||
|
||||
var opts;
|
||||
before(function() {
|
||||
flow.init({
|
||||
flows: {
|
||||
updateFlow: function(_opts) {
|
||||
opts = _opts;
|
||||
if (opts.id === "123") {
|
||||
return Promise.resolve('123')
|
||||
} else {
|
||||
var err = new Error("random error");
|
||||
err.code = "random_error";
|
||||
err.status = 400;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
it('updates an existing flow', function(done) {
|
||||
request(app)
|
||||
.put('/flow/123')
|
||||
.set('Accept', 'application/json')
|
||||
.send({id:'123'})
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.has.a.property('id','123');
|
||||
opts.should.have.property('id','123');
|
||||
opts.should.have.property('flow',{id:'123'})
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
it('400 an invalid flow', function(done) {
|
||||
request(app)
|
||||
.put('/flow/456')
|
||||
.set('Accept', 'application/json')
|
||||
.send({id:'456'})
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.has.a.property('code','random_error');
|
||||
res.body.should.has.a.property('message','random error');
|
||||
|
||||
done();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete", function() {
|
||||
|
||||
var opts;
|
||||
before(function() {
|
||||
flow.init({
|
||||
flows: {
|
||||
deleteFlow: function(_opts) {
|
||||
opts = _opts;
|
||||
if (opts.id === "123") {
|
||||
return Promise.resolve()
|
||||
} else {
|
||||
var err = new Error("random error");
|
||||
err.code = "random_error";
|
||||
err.status = 400;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
|
||||
it('deletes an existing flow', function(done) {
|
||||
request(app)
|
||||
.del('/flow/123')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
opts.should.have.property('id','123');
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
it('400 an invalid flow', function(done) {
|
||||
request(app)
|
||||
.del('/flow/456')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.has.a.property('code','random_error');
|
||||
res.body.should.has.a.property('message','random error');
|
||||
|
||||
done();
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
});
|
||||
211
test/unit/@node-red/editor-api/lib/admin/flows_spec.js
Normal file
211
test/unit/@node-red/editor-api/lib/admin/flows_spec.js
Normal file
@@ -0,0 +1,211 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var sinon = require('sinon');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var flows = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/flows");
|
||||
|
||||
describe("api/admin/flows", function() {
|
||||
|
||||
var app;
|
||||
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.get("/flows",flows.get);
|
||||
app.post("/flows",flows.post);
|
||||
});
|
||||
|
||||
it('returns flow - v1', function(done) {
|
||||
flows.init({
|
||||
flows:{
|
||||
getFlows: function() { return Promise.resolve({rev:"123",flows:[1,2,3]}); }
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/flows')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
try {
|
||||
res.body.should.have.lengthOf(3);
|
||||
done();
|
||||
} catch(e) {
|
||||
return done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
it('returns flow - v2', function(done) {
|
||||
flows.init({
|
||||
flows:{
|
||||
getFlows: function() { return Promise.resolve({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('code','invalid_api_version');
|
||||
done();
|
||||
} catch(e) {
|
||||
return done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
it('sets flows - default - v1', function(done) {
|
||||
var setFlows = sinon.spy(function() { return Promise.resolve();});
|
||||
flows.init({
|
||||
flows:{
|
||||
setFlows: setFlows
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/flows')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
setFlows.calledOnce.should.be.true();
|
||||
setFlows.lastCall.args[0].should.have.property('deploymentType','full');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('sets flows - non-default - v1', function(done) {
|
||||
var setFlows = sinon.spy(function() { return Promise.resolve();});
|
||||
flows.init({
|
||||
flows:{
|
||||
setFlows: setFlows
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/flows')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Node-RED-Deployment-Type','nodes')
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
setFlows.calledOnce.should.be.true();
|
||||
setFlows.lastCall.args[0].should.have.property('deploymentType','nodes');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('set flows - rejects mismatched revision - v2', function(done) {
|
||||
flows.init({
|
||||
flows:{
|
||||
setFlows: function() {
|
||||
var err = new Error("mismatch");
|
||||
err.code = "version_mismatch";
|
||||
err.status = 409;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
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("code","version_mismatch");
|
||||
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('code','invalid_api_version');
|
||||
done();
|
||||
} catch(e) {
|
||||
return done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
it('reloads flows', function(done) {
|
||||
var setFlows = sinon.spy(function() { return Promise.resolve();});
|
||||
flows.init({
|
||||
flows:{
|
||||
setFlows: setFlows
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/flows')
|
||||
.set('Accept', 'application/json')
|
||||
.set('Node-RED-Deployment-Type','reload')
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
setFlows.called.should.be.true();
|
||||
setFlows.lastCall.args[0].should.not.have.property('flows');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
321
test/unit/@node-red/editor-api/lib/admin/index_spec.js
Normal file
321
test/unit/@node-red/editor-api/lib/admin/index_spec.js
Normal file
@@ -0,0 +1,321 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require("supertest");
|
||||
var express = require("express");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var adminApi = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin");
|
||||
var auth = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth");
|
||||
var nodes = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/nodes");
|
||||
var flows = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/flows");
|
||||
var flow = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/flow");
|
||||
|
||||
/**
|
||||
* Ensure all API routes are correctly mounted, with the expected permissions checks
|
||||
*/
|
||||
describe("api/admin/index", function() {
|
||||
describe("Ensure all API routes are correctly mounted, with the expected permissions checks", function() {
|
||||
var app;
|
||||
var mockList = [
|
||||
flows,flow,nodes
|
||||
]
|
||||
var permissionChecks = {};
|
||||
var lastRequest;
|
||||
var stubApp = function(req,res,next) {
|
||||
lastRequest = req;
|
||||
res.status(200).end();
|
||||
};
|
||||
before(function() {
|
||||
mockList.forEach(function(m) {
|
||||
sinon.stub(m,"init",function(){});
|
||||
});
|
||||
sinon.stub(auth,"needsPermission", function(permission) {
|
||||
return function(req,res,next) {
|
||||
permissionChecks[permission] = (permissionChecks[permission]||0)+1;
|
||||
next();
|
||||
}
|
||||
});
|
||||
|
||||
sinon.stub(flows,"get",stubApp);
|
||||
sinon.stub(flows,"post",stubApp);
|
||||
|
||||
sinon.stub(flow,"get",stubApp);
|
||||
sinon.stub(flow,"post",stubApp);
|
||||
sinon.stub(flow,"delete",stubApp);
|
||||
sinon.stub(flow,"put",stubApp);
|
||||
|
||||
sinon.stub(nodes,"getAll",stubApp);
|
||||
sinon.stub(nodes,"post",stubApp);
|
||||
sinon.stub(nodes,"getModule",stubApp);
|
||||
sinon.stub(nodes,"putModule",stubApp);
|
||||
sinon.stub(nodes,"delete",stubApp);
|
||||
sinon.stub(nodes,"getSet",stubApp);
|
||||
sinon.stub(nodes,"putSet",stubApp);
|
||||
sinon.stub(nodes,"getModuleCatalog",stubApp);
|
||||
sinon.stub(nodes,"getModuleCatalogs",stubApp);
|
||||
});
|
||||
after(function() {
|
||||
mockList.forEach(function(m) {
|
||||
m.init.restore();
|
||||
});
|
||||
auth.needsPermission.restore();
|
||||
|
||||
flows.get.restore();
|
||||
flows.post.restore();
|
||||
flow.get.restore();
|
||||
flow.post.restore();
|
||||
flow.delete.restore();
|
||||
flow.put.restore();
|
||||
nodes.getAll.restore();
|
||||
nodes.post.restore();
|
||||
nodes.getModule.restore();
|
||||
nodes.putModule.restore();
|
||||
nodes.delete.restore();
|
||||
nodes.getSet.restore();
|
||||
nodes.putSet.restore();
|
||||
nodes.getModuleCatalog.restore();
|
||||
nodes.getModuleCatalogs.restore();
|
||||
|
||||
});
|
||||
|
||||
before(function() {
|
||||
app = adminApi.init({});
|
||||
});
|
||||
beforeEach(function() {
|
||||
permissionChecks = {};
|
||||
})
|
||||
it('GET /flows', function(done) {
|
||||
request(app).get("/flows").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('flows.read',1);
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('POST /flows', function(done) {
|
||||
request(app).post("/flows").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('flows.write',1);
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('GET /flow/1234', function(done) {
|
||||
request(app).get("/flow/1234").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('flows.read',1);
|
||||
lastRequest.params.should.have.property('id','1234')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('POST /flow', function(done) {
|
||||
request(app).post("/flow").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('flows.write',1);
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('DELETE /flow/1234', function(done) {
|
||||
request(app).del("/flow/1234").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('flows.write',1);
|
||||
lastRequest.params.should.have.property('id','1234')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('PUT /flow/1234', function(done) {
|
||||
request(app).put("/flow/1234").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('flows.write',1);
|
||||
lastRequest.params.should.have.property('id','1234')
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('GET /nodes', function(done) {
|
||||
request(app).get("/nodes").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('POST /nodes', function(done) {
|
||||
request(app).post("/nodes").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('GET /nodes/module', function(done) {
|
||||
request(app).get("/nodes/module").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
lastRequest.params.should.have.property(0,'module')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('GET /nodes/@scope/module', function(done) {
|
||||
request(app).get("/nodes/@scope/module").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
lastRequest.params.should.have.property(0,'@scope/module')
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('PUT /nodes/module', function(done) {
|
||||
request(app).put("/nodes/module").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
lastRequest.params.should.have.property(0,'module')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('PUT /nodes/@scope/module', function(done) {
|
||||
request(app).put("/nodes/@scope/module").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
lastRequest.params.should.have.property(0,'@scope/module')
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('DELETE /nodes/module', function(done) {
|
||||
request(app).del("/nodes/module").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
lastRequest.params.should.have.property(0,'module')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('DELETE /nodes/@scope/module', function(done) {
|
||||
request(app).del("/nodes/@scope/module").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
lastRequest.params.should.have.property(0,'@scope/module')
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('GET /nodes/module/set', function(done) {
|
||||
request(app).get("/nodes/module/set").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
lastRequest.params.should.have.property(0,'module')
|
||||
lastRequest.params.should.have.property(2,'set')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('GET /nodes/@scope/module/set', function(done) {
|
||||
request(app).get("/nodes/@scope/module/set").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
lastRequest.params.should.have.property(0,'@scope/module')
|
||||
lastRequest.params.should.have.property(2,'set')
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('PUT /nodes/module/set', function(done) {
|
||||
request(app).put("/nodes/module/set").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
lastRequest.params.should.have.property(0,'module')
|
||||
lastRequest.params.should.have.property(2,'set')
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('PUT /nodes/@scope/module/set', function(done) {
|
||||
request(app).put("/nodes/@scope/module/set").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.write',1);
|
||||
lastRequest.params.should.have.property(0,'@scope/module')
|
||||
lastRequest.params.should.have.property(2,'set')
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
it('GET /nodes/messages', function(done) {
|
||||
request(app).get("/nodes/messages").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('GET /nodes/module/set/messages', function(done) {
|
||||
request(app).get("/nodes/module/set/messages").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
lastRequest.params.should.have.property(0,'module/set');
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('GET /nodes/@scope/module/set/messages', function(done) {
|
||||
request(app).get("/nodes/@scope/module/set/messages").expect(200).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
permissionChecks.should.have.property('nodes.read',1);
|
||||
lastRequest.params.should.have.property(0,'@scope/module/set');
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
479
test/unit/@node-red/editor-api/lib/admin/nodes_spec.js
Normal file
479
test/unit/@node-red/editor-api/lib/admin/nodes_spec.js
Normal file
@@ -0,0 +1,479 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
var sinon = require('sinon');
|
||||
var when = require('when');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var nodes = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/nodes");
|
||||
var apiUtil = NR_TEST_UTILS.require("@node-red/editor-api/lib/util");
|
||||
|
||||
describe("api/admin/nodes", function() {
|
||||
|
||||
var app;
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.get("/nodes",nodes.getAll);
|
||||
app.post("/nodes",nodes.post);
|
||||
app.get(/\/nodes\/messages/,nodes.getModuleCatalogs);
|
||||
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+\/[^\/]+)\/messages/,nodes.getModuleCatalog);
|
||||
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.getModule);
|
||||
app.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.putModule);
|
||||
app.get(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,nodes.getSet);
|
||||
app.put(/\/nodes\/((@[^\/]+\/)?[^\/]+)\/([^\/]+)$/,nodes.putSet);
|
||||
app.get("/getIcons",nodes.getIcons);
|
||||
app.delete(/\/nodes\/((@[^\/]+\/)?[^\/]+)$/,nodes.delete);
|
||||
sinon.stub(apiUtil,"determineLangFromHeaders", function() {
|
||||
return "en-US";
|
||||
});
|
||||
});
|
||||
after(function() {
|
||||
apiUtil.determineLangFromHeaders.restore();
|
||||
})
|
||||
|
||||
describe('get nodes', function() {
|
||||
it('returns node list', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getNodeList: function() {
|
||||
return Promise.resolve([1,2,3]);
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.be.an.Array();
|
||||
res.body.should.have.lengthOf(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns node configs', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getNodeConfigs: function() {
|
||||
return Promise.resolve("<script></script>");
|
||||
}
|
||||
},
|
||||
i18n: {
|
||||
determineLangFromHeaders: function(){}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200)
|
||||
.expect("<script></script>")
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns node module info', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getModuleInfo: function(opts) {
|
||||
return Promise.resolve({"node-red":{name:"node-red"}}[opts.module]);
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/node-red')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("name","node-red");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns 404 for unknown module', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getModuleInfo: function(opts) {
|
||||
var errInstance = new Error("Not Found");
|
||||
errInstance.code = "not_found";
|
||||
errInstance.status = 404;
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/node-blue')
|
||||
.expect(404)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns individual node info', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getNodeInfo: function(opts) {
|
||||
return Promise.resolve({"node-red/123":{id:"node-red/123"}}[opts.id]);
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/node-red/123')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("id","node-red/123");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns individual node configs', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getNodeConfig: function(opts) {
|
||||
return Promise.resolve({"node-red/123":"<script></script>"}[opts.id]);
|
||||
}
|
||||
},
|
||||
i18n: {
|
||||
determineLangFromHeaders: function(){}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/node-red/123')
|
||||
.set('Accept', 'text/html')
|
||||
.expect(200)
|
||||
.expect("<script></script>")
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns 404 for unknown node', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getNodeInfo: function(opts) {
|
||||
var errInstance = new Error("Not Found");
|
||||
errInstance.code = "not_found";
|
||||
errInstance.status = 404;
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/node-red/456')
|
||||
.set('Accept', 'application/json')
|
||||
.expect(404)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('install', function() {
|
||||
it('installs the module and returns module info', function(done) {
|
||||
var opts;
|
||||
nodes.init({
|
||||
nodes:{
|
||||
addModule: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve({
|
||||
name:"foo",
|
||||
nodes:[{id:"123"}]
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/nodes')
|
||||
.send({module: 'foo',version:"1.2.3"})
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("name","foo");
|
||||
res.body.should.have.property("nodes");
|
||||
res.body.nodes[0].should.have.property("id","123");
|
||||
opts.should.have.property("module","foo");
|
||||
opts.should.have.property("version","1.2.3");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns error', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
addModule: function(opts) {
|
||||
var errInstance = new Error("Message");
|
||||
errInstance.code = "random_error";
|
||||
errInstance.status = 400;
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/nodes')
|
||||
.send({module: 'foo',version:"1.2.3"})
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.a.property('code','random_error');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('delete', function() {
|
||||
it('uninstalls the module', function(done) {
|
||||
var opts;
|
||||
nodes.init({
|
||||
nodes:{
|
||||
removeModule: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.del('/nodes/123')
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
opts.should.have.property("module","123");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns error', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
removeModule: function(opts) {
|
||||
var errInstance = new Error("Message");
|
||||
errInstance.code = "random_error";
|
||||
errInstance.status = 400;
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.del('/nodes/123')
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.a.property('code','random_error');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('enable/disable node set', function() {
|
||||
it('returns 400 for invalid request payload', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
setNodeSetState: function(opts) {return Promise.resolve()}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.put('/nodes/node-red/foo')
|
||||
.send({})
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("code","invalid_request");
|
||||
res.body.should.have.property("message","Invalid request");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('sets node state and returns node info', function(done) {
|
||||
var opts;
|
||||
nodes.init({
|
||||
nodes:{
|
||||
setNodeSetState: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve({id:"123",enabled: true });
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
request(app)
|
||||
.put('/nodes/node-red/foo')
|
||||
.send({enabled:true})
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("id","123");
|
||||
res.body.should.have.property("enabled",true);
|
||||
opts.should.have.property("enabled",true);
|
||||
opts.should.have.property("id","node-red/foo");
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('enable/disable module' ,function() {
|
||||
it('returns 400 for invalid request payload', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
setModuleState: function(opts) {return Promise.resolve()}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.put('/nodes/node-red')
|
||||
.send({})
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("code","invalid_request");
|
||||
res.body.should.have.property("message","Invalid request");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('sets module state and returns module info', function(done) {
|
||||
var opts;
|
||||
nodes.init({
|
||||
nodes:{
|
||||
setModuleState: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve({name:"node-red"});
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
request(app)
|
||||
.put('/nodes/node-red')
|
||||
.send({enabled:true})
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("name","node-red");
|
||||
opts.should.have.property("enabled",true);
|
||||
opts.should.have.property("module","node-red");
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('get icons', function() {
|
||||
it('returns icon list', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getIconList: function() {
|
||||
return Promise.resolve({module:[1,2,3]});
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/getIcons')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property("module");
|
||||
res.body.module.should.be.an.Array();
|
||||
res.body.module.should.have.lengthOf(3);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('get module messages', function() {
|
||||
it('returns message catalog', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getModuleCatalog: function(opts) {
|
||||
return Promise.resolve(opts);
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/module/set/messages')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.eql({ module: 'module/set' });
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns all node catalogs', function(done) {
|
||||
nodes.init({
|
||||
nodes:{
|
||||
getModuleCatalogs: function(opts) {
|
||||
return Promise.resolve({a:1});
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/nodes/messages')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.eql({a:1});
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
});
|
||||
47
test/unit/@node-red/editor-api/lib/auth/clients_spec.js
Normal file
47
test/unit/@node-red/editor-api/lib/auth/clients_spec.js
Normal file
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var Clients = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/clients");
|
||||
|
||||
describe("api/auth/clients", function() {
|
||||
it('finds the known editor client',function(done) {
|
||||
Clients.get("node-red-editor").then(function(client) {
|
||||
client.should.have.property("id","node-red-editor");
|
||||
client.should.have.property("secret","not_available");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('finds the known admin client',function(done) {
|
||||
Clients.get("node-red-admin").then(function(client) {
|
||||
client.should.have.property("id","node-red-admin");
|
||||
client.should.have.property("secret","not_available");
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
it('returns null for unknown client',function(done) {
|
||||
Clients.get("unknown-client").then(function(client) {
|
||||
should.not.exist(client);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
217
test/unit/@node-red/editor-api/lib/auth/index_spec.js
Normal file
217
test/unit/@node-red/editor-api/lib/auth/index_spec.js
Normal file
@@ -0,0 +1,217 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require("when");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var passport = require("passport");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var auth = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth");
|
||||
var Users = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/users");
|
||||
var Tokens = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/tokens");
|
||||
var Permissions = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/permissions");
|
||||
|
||||
describe("api/auth/index",function() {
|
||||
|
||||
|
||||
|
||||
describe("ensureClientSecret", function() {
|
||||
before(function() {
|
||||
auth.init({},{})
|
||||
});
|
||||
it("leaves client_secret alone if not present",function(done) {
|
||||
var req = {
|
||||
body: {
|
||||
client_secret: "test_value"
|
||||
}
|
||||
};
|
||||
auth.ensureClientSecret(req,null,function() {
|
||||
req.body.should.have.a.property("client_secret","test_value");
|
||||
done();
|
||||
})
|
||||
});
|
||||
it("applies a default client_secret if not present",function(done) {
|
||||
var req = {
|
||||
body: { }
|
||||
};
|
||||
auth.ensureClientSecret(req,null,function() {
|
||||
req.body.should.have.a.property("client_secret","not_available");
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe("revoke", function() {
|
||||
it("revokes a token", function(done) {
|
||||
var revokeToken = sinon.stub(Tokens,"revoke",function() {
|
||||
return when.resolve();
|
||||
});
|
||||
|
||||
var req = { body: { token: "abcdef" } };
|
||||
|
||||
var res = { status: function(resp) {
|
||||
revokeToken.restore();
|
||||
|
||||
resp.should.equal(200);
|
||||
return {
|
||||
end: done
|
||||
}
|
||||
}};
|
||||
|
||||
auth.revoke(req,res);
|
||||
});
|
||||
});
|
||||
|
||||
describe("login", function() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(Tokens,"init",function(){});
|
||||
sinon.stub(Users,"init",function(){});
|
||||
});
|
||||
afterEach(function() {
|
||||
Tokens.init.restore();
|
||||
Users.init.restore();
|
||||
});
|
||||
it("returns login details - credentials", function(done) {
|
||||
auth.init({adminAuth:{type:"credentials"}},{})
|
||||
auth.login(null,{json: function(resp) {
|
||||
resp.should.have.a.property("type","credentials");
|
||||
resp.should.have.a.property("prompts");
|
||||
resp.prompts.should.have.a.lengthOf(2);
|
||||
done();
|
||||
}});
|
||||
});
|
||||
it("returns login details - none", function(done) {
|
||||
auth.init({},{})
|
||||
auth.login(null,{json: function(resp) {
|
||||
resp.should.eql({});
|
||||
done();
|
||||
}});
|
||||
});
|
||||
it("returns login details - strategy", function(done) {
|
||||
auth.init({adminAuth:{type:"strategy",strategy:{label:"test-strategy",icon:"test-icon"}}},{})
|
||||
auth.login(null,{json: function(resp) {
|
||||
resp.should.have.a.property("type","strategy");
|
||||
resp.should.have.a.property("prompts");
|
||||
resp.prompts.should.have.a.lengthOf(1);
|
||||
resp.prompts[0].should.have.a.property("type","button");
|
||||
resp.prompts[0].should.have.a.property("label","test-strategy");
|
||||
resp.prompts[0].should.have.a.property("icon","test-icon");
|
||||
|
||||
done();
|
||||
}});
|
||||
});
|
||||
|
||||
});
|
||||
describe("needsPermission", function() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(Tokens,"init",function(){});
|
||||
sinon.stub(Users,"init",function(){});
|
||||
});
|
||||
afterEach(function() {
|
||||
Tokens.init.restore();
|
||||
Users.init.restore();
|
||||
if (passport.authenticate.restore) {
|
||||
passport.authenticate.restore();
|
||||
}
|
||||
if (Permissions.hasPermission.restore) {
|
||||
Permissions.hasPermission.restore();
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
it('no-ops if adminAuth not set', function(done) {
|
||||
sinon.stub(passport,"authenticate",function(scopes,opts) {
|
||||
return function(req,res,next) {
|
||||
}
|
||||
});
|
||||
auth.init({});
|
||||
var func = auth.needsPermission("foo");
|
||||
func({},{},function() {
|
||||
passport.authenticate.called.should.be.false();
|
||||
done();
|
||||
})
|
||||
});
|
||||
it('skips auth if req.user undefined', function(done) {
|
||||
sinon.stub(passport,"authenticate",function(scopes,opts) {
|
||||
return function(req,res,next) {
|
||||
next();
|
||||
}
|
||||
});
|
||||
sinon.stub(Permissions,"hasPermission",function(perm) { return true });
|
||||
auth.init({adminAuth:{}});
|
||||
var func = auth.needsPermission("foo");
|
||||
func({user:null},{},function() {
|
||||
try {
|
||||
passport.authenticate.called.should.be.true();
|
||||
Permissions.hasPermission.called.should.be.false();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
it('passes for valid user permission', function(done) {
|
||||
sinon.stub(passport,"authenticate",function(scopes,opts) {
|
||||
return function(req,res,next) {
|
||||
next();
|
||||
}
|
||||
});
|
||||
sinon.stub(Permissions,"hasPermission",function(perm) { return true });
|
||||
auth.init({adminAuth:{}});
|
||||
var func = auth.needsPermission("foo");
|
||||
func({user:true,authInfo: { scope: "read"}},{},function() {
|
||||
try {
|
||||
passport.authenticate.called.should.be.true();
|
||||
Permissions.hasPermission.called.should.be.true();
|
||||
Permissions.hasPermission.lastCall.args[0].should.eql("read");
|
||||
Permissions.hasPermission.lastCall.args[1].should.eql("foo");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
it('rejects for invalid user permission', function(done) {
|
||||
sinon.stub(passport,"authenticate",function(scopes,opts) {
|
||||
return function(req,res,next) {
|
||||
next();
|
||||
}
|
||||
});
|
||||
sinon.stub(Permissions,"hasPermission",function(perm) { return false });
|
||||
auth.init({adminAuth:{}});
|
||||
var func = auth.needsPermission("foo");
|
||||
func({user:true,authInfo: { scope: "read"}},{
|
||||
status: function(status) {
|
||||
return { end: function() {
|
||||
try {
|
||||
status.should.eql(401);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
}}
|
||||
}
|
||||
},function() {
|
||||
done(new Error("hasPermission unexpected passed"))
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
59
test/unit/@node-red/editor-api/lib/auth/permissions_spec.js
Normal file
59
test/unit/@node-red/editor-api/lib/auth/permissions_spec.js
Normal file
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var permissions = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/permissions");
|
||||
|
||||
describe("api/auth/permissions", function() {
|
||||
describe("hasPermission", function() {
|
||||
it('a user with no permissions',function() {
|
||||
permissions.hasPermission([],"*").should.be.false();
|
||||
});
|
||||
it('a user with global permissions',function() {
|
||||
permissions.hasPermission("*","read").should.be.true();
|
||||
permissions.hasPermission(["*"],"write").should.be.true();
|
||||
});
|
||||
it('a user with read permissions',function() {
|
||||
permissions.hasPermission(["read"],"read").should.be.true();
|
||||
permissions.hasPermission(["read"],"node.read").should.be.true();
|
||||
permissions.hasPermission(["read"],"write").should.be.false();
|
||||
permissions.hasPermission(["read"],"node.write").should.be.false();
|
||||
permissions.hasPermission(["*.read"],"read").should.be.true();
|
||||
permissions.hasPermission(["*.read"],"node.read").should.be.true();
|
||||
permissions.hasPermission(["*.read"],"write").should.be.false();
|
||||
permissions.hasPermission(["*.read"],"node.write").should.be.false();
|
||||
});
|
||||
it('a user with foo permissions',function() {
|
||||
permissions.hasPermission("foo","foo").should.be.true();
|
||||
});
|
||||
it('an array of permissions', function() {
|
||||
permissions.hasPermission(["*"],["foo.read","foo.write"]).should.be.true();
|
||||
permissions.hasPermission("read",["foo.read","foo.write"]).should.be.false();
|
||||
permissions.hasPermission("read",["foo.read","bar.read"]).should.be.true();
|
||||
permissions.hasPermission(["flows.read"],["flows.read"]).should.be.true();
|
||||
permissions.hasPermission(["flows.read"],["flows.write"]).should.be.false();
|
||||
permissions.hasPermission(["flows.read","nodes.write"],["flows.write"]).should.be.false();
|
||||
permissions.hasPermission(["flows.read","nodes.write"],["nodes.write"]).should.be.true();
|
||||
});
|
||||
it('permits an empty permission', function() {
|
||||
permissions.hasPermission("*","").should.be.true();
|
||||
permissions.hasPermission("read",[""]).should.be.true();
|
||||
});
|
||||
});
|
||||
});
|
||||
273
test/unit/@node-red/editor-api/lib/auth/strategies_spec.js
Normal file
273
test/unit/@node-red/editor-api/lib/auth/strategies_spec.js
Normal file
@@ -0,0 +1,273 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require('when');
|
||||
var sinon = require('sinon');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var strategies = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/strategies");
|
||||
var Users = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/users");
|
||||
var Tokens = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/tokens");
|
||||
var Clients = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/clients");
|
||||
|
||||
describe("api/auth/strategies", function() {
|
||||
describe("Password Token Exchange", function() {
|
||||
var userAuthentication;
|
||||
afterEach(function() {
|
||||
if (userAuthentication) {
|
||||
userAuthentication.restore();
|
||||
userAuthentication = null;
|
||||
}
|
||||
});
|
||||
|
||||
it('Handles authentication failure',function(done) {
|
||||
userAuthentication = sinon.stub(Users,"authenticate",function(username,password) {
|
||||
return when.resolve(null);
|
||||
});
|
||||
|
||||
strategies.passwordTokenExchange({},"user","password","scope",function(err,token) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
token.should.be.false();
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('Handles scope overreach',function(done) {
|
||||
userAuthentication = sinon.stub(Users,"authenticate",function(username,password) {
|
||||
return when.resolve({username:"user",permissions:"read"});
|
||||
});
|
||||
|
||||
strategies.passwordTokenExchange({},"user","password","*",function(err,token) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
token.should.be.false();
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('Creates new token on authentication success',function(done) {
|
||||
userAuthentication = sinon.stub(Users,"authenticate",function(username,password) {
|
||||
return when.resolve({username:"user",permissions:"*"});
|
||||
});
|
||||
var tokenDetails = {};
|
||||
var tokenCreate = sinon.stub(Tokens,"create",function(username,client,scope) {
|
||||
tokenDetails.username = username;
|
||||
tokenDetails.client = client;
|
||||
tokenDetails.scope = scope;
|
||||
return when.resolve({accessToken: "123456"});
|
||||
});
|
||||
|
||||
strategies.passwordTokenExchange({id:"myclient"},"user","password","read",function(err,token) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
token.should.equal("123456");
|
||||
tokenDetails.should.have.property("username","user");
|
||||
tokenDetails.should.have.property("client","myclient");
|
||||
tokenDetails.should.have.property("scope","read");
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
tokenCreate.restore();
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("Anonymous Strategy", function() {
|
||||
it('Succeeds if anon user enabled',function(done) {
|
||||
var userDefault = sinon.stub(Users,"default",function() {
|
||||
return when.resolve("anon");
|
||||
});
|
||||
strategies.anonymousStrategy._success = strategies.anonymousStrategy.success;
|
||||
strategies.anonymousStrategy.success = function(user) {
|
||||
user.should.equal("anon");
|
||||
strategies.anonymousStrategy.success = strategies.anonymousStrategy._success;
|
||||
delete strategies.anonymousStrategy._success;
|
||||
done();
|
||||
};
|
||||
strategies.anonymousStrategy.authenticate({});
|
||||
});
|
||||
it('Fails if anon user not enabled',function(done) {
|
||||
var userDefault = sinon.stub(Users,"default",function() {
|
||||
return when.resolve(null);
|
||||
});
|
||||
strategies.anonymousStrategy._fail = strategies.anonymousStrategy.fail;
|
||||
strategies.anonymousStrategy.fail = function(err) {
|
||||
err.should.equal(401);
|
||||
strategies.anonymousStrategy.fail = strategies.anonymousStrategy._fail;
|
||||
delete strategies.anonymousStrategy._fail;
|
||||
done();
|
||||
};
|
||||
strategies.anonymousStrategy.authenticate({});
|
||||
});
|
||||
afterEach(function() {
|
||||
Users.default.restore();
|
||||
})
|
||||
});
|
||||
|
||||
describe("Bearer Strategy", function() {
|
||||
it('Rejects invalid token',function(done) {
|
||||
var getToken = sinon.stub(Tokens,"get",function(token) {
|
||||
return when.resolve(null);
|
||||
});
|
||||
|
||||
strategies.bearerStrategy("1234",function(err,user) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
user.should.be.false();
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
getToken.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
it('Accepts valid token',function(done) {
|
||||
var getToken = sinon.stub(Tokens,"get",function(token) {
|
||||
return when.resolve({user:"user",scope:"scope"});
|
||||
});
|
||||
var getUser = sinon.stub(Users,"get",function(username) {
|
||||
return when.resolve("aUser");
|
||||
});
|
||||
|
||||
strategies.bearerStrategy("1234",function(err,user,opts) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
user.should.equal("aUser");
|
||||
opts.should.have.a.property("scope","scope");
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
getToken.restore();
|
||||
getUser.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
it('Fail if no user for token',function(done) {
|
||||
var getToken = sinon.stub(Tokens,"get",function(token) {
|
||||
return when.resolve({user:"user",scope:"scope"});
|
||||
});
|
||||
var getUser = sinon.stub(Users,"get",function(username) {
|
||||
return when.resolve(null);
|
||||
});
|
||||
|
||||
strategies.bearerStrategy("1234",function(err,user,opts) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
user.should.equal(false);
|
||||
should.not.exist(opts);
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
getToken.restore();
|
||||
getUser.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("Client Password Strategy", function() {
|
||||
it('Accepts valid client',function(done) {
|
||||
var testClient = {id:"node-red-editor",secret:"not_available"};
|
||||
var getClient = sinon.stub(Clients,"get",function(client) {
|
||||
return when.resolve(testClient);
|
||||
});
|
||||
|
||||
strategies.clientPasswordStrategy(testClient.id,testClient.secret,function(err,client) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
client.should.eql(testClient);
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
getClient.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
it('Rejects invalid client secret',function(done) {
|
||||
var testClient = {id:"node-red-editor",secret:"not_available"};
|
||||
var getClient = sinon.stub(Clients,"get",function(client) {
|
||||
return when.resolve(testClient);
|
||||
});
|
||||
|
||||
strategies.clientPasswordStrategy(testClient.id,"invalid_secret",function(err,client) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
client.should.be.false();
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
getClient.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
it('Rejects invalid client id',function(done) {
|
||||
var getClient = sinon.stub(Clients,"get",function(client) {
|
||||
return when.resolve(null);
|
||||
});
|
||||
strategies.clientPasswordStrategy("invalid_id","invalid_secret",function(err,client) {
|
||||
try {
|
||||
should.not.exist(err);
|
||||
client.should.be.false();
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
getClient.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
var userAuthentication;
|
||||
it('Blocks after 5 failures',function(done) {
|
||||
userAuthentication = sinon.stub(Users,"authenticate",function(username,password) {
|
||||
return when.resolve(null);
|
||||
});
|
||||
for (var z=0; z<5; z++) {
|
||||
strategies.passwordTokenExchange({},"user","badpassword","scope",function(err,token) {
|
||||
});
|
||||
}
|
||||
strategies.passwordTokenExchange({},"user","badpassword","scope",function(err,token) {
|
||||
try {
|
||||
err.toString().should.equal("Error: Too many login attempts. Wait 10 minutes and try again");
|
||||
token.should.be.false();
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
} finally {
|
||||
userAuthentication.restore();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
181
test/unit/@node-red/editor-api/lib/auth/tokens_spec.js
Normal file
181
test/unit/@node-red/editor-api/lib/auth/tokens_spec.js
Normal file
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require("when");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var Tokens = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/tokens");
|
||||
|
||||
|
||||
describe("api/auth/tokens", function() {
|
||||
describe("#init",function() {
|
||||
it('loads sessions', function(done) {
|
||||
Tokens.init({}).then(done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("#get",function() {
|
||||
it('returns a valid token', function(done) {
|
||||
Tokens.init({},{
|
||||
getSessions:function() {
|
||||
return when.resolve({"1234":{"user":"fred","expires":Date.now()+1000}});
|
||||
}
|
||||
}).then(function() {
|
||||
Tokens.get("1234").then(function(token) {
|
||||
try {
|
||||
token.should.have.a.property("user","fred");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('returns null for an invalid token', function(done) {
|
||||
Tokens.init({},{
|
||||
getSessions:function() {
|
||||
return when.resolve({});
|
||||
}
|
||||
}).then(function() {
|
||||
Tokens.get("1234").then(function(token) {
|
||||
try {
|
||||
should.not.exist(token);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
it('returns null for an expired token', function(done) {
|
||||
var saveSessions = sinon.stub().returns(when.resolve());
|
||||
var expiryTime = Date.now()+50;
|
||||
Tokens.init({},{
|
||||
getSessions:function() {
|
||||
return when.resolve({"1234":{"user":"fred","expires":expiryTime}});
|
||||
},
|
||||
saveSessions: saveSessions
|
||||
}).then(function() {
|
||||
Tokens.get("1234").then(function(token) {
|
||||
try {
|
||||
should.exist(token);
|
||||
setTimeout(function() {
|
||||
Tokens.get("1234").then(function(token) {
|
||||
try {
|
||||
should.not.exist(token);
|
||||
saveSessions.calledOnce.should.be.true();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
},100);
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('returns a valid api token', function(done) {
|
||||
Tokens.init({
|
||||
tokens: [{
|
||||
token: "1234",
|
||||
user: "fred",
|
||||
}]
|
||||
},{
|
||||
getSessions:function() {
|
||||
return when.resolve({});
|
||||
}
|
||||
}).then(function() {
|
||||
Tokens.get("1234").then(function(token) {
|
||||
try {
|
||||
token.should.have.a.property("user","fred");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("#create",function() {
|
||||
it('creates a token', function(done) {
|
||||
var savedSession;
|
||||
Tokens.init({sessionExpiryTime: 10},{
|
||||
getSessions:function() {
|
||||
return when.resolve({});
|
||||
},
|
||||
saveSessions:function(sess) {
|
||||
savedSession = sess;
|
||||
return when.resolve();
|
||||
}
|
||||
});
|
||||
var expectedExpiryTime = Date.now()+10000;
|
||||
|
||||
|
||||
Tokens.create("user","client","scope").then(function(token) {
|
||||
try {
|
||||
should.exist(savedSession);
|
||||
var sessionKeys = Object.keys(savedSession);
|
||||
sessionKeys.should.have.lengthOf(1);
|
||||
|
||||
token.should.have.a.property('accessToken',sessionKeys[0]);
|
||||
savedSession[sessionKeys[0]].should.have.a.property('user','user');
|
||||
savedSession[sessionKeys[0]].should.have.a.property('client','client');
|
||||
savedSession[sessionKeys[0]].should.have.a.property('scope','scope');
|
||||
savedSession[sessionKeys[0]].should.have.a.property('expires');
|
||||
savedSession[sessionKeys[0]].expires.should.be.within(expectedExpiryTime-200,expectedExpiryTime+200);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#revoke", function() {
|
||||
it('revokes a token', function(done) {
|
||||
var savedSession;
|
||||
Tokens.init({},{
|
||||
getSessions:function() {
|
||||
return when.resolve({"1234":{"user":"fred","expires":Date.now()+1000}});
|
||||
},
|
||||
saveSessions:function(sess) {
|
||||
savedSession = sess;
|
||||
return when.resolve();
|
||||
}
|
||||
}).then(function() {
|
||||
Tokens.revoke("1234").then(function() {
|
||||
try {
|
||||
savedSession.should.not.have.a.property("1234");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
230
test/unit/@node-red/editor-api/lib/auth/users_spec.js
Normal file
230
test/unit/@node-red/editor-api/lib/auth/users_spec.js
Normal file
@@ -0,0 +1,230 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require('when');
|
||||
var sinon = require('sinon');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var Users = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/users");
|
||||
|
||||
describe("api/auth/users", function() {
|
||||
after(function() {
|
||||
Users.init({});
|
||||
})
|
||||
describe('Initalised with a credentials object, no anon',function() {
|
||||
before(function() {
|
||||
Users.init({
|
||||
type:"credentials",
|
||||
users:{
|
||||
username:"fred",
|
||||
password:'$2a$08$LpYMefvGZ3MjAfZGzcoyR.1BcfHh4wy4NpbN.cEny5aHnWOqjKOXK',
|
||||
// 'password' -> require('bcryptjs').hashSync('password', 8);
|
||||
permissions:"*"
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('#get',function() {
|
||||
it('returns known user',function(done) {
|
||||
Users.get("fred").then(function(user) {
|
||||
try {
|
||||
user.should.have.a.property("username","fred");
|
||||
user.should.have.a.property("permissions","*");
|
||||
user.should.not.have.a.property("password");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('returns null for unknown user', function(done) {
|
||||
Users.get("barney").then(function(user) {
|
||||
try {
|
||||
should.not.exist(user);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#default',function() {
|
||||
it('returns null for default user', function(done) {
|
||||
Users.default().then(function(user) {
|
||||
try {
|
||||
should.not.exist(user);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#authenticate',function() {
|
||||
it('authenticates a known user', function(done) {
|
||||
Users.authenticate('fred','password').then(function(user) {
|
||||
try {
|
||||
user.should.have.a.property("username","fred");
|
||||
user.should.have.a.property("permissions","*");
|
||||
user.should.not.have.a.property("password");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
it('rejects invalid password for a known user', function(done) {
|
||||
Users.authenticate('fred','wrong').then(function(user) {
|
||||
try {
|
||||
should.not.exist(user);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects invalid user', function(done) {
|
||||
Users.authenticate('barney','wrong').then(function(user) {
|
||||
try {
|
||||
should.not.exist(user);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Initalised with a credentials object including anon',function() {
|
||||
before(function() {
|
||||
Users.init({
|
||||
type:"credentials",
|
||||
users:[],
|
||||
default: { permissions: "*" }
|
||||
});
|
||||
});
|
||||
describe('#default',function() {
|
||||
it('returns default user', function(done) {
|
||||
Users.default().then(function(user) {
|
||||
try {
|
||||
user.should.have.a.property('anonymous',true);
|
||||
user.should.have.a.property('permissions','*');
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Initialised with a credentials object with user functions',function() {
|
||||
var authUsername = '';
|
||||
var authPassword = '';
|
||||
before(function() {
|
||||
Users.init({
|
||||
type:"credentials",
|
||||
users:function(username) {
|
||||
return when.resolve({'username':'dave','permissions':'read'});
|
||||
},
|
||||
authenticate: function(username,password) {
|
||||
authUsername = username;
|
||||
authPassword = password;
|
||||
return when.resolve({'username':'pete','permissions':'write'});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#get',function() {
|
||||
it('delegates get user',function(done) {
|
||||
Users.get('dave').then(function(user) {
|
||||
try {
|
||||
user.should.have.a.property("username","dave");
|
||||
user.should.have.a.property("permissions","read");
|
||||
user.should.not.have.a.property("password");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
it('delegates authenticate user',function(done) {
|
||||
Users.authenticate('pete','secret').then(function(user) {
|
||||
try {
|
||||
user.should.have.a.property("username","pete");
|
||||
user.should.have.a.property("permissions","write");
|
||||
user.should.not.have.a.property("password");
|
||||
authUsername.should.equal('pete');
|
||||
authPassword.should.equal('secret');
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Initialised with bad settings to test else cases',function() {
|
||||
before(function() {
|
||||
Users.init({
|
||||
type:"foo",
|
||||
users:{
|
||||
username:"fred",
|
||||
password:'$2a$08$LpYMefvGZ3MjAfZGzcoyR.1BcfHh4wy4NpbN.cEny5aHnWOqjKOXK',
|
||||
permissions:"*"
|
||||
}
|
||||
});
|
||||
});
|
||||
describe('#get',function() {
|
||||
it('should fail to return user fred',function(done) {
|
||||
Users.get("fred").then(function(userf) {
|
||||
try {
|
||||
should.not.exist(userf);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Initialised with default set as function',function() {
|
||||
before(function() {
|
||||
Users.init({
|
||||
type:"credentials",
|
||||
default: function() { return("Done"); }
|
||||
});
|
||||
});
|
||||
after(function() {
|
||||
Users.init({});
|
||||
});
|
||||
describe('#default',function() {
|
||||
it('handles api.default being a function',function(done) {
|
||||
Users.should.have.property('default').which.is.a.Function();
|
||||
(Users.default()).should.equal("Done");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
496
test/unit/@node-red/editor-api/lib/editor/comms_spec.js
Normal file
496
test/unit/@node-red/editor-api/lib/editor/comms_spec.js
Normal file
@@ -0,0 +1,496 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
const stoppable = require('stoppable');
|
||||
|
||||
var when = require("when");
|
||||
var http = require('http');
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var WebSocket = require('ws');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var comms = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/comms");
|
||||
var Users = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/users");
|
||||
var Tokens = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth/tokens");
|
||||
|
||||
var address = '127.0.0.1';
|
||||
var listenPort = 0; // use ephemeral port
|
||||
|
||||
|
||||
describe("api/editor/comms", function() {
|
||||
var connections = [];
|
||||
var mockComms = {
|
||||
addConnection: function(opts) {
|
||||
connections.push(opts.client);
|
||||
return Promise.resolve()
|
||||
},
|
||||
removeConnection: function(opts) {
|
||||
for (var i=0;i<connections.length;i++) {
|
||||
if (connections[i] === opts.client) {
|
||||
connections.splice(i,1);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return Promise.resolve()
|
||||
},
|
||||
subscribe: function() { return Promise.resolve()},
|
||||
unsubscribe: function() { return Promise.resolve(); }
|
||||
}
|
||||
|
||||
describe("with default keepalive", function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
before(function(done) {
|
||||
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
Users.default.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('accepts connection', function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
connections.length.should.eql(0);
|
||||
ws.on('open', function() {
|
||||
try {
|
||||
connections.length.should.eql(1);
|
||||
ws.close();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('publishes message after subscription', function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
ws.on('open', function() {
|
||||
ws.send('{"subscribe":"topic1"}');
|
||||
connections.length.should.eql(1);
|
||||
connections[0].send('topic1', 'foo');
|
||||
});
|
||||
ws.on('message', function(msg) {
|
||||
msg.should.equal('[{"topic":"topic1","data":"foo"}]');
|
||||
ws.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('malformed messages are ignored',function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
ws.on('open', function() {
|
||||
ws.send('not json');
|
||||
ws.send('[]');
|
||||
ws.send('{"subscribe":"topic3"}');
|
||||
connections[0].send('topic3', 'correct');
|
||||
});
|
||||
ws.on('message', function(msg) {
|
||||
console.log(msg);
|
||||
msg.should.equal('[{"topic":"topic3","data":"correct"}]');
|
||||
ws.close();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("disabled editor", function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
before(function(done) {
|
||||
sinon.stub(Users,"default",function() { return Promise.resolve(null);});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {disableEditor:true}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
Users.default.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('rejects websocket connections',function(done) {
|
||||
connections.length.should.eql(0);
|
||||
var ws = new WebSocket(url);
|
||||
ws.on('open', function() {
|
||||
done(new Error("Socket connection unexpectedly accepted"));
|
||||
ws.close();
|
||||
});
|
||||
ws.on('error', function() {
|
||||
connections.length.should.eql(0);
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("non-default httpAdminRoot set: /adminPath", function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
before(function(done) {
|
||||
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {httpAdminRoot:"/adminPath"}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/adminPath/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
Users.default.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('accepts connections',function(done) {
|
||||
connections.length.should.eql(0);
|
||||
var ws = new WebSocket(url);
|
||||
ws.on('open', function() {
|
||||
connections.length.should.eql(1);
|
||||
ws.close();
|
||||
done();
|
||||
});
|
||||
ws.on('error', function() {
|
||||
done(new Error("Socket connection failed"));
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("non-default httpAdminRoot set: /adminPath/", function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
before(function(done) {
|
||||
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {httpAdminRoot:"/adminPath/"}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/adminPath/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
Users.default.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('accepts connections',function(done) {
|
||||
connections.length.should.eql(0);
|
||||
var ws = new WebSocket(url);
|
||||
ws.on('open', function() {
|
||||
connections.length.should.eql(1);
|
||||
ws.close();
|
||||
done();
|
||||
});
|
||||
ws.on('error', function() {
|
||||
done(new Error("Socket connection failed"));
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("non-default httpAdminRoot set: adminPath", function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
before(function(done) {
|
||||
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {httpAdminRoot:"adminPath"}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/adminPath/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
Users.default.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('accepts connections',function(done) {
|
||||
connections.length.should.eql(0);
|
||||
var ws = new WebSocket(url);
|
||||
ws.on('open', function() {
|
||||
connections.length.should.eql(1);
|
||||
ws.close();
|
||||
done();
|
||||
});
|
||||
ws.on('error', function() {
|
||||
done(new Error("Socket connection failed"));
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("keep alives", function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
before(function(done) {
|
||||
sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {webSocketKeepAliveTime: 100}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
after(function(done) {
|
||||
Users.default.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
it('are sent', function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var count = 0;
|
||||
ws.on('message', function(data) {
|
||||
var msg = JSON.parse(data)[0];
|
||||
msg.should.have.property('topic','hb');
|
||||
msg.should.have.property('data').be.a.Number();
|
||||
count++;
|
||||
if (count == 3) {
|
||||
ws.close();
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
it('are not sent if other messages are sent', function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var count = 0;
|
||||
var interval;
|
||||
ws.on('open', function() {
|
||||
ws.send('{"subscribe":"foo"}');
|
||||
interval = setInterval(function() {
|
||||
connections[0].send('foo', 'bar');
|
||||
}, 50);
|
||||
});
|
||||
ws.on('message', function(data) {
|
||||
var msg = JSON.parse(data)[0];
|
||||
// It is possible a heartbeat message may arrive - so ignore them
|
||||
if (msg.topic != "hb") {
|
||||
msg.should.have.property('topic', 'foo');
|
||||
msg.should.have.property('data', 'bar');
|
||||
count++;
|
||||
if (count == 5) {
|
||||
clearInterval(interval);
|
||||
ws.close();
|
||||
done();
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('authentication required, no anonymous',function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
var getDefaultUser;
|
||||
var getUser;
|
||||
var getToken;
|
||||
before(function(done) {
|
||||
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve(null);});
|
||||
getUser = sinon.stub(Users,"get", function(username) {
|
||||
if (username == "fred") {
|
||||
return when.resolve({permissions:"read"});
|
||||
} else {
|
||||
return when.resolve(null);
|
||||
}
|
||||
});
|
||||
getToken = sinon.stub(Tokens,"get",function(token) {
|
||||
if (token == "1234") {
|
||||
return when.resolve({user:"fred",scope:["*"]});
|
||||
} else if (token == "5678") {
|
||||
return when.resolve({user:"barney",scope:["*"]});
|
||||
} else {
|
||||
return when.resolve(null);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {adminAuth:{}}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
after(function(done) {
|
||||
getDefaultUser.restore();
|
||||
getUser.restore();
|
||||
getToken.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('prevents connections that do not authenticate',function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var count = 0;
|
||||
var interval;
|
||||
ws.on('open', function() {
|
||||
ws.send('{"subscribe":"foo"}');
|
||||
});
|
||||
ws.on('close', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('allows connections that do authenticate',function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var received = 0;
|
||||
ws.on('open', function() {
|
||||
ws.send('{"auth":"1234"}');
|
||||
});
|
||||
ws.on('message', function(msg) {
|
||||
received++;
|
||||
if (received == 1) {
|
||||
msg.should.equal('{"auth":"ok"}');
|
||||
ws.send('{"subscribe":"foo"}');
|
||||
connections[0].send('foo', 'correct');
|
||||
} else {
|
||||
msg.should.equal('[{"topic":"foo","data":"correct"}]');
|
||||
ws.close();
|
||||
}
|
||||
});
|
||||
|
||||
ws.on('close', function() {
|
||||
try {
|
||||
received.should.equal(2);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('rejects connections for non-existant token',function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var received = 0;
|
||||
ws.on('open', function() {
|
||||
ws.send('{"auth":"2345"}');
|
||||
});
|
||||
ws.on('close', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('rejects connections for invalid token',function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var received = 0;
|
||||
ws.on('open', function() {
|
||||
ws.send('{"auth":"5678"}');
|
||||
});
|
||||
ws.on('close', function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('authentication required, anonymous enabled',function() {
|
||||
var server;
|
||||
var url;
|
||||
var port;
|
||||
var getDefaultUser;
|
||||
before(function(done) {
|
||||
getDefaultUser = sinon.stub(Users,"default",function() { return when.resolve({permissions:"read"});});
|
||||
server = stoppable(http.createServer(function(req,res){app(req,res)}));
|
||||
comms.init(server, {adminAuth:{}}, {comms: mockComms});
|
||||
server.listen(listenPort, address);
|
||||
server.on('listening', function() {
|
||||
port = server.address().port;
|
||||
url = 'http://' + address + ':' + port + '/comms';
|
||||
comms.start();
|
||||
done();
|
||||
});
|
||||
});
|
||||
after(function(done) {
|
||||
getDefaultUser.restore();
|
||||
comms.stop();
|
||||
server.stop(done);
|
||||
});
|
||||
|
||||
it('allows anonymous connections that do not authenticate',function(done) {
|
||||
var ws = new WebSocket(url);
|
||||
var count = 0;
|
||||
var interval;
|
||||
ws.on('open', function() {
|
||||
ws.send('{"subscribe":"foo"}');
|
||||
setTimeout(function() {
|
||||
connections[0].send('foo', 'correct');
|
||||
},200);
|
||||
});
|
||||
ws.on('message', function(msg) {
|
||||
msg.should.equal('[{"topic":"foo","data":"correct"}]');
|
||||
count++;
|
||||
ws.close();
|
||||
});
|
||||
ws.on('close', function() {
|
||||
try {
|
||||
count.should.equal(1);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var sinon = require('sinon');
|
||||
var when = require('when');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var credentials = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/credentials");
|
||||
|
||||
describe('api/editor/credentials', function() {
|
||||
var app;
|
||||
|
||||
before(function() {
|
||||
app = express();
|
||||
app.get('/credentials/:type/:id',credentials.get);
|
||||
credentials.init({
|
||||
flows: {
|
||||
getNodeCredentials: function(opts) {
|
||||
if (opts.type === "known-type" && opts.id === "n1") {
|
||||
return Promise.resolve({
|
||||
user1:"abc",
|
||||
has_password1: true
|
||||
});
|
||||
} else {
|
||||
var err = new Error("message");
|
||||
err.code = "test_code";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('returns stored credentials',function(done) {
|
||||
request(app)
|
||||
.get("/credentials/known-type/n1")
|
||||
.expect("Content-Type",/json/)
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
try {
|
||||
res.body.should.have.a.property("user1","abc");
|
||||
res.body.should.not.have.a.property("password1");
|
||||
res.body.should.have.a.property("has_password1",true);
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
it('returns any error',function(done) {
|
||||
request(app)
|
||||
.get("/credentials/unknown-type/n2")
|
||||
.expect("Content-Type",/json/)
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
try {
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("test_code");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal('message');
|
||||
done();
|
||||
} catch(e) {
|
||||
done(e);
|
||||
}
|
||||
}
|
||||
})
|
||||
});
|
||||
|
||||
});
|
||||
136
test/unit/@node-red/editor-api/lib/editor/index_spec.js
Normal file
136
test/unit/@node-red/editor-api/lib/editor/index_spec.js
Normal file
@@ -0,0 +1,136 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require("supertest");
|
||||
var express = require("express");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var editorApi = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor");
|
||||
var comms = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/comms");
|
||||
var info = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/settings");
|
||||
var auth = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth");
|
||||
|
||||
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
|
||||
|
||||
var when = require("when");
|
||||
|
||||
|
||||
describe("api/editor/index", function() {
|
||||
var app;
|
||||
describe("disabled the editor", function() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(comms,'init', function(){});
|
||||
sinon.stub(info,'init', function(){});
|
||||
});
|
||||
afterEach(function() {
|
||||
comms.init.restore();
|
||||
info.init.restore();
|
||||
});
|
||||
it("disables the editor", function() {
|
||||
var editorApp = editorApi.init({},{disableEditor:true},{});
|
||||
should.not.exist(editorApp);
|
||||
comms.init.called.should.be.false();
|
||||
info.init.called.should.be.false();
|
||||
});
|
||||
});
|
||||
describe("enables the editor", function() {
|
||||
var mockList = [
|
||||
'library','theme','locales','credentials','comms',"settings"
|
||||
]
|
||||
var isStarted = true;
|
||||
var errors = [];
|
||||
var session_data = {};
|
||||
before(function() {
|
||||
sinon.stub(auth,'needsPermission',function(permission) {
|
||||
return function(req,res,next) { next(); }
|
||||
});
|
||||
mockList.forEach(function(m) {
|
||||
sinon.stub(NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/"+m),"init",function(){});
|
||||
});
|
||||
sinon.stub(NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/theme"),"app",function(){ return express()});
|
||||
});
|
||||
after(function() {
|
||||
mockList.forEach(function(m) {
|
||||
NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/"+m).init.restore();
|
||||
})
|
||||
NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/theme").app.restore();
|
||||
auth.needsPermission.restore();
|
||||
log.error.restore();
|
||||
});
|
||||
|
||||
before(function() {
|
||||
sinon.stub(log,"error",function(err) { errors.push(err)})
|
||||
app = editorApi.init({},{httpNodeRoot:true, httpAdminRoot: true,disableEditor:false,exportNodeSettings:function(){}},{
|
||||
isStarted: () => Promise.resolve(isStarted)
|
||||
});
|
||||
});
|
||||
it('serves the editor', function(done) {
|
||||
request(app)
|
||||
.get("/")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
// Index page should probably mention Node-RED somewhere
|
||||
res.text.indexOf("Node-RED").should.not.eql(-1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('serves icons', function(done) {
|
||||
request(app)
|
||||
.get("/red/images/icons/node-changed.png")
|
||||
.expect(200)
|
||||
.expect("Content-Type", /image\/png/)
|
||||
.end(function(err,res) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
it('handles page not there', function(done) {
|
||||
request(app)
|
||||
.get("/foo")
|
||||
.expect(404,done)
|
||||
});
|
||||
it('warns if runtime not started', function(done) {
|
||||
isStarted = false;
|
||||
request(app)
|
||||
.get("/")
|
||||
.expect(503)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.text.should.eql("Not started");
|
||||
errors.should.have.lengthOf(1);
|
||||
errors[0].should.eql("Node-RED runtime not started");
|
||||
done();
|
||||
});
|
||||
});
|
||||
// it.skip('GET /settings', function(done) {
|
||||
// request(app).get("/settings").expect(200).end(function(err,res) {
|
||||
// if (err) {
|
||||
// return done(err);
|
||||
// }
|
||||
// // permissionChecks.should.have.property('settings.read',1);
|
||||
// done();
|
||||
// })
|
||||
// });
|
||||
});
|
||||
});
|
||||
304
test/unit/@node-red/editor-api/lib/editor/library_spec.js
Normal file
304
test/unit/@node-red/editor-api/lib/editor/library_spec.js
Normal file
@@ -0,0 +1,304 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var library = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/library");
|
||||
|
||||
var app;
|
||||
|
||||
describe("api/editor/library", function() {
|
||||
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
|
||||
app.get("/library/flows",library.getAll);
|
||||
app.post(/library\/([^\/]+)\/(.*)/,library.saveEntry);
|
||||
app.get(/library\/([^\/]+)(?:$|\/(.*))/,library.getEntry);
|
||||
});
|
||||
after(function() {
|
||||
});
|
||||
it('returns all flows', function(done) {
|
||||
library.init({
|
||||
library: {
|
||||
getEntries: function(opts) {
|
||||
return Promise.resolve({a:1,b:2});
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('a',1);
|
||||
res.body.should.have.property('b',2);
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('returns an error on all flows', function(done) {
|
||||
library.init({
|
||||
library: {
|
||||
getEntries: function(opts) {
|
||||
var err = new Error("message");
|
||||
err.code = "random_error";
|
||||
err.status = 400;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows')
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("random_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal("message");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns an individual entry - flow type', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
getEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve('{"a":1,"b":2}');
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows/abc')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('a',1);
|
||||
res.body.should.have.property('b',2);
|
||||
opts.should.have.property('type','flows');
|
||||
opts.should.have.property('path','abc');
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('returns a directory listing - flow type', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
getEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve({"a":1,"b":2});
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows/abc/def')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('a',1);
|
||||
res.body.should.have.property('b',2);
|
||||
opts.should.have.property('type','flows');
|
||||
opts.should.have.property('path','abc/def');
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('returns an individual entry - non-flow type', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
getEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve('{"a":1,"b":2}');
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/non-flow/abc')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
opts.should.have.property('type','non-flow');
|
||||
opts.should.have.property('path','abc');
|
||||
res.text.should.eql('{"a":1,"b":2}');
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('returns a directory listing - non-flow type', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
getEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve({"a":1,"b":2});
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/non-flow/abc/def')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('a',1);
|
||||
res.body.should.have.property('b',2);
|
||||
opts.should.have.property('type','non-flow');
|
||||
opts.should.have.property('path','abc/def');
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
it('returns an error on individual get', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
getEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
var err = new Error("message");
|
||||
err.code = "random_error";
|
||||
err.status = 400;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows/123')
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
opts.should.have.property('type','flows');
|
||||
opts.should.have.property('path','123');
|
||||
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("random_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal("message");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('saves an individual entry - flow type', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
saveEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/library/flows/abc/def')
|
||||
.expect(204)
|
||||
.send({a:1,b:2,c:3})
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
opts.should.have.property('type','flows');
|
||||
opts.should.have.property('path','abc/def');
|
||||
opts.should.have.property('meta',{});
|
||||
opts.should.have.property('body',JSON.stringify({a:1,b:2,c:3}));
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
it('saves an individual entry - non-flow type', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
saveEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/library/non-flow/abc/def')
|
||||
.expect(204)
|
||||
.send({a:1,b:2,text:"123"})
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
opts.should.have.property('type','non-flow');
|
||||
opts.should.have.property('path','abc/def');
|
||||
opts.should.have.property('meta',{a:1,b:2});
|
||||
opts.should.have.property('body',"123");
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
it('returns an error on individual save', function(done) {
|
||||
var opts;
|
||||
library.init({
|
||||
library: {
|
||||
saveEntry: function(_opts) {
|
||||
opts = _opts;
|
||||
var err = new Error("message");
|
||||
err.code = "random_error";
|
||||
err.status = 400;
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post('/library/non-flow/abc/def')
|
||||
.send({a:1,b:2,text:"123"})
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
opts.should.have.property('type','non-flow');
|
||||
opts.should.have.property('path','abc/def');
|
||||
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("random_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal("message");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
165
test/unit/@node-red/editor-api/lib/editor/locales_spec.js
Normal file
165
test/unit/@node-red/editor-api/lib/editor/locales_spec.js
Normal file
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var sinon = require('sinon');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var locales = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/locales");
|
||||
var i18n = NR_TEST_UTILS.require("@node-red/util").i18n;
|
||||
|
||||
describe("api/editor/locales", function() {
|
||||
beforeEach(function() {
|
||||
})
|
||||
afterEach(function() {
|
||||
})
|
||||
describe('get named resource catalog',function() {
|
||||
var app;
|
||||
before(function() {
|
||||
// locales.init({
|
||||
// i18n: {
|
||||
// i: {
|
||||
// language: function() { return 'en-US'},
|
||||
// changeLanguage: function(lang,callback) {
|
||||
// if (callback) {
|
||||
// callback();
|
||||
// }
|
||||
// },
|
||||
// getResourceBundle: function(lang, namespace) {
|
||||
// return {namespace:namespace, lang:lang};
|
||||
// }
|
||||
// },
|
||||
// }
|
||||
// });
|
||||
locales.init({});
|
||||
|
||||
// bit of a mess of internal workings
|
||||
sinon.stub(i18n.i,'changeLanguage',function(lang,callback) { if (callback) {callback();}});
|
||||
if (i18n.i.getResourceBundle) {
|
||||
sinon.stub(i18n.i,'getResourceBundle',function(lang, namespace) {return {namespace:namespace, lang:lang};});
|
||||
} else {
|
||||
// If i18n.init has not been called, then getResourceBundle isn't
|
||||
// defined - so hardcode a stub
|
||||
i18n.i.getResourceBundle = function(lang, namespace) {return {namespace:namespace, lang:lang};};
|
||||
i18n.i.getResourceBundle.restore = function() { delete i18n.i.getResourceBundle };
|
||||
}
|
||||
app = express();
|
||||
app.get(/locales\/(.+)\/?$/,locales.get);
|
||||
});
|
||||
after(function() {
|
||||
i18n.i.changeLanguage.restore();
|
||||
i18n.i.getResourceBundle.restore();
|
||||
})
|
||||
it('returns with default language', function(done) {
|
||||
request(app)
|
||||
.get("/locales/message-catalog")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('namespace','message-catalog');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns with selected language', function(done) {
|
||||
request(app)
|
||||
.get("/locales/message-catalog?lng=fr-FR")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('namespace','message-catalog');
|
||||
res.body.should.have.property('lang','fr-FR');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns for locale defined only with primary tag ', function(done) {
|
||||
var orig = i18n.i.getResourceBundle;
|
||||
i18n.i.getResourceBundle = function (lang, ns) {
|
||||
if (lang === "ja-JP") {
|
||||
return undefined;
|
||||
}
|
||||
return orig(lang, ns);
|
||||
};
|
||||
request(app)
|
||||
// returns `ja` instead of `ja-JP`
|
||||
.get("/locales/message-catalog?lng=ja-JP")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
i18n.i.getResourceBundle = orig;
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('namespace','message-catalog');
|
||||
res.body.should.have.property('lang','ja');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
// describe('get all node resource catalogs',function() {
|
||||
// var app;
|
||||
// before(function() {
|
||||
// // bit of a mess of internal workings
|
||||
// sinon.stub(i18n,'catalog',function(namespace, lang) {
|
||||
// return {
|
||||
// "node-red": "should not return",
|
||||
// "test-module-a-id": "test-module-a-catalog",
|
||||
// "test-module-b-id": "test-module-b-catalog",
|
||||
// "test-module-c-id": "test-module-c-catalog"
|
||||
// }[namespace]
|
||||
// });
|
||||
// locales.init({
|
||||
// nodes: {
|
||||
// getNodeList: function(opts) {
|
||||
// return Promise.resolve([
|
||||
// {module:"node-red",id:"node-red-id"},
|
||||
// {module:"test-module-a",id:"test-module-a-id"},
|
||||
// {module:"test-module-b",id:"test-module-b-id"}
|
||||
// ]);
|
||||
// }
|
||||
// }
|
||||
// });
|
||||
// app = express();
|
||||
// app.get("/locales/nodes",locales.getAllNodes);
|
||||
// });
|
||||
// after(function() {
|
||||
// i18n.catalog.restore();
|
||||
// })
|
||||
// it('returns with the node catalogs', function(done) {
|
||||
// request(app)
|
||||
// .get("/locales/nodes")
|
||||
// .expect(200)
|
||||
// .end(function(err,res) {
|
||||
// if (err) {
|
||||
// return done(err);
|
||||
// }
|
||||
// res.body.should.eql({
|
||||
// 'test-module-a-id': 'test-module-a-catalog',
|
||||
// 'test-module-b-id': 'test-module-b-catalog'
|
||||
// });
|
||||
// done();
|
||||
// });
|
||||
// });
|
||||
// });
|
||||
});
|
||||
21
test/unit/@node-red/editor-api/lib/editor/projects_spec.js
Normal file
21
test/unit/@node-red/editor-api/lib/editor/projects_spec.js
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("api/editor/projects", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
});
|
||||
122
test/unit/@node-red/editor-api/lib/editor/settings_spec.js
Normal file
122
test/unit/@node-red/editor-api/lib/editor/settings_spec.js
Normal file
@@ -0,0 +1,122 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require("body-parser");
|
||||
var sinon = require('sinon');
|
||||
|
||||
var app;
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var info = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/settings");
|
||||
var theme = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/theme");
|
||||
|
||||
describe("api/editor/settings", function() {
|
||||
before(function() {
|
||||
sinon.stub(theme,"settings",function() { return { test: 456 };});
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.get("/settings",info.runtimeSettings);
|
||||
app.get("/settings/user",function(req,res,next) {req.user = "fred"; next()}, info.userSettings);
|
||||
app.post("/settings/user",function(req,res,next) {req.user = "fred"; next()},info.updateUserSettings);
|
||||
});
|
||||
|
||||
after(function() {
|
||||
theme.settings.restore();
|
||||
});
|
||||
|
||||
it('returns the runtime settings', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
getRuntimeSettings: function(opts) {
|
||||
return Promise.resolve({
|
||||
a:1,
|
||||
b:2
|
||||
})
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get("/settings")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("a",1);
|
||||
res.body.should.have.property("b",2);
|
||||
res.body.should.have.property("editorTheme",{test:456});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns the user settings', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
getUserSettings: function(opts) {
|
||||
if (opts.user !== "fred") {
|
||||
return Promise.reject(new Error("Unknown user"));
|
||||
}
|
||||
return Promise.resolve({
|
||||
c:3,
|
||||
d:4
|
||||
})
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get("/settings/user")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.eql({c:3,d:4});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('updates the user settings', function(done) {
|
||||
var update;
|
||||
info.init({
|
||||
settings: {
|
||||
updateUserSettings: function(opts) {
|
||||
if (opts.user !== "fred") {
|
||||
return Promise.reject(new Error("Unknown user"));
|
||||
}
|
||||
update = opts.settings;
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.post("/settings/user")
|
||||
.send({
|
||||
e:4,
|
||||
f:5
|
||||
})
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
update.should.eql({e:4,f:5});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
333
test/unit/@node-red/editor-api/lib/editor/sshkeys_spec.js
Normal file
333
test/unit/@node-red/editor-api/lib/editor/sshkeys_spec.js
Normal file
@@ -0,0 +1,333 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require("supertest");
|
||||
var express = require("express");
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var sshkeys = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/sshkeys");
|
||||
var bodyParser = require("body-parser");
|
||||
|
||||
|
||||
describe("api/editor/sshkeys", function() {
|
||||
var app;
|
||||
var mockRuntime = {
|
||||
settings: {
|
||||
getUserKeys: function() {},
|
||||
getUserKey: function() {},
|
||||
generateUserKey: function() {},
|
||||
removeUserKey: function() {}
|
||||
}
|
||||
}
|
||||
before(function() {
|
||||
sshkeys.init(mockRuntime);
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use("/settings/user/keys", sshkeys.app());
|
||||
});
|
||||
|
||||
beforeEach(function() {
|
||||
sinon.stub(mockRuntime.settings, "getUserKeys");
|
||||
sinon.stub(mockRuntime.settings, "getUserKey");
|
||||
sinon.stub(mockRuntime.settings, "generateUserKey");
|
||||
sinon.stub(mockRuntime.settings, "removeUserKey");
|
||||
})
|
||||
afterEach(function() {
|
||||
mockRuntime.settings.getUserKeys.restore();
|
||||
mockRuntime.settings.getUserKey.restore();
|
||||
mockRuntime.settings.generateUserKey.restore();
|
||||
mockRuntime.settings.removeUserKey.restore();
|
||||
})
|
||||
|
||||
it('GET /settings/user/keys --- return empty list', function(done) {
|
||||
mockRuntime.settings.getUserKeys.returns(Promise.resolve([]));
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('keys');
|
||||
res.body.keys.should.be.empty();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys --- return normal list', function(done) {
|
||||
var fileList = [
|
||||
'test_key01',
|
||||
'test_key02'
|
||||
];
|
||||
var retList = fileList.map(function(elem) {
|
||||
return {
|
||||
name: elem
|
||||
};
|
||||
});
|
||||
mockRuntime.settings.getUserKeys.returns(Promise.resolve(retList));
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('keys');
|
||||
for (var item of retList) {
|
||||
res.body.keys.should.containEql(item);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys --- return Error', function(done) {
|
||||
var errInstance = new Error("Messages here.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.getUserKeys.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal(errInstance.code);
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return 404', function(done) {
|
||||
var errInstance = new Error("Not Found.");
|
||||
errInstance.code = "not_found";
|
||||
errInstance.status = 404;
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.getUserKey.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys/NOT_REAL")
|
||||
.expect(404)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('GET /settings/user/keys --- return Unexpected Error', function(done) {
|
||||
var errInstance = new Error();
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.getUserKeys.returns(p)
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.toString());
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return content', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var fileContent = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQD3a+sgtgzSbbliWxmOq5p6+H/mE+0gjWfLWrkIVmHENd1mifV4uCmIHAR2NfuadUYMQ3+bQ90kpmmEKTMYPsyentsKpHQZxTzG7wOCAIpJnbPTHDMxEJhVTaAwEjbVyMSIzTTPfnhoavWIBu0+uMgKDDlBm+RjlgkFlyhXyCN6UwFrIUUMH6Gw+eQHLiooKIl8ce7uDxIlt+9b7hFCU+sQ3kvuse239DZluu6+8buMWqJvrEHgzS9adRFKku8nSPAEPYn85vDi7OgVAcLQufknNgs47KHBAx9h04LeSrFJ/P5J1b//ItRpMOIme+O9d1BR46puzhvUaCHLdvO9czj+OmW+dIm+QIk6lZIOOMnppG72kZxtLfeKT16ur+2FbwAdL9ItBp4BI/YTlBPoa5mLMxpuWfmX1qHntvtGc9wEwS1P7YFfmF3XiK5apxalzrn0Qlr5UmDNbVIqJb1OlbC0w03Z0oktti1xT+R2DGOLWM4lBbpXDHV1BhQ7oYOvbUD8Cnof55lTP0WHHsOHlQc/BGDti1XA9aBX/OzVyzBUYEf0pkimsD0RYo6aqt7QwehJYdlz9x1NBguBffT0s4NhNb9IWr+ASnFPvNl2sw4XH/8U0J0q8ZkMpKkbLM1Zdp1Fv00GF0f5UNRokai6uM3w/ccantJ3WvZ6GtctqytWrw== \n";
|
||||
mockRuntime.settings.getUserKey.returns(Promise.resolve(fileContent));
|
||||
request(app)
|
||||
.get("/settings/user/keys/" + key_file_name)
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
mockRuntime.settings.getUserKey.called.should.be.true();
|
||||
mockRuntime.settings.getUserKey.firstCall.args[0].should.eql({ user: undefined, id: 'test_key' });
|
||||
res.body.should.be.deepEqual({ publickey: fileContent });
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.getUserKey.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys/" + key_file_name)
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal(errInstance.code);
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return Unexpected Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.getUserKey.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys/" + key_file_name)
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal("Messages.....");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- success', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
mockRuntime.settings.generateUserKey.returns(Promise.resolve(key_file_name));
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.send({ name: key_file_name })
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- return Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.generateUserKey.returns(p);
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.send({ name: key_file_name })
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("test_code");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- return Unexpected error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.generateUserKey.returns(p);
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.send({ name: key_file_name })
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal("Messages.....");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /settings/user/keys/<key_file_name> --- success', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
mockRuntime.settings.removeUserKey.returns(Promise.resolve(true));
|
||||
request(app)
|
||||
.delete("/settings/user/keys/" + key_file_name)
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.be.deepEqual({});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /settings/user/keys/<key_file_name> --- return Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.removeUserKey.returns(p);
|
||||
request(app)
|
||||
.delete("/settings/user/keys/" + key_file_name)
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("test_code");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /settings/user/keys/<key_file_name> --- return Unexpected Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.settings.removeUserKey.returns(p);
|
||||
request(app)
|
||||
.delete("/settings/user/keys/" + key_file_name)
|
||||
.expect(500)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('code');
|
||||
res.body.code.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal('Messages.....');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
146
test/unit/@node-red/editor-api/lib/editor/theme_spec.js
Normal file
146
test/unit/@node-red/editor-api/lib/editor/theme_spec.js
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var express = require('express');
|
||||
var sinon = require('sinon');
|
||||
var when = require('when');
|
||||
var fs = require("fs");
|
||||
|
||||
var app = express();
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var theme = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/theme");
|
||||
|
||||
describe("api/editor/theme", function () {
|
||||
beforeEach(function () {
|
||||
sinon.stub(fs, "statSync", function () { return true; });
|
||||
});
|
||||
afterEach(function () {
|
||||
theme.init({settings: {}});
|
||||
fs.statSync.restore();
|
||||
});
|
||||
it("applies the default theme", function () {
|
||||
var result = theme.init({});
|
||||
should.not.exist(result);
|
||||
|
||||
var context = theme.context();
|
||||
context.should.have.a.property("page");
|
||||
context.page.should.have.a.property("title", "Node-RED");
|
||||
context.page.should.have.a.property("favicon", "favicon.ico");
|
||||
context.page.should.have.a.property("tabicon", "red/images/node-red-icon-black.svg");
|
||||
context.should.have.a.property("header");
|
||||
context.header.should.have.a.property("title", "Node-RED");
|
||||
context.header.should.have.a.property("image", "red/images/node-red.png");
|
||||
context.should.have.a.property("asset");
|
||||
context.asset.should.have.a.property("red", "red/red.min.js");
|
||||
context.asset.should.have.a.property("main", "red/main.min.js");
|
||||
|
||||
should.not.exist(theme.settings());
|
||||
});
|
||||
|
||||
it("picks up custom theme", function () {
|
||||
theme.init({
|
||||
editorTheme: {
|
||||
page: {
|
||||
title: "Test Page Title",
|
||||
favicon: "/absolute/path/to/theme/favicon",
|
||||
tabicon: "/absolute/path/to/theme/tabicon",
|
||||
css: "/absolute/path/to/custom/css/file.css",
|
||||
scripts: "/absolute/path/to/script.js"
|
||||
},
|
||||
header: {
|
||||
title: "Test Header Title",
|
||||
url: "http://nodered.org",
|
||||
image: "/absolute/path/to/header/image" // or null to remove image
|
||||
},
|
||||
|
||||
deployButton: {
|
||||
type: "simple",
|
||||
label: "Save",
|
||||
icon: "/absolute/path/to/deploy/button/image" // or null to remove image
|
||||
},
|
||||
|
||||
menu: { // Hide unwanted menu items by id. see editor/js/main.js:loadEditor for complete list
|
||||
"menu-item-import-library": false,
|
||||
"menu-item-export-library": false,
|
||||
"menu-item-keyboard-shortcuts": false,
|
||||
"menu-item-help": {
|
||||
label: "Alternative Help Link Text",
|
||||
url: "http://example.com"
|
||||
}
|
||||
},
|
||||
|
||||
userMenu: false, // Hide the user-menu even if adminAuth is enabled
|
||||
|
||||
login: {
|
||||
image: "/absolute/path/to/login/page/big/image" // a 256x256 image
|
||||
},
|
||||
|
||||
palette: {
|
||||
editable: true,
|
||||
catalogues: ['https://catalogue.nodered.org/catalogue.json'],
|
||||
theme: [{ category: ".*", type: ".*", color: "#f0f" }]
|
||||
},
|
||||
|
||||
projects: {
|
||||
enabled: false
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
theme.app();
|
||||
|
||||
var context = theme.context();
|
||||
context.should.have.a.property("page");
|
||||
context.page.should.have.a.property("title", "Test Page Title");
|
||||
context.page.should.have.a.property("favicon", "theme/favicon/favicon");
|
||||
context.page.should.have.a.property("tabicon", "theme/tabicon/tabicon");
|
||||
context.should.have.a.property("header");
|
||||
context.header.should.have.a.property("title", "Test Header Title");
|
||||
context.header.should.have.a.property("url", "http://nodered.org");
|
||||
context.header.should.have.a.property("image", "theme/header/image");
|
||||
context.page.should.have.a.property("css");
|
||||
context.page.css.should.have.lengthOf(1);
|
||||
context.page.css[0].should.eql('theme/css/file.css');
|
||||
context.page.should.have.a.property("scripts");
|
||||
context.page.scripts.should.have.lengthOf(1);
|
||||
context.page.scripts[0].should.eql('theme/scripts/script.js');
|
||||
context.should.have.a.property("login");
|
||||
context.login.should.have.a.property("image", "theme/login/image");
|
||||
|
||||
var settings = theme.settings();
|
||||
settings.should.have.a.property("deployButton");
|
||||
settings.deployButton.should.have.a.property("type", "simple");
|
||||
settings.deployButton.should.have.a.property("label", "Save");
|
||||
settings.deployButton.should.have.a.property("icon", "theme/deploy/image");
|
||||
settings.should.have.a.property("userMenu");
|
||||
settings.userMenu.should.be.eql(false);
|
||||
settings.should.have.a.property("menu");
|
||||
settings.menu.should.have.a.property("menu-item-import-library", false);
|
||||
settings.menu.should.have.a.property("menu-item-export-library", false);
|
||||
settings.menu.should.have.a.property("menu-item-keyboard-shortcuts", false);
|
||||
settings.menu.should.have.a.property("menu-item-help", { label: "Alternative Help Link Text", url: "http://example.com" });
|
||||
settings.should.have.a.property("palette");
|
||||
settings.palette.should.have.a.property("editable", true);
|
||||
settings.palette.should.have.a.property("catalogues", ['https://catalogue.nodered.org/catalogue.json']);
|
||||
settings.palette.should.have.a.property("theme", [{ category: ".*", type: ".*", color: "#f0f" }]);
|
||||
settings.should.have.a.property("projects");
|
||||
settings.projects.should.have.a.property("enabled", false);
|
||||
});
|
||||
|
||||
});
|
||||
152
test/unit/@node-red/editor-api/lib/editor/ui_spec.js
Normal file
152
test/unit/@node-red/editor-api/lib/editor/ui_spec.js
Normal file
@@ -0,0 +1,152 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var request = require("supertest");
|
||||
var express = require("express");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var ui = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor/ui");
|
||||
|
||||
|
||||
describe("api/editor/ui", function() {
|
||||
var app;
|
||||
|
||||
before(function() {
|
||||
ui.init({
|
||||
nodes: {
|
||||
getIcon: function(opts) {
|
||||
return new Promise(function(resolve,reject) {
|
||||
fs.readFile(NR_TEST_UTILS.resolve("@node-red/editor-client/src/images/icons/arrow-in.png"), function(err,data) {
|
||||
resolve(data);
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
describe("slash handler", function() {
|
||||
before(function() {
|
||||
app = express();
|
||||
app.get("/foo",ui.ensureSlash,function(req,res) { res.sendStatus(200);});
|
||||
});
|
||||
it('redirects if the path does not end in a slash',function(done) {
|
||||
request(app)
|
||||
.get('/foo')
|
||||
.expect(301,done);
|
||||
});
|
||||
it('redirects if the path, with query string, does not end in a slash',function(done) {
|
||||
request(app)
|
||||
.get('/foo?abc=def')
|
||||
.expect(301)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.header['location'].should.equal("/foo/?abc=def");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('does not redirect if the path ends in a slash',function(done) {
|
||||
request(app)
|
||||
.get('/foo/')
|
||||
.expect(200,done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("icon handler", function() {
|
||||
before(function() {
|
||||
app = express();
|
||||
app.get("/icons/:module/:icon",ui.icon);
|
||||
});
|
||||
|
||||
function binaryParser(res, callback) {
|
||||
res.setEncoding('binary');
|
||||
res.data = '';
|
||||
res.on('data', function (chunk) {
|
||||
res.data += chunk;
|
||||
});
|
||||
res.on('end', function () {
|
||||
callback(null, new Buffer(res.data, 'binary'));
|
||||
});
|
||||
}
|
||||
function compareBuffers(b1,b2) {
|
||||
b1.length.should.equal(b2.length);
|
||||
for (var i=0;i<b1.length;i++) {
|
||||
b1[i].should.equal(b2[i]);
|
||||
}
|
||||
}
|
||||
it('returns the requested icon', function(done) {
|
||||
var defaultIcon = fs.readFileSync(NR_TEST_UTILS.resolve("@node-red/editor-client/src/images/icons/arrow-in.png"));
|
||||
request(app)
|
||||
.get("/icons/module/icon.png")
|
||||
.expect("Content-Type", /image\/png/)
|
||||
.expect(200)
|
||||
.parse(binaryParser)
|
||||
.end(function(err,res) {
|
||||
if (err){
|
||||
return done(err);
|
||||
}
|
||||
Buffer.isBuffer(res.body).should.be.true();
|
||||
compareBuffers(res.body,defaultIcon);
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe("editor ui handler", function() {
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use("/",ui.editor);
|
||||
});
|
||||
it('serves the editor', function(done) {
|
||||
request(app)
|
||||
.get("/")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
// Index page should probably mention Node-RED somewhere
|
||||
res.text.indexOf("Node-RED").should.not.eql(-1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("editor ui resource handler", function() {
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use("/",ui.editorResources);
|
||||
});
|
||||
it('serves the editor resources', function(done) {
|
||||
request(app)
|
||||
.get("/favicon.ico")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
99
test/unit/@node-red/editor-api/lib/index_spec.js
Normal file
99
test/unit/@node-red/editor-api/lib/index_spec.js
Normal file
@@ -0,0 +1,99 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require("supertest");
|
||||
var express = require("express");
|
||||
var when = require("when");
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var api = NR_TEST_UTILS.require("@node-red/editor-api");
|
||||
|
||||
var apiAuth = NR_TEST_UTILS.require("@node-red/editor-api/lib/auth");
|
||||
var apiEditor = NR_TEST_UTILS.require("@node-red/editor-api/lib/editor");
|
||||
var apiAdmin = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin");
|
||||
|
||||
|
||||
describe("api/index", function() {
|
||||
var beforeEach = function() {
|
||||
sinon.stub(apiAuth,"init",function(){});
|
||||
sinon.stub(apiEditor,"init",function(){
|
||||
var app = express();
|
||||
app.get("/editor",function(req,res) { res.status(200).end(); });
|
||||
return app;
|
||||
});
|
||||
sinon.stub(apiAdmin,"init",function(){
|
||||
var app = express();
|
||||
app.get("/admin",function(req,res) { res.status(200).end(); });
|
||||
return app;
|
||||
});
|
||||
sinon.stub(apiAuth,"login",function(req,res){
|
||||
res.status(200).end();
|
||||
});
|
||||
};
|
||||
var afterEach = function() {
|
||||
apiAuth.init.restore();
|
||||
apiAuth.login.restore();
|
||||
apiEditor.init.restore();
|
||||
apiAdmin.init.restore();
|
||||
};
|
||||
|
||||
beforeEach(beforeEach);
|
||||
afterEach(afterEach);
|
||||
|
||||
it("does not setup admin api if httpAdminRoot is false", function(done) {
|
||||
api.init({},{ httpAdminRoot: false },{},{});
|
||||
should.not.exist(api.adminApp);
|
||||
done();
|
||||
});
|
||||
describe('initalises admin api without adminAuth', function(done) {
|
||||
before(function() {
|
||||
beforeEach();
|
||||
api.init({},{},{},{});
|
||||
});
|
||||
after(afterEach);
|
||||
it('exposes the editor',function(done) {
|
||||
request(api.adminApp).get("/editor").expect(200).end(done);
|
||||
})
|
||||
it('exposes the admin api',function(done) {
|
||||
request(api.adminApp).get("/admin").expect(200).end(done);
|
||||
})
|
||||
it('exposes the auth api',function(done) {
|
||||
request(api.adminApp).get("/auth/login").expect(200).end(done);
|
||||
})
|
||||
});
|
||||
|
||||
describe('initalises admin api without editor', function(done) {
|
||||
before(function() {
|
||||
beforeEach();
|
||||
api.init({},{ disableEditor: true },{},{});
|
||||
});
|
||||
after(afterEach);
|
||||
it('does not expose the editor',function(done) {
|
||||
request(api.adminApp).get("/editor").expect(404).end(done);
|
||||
})
|
||||
it('exposes the admin api',function(done) {
|
||||
request(api.adminApp).get("/admin").expect(200).end(done);
|
||||
})
|
||||
it('exposes the auth api',function(done) {
|
||||
request(api.adminApp).get("/auth/login").expect(200).end(done)
|
||||
})
|
||||
});
|
||||
});
|
||||
110
test/unit/@node-red/editor-api/lib/util_spec.js
Normal file
110
test/unit/@node-red/editor-api/lib/util_spec.js
Normal file
@@ -0,0 +1,110 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var apiUtil = NR_TEST_UTILS.require("@node-red/editor-api/lib/util");
|
||||
|
||||
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
var i18n = NR_TEST_UTILS.require("@node-red/util").i18n;
|
||||
|
||||
describe("api/util", function() {
|
||||
describe("errorHandler", function() {
|
||||
var loggedError = null;
|
||||
var loggedEvent = null;
|
||||
var app;
|
||||
before(function() {
|
||||
app = express();
|
||||
sinon.stub(log,'error',function(msg) {loggedError = msg;});
|
||||
sinon.stub(log,'audit',function(event) {loggedEvent = event;});
|
||||
app.get("/tooLarge", function(req,res) {
|
||||
var err = new Error();
|
||||
err.message = "request entity too large";
|
||||
throw err;
|
||||
},apiUtil.errorHandler)
|
||||
app.get("/stack", function(req,res) {
|
||||
var err = new Error();
|
||||
err.message = "stacktrace";
|
||||
throw err;
|
||||
},apiUtil.errorHandler)
|
||||
});
|
||||
after(function() {
|
||||
log.error.restore();
|
||||
log.audit.restore();
|
||||
})
|
||||
beforeEach(function() {
|
||||
loggedError = null;
|
||||
loggedEvent = null;
|
||||
})
|
||||
it("logs an error for request entity too large", function(done) {
|
||||
request(app).get("/tooLarge").expect(400).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("error","unexpected_error");
|
||||
res.body.should.have.property("message","Error: request entity too large");
|
||||
|
||||
loggedError.should.have.property("message","request entity too large");
|
||||
|
||||
loggedEvent.should.have.property("event","api.error");
|
||||
loggedEvent.should.have.property("error","unexpected_error");
|
||||
loggedEvent.should.have.property("message","Error: request entity too large");
|
||||
done();
|
||||
});
|
||||
})
|
||||
it("logs an error plus stack for other errors", function(done) {
|
||||
request(app).get("/stack").expect(400).end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("error","unexpected_error");
|
||||
res.body.should.have.property("message","Error: stacktrace");
|
||||
|
||||
/Error: stacktrace\s*at.*util_spec.js/m.test(loggedError).should.be.true();
|
||||
|
||||
loggedEvent.should.have.property("event","api.error");
|
||||
loggedEvent.should.have.property("error","unexpected_error");
|
||||
loggedEvent.should.have.property("message","Error: stacktrace");
|
||||
|
||||
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
describe('determineLangFromHeaders', function() {
|
||||
var oldDefaultLang;
|
||||
before(function() {
|
||||
oldDefaultLang = i18n.defaultLang;
|
||||
i18n.defaultLang = "en-US";
|
||||
})
|
||||
after(function() {
|
||||
i18n.defaultLang = oldDefaultLang;
|
||||
})
|
||||
it('returns the default lang if non provided', function() {
|
||||
apiUtil.determineLangFromHeaders(null).should.eql("en-US");
|
||||
})
|
||||
it('returns the first language accepted', function() {
|
||||
apiUtil.determineLangFromHeaders(['fr-FR','en-GB']).should.eql("fr-FR");
|
||||
})
|
||||
})
|
||||
});
|
||||
30
test/unit/@node-red/registry/lib/deprecated_spec.js
Normal file
30
test/unit/@node-red/registry/lib/deprecated_spec.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var deprecated = NR_TEST_UTILS.require("@node-red/registry/lib/deprecated.js");
|
||||
|
||||
describe('deprecated', function() {
|
||||
it('should return info on a node',function() {
|
||||
deprecated.get("irc in").should.eql({module:"node-red-node-irc"});
|
||||
});
|
||||
it('should return null for non-deprecated node',function() {
|
||||
should.not.exist(deprecated.get("foo"));
|
||||
});
|
||||
});
|
||||
128
test/unit/@node-red/registry/lib/index_spec.js
Normal file
128
test/unit/@node-red/registry/lib/index_spec.js
Normal file
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var path = require("path");
|
||||
var when = require("when");
|
||||
var fs = require("fs");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var registry = NR_TEST_UTILS.require("@node-red/registry");
|
||||
|
||||
var installer = NR_TEST_UTILS.require("@node-red/registry/lib/installer");
|
||||
var loader = NR_TEST_UTILS.require("@node-red/registry/lib/loader");
|
||||
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
|
||||
|
||||
describe('red/registry/index', function() {
|
||||
var stubs = [];
|
||||
afterEach(function() {
|
||||
while(stubs.length) {
|
||||
stubs.pop().restore();
|
||||
}
|
||||
})
|
||||
describe('#init',function() {
|
||||
it('intialises components', function() {
|
||||
stubs.push(sinon.stub(installer,"init"));
|
||||
stubs.push(sinon.stub(loader,"init"));
|
||||
stubs.push(sinon.stub(typeRegistry,"init"));
|
||||
|
||||
registry.init({});
|
||||
installer.init.called.should.be.true();
|
||||
loader.init.called.should.be.true();
|
||||
typeRegistry.init.called.should.be.true();
|
||||
})
|
||||
});
|
||||
|
||||
describe('#addModule', function() {
|
||||
it('loads the module and returns its info', function(done) {
|
||||
stubs.push(sinon.stub(loader,"addModule",function(module) {
|
||||
return when.resolve();
|
||||
}));
|
||||
stubs.push(sinon.stub(typeRegistry,"getModuleInfo", function(module) {
|
||||
return "info";
|
||||
}));
|
||||
registry.addModule("foo").then(function(info) {
|
||||
info.should.eql("info");
|
||||
done();
|
||||
}).catch(function(err) { done(err); });
|
||||
});
|
||||
it('rejects if loader rejects', function(done) {
|
||||
stubs.push(sinon.stub(loader,"addModule",function(module) {
|
||||
return when.reject("error");
|
||||
}));
|
||||
stubs.push(sinon.stub(typeRegistry,"getModuleInfo", function(module) {
|
||||
return "info";
|
||||
}));
|
||||
registry.addModule("foo").then(function(info) {
|
||||
done(new Error("unexpected resolve"));
|
||||
}).catch(function(err) {
|
||||
err.should.eql("error");
|
||||
done();
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe('#enableNode',function() {
|
||||
it('enables a node set',function(done) {
|
||||
stubs.push(sinon.stub(typeRegistry,"enableNodeSet",function() {
|
||||
return when.resolve();
|
||||
}));
|
||||
stubs.push(sinon.stub(typeRegistry,"getNodeInfo", function() {
|
||||
return {id:"node-set",loaded:true};
|
||||
}));
|
||||
registry.enableNode("node-set").then(function(ns) {
|
||||
typeRegistry.enableNodeSet.called.should.be.true();
|
||||
ns.should.have.a.property('id','node-set');
|
||||
done();
|
||||
}).catch(function(err) { done(err); });
|
||||
});
|
||||
|
||||
it('rejects if node unknown',function() {
|
||||
stubs.push(sinon.stub(typeRegistry,"enableNodeSet",function() {
|
||||
throw new Error('failure');
|
||||
}));
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
registry.enableNode("node-set")
|
||||
}).should.throw();
|
||||
});
|
||||
|
||||
it('triggers a node load',function(done) {
|
||||
stubs.push(sinon.stub(typeRegistry,"enableNodeSet",function() {
|
||||
return when.resolve();
|
||||
}));
|
||||
var calls = 0;
|
||||
stubs.push(sinon.stub(typeRegistry,"getNodeInfo", function() {
|
||||
// loaded=false on first call, true on subsequent
|
||||
return {id:"node-set",loaded:(calls++>0)};
|
||||
}));
|
||||
stubs.push(sinon.stub(loader,"loadNodeSet",function(){return when.resolve();}));
|
||||
stubs.push(sinon.stub(typeRegistry,"getFullNodeInfo"));
|
||||
|
||||
registry.enableNode("node-set").then(function(ns) {
|
||||
typeRegistry.enableNodeSet.called.should.be.true();
|
||||
loader.loadNodeSet.called.should.be.true();
|
||||
ns.should.have.a.property('id','node-set');
|
||||
ns.should.have.a.property('loaded',true);
|
||||
done();
|
||||
}).catch(function(err) { done(err); });
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
268
test/unit/@node-red/registry/lib/installer_spec.js
Normal file
268
test/unit/@node-red/registry/lib/installer_spec.js
Normal file
@@ -0,0 +1,268 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var when = require("when");
|
||||
var path = require("path");
|
||||
var fs = require('fs');
|
||||
var EventEmitter = require('events');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var installer = NR_TEST_UTILS.require("@node-red/registry/lib/installer");
|
||||
var registry = NR_TEST_UTILS.require("@node-red/registry/lib/index");
|
||||
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
|
||||
|
||||
describe('nodes/registry/installer', function() {
|
||||
|
||||
var mockLog = {
|
||||
log: sinon.stub(),
|
||||
debug: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
metric: sinon.stub(),
|
||||
_: function() { return "abc"}
|
||||
}
|
||||
|
||||
beforeEach(function() {
|
||||
installer.init({log:mockLog, settings:{}, events: new EventEmitter(), exec: {
|
||||
run: function() {
|
||||
return Promise.resolve("");
|
||||
}
|
||||
}});
|
||||
});
|
||||
function initInstaller(execResult) {
|
||||
installer.init({log:mockLog, settings:{}, events: new EventEmitter(), exec: {
|
||||
run: function() {
|
||||
return execResult;
|
||||
}
|
||||
}});
|
||||
}
|
||||
afterEach(function() {
|
||||
if (registry.addModule.restore) {
|
||||
registry.addModule.restore();
|
||||
}
|
||||
if (registry.removeModule.restore) {
|
||||
registry.removeModule.restore();
|
||||
}
|
||||
if (typeRegistry.removeModule.restore) {
|
||||
typeRegistry.removeModule.restore();
|
||||
}
|
||||
if (registry.getModuleInfo.restore) {
|
||||
registry.getModuleInfo.restore();
|
||||
}
|
||||
if (typeRegistry.getModuleInfo.restore) {
|
||||
typeRegistry.getModuleInfo.restore();
|
||||
}
|
||||
|
||||
if (require('fs').statSync.restore) {
|
||||
require('fs').statSync.restore();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
describe("installs module", function() {
|
||||
it("rejects when npm returns a 404", function(done) {
|
||||
var res = {
|
||||
code: 1,
|
||||
stdout:"",
|
||||
stderr:" 404 this_wont_exist"
|
||||
}
|
||||
var p = Promise.reject(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
installer.installModule("this_wont_exist").catch(function(err) {
|
||||
err.should.have.property("code",404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("rejects when npm does not find specified version", function(done) {
|
||||
var res = {
|
||||
code: 1,
|
||||
stdout:"",
|
||||
stderr:" version not found: this_wont_exist@0.1.2"
|
||||
}
|
||||
var p = Promise.reject(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
sinon.stub(typeRegistry,"getModuleInfo", function() {
|
||||
return {
|
||||
version: "0.1.1"
|
||||
}
|
||||
});
|
||||
installer.installModule("this_wont_exist","0.1.2").catch(function(err) {
|
||||
err.code.should.be.eql(404);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("rejects when update requested to existing version", function(done) {
|
||||
sinon.stub(typeRegistry,"getModuleInfo", function() {
|
||||
return {
|
||||
version: "0.1.1"
|
||||
}
|
||||
});
|
||||
installer.installModule("this_wont_exist","0.1.1").catch(function(err) {
|
||||
err.code.should.be.eql('module_already_loaded');
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("rejects with generic error", function(done) {
|
||||
var res = {
|
||||
code: 1,
|
||||
stdout:"",
|
||||
stderr:" kaboom!"
|
||||
}
|
||||
var p = Promise.reject(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
installer.installModule("this_wont_exist").then(function() {
|
||||
done(new Error("Unexpected success"));
|
||||
}).catch(function(err) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("succeeds when module is found", function(done) {
|
||||
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
||||
|
||||
var res = {
|
||||
code: 0,
|
||||
stdout:"",
|
||||
stderr:""
|
||||
}
|
||||
var p = Promise.resolve(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
|
||||
var addModule = sinon.stub(registry,"addModule",function(md) {
|
||||
return when.resolve(nodeInfo);
|
||||
});
|
||||
|
||||
installer.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();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
it("rejects when non-existant path is provided", function(done) {
|
||||
this.timeout(20000);
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"resources","local","TestNodeModule","node_modules","NonExistant"));
|
||||
installer.installModule(resourcesDir).then(function() {
|
||||
done(new Error("Unexpected success"));
|
||||
}).catch(function(err) {
|
||||
if (err.hasOwnProperty("code")) {
|
||||
err.code.should.eql(404);
|
||||
done();
|
||||
}
|
||||
else {
|
||||
console.log("ERRROR::"+err.toString()+"::");
|
||||
err.toString().should.eql("Error: Install failed");
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
it("succeeds when path is valid node-red module", function(done) {
|
||||
var nodeInfo = {nodes:{module:"foo",types:["a"]}};
|
||||
var addModule = sinon.stub(registry,"addModule",function(md) {
|
||||
return when.resolve(nodeInfo);
|
||||
});
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"resources","local","TestNodeModule","node_modules","TestNodeModule"));
|
||||
|
||||
var res = {
|
||||
code: 0,
|
||||
stdout:"",
|
||||
stderr:""
|
||||
}
|
||||
var p = Promise.resolve(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
installer.installModule(resourcesDir).then(function(info) {
|
||||
info.should.eql(nodeInfo);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
describe("uninstalls module", function() {
|
||||
it("rejects invalid module names", function(done) {
|
||||
var promises = [];
|
||||
promises.push(installer.uninstallModule("this_wont_exist "));
|
||||
promises.push(installer.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);
|
||||
});
|
||||
var res = {
|
||||
code: 1,
|
||||
stdout:"",
|
||||
stderr:"error"
|
||||
}
|
||||
var p = Promise.reject(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
|
||||
installer.uninstallModule("this_wont_exist").then(function() {
|
||||
done(new Error("Unexpected success"));
|
||||
}).catch(function(err) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("succeeds when module is found", function(done) {
|
||||
var nodeInfo = [{module:"foo",types:["a"]}];
|
||||
var removeModule = sinon.stub(typeRegistry,"removeModule",function(md) {
|
||||
return nodeInfo;
|
||||
});
|
||||
var getModuleInfo = sinon.stub(registry,"getModuleInfo",function(md) {
|
||||
return {nodes:[]};
|
||||
});
|
||||
var res = {
|
||||
code: 0,
|
||||
stdout:"",
|
||||
stderr:""
|
||||
}
|
||||
var p = Promise.resolve(res);
|
||||
p.catch((err)=>{});
|
||||
initInstaller(p)
|
||||
|
||||
sinon.stub(fs,"statSync", function(fn) { return {}; });
|
||||
|
||||
installer.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();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
62
test/unit/@node-red/registry/lib/library_spec.js
Normal file
62
test/unit/@node-red/registry/lib/library_spec.js
Normal file
@@ -0,0 +1,62 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
|
||||
var fs = require("fs");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var library = NR_TEST_UTILS.require("@node-red/registry/lib/library");
|
||||
|
||||
describe("library api", function() {
|
||||
it('returns null list when no modules have been registered', function() {
|
||||
library.init();
|
||||
should.not.exist(library.getExampleFlows());
|
||||
});
|
||||
it('returns null path when module is not known', function() {
|
||||
library.init();
|
||||
should.not.exist(library.getExampleFlowPath('foo','bar'));
|
||||
});
|
||||
|
||||
it('returns a valid example path', function(done) {
|
||||
library.init();
|
||||
library.addExamplesDir("test-module",path.resolve(__dirname+'/resources/examples')).then(function() {
|
||||
try {
|
||||
var flows = library.getExampleFlows();
|
||||
flows.should.deepEqual({"d":{"test-module":{"f":["one"]}}});
|
||||
|
||||
var examplePath = library.getExampleFlowPath('test-module','one');
|
||||
examplePath.should.eql(path.resolve(__dirname+'/resources/examples/one.json'))
|
||||
|
||||
|
||||
library.removeExamplesDir('test-module');
|
||||
|
||||
try {
|
||||
should.not.exist(library.getExampleFlows());
|
||||
should.not.exist(library.getExampleFlowPath('test-module','one'));
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
}catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
})
|
||||
});
|
||||
655
test/unit/@node-red/registry/lib/loader_spec.js
Normal file
655
test/unit/@node-red/registry/lib/loader_spec.js
Normal file
@@ -0,0 +1,655 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require("when");
|
||||
var sinon = require("sinon");
|
||||
var path = require("path");
|
||||
var fs = require("fs");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var loader = NR_TEST_UTILS.require("@node-red/registry/lib/loader");
|
||||
|
||||
var localfilesystem = NR_TEST_UTILS.require("@node-red/registry/lib/localfilesystem");
|
||||
var registry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
|
||||
|
||||
var nodes = NR_TEST_UTILS.require("@node-red/registry");
|
||||
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"resources","local"));
|
||||
|
||||
describe("red/nodes/registry/loader",function() {
|
||||
var stubs = [];
|
||||
before(function() {
|
||||
sinon.stub(localfilesystem,"init");
|
||||
});
|
||||
after(function() {
|
||||
localfilesystem.init.restore();
|
||||
});
|
||||
afterEach(function() {
|
||||
while(stubs.length) {
|
||||
stubs.pop().restore();
|
||||
}
|
||||
})
|
||||
|
||||
describe("#load",function() {
|
||||
it("load empty set without settings available", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){ return {};}));
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return {};}));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return false;}}});
|
||||
loader.load("foo",true).then(function() {
|
||||
localfilesystem.getNodeFiles.called.should.be.true();
|
||||
localfilesystem.getNodeFiles.lastCall.args[0].should.eql('foo');
|
||||
localfilesystem.getNodeFiles.lastCall.args[1].should.be.true();
|
||||
registry.saveNodeList.called.should.be.false();
|
||||
done();
|
||||
})
|
||||
});
|
||||
it("load empty set with settings available triggers registery save", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){ return {};}));
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return {};}));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load("foo",true).then(function() {
|
||||
registry.saveNodeList.called.should.be.true();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
})
|
||||
});
|
||||
|
||||
it("load core node files scanned by lfs - single node single file", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
|
||||
var result = {};
|
||||
result["node-red"] = {
|
||||
"name": "node-red",
|
||||
"version": "1.2.3",
|
||||
"nodes": {
|
||||
"TestNode1": {
|
||||
"file": path.join(resourcesDir,"TestNode1","TestNode1.js"),
|
||||
"module": "node-red",
|
||||
"name": "TestNode1"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load().then(function(result) {
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","node-red");
|
||||
module.should.have.property("version","1.2.3");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("TestNode1");
|
||||
module.nodes.TestNode1.should.have.property("id","node-red/TestNode1");
|
||||
module.nodes.TestNode1.should.have.property("module","node-red");
|
||||
module.nodes.TestNode1.should.have.property("name","TestNode1");
|
||||
module.nodes.TestNode1.should.have.property("file");
|
||||
module.nodes.TestNode1.should.have.property("template");
|
||||
module.nodes.TestNode1.should.have.property("enabled",true);
|
||||
module.nodes.TestNode1.should.have.property("loaded",true);
|
||||
module.nodes.TestNode1.should.have.property("types");
|
||||
module.nodes.TestNode1.types.should.have.a.length(1);
|
||||
module.nodes.TestNode1.types[0].should.eql('test-node-1');
|
||||
module.nodes.TestNode1.should.have.property("config");
|
||||
module.nodes.TestNode1.should.have.property("help");
|
||||
module.nodes.TestNode1.should.have.property("namespace","node-red");
|
||||
|
||||
nodes.registerType.calledOnce.should.be.true();
|
||||
nodes.registerType.lastCall.args[0].should.eql('node-red/TestNode1');
|
||||
nodes.registerType.lastCall.args[1].should.eql('test-node-1');
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it("load core node files scanned by lfs - multiple nodes single file", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
|
||||
var result = {};
|
||||
result["node-red"] = {
|
||||
"name": "node-red",
|
||||
"version": "4.5.6",
|
||||
"nodes": {
|
||||
"MultipleNodes1": {
|
||||
"file": path.join(resourcesDir,"MultipleNodes1","MultipleNodes1.js"),
|
||||
"module": "node-red",
|
||||
"name": "MultipleNodes1"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load().then(function(result) {
|
||||
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","node-red");
|
||||
module.should.have.property("version","4.5.6");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("MultipleNodes1");
|
||||
module.nodes.MultipleNodes1.should.have.property("id","node-red/MultipleNodes1");
|
||||
module.nodes.MultipleNodes1.should.have.property("module","node-red");
|
||||
module.nodes.MultipleNodes1.should.have.property("name","MultipleNodes1");
|
||||
module.nodes.MultipleNodes1.should.have.property("file");
|
||||
module.nodes.MultipleNodes1.should.have.property("template");
|
||||
module.nodes.MultipleNodes1.should.have.property("enabled",true);
|
||||
module.nodes.MultipleNodes1.should.have.property("loaded",true);
|
||||
module.nodes.MultipleNodes1.should.have.property("types");
|
||||
module.nodes.MultipleNodes1.types.should.have.a.length(2);
|
||||
module.nodes.MultipleNodes1.types[0].should.eql('test-node-multiple-1a');
|
||||
module.nodes.MultipleNodes1.types[1].should.eql('test-node-multiple-1b');
|
||||
module.nodes.MultipleNodes1.should.have.property("config");
|
||||
module.nodes.MultipleNodes1.should.have.property("help");
|
||||
module.nodes.MultipleNodes1.should.have.property("namespace","node-red");
|
||||
|
||||
nodes.registerType.calledTwice.should.be.true();
|
||||
nodes.registerType.firstCall.args[0].should.eql('node-red/MultipleNodes1');
|
||||
nodes.registerType.firstCall.args[1].should.eql('test-node-multiple-1a');
|
||||
nodes.registerType.secondCall.args[0].should.eql('node-red/MultipleNodes1');
|
||||
nodes.registerType.secondCall.args[1].should.eql('test-node-multiple-1b');
|
||||
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it("load core node files scanned by lfs - node with promise", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
|
||||
var result = {};
|
||||
result["node-red"] = {
|
||||
"name": "node-red",
|
||||
"version":"2.4.6",
|
||||
"nodes": {
|
||||
"TestNode2": {
|
||||
"file": path.join(resourcesDir,"TestNode2","TestNode2.js"),
|
||||
"module": "node-red",
|
||||
"name": "TestNode2"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load().then(function(result) {
|
||||
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","node-red");
|
||||
module.should.have.property("version","2.4.6");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("TestNode2");
|
||||
module.nodes.TestNode2.should.have.property("id","node-red/TestNode2");
|
||||
module.nodes.TestNode2.should.have.property("module","node-red");
|
||||
module.nodes.TestNode2.should.have.property("name","TestNode2");
|
||||
module.nodes.TestNode2.should.have.property("file");
|
||||
module.nodes.TestNode2.should.have.property("template");
|
||||
module.nodes.TestNode2.should.have.property("enabled",true);
|
||||
module.nodes.TestNode2.should.have.property("loaded",true);
|
||||
module.nodes.TestNode2.should.have.property("types");
|
||||
module.nodes.TestNode2.types.should.have.a.length(1);
|
||||
module.nodes.TestNode2.types[0].should.eql('test-node-2');
|
||||
module.nodes.TestNode2.should.have.property("config");
|
||||
module.nodes.TestNode2.should.have.property("help");
|
||||
module.nodes.TestNode2.should.have.property("namespace","node-red");
|
||||
module.nodes.TestNode2.should.not.have.property('err');
|
||||
|
||||
nodes.registerType.calledOnce.should.be.true();
|
||||
nodes.registerType.lastCall.args[0].should.eql('node-red/TestNode2');
|
||||
nodes.registerType.lastCall.args[1].should.eql('test-node-2');
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it("load core node files scanned by lfs - node with rejecting promise", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
|
||||
var result = {};
|
||||
result["node-red"] = {
|
||||
"name": "node-red",
|
||||
"version":"1.2.3",
|
||||
"nodes": {
|
||||
"TestNode3": {
|
||||
"file": path.join(resourcesDir,"TestNode3","TestNode3.js"),
|
||||
"module": "node-red",
|
||||
"name": "TestNode3"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load().then(function(result) {
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","node-red");
|
||||
module.should.have.property("version","1.2.3");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("TestNode3");
|
||||
module.nodes.TestNode3.should.have.property("id","node-red/TestNode3");
|
||||
module.nodes.TestNode3.should.have.property("module","node-red");
|
||||
module.nodes.TestNode3.should.have.property("name","TestNode3");
|
||||
module.nodes.TestNode3.should.have.property("file");
|
||||
module.nodes.TestNode3.should.have.property("template");
|
||||
module.nodes.TestNode3.should.have.property("enabled",true);
|
||||
module.nodes.TestNode3.should.have.property("loaded",false);
|
||||
module.nodes.TestNode3.should.have.property("types");
|
||||
module.nodes.TestNode3.types.should.have.a.length(1);
|
||||
module.nodes.TestNode3.types[0].should.eql('test-node-3');
|
||||
module.nodes.TestNode3.should.have.property("config");
|
||||
module.nodes.TestNode3.should.have.property("help");
|
||||
module.nodes.TestNode3.should.have.property("namespace","node-red");
|
||||
module.nodes.TestNode3.should.have.property('err','fail');
|
||||
|
||||
nodes.registerType.called.should.be.false();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it("load core node files scanned by lfs - missing file", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
|
||||
var result = {};
|
||||
result["node-red"] = {
|
||||
"name": "node-red",
|
||||
"version":"1.2.3",
|
||||
"nodes": {
|
||||
"DoesNotExist": {
|
||||
"file": path.join(resourcesDir,"doesnotexist"),
|
||||
"module": "node-red",
|
||||
"name": "DoesNotExist"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load().then(function(result) {
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","node-red");
|
||||
module.should.have.property("version","1.2.3");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("DoesNotExist");
|
||||
module.nodes.DoesNotExist.should.have.property("id","node-red/DoesNotExist");
|
||||
module.nodes.DoesNotExist.should.have.property("module","node-red");
|
||||
module.nodes.DoesNotExist.should.have.property("name","DoesNotExist");
|
||||
module.nodes.DoesNotExist.should.have.property("file");
|
||||
module.nodes.DoesNotExist.should.have.property("template");
|
||||
module.nodes.DoesNotExist.should.have.property("enabled",true);
|
||||
module.nodes.DoesNotExist.should.have.property("loaded",false);
|
||||
module.nodes.DoesNotExist.should.have.property("types");
|
||||
module.nodes.DoesNotExist.types.should.have.a.length(0);
|
||||
module.nodes.DoesNotExist.should.not.have.property("config");
|
||||
module.nodes.DoesNotExist.should.not.have.property("help");
|
||||
module.nodes.DoesNotExist.should.not.have.property("namespace","node-red");
|
||||
module.nodes.DoesNotExist.should.have.property('err');
|
||||
|
||||
nodes.registerType.called.should.be.false();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it("load core node files scanned by lfs - missing html file", function(done) {
|
||||
stubs.push(sinon.stub(localfilesystem,"getNodeFiles", function(){
|
||||
var result = {};
|
||||
result["node-red"] = {
|
||||
"name": "node-red",
|
||||
"version": "1.2.3",
|
||||
"nodes": {
|
||||
"DuffNode": {
|
||||
"file": path.join(resourcesDir,"DuffNode","DuffNode.js"),
|
||||
"module": "node-red",
|
||||
"name": "DuffNode"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.load().then(function(result) {
|
||||
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","node-red");
|
||||
module.should.have.property("version","1.2.3");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("DuffNode");
|
||||
module.nodes.DuffNode.should.have.property("id","node-red/DuffNode");
|
||||
module.nodes.DuffNode.should.have.property("module","node-red");
|
||||
module.nodes.DuffNode.should.have.property("name","DuffNode");
|
||||
module.nodes.DuffNode.should.have.property("file");
|
||||
module.nodes.DuffNode.should.have.property("template");
|
||||
module.nodes.DuffNode.should.have.property("enabled",true);
|
||||
module.nodes.DuffNode.should.have.property("loaded",false);
|
||||
module.nodes.DuffNode.should.have.property("types");
|
||||
module.nodes.DuffNode.types.should.have.a.length(0);
|
||||
module.nodes.DuffNode.should.not.have.property("config");
|
||||
module.nodes.DuffNode.should.not.have.property("help");
|
||||
module.nodes.DuffNode.should.not.have.property("namespace","node-red");
|
||||
module.nodes.DuffNode.should.have.property('err');
|
||||
module.nodes.DuffNode.err.should.endWith("DuffNode.html does not exist");
|
||||
|
||||
nodes.registerType.called.should.be.false();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("#addModule",function() {
|
||||
it("throws error if settings unavailable", function() {
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return false;}}});
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
loader.addModule("test-module");
|
||||
}).should.throw("Settings unavailable");
|
||||
});
|
||||
|
||||
it("returns rejected error if module already loaded", function(done) {
|
||||
stubs.push(sinon.stub(registry,"getModuleInfo",function(){return{}}));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
|
||||
loader.addModule("test-module").catch(function(err) {
|
||||
err.code.should.eql("module_already_loaded");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it("returns rejected error if module not found", function(done) {
|
||||
stubs.push(sinon.stub(registry,"getModuleInfo",function(){return null}));
|
||||
stubs.push(sinon.stub(localfilesystem,"getModuleFiles",function() {
|
||||
throw new Error("failure");
|
||||
}));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.addModule("test-module").catch(function(err) {
|
||||
err.message.should.eql("failure");
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it("loads module by name", function(done) {
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
stubs.push(sinon.stub(registry,"getModuleInfo",function(){ return null; }));
|
||||
stubs.push(sinon.stub(localfilesystem,"getModuleFiles", function(){
|
||||
var result = {};
|
||||
result["TestNodeModule"] = {
|
||||
"name": "TestNodeModule",
|
||||
"version": "1.2.3",
|
||||
"nodes": {
|
||||
"TestNode1": {
|
||||
"file": path.join(resourcesDir,"TestNodeModule","node_modules","TestNodeModule","TestNodeModule.js"),
|
||||
"module": "TestNodeModule",
|
||||
"name": "TestNode1",
|
||||
"version": "1.2.3"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return "a node list" }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({nodes:nodes,log:{info:function(){},_:function(){}},settings:{available:function(){return true;}}});
|
||||
loader.addModule("TestNodeModule").then(function(result) {
|
||||
result.should.eql("a node list");
|
||||
|
||||
registry.addModule.called.should.be.true();
|
||||
var module = registry.addModule.lastCall.args[0];
|
||||
module.should.have.property("name","TestNodeModule");
|
||||
module.should.have.property("version","1.2.3");
|
||||
module.should.have.property("nodes");
|
||||
module.nodes.should.have.property("TestNode1");
|
||||
module.nodes.TestNode1.should.have.property("id","TestNodeModule/TestNode1");
|
||||
module.nodes.TestNode1.should.have.property("module","TestNodeModule");
|
||||
module.nodes.TestNode1.should.have.property("name","TestNode1");
|
||||
module.nodes.TestNode1.should.have.property("file");
|
||||
module.nodes.TestNode1.should.have.property("template");
|
||||
module.nodes.TestNode1.should.have.property("enabled",true);
|
||||
module.nodes.TestNode1.should.have.property("loaded",true);
|
||||
module.nodes.TestNode1.should.have.property("types");
|
||||
module.nodes.TestNode1.types.should.have.a.length(1);
|
||||
module.nodes.TestNode1.types[0].should.eql('test-node-mod-1');
|
||||
module.nodes.TestNode1.should.have.property("config");
|
||||
module.nodes.TestNode1.should.have.property("help");
|
||||
module.nodes.TestNode1.should.have.property("namespace","TestNodeModule");
|
||||
module.nodes.TestNode1.should.not.have.property('err');
|
||||
|
||||
nodes.registerType.calledOnce.should.be.true();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it("skips module that fails version check", function(done) {
|
||||
// This module isn't already loaded
|
||||
stubs.push(sinon.stub(registry,"getNodeInfo", function(){ return null; }));
|
||||
stubs.push(sinon.stub(registry,"getModuleInfo",function(){ return null; }));
|
||||
stubs.push(sinon.stub(localfilesystem,"getModuleFiles", function(){
|
||||
var result = {};
|
||||
result["TestNodeModule"] = {
|
||||
"name": "TestNodeModule",
|
||||
"version": "1.2.3",
|
||||
"redVersion":"999.0.0",
|
||||
"nodes": {
|
||||
"TestNode1": {
|
||||
"file": path.join(resourcesDir,"TestNodeModule","node_modules","TestNodeModule","TestNodeModule.js"),
|
||||
"module": "TestNodeModule",
|
||||
"name": "TestNode1",
|
||||
"version": "1.2.3"
|
||||
}
|
||||
}
|
||||
};
|
||||
return result;
|
||||
}));
|
||||
|
||||
stubs.push(sinon.stub(registry,"saveNodeList", function(){ return "a node list" }));
|
||||
stubs.push(sinon.stub(registry,"addModule", function(){ return }));
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.init({log:{"_":function(){},warn:function(){}},nodes:nodes,version: function() { return "0.12.0"}, settings:{available:function(){return true;}}});
|
||||
loader.addModule("TestNodeModule").then(function(result) {
|
||||
result.should.eql("a node list");
|
||||
registry.addModule.called.should.be.false();
|
||||
nodes.registerType.called.should.be.false();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it.skip('registers a message catalog');
|
||||
|
||||
|
||||
});
|
||||
describe("#loadNodeSet",function() {
|
||||
it("no-ops the load if node is not enabled", function(done) {
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.loadNodeSet({
|
||||
"file": path.join(resourcesDir,"TestNode1","TestNode1.js"),
|
||||
"module": "node-red",
|
||||
"name": "TestNode1",
|
||||
"enabled": false
|
||||
}).then(function(node) {
|
||||
node.enabled.should.be.false();
|
||||
nodes.registerType.called.should.be.false();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it("handles node that errors on require", function(done) {
|
||||
stubs.push(sinon.stub(nodes,"registerType"));
|
||||
loader.loadNodeSet({
|
||||
"file": path.join(resourcesDir,"TestNode4","TestNode4.js"),
|
||||
"module": "node-red",
|
||||
"name": "TestNode4",
|
||||
"enabled": true
|
||||
}).then(function(node) {
|
||||
node.enabled.should.be.true();
|
||||
nodes.registerType.called.should.be.false();
|
||||
node.should.have.property('err');
|
||||
node.err.toString().should.eql("Error: fail to require (line:1)");
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("#getNodeHelp",function() {
|
||||
it("returns preloaded help", function() {
|
||||
loader.getNodeHelp({
|
||||
help:{
|
||||
en:"foo"
|
||||
}
|
||||
},"en").should.eql("foo");
|
||||
});
|
||||
it("loads help, caching result", function() {
|
||||
stubs.push(sinon.stub(fs,"readFileSync", function(path) {
|
||||
return 'bar';
|
||||
}))
|
||||
var node = {
|
||||
template: "/tmp/node/directory/file.html",
|
||||
help:{
|
||||
en:"foo"
|
||||
}
|
||||
};
|
||||
loader.getNodeHelp(node,"fr").should.eql("bar");
|
||||
node.help['fr'].should.eql("bar");
|
||||
fs.readFileSync.calledOnce.should.be.true();
|
||||
fs.readFileSync.lastCall.args[0].should.eql(path.normalize("/tmp/node/directory/locales/fr/file.html"));
|
||||
loader.getNodeHelp(node,"fr").should.eql("bar");
|
||||
fs.readFileSync.calledOnce.should.be.true();
|
||||
});
|
||||
it("loads help, defaulting to en-US content", function() {
|
||||
stubs.push(sinon.stub(fs,"readFileSync", function(path) {
|
||||
throw new Error("not found");
|
||||
}))
|
||||
var node = {
|
||||
template: "/tmp/node/directory/file.html",
|
||||
help:{}
|
||||
};
|
||||
node.help['en-US'] = 'foo';
|
||||
|
||||
loader.getNodeHelp(node,"fr").should.eql("foo");
|
||||
node.help['fr'].should.eql("foo");
|
||||
fs.readFileSync.calledOnce.should.be.true();
|
||||
fs.readFileSync.lastCall.args[0].should.eql(path.normalize("/tmp/node/directory/locales/fr/file.html"));
|
||||
loader.getNodeHelp(node,"fr").should.eql("foo");
|
||||
fs.readFileSync.calledOnce.should.be.true();
|
||||
});
|
||||
it("loads help, defaulting to en-US content for extra nodes", function() {
|
||||
stubs.push(sinon.stub(fs,"readFileSync", function(path) {
|
||||
if (path.indexOf("en-US") >= 0) {
|
||||
return 'bar';
|
||||
}
|
||||
throw new Error("not found");
|
||||
}));
|
||||
var node = {
|
||||
template: "/tmp/node/directory/file.html",
|
||||
help:{}
|
||||
};
|
||||
delete node.help['en-US'];
|
||||
|
||||
loader.getNodeHelp(node,"fr").should.eql("bar");
|
||||
node.help['fr'].should.eql("bar");
|
||||
fs.readFileSync.calledTwice.should.be.true();
|
||||
fs.readFileSync.firstCall.args[0].should.eql(path.normalize("/tmp/node/directory/locales/fr/file.html"));
|
||||
fs.readFileSync.lastCall.args[0].should.eql(path.normalize("/tmp/node/directory/locales/en-US/file.html"));
|
||||
loader.getNodeHelp(node,"fr").should.eql("bar");
|
||||
fs.readFileSync.calledTwice.should.be.true();
|
||||
});
|
||||
it("fails to load en-US help content", function() {
|
||||
stubs.push(sinon.stub(fs,"readFileSync", function(path) {
|
||||
throw new Error("not found");
|
||||
}));
|
||||
var node = {
|
||||
template: "/tmp/node/directory/file.html",
|
||||
help:{}
|
||||
};
|
||||
delete node.help['en-US'];
|
||||
|
||||
should.not.exist(loader.getNodeHelp(node,"en-US"));
|
||||
should.not.exist(node.help['en-US']);
|
||||
fs.readFileSync.calledTwice.should.be.true();
|
||||
fs.readFileSync.firstCall.args[0].should.eql(path.normalize("/tmp/node/directory/locales/en-US/file.html"));
|
||||
fs.readFileSync.lastCall.args[0].should.eql(path.normalize("/tmp/node/directory/locales/en/file.html"));
|
||||
should.not.exist(loader.getNodeHelp(node,"en-US"));
|
||||
fs.readFileSync.callCount.should.eql(4);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
304
test/unit/@node-red/registry/lib/localfilesystem_spec.js
Normal file
304
test/unit/@node-red/registry/lib/localfilesystem_spec.js
Normal file
@@ -0,0 +1,304 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require("when");
|
||||
var sinon = require("sinon");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var localfilesystem = NR_TEST_UTILS.require("@node-red/registry/lib/localfilesystem");
|
||||
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"resources","local"));
|
||||
var userDir = path.resolve(path.join(__dirname,"resources","userDir"));
|
||||
var moduleDir = path.resolve(path.join(__dirname,"resources","local","TestNodeModule"));
|
||||
|
||||
var i18n = NR_TEST_UTILS.require("@node-red/util").i18n;
|
||||
|
||||
describe("red/nodes/registry/localfilesystem",function() {
|
||||
beforeEach(function() {
|
||||
stubs.push(sinon.stub(i18n,"registerMessageCatalog", function() { return Promise.resolve(); }));
|
||||
})
|
||||
|
||||
var stubs = [];
|
||||
afterEach(function() {
|
||||
while(stubs.length) {
|
||||
stubs.pop().restore();
|
||||
}
|
||||
})
|
||||
function checkNodes(nodes,shouldHaveNodes,shouldNotHaveNodes,module) {
|
||||
for (var i=0;i<shouldHaveNodes.length;i++) {
|
||||
nodes.should.have.a.property(shouldHaveNodes[i]);
|
||||
nodes[shouldHaveNodes[i]].should.have.a.property('file');
|
||||
nodes[shouldHaveNodes[i]].file.should.equal(path.resolve(nodes[shouldHaveNodes[i]].file));
|
||||
nodes[shouldHaveNodes[i]].should.have.a.property('module',module||'node-red');
|
||||
nodes[shouldHaveNodes[i]].should.have.a.property('name',shouldHaveNodes[i]);
|
||||
}
|
||||
for (i=0;i<shouldNotHaveNodes.length;i++) {
|
||||
nodes.should.not.have.a.property(shouldNotHaveNodes[i]);
|
||||
}
|
||||
}
|
||||
describe("#getNodeFiles",function() {
|
||||
it("Finds all the node files in the resources tree",function(done) {
|
||||
localfilesystem.init({settings:{coreNodesDir:resourcesDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
var nodes = nm.nodes;
|
||||
checkNodes(nm.nodes,['TestNode1','MultipleNodes1','NestedNode','TestNode2','TestNode3','TestNode4'],['TestNodeModule']);
|
||||
i18n.registerMessageCatalog.called.should.be.true();
|
||||
i18n.registerMessageCatalog.lastCall.args[0].should.eql('node-red');
|
||||
i18n.registerMessageCatalog.lastCall.args[1].should.eql(path.resolve(path.join(resourcesDir,"locales")));
|
||||
i18n.registerMessageCatalog.lastCall.args[2].should.eql('messages.json');
|
||||
done();
|
||||
});
|
||||
it("Includes node files from settings",function(done) {
|
||||
localfilesystem.init({settings:{nodesIncludes:['TestNode1.js'],coreNodesDir:resourcesDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['TestNode1'],['MultipleNodes1','NestedNode','TestNode2','TestNode3','TestNode4','TestNodeModule']);
|
||||
done();
|
||||
});
|
||||
it("Excludes node files from settings",function(done) {
|
||||
localfilesystem.init({settings:{nodesExcludes:['TestNode1.js'],coreNodesDir:resourcesDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['MultipleNodes1','NestedNode','TestNode2','TestNode3','TestNode4'],['TestNode1','TestNodeModule']);
|
||||
done();
|
||||
});
|
||||
it("Finds nodes in userDir/nodes",function(done) {
|
||||
localfilesystem.init({settings:{userDir:userDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
|
||||
done();
|
||||
});
|
||||
|
||||
it("Finds nodes in settings.nodesDir (string)",function(done) {
|
||||
localfilesystem.init({settings:{nodesDir:userDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
|
||||
done();
|
||||
});
|
||||
it("Finds nodes in settings.nodesDir (string,relative path)",function(done) {
|
||||
var relativeUserDir = path.join("test","unit","@node-red","registry","lib","resources","userDir");
|
||||
localfilesystem.init({settings:{nodesDir:relativeUserDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
|
||||
done();
|
||||
});
|
||||
it("Finds nodes in settings.nodesDir (array)",function(done) {
|
||||
localfilesystem.init({settings:{nodesDir:[userDir]}});
|
||||
var nodeList = localfilesystem.getNodeFiles(true);
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['TestNode5'],['TestNode1']);
|
||||
done();
|
||||
});
|
||||
it("Finds nodes module path",function(done) {
|
||||
var _join = path.join;
|
||||
stubs.push(sinon.stub(path,"join",function() {
|
||||
if (arguments[0] == resourcesDir) {
|
||||
// This stops the module tree scan from going any higher
|
||||
// up the tree than resourcesDir.
|
||||
return arguments[0];
|
||||
}
|
||||
return _join.apply(null,arguments);
|
||||
}));
|
||||
localfilesystem.init({settings:{coreNodesDir:moduleDir}});
|
||||
var nodeList = localfilesystem.getNodeFiles();
|
||||
nodeList.should.have.a.property("node-red");
|
||||
var nm = nodeList['node-red'];
|
||||
nm.should.have.a.property('name','node-red');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,[],['TestNode1']);
|
||||
|
||||
nm = nodeList['TestNodeModule'];
|
||||
nm.should.have.a.property('name','TestNodeModule');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['TestNodeMod1','TestNodeMod2'],[],'TestNodeModule');
|
||||
|
||||
nm = nodeList['VersionMismatchModule'];
|
||||
nm.should.have.a.property('name','VersionMismatchModule');
|
||||
nm.should.have.a.property("nodes");
|
||||
checkNodes(nm.nodes,['VersionMismatchMod1','VersionMismatchMod2'],[],'VersionMismatchModule');
|
||||
|
||||
i18n.registerMessageCatalog.called.should.be.true();
|
||||
i18n.registerMessageCatalog.lastCall.args[0].should.eql('node-red');
|
||||
i18n.registerMessageCatalog.lastCall.args[1].should.eql(path.resolve(path.join(moduleDir,"locales")));
|
||||
i18n.registerMessageCatalog.lastCall.args[2].should.eql('messages.json');
|
||||
|
||||
|
||||
|
||||
done();
|
||||
});
|
||||
it.skip("finds locales directory");
|
||||
it.skip("finds icon path directory");
|
||||
it("scans icon files in the resources tree",function(done) {
|
||||
var count = 0;
|
||||
localfilesystem.init({
|
||||
|
||||
// events:{emit:function(eventName,dir){
|
||||
// if (count === 0) {
|
||||
// eventName.should.equal("node-icon-dir");
|
||||
// dir.name.should.equal("node-red");
|
||||
// dir.icons.should.be.an.Array();
|
||||
// count = 1;
|
||||
// } else if (count === 1) {
|
||||
// done();
|
||||
// }
|
||||
// }},
|
||||
settings:{coreNodesDir:resourcesDir}
|
||||
});
|
||||
var list = localfilesystem.getNodeFiles(true);
|
||||
list.should.have.property("node-red");
|
||||
list["node-red"].should.have.property("icons");
|
||||
list["node-red"].icons.should.have.length(1);
|
||||
list["node-red"].icons[0].should.have.property("path",path.join(__dirname,"resources/local/NestedDirectoryNode/NestedNode/icons"))
|
||||
list["node-red"].icons[0].should.have.property("icons");
|
||||
list["node-red"].icons[0].icons.should.have.length(1);
|
||||
list["node-red"].icons[0].icons[0].should.eql("arrow-in.png");
|
||||
done();
|
||||
});
|
||||
it("scans icons dir in library",function(done) {
|
||||
var count = 0;
|
||||
localfilesystem.init({
|
||||
//
|
||||
// events:{emit:function(eventName,dir){
|
||||
// eventName.should.equal("node-icon-dir");
|
||||
// if (count === 0) {
|
||||
// dir.name.should.equal("node-red");
|
||||
// dir.icons.should.be.an.Array();
|
||||
// count = 1;
|
||||
// } else if (count === 1) {
|
||||
// dir.name.should.equal("Library");
|
||||
// dir.icons.should.be.an.Array();
|
||||
// dir.icons.length.should.equal(1);
|
||||
// dir.icons[0].should.be.equal("test_icon.png");
|
||||
// done();
|
||||
// }
|
||||
// }},
|
||||
settings:{userDir:userDir}
|
||||
});
|
||||
var list = localfilesystem.getNodeFiles(true);
|
||||
list.should.have.property("node-red");
|
||||
list["node-red"].should.have.property("icons");
|
||||
list["node-red"].icons.should.have.length(1);
|
||||
list["node-red"].icons[0].should.have.property("path",path.join(__dirname,"resources/userDir/lib/icons"))
|
||||
list["node-red"].icons[0].should.have.property("icons");
|
||||
list["node-red"].icons[0].icons.should.have.length(1);
|
||||
list["node-red"].icons[0].icons[0].should.eql("test_icon.png");
|
||||
done();
|
||||
});
|
||||
});
|
||||
describe("#getModuleFiles",function() {
|
||||
it("gets a nodes module files",function(done) {
|
||||
var _join = path.join;
|
||||
stubs.push(sinon.stub(path,"join",function() {
|
||||
if (arguments[0] == resourcesDir) {
|
||||
// This stops the module tree scan from going any higher
|
||||
// up the tree than resourcesDir.
|
||||
return arguments[0];
|
||||
}
|
||||
return _join.apply(null,arguments);
|
||||
}));
|
||||
localfilesystem.init({settings:{coreNodesDir:moduleDir}});
|
||||
var nodeModule = localfilesystem.getModuleFiles('TestNodeModule');
|
||||
nodeModule.should.have.a.property('TestNodeModule');
|
||||
nodeModule['TestNodeModule'].should.have.a.property('name','TestNodeModule');
|
||||
nodeModule['TestNodeModule'].should.have.a.property('version','0.0.1');
|
||||
nodeModule['TestNodeModule'].should.have.a.property('nodes');
|
||||
checkNodes(nodeModule['TestNodeModule'].nodes,['TestNodeMod1','TestNodeMod2'],[],'TestNodeModule');
|
||||
|
||||
nodeModule = localfilesystem.getModuleFiles('VersionMismatchModule');
|
||||
nodeModule.should.have.a.property('VersionMismatchModule');
|
||||
nodeModule['VersionMismatchModule'].should.have.a.property('redVersion','100.0.0');
|
||||
|
||||
done();
|
||||
});
|
||||
it("throws an error if a node isn't found",function(done) {
|
||||
var _join = path.join;
|
||||
stubs.push(sinon.stub(path,"join",function() {
|
||||
if (arguments[0] == resourcesDir) {
|
||||
// This stops the module tree scan from going any higher
|
||||
// up the tree than resourcesDir.
|
||||
return arguments[0];
|
||||
}
|
||||
return _join.apply(null,arguments);
|
||||
}));
|
||||
localfilesystem.init({settings:{coreNodesDir:moduleDir}});
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
localfilesystem.getModuleFiles('WontExistModule');
|
||||
}).should.throw();
|
||||
done();
|
||||
});
|
||||
it.skip("finds locales directory");
|
||||
it.skip("finds icon path directory");
|
||||
it("scans icon files with a module file",function(done) {
|
||||
var _join = path.join;
|
||||
stubs.push(sinon.stub(path,"join",function() {
|
||||
if (arguments[0] == resourcesDir) {
|
||||
// This stops the module tree scan from going any higher
|
||||
// up the tree than resourcesDir.
|
||||
return arguments[0];
|
||||
}
|
||||
return _join.apply(null,arguments);
|
||||
}));
|
||||
localfilesystem.init({
|
||||
|
||||
// events:{emit:function(eventName,dir){
|
||||
// eventName.should.equal("node-icon-dir");
|
||||
// dir.name.should.equal("TestNodeModule");
|
||||
// dir.icons.should.be.an.Array();
|
||||
// done();
|
||||
// }},
|
||||
settings:{coreNodesDir:moduleDir}
|
||||
});
|
||||
var nodeModule = localfilesystem.getModuleFiles('TestNodeModule');
|
||||
nodeModule.should.have.property("TestNodeModule");
|
||||
nodeModule.TestNodeModule.should.have.property('icons');
|
||||
|
||||
nodeModule.TestNodeModule.icons.should.have.length(1);
|
||||
nodeModule.TestNodeModule.icons[0].should.have.property("path");
|
||||
nodeModule.TestNodeModule.icons[0].should.have.property("icons");
|
||||
nodeModule.TestNodeModule.icons[0].icons[0].should.eql("arrow-in.png");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
580
test/unit/@node-red/registry/lib/registry_spec.js
Normal file
580
test/unit/@node-red/registry/lib/registry_spec.js
Normal file
@@ -0,0 +1,580 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var when = require("when");
|
||||
var sinon = require("sinon");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry/lib/registry");
|
||||
var EventEmitter = require('events');
|
||||
|
||||
var events = new EventEmitter();
|
||||
|
||||
describe("red/nodes/registry/registry",function() {
|
||||
|
||||
afterEach(function() {
|
||||
typeRegistry.clear();
|
||||
});
|
||||
|
||||
function stubSettings(s,available,initialConfig) {
|
||||
s.available = function() {return available;};
|
||||
s.set = sinon.spy(function(s,v) { return when.resolve();});
|
||||
s.get = function(s) { return initialConfig;};
|
||||
return s;
|
||||
}
|
||||
|
||||
var settings = stubSettings({},false,null);
|
||||
var settingsWithStorageAndInitialConfig = stubSettings({},true,{"node-red":{module:"testModule",name:"testName",version:"testVersion",nodes:{"node":{id:"node-red/testName",name:"test",types:["a","b"],enabled:true}}}});
|
||||
|
||||
var testNodeSet1 = {
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"]
|
||||
};
|
||||
|
||||
var testNodeSet2 = {
|
||||
id: "test-module/test-name-2",
|
||||
module: "test-module",
|
||||
name: "test-name-2",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configB",
|
||||
types: [ "test-c","test-d"]
|
||||
};
|
||||
var testNodeSet2WithError = {
|
||||
id: "test-module/test-name-2",
|
||||
module: "test-module",
|
||||
name: "test-name-2",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
err: "I have an error",
|
||||
config: "configC",
|
||||
types: [ "test-c","test-d"]
|
||||
};
|
||||
var testNodeSet3 = {
|
||||
id: "test-module-2/test-name-3",
|
||||
module: "test-module-2",
|
||||
name: "test-name-3",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configB",
|
||||
types: [ "test-a","test-e"]
|
||||
};
|
||||
|
||||
|
||||
|
||||
describe('#init/load', function() {
|
||||
it('loads initial config', function(done) {
|
||||
typeRegistry.init(settingsWithStorageAndInitialConfig,null,events);
|
||||
typeRegistry.getNodeList().should.have.lengthOf(0);
|
||||
typeRegistry.load();
|
||||
typeRegistry.getNodeList().should.have.lengthOf(1);
|
||||
done();
|
||||
});
|
||||
|
||||
it('migrates legacy format', function(done) {
|
||||
var legacySettings = {
|
||||
available: function() { return true; },
|
||||
set: sinon.stub().returns(when.resolve()),
|
||||
get: function() { return {
|
||||
"123": {
|
||||
"name": "72-sentiment.js",
|
||||
"types": [
|
||||
"sentiment"
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
"456": {
|
||||
"name": "20-inject.js",
|
||||
"types": [
|
||||
"inject"
|
||||
],
|
||||
"enabled": true
|
||||
},
|
||||
"789": {
|
||||
"name": "testModule:a-module.js",
|
||||
"types": [
|
||||
"example"
|
||||
],
|
||||
"enabled":true,
|
||||
"module":"testModule"
|
||||
}
|
||||
}}
|
||||
};
|
||||
var expected = JSON.parse('{"node-red":{"name":"node-red","nodes":{"sentiment":{"name":"sentiment","types":["sentiment"],"enabled":true,"module":"node-red"},"inject":{"name":"inject","types":["inject"],"enabled":true,"module":"node-red"}}},"testModule":{"name":"testModule","nodes":{"a-module.js":{"name":"a-module.js","types":["example"],"enabled":true,"module":"testModule"}}}}');
|
||||
typeRegistry.init(legacySettings,null,events);
|
||||
typeRegistry.load();
|
||||
legacySettings.set.calledOnce.should.be.true();
|
||||
legacySettings.set.args[0][1].should.eql(expected);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe.skip('#addNodeSet', function() {
|
||||
it('adds a node set for an unknown module', function() {
|
||||
|
||||
typeRegistry.init(settings,null,events);
|
||||
|
||||
typeRegistry.getNodeList().should.have.lengthOf(0);
|
||||
typeRegistry.getModuleList().should.eql({});
|
||||
|
||||
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
|
||||
|
||||
typeRegistry.getNodeList().should.have.lengthOf(1);
|
||||
var moduleList = typeRegistry.getModuleList();
|
||||
moduleList.should.have.a.property("test-module");
|
||||
moduleList["test-module"].should.have.a.property("name","test-module");
|
||||
moduleList["test-module"].should.have.a.property("version","0.0.1");
|
||||
moduleList["test-module"].should.have.a.property("nodes");
|
||||
moduleList["test-module"].nodes.should.have.a.property("test-name");
|
||||
|
||||
moduleList["test-module"].nodes["test-name"].should.eql({
|
||||
config: 'configA',
|
||||
id: 'test-module/test-name',
|
||||
module: 'test-module',
|
||||
name: 'test-name',
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
types: [ 'test-a', 'test-b' ]
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('adds a node set to an existing module', function() {
|
||||
|
||||
typeRegistry.init(settings,null,events);
|
||||
typeRegistry.getNodeList().should.have.lengthOf(0);
|
||||
typeRegistry.getModuleList().should.eql({});
|
||||
|
||||
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
|
||||
|
||||
typeRegistry.getNodeList().should.have.lengthOf(1);
|
||||
var moduleList = typeRegistry.getModuleList();
|
||||
Object.keys(moduleList).should.have.a.lengthOf(1);
|
||||
moduleList.should.have.a.property("test-module");
|
||||
moduleList["test-module"].should.have.a.property("name","test-module");
|
||||
moduleList["test-module"].should.have.a.property("version","0.0.1");
|
||||
moduleList["test-module"].should.have.a.property("nodes");
|
||||
|
||||
Object.keys(moduleList["test-module"].nodes).should.have.a.lengthOf(1);
|
||||
moduleList["test-module"].nodes.should.have.a.property("test-name");
|
||||
|
||||
|
||||
typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2);
|
||||
|
||||
typeRegistry.getNodeList().should.have.lengthOf(2);
|
||||
moduleList = typeRegistry.getModuleList();
|
||||
Object.keys(moduleList).should.have.a.lengthOf(1);
|
||||
Object.keys(moduleList["test-module"].nodes).should.have.a.lengthOf(2);
|
||||
moduleList["test-module"].nodes.should.have.a.property("test-name");
|
||||
moduleList["test-module"].nodes.should.have.a.property("test-name-2");
|
||||
});
|
||||
|
||||
it('doesnt add node set types if node set has an error', function() {
|
||||
typeRegistry.init(settings,null,events);
|
||||
typeRegistry.getNodeList().should.have.lengthOf(0);
|
||||
typeRegistry.getModuleList().should.eql({});
|
||||
|
||||
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
|
||||
|
||||
typeRegistry.getTypeId("test-a").should.eql("test-module/test-name");
|
||||
|
||||
should.not.exist(typeRegistry.getTypeId("test-c"));
|
||||
|
||||
typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2WithError, "0.0.1");
|
||||
|
||||
should.not.exist(typeRegistry.getTypeId("test-c"));
|
||||
});
|
||||
|
||||
it('doesnt add node set if type already exists', function() {
|
||||
typeRegistry.init(settings,null,events);
|
||||
typeRegistry.getNodeList().should.have.lengthOf(0);
|
||||
typeRegistry.getModuleList().should.eql({});
|
||||
|
||||
should.not.exist(typeRegistry.getTypeId("test-e"));
|
||||
|
||||
typeRegistry.addNodeSet("test-module/test-name",testNodeSet1, "0.0.1");
|
||||
typeRegistry.getNodeList().should.have.lengthOf(1);
|
||||
should.exist(typeRegistry.getTypeId("test-a"));
|
||||
typeRegistry.addNodeSet(testNodeSet3.id,testNodeSet3, "0.0.1");
|
||||
typeRegistry.getNodeList().should.have.lengthOf(2);
|
||||
|
||||
// testNodeSet3 registers a duplicate test-a and unique test-e
|
||||
// as test-a is a duplicate, test-e should not get registered
|
||||
should.not.exist(typeRegistry.getTypeId("test-e"));
|
||||
|
||||
var testNodeSet3Result = typeRegistry.getNodeList()[1];
|
||||
should.exist(testNodeSet3Result.err);
|
||||
testNodeSet3Result.err.code.should.equal("type_already_registered");
|
||||
testNodeSet3Result.err.details.type.should.equal("test-a");
|
||||
testNodeSet3Result.err.details.moduleA.should.equal("test-module");
|
||||
testNodeSet3Result.err.details.moduleB.should.equal("test-module-2");
|
||||
|
||||
//
|
||||
// typeRegistry.addNodeSet("test-module/test-name-2",testNodeSet2WithError, "0.0.1");
|
||||
//
|
||||
// should.not.exist(typeRegistry.getTypeId("test-c"));
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe("#enableNodeSet", function() {
|
||||
it('throws error if settings unavailable', function() {
|
||||
typeRegistry.init(settings,null,events);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.enableNodeSet("test-module/test-name");
|
||||
}).should.throw("Settings unavailable");
|
||||
});
|
||||
|
||||
it('throws error if module unknown', function() {
|
||||
typeRegistry.init(settingsWithStorageAndInitialConfig,null,events);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.enableNodeSet("test-module/unknown");
|
||||
}).should.throw("Unrecognised id: test-module/unknown");
|
||||
});
|
||||
it.skip('enables the node',function(){})
|
||||
|
||||
});
|
||||
describe("#disableNodeSet", function() {
|
||||
it('throws error if settings unavailable', function() {
|
||||
typeRegistry.init(settings,null,events);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.disableNodeSet("test-module/test-name");
|
||||
}).should.throw("Settings unavailable");
|
||||
});
|
||||
|
||||
it('throws error if module unknown', function() {
|
||||
typeRegistry.init(settingsWithStorageAndInitialConfig,null,events);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.disableNodeSet("test-module/unknown");
|
||||
}).should.throw("Unrecognised id: test-module/unknown");
|
||||
});
|
||||
it.skip('disables the node',function(){})
|
||||
});
|
||||
|
||||
describe('#getNodeConfig', function() {
|
||||
it('returns nothing for an unregistered type config', function(done) {
|
||||
typeRegistry.init(settings,null,events);
|
||||
var config = typeRegistry.getNodeConfig("imaginary-shark");
|
||||
(config === null).should.be.true();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#saveNodeList',function() {
|
||||
it('rejects when settings unavailable',function(done) {
|
||||
typeRegistry.init(stubSettings({},false,{}),null,events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {"test-name":{module:"test-module",name:"test-name",types:[]}}});
|
||||
typeRegistry.saveNodeList().catch(function(err) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('saves the list',function(done) {
|
||||
var s = stubSettings({},true,{});
|
||||
typeRegistry.init(s,null,events);
|
||||
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":testNodeSet1,
|
||||
"test-name-2":testNodeSet2WithError
|
||||
}});
|
||||
|
||||
typeRegistry.saveNodeList().then(function() {
|
||||
s.set.called.should.be.true();
|
||||
s.set.lastCall.args[0].should.eql('nodes');
|
||||
var nodes = s.set.lastCall.args[1];
|
||||
nodes.should.have.property('test-module');
|
||||
for (var n in nodes['test-module'].nodes) {
|
||||
if (nodes['test-module'].nodes.hasOwnProperty(n)) {
|
||||
var nn = nodes['test-module'].nodes[n];
|
||||
nn.should.not.have.property('err');
|
||||
nn.should.not.have.property('id');
|
||||
}
|
||||
}
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#removeModule',function() {
|
||||
it('throws error for unknown module', function() {
|
||||
var s = stubSettings({},true,{});
|
||||
typeRegistry.init(s,null,events);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.removeModule("test-module/unknown");
|
||||
}).should.throw("Unrecognised module: test-module/unknown");
|
||||
});
|
||||
it('throws error for unavaiable settings', function() {
|
||||
var s = stubSettings({},false,{});
|
||||
typeRegistry.init(s,null,events);
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.removeModule("test-module/unknown");
|
||||
}).should.throw("Settings unavailable");
|
||||
});
|
||||
it('removes a known module', function() {
|
||||
var s = stubSettings({},true,{});
|
||||
typeRegistry.init(s,null,events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":testNodeSet1
|
||||
}});
|
||||
var moduleList = typeRegistry.getModuleList();
|
||||
moduleList.should.have.a.property("test-module");
|
||||
typeRegistry.getNodeList().should.have.lengthOf(1);
|
||||
|
||||
var info = typeRegistry.removeModule('test-module');
|
||||
moduleList = typeRegistry.getModuleList();
|
||||
moduleList.should.not.have.a.property("test-module");
|
||||
typeRegistry.getNodeList().should.have.lengthOf(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#get[All]NodeConfigs', function() {
|
||||
it('returns node config', function() {
|
||||
typeRegistry.init(settings,{
|
||||
getNodeHelp: function(config) { return "HE"+config.name+"LP" }
|
||||
},events);
|
||||
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"]
|
||||
},
|
||||
"test-name-2":{
|
||||
id: "test-module/test-name-2",
|
||||
module: "test-module",
|
||||
name: "test-name-2",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configB",
|
||||
types: [ "test-c","test-d"]
|
||||
}
|
||||
}});
|
||||
typeRegistry.getNodeConfig("test-module/test-name").should.eql('<!-- --- [red-module:test-module/test-name] --- -->\nconfigAHEtest-nameLP');
|
||||
typeRegistry.getNodeConfig("test-module/test-name-2").should.eql('<!-- --- [red-module:test-module/test-name-2] --- -->\nconfigBHEtest-name-2LP');
|
||||
typeRegistry.getAllNodeConfigs().should.eql('\n<!-- --- [red-module:test-module/test-name] --- -->\nconfigAHEtest-nameLP\n<!-- --- [red-module:test-module/test-name-2] --- -->\nconfigBHEtest-name-2LP');
|
||||
});
|
||||
});
|
||||
describe('#getModuleInfo', function() {
|
||||
it('returns module info', function() {
|
||||
typeRegistry.init(settings,{},events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"],
|
||||
file: "abc"
|
||||
}
|
||||
}});
|
||||
var moduleInfo = typeRegistry.getModuleInfo("test-module");
|
||||
moduleInfo.should.have.a.property('name','test-module');
|
||||
moduleInfo.should.have.a.property('version','0.0.1');
|
||||
moduleInfo.should.have.a.property('nodes');
|
||||
moduleInfo.nodes.should.have.a.lengthOf(1);
|
||||
moduleInfo.nodes[0].should.have.a.property('id','test-module/test-name');
|
||||
moduleInfo.nodes[0].should.not.have.a.property('file');
|
||||
});
|
||||
});
|
||||
describe('#getNodeInfo', function() {
|
||||
it('returns node info', function() {
|
||||
typeRegistry.init(settings,{},events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"],
|
||||
file: "abc"
|
||||
}
|
||||
}});
|
||||
var nodeSetInfo = typeRegistry.getNodeInfo("test-module/test-name");
|
||||
nodeSetInfo.should.have.a.property('id',"test-module/test-name");
|
||||
nodeSetInfo.should.not.have.a.property('config');
|
||||
nodeSetInfo.should.not.have.a.property('file');
|
||||
});
|
||||
});
|
||||
describe('#getFullNodeInfo', function() {
|
||||
it('returns node info', function() {
|
||||
typeRegistry.init(settings,{},events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"],
|
||||
file: "abc"
|
||||
|
||||
}
|
||||
}});
|
||||
var nodeSetInfo = typeRegistry.getFullNodeInfo("test-module/test-name");
|
||||
nodeSetInfo.should.have.a.property('id',"test-module/test-name");
|
||||
nodeSetInfo.should.have.a.property('config');
|
||||
nodeSetInfo.should.have.a.property('file');
|
||||
});
|
||||
});
|
||||
describe('#cleanModuleList', function() {
|
||||
it.skip("cleans the module list");
|
||||
});
|
||||
describe('#getNodeList', function() {
|
||||
it("returns a filtered list", function() {
|
||||
typeRegistry.init(settings,{},events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"],
|
||||
file: "abc"
|
||||
},
|
||||
"test-name-2":{
|
||||
id: "test-module/test-name-2",
|
||||
module: "test-module",
|
||||
name: "test-name-2",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configB",
|
||||
types: [ "test-c","test-d"],
|
||||
file: "def"
|
||||
}
|
||||
}});
|
||||
var filterCallCount = 0;
|
||||
var filteredList = typeRegistry.getNodeList(function(n) { filterCallCount++; return n.name === 'test-name-2';});
|
||||
filterCallCount.should.eql(2);
|
||||
filteredList.should.have.a.lengthOf(1);
|
||||
filteredList[0].should.have.a.property('id',"test-module/test-name-2");
|
||||
});
|
||||
});
|
||||
|
||||
describe('#registerNodeConstructor', function() {
|
||||
var TestNodeConstructor;
|
||||
beforeEach(function() {
|
||||
TestNodeConstructor = function TestNodeConstructor() {};
|
||||
sinon.stub(events,'emit');
|
||||
});
|
||||
afterEach(function() {
|
||||
events.emit.restore();
|
||||
});
|
||||
it('registers a node constructor', function() {
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
events.emit.calledOnce.should.be.true();
|
||||
events.emit.lastCall.args[0].should.eql('type-registered');
|
||||
events.emit.lastCall.args[1].should.eql('node-type');
|
||||
})
|
||||
it('throws error on duplicate node registration', function() {
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
events.emit.calledOnce.should.be.true();
|
||||
events.emit.lastCall.args[0].should.eql('type-registered');
|
||||
events.emit.lastCall.args[1].should.eql('node-type');
|
||||
/*jshint immed: false */
|
||||
(function(){
|
||||
typeRegistry.registerNodeConstructor('node-set','node-type',TestNodeConstructor);
|
||||
}).should.throw("node-type already registered");
|
||||
events.emit.calledOnce.should.be.true();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getNodeIconPath', function() {
|
||||
it('returns the null when getting an unknown icon', function() {
|
||||
var iconPath = typeRegistry.getNodeIconPath('random-module','youwonthaveme.png');
|
||||
should.not.exist(iconPath);
|
||||
});
|
||||
|
||||
it('returns a registered icon' , function() {
|
||||
var testIcon = path.resolve(__dirname+'/resources/userDir/lib/icons/');
|
||||
typeRegistry.init(settings,{},events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"],
|
||||
file: "abc"
|
||||
}
|
||||
},icons: [{path:testIcon,icons:['test_icon.png']}]});
|
||||
var iconPath = typeRegistry.getNodeIconPath('test-module','test_icon.png');
|
||||
iconPath.should.eql(path.resolve(testIcon+"/test_icon.png"));
|
||||
});
|
||||
|
||||
it('returns null when getting an unknown module', function() {
|
||||
var debugIcon = path.resolve(__dirname+'/../../../public/icons/debug.png');
|
||||
var iconPath = typeRegistry.getNodeIconPath('unknown-module', 'debug.png');
|
||||
should.not.exist(iconPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#getNodeIcons', function() {
|
||||
it('returns empty icon list when no modules are registered', function() {
|
||||
var iconList = typeRegistry.getNodeIcons();
|
||||
iconList.should.eql({});
|
||||
});
|
||||
|
||||
it('returns an icon list of registered node module', function() {
|
||||
var testIcon = path.resolve(__dirname+'/resources/userDir/lib/icons/');
|
||||
typeRegistry.init(settings,{},events);
|
||||
typeRegistry.addModule({name: "test-module",version:"0.0.1",nodes: {
|
||||
"test-name":{
|
||||
id: "test-module/test-name",
|
||||
module: "test-module",
|
||||
name: "test-name",
|
||||
enabled: true,
|
||||
loaded: false,
|
||||
config: "configA",
|
||||
types: [ "test-a","test-b"],
|
||||
file: "abc"
|
||||
}
|
||||
},icons: [{path:testIcon,icons:['test_icon.png']}]});
|
||||
var iconList = typeRegistry.getNodeIcons();
|
||||
iconList.should.eql({"test-module":["test_icon.png"]});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function DuffNode(n) {}
|
||||
RED.nodes.registerType("duff-node",DuffNode);
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<script type="text/x-red" data-template-name="test-node-1"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-1"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-1',{});</script>
|
||||
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function DuplicateTestNode(n) {}
|
||||
RED.nodes.registerType("test-node-1",DuplicateTestNode);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<script type="text/x-red" data-template-name="test-node-multiple-1a"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-multiple-1a"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-multiple-1a',{});</script>
|
||||
<script type="text/x-red" data-template-name="test-node-multiple-1b"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-multiple-1b"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-multiple-1b',{});</script>
|
||||
@@ -0,0 +1,7 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode1(n) {}
|
||||
RED.nodes.registerType("test-node-multiple-1a",TestNode1);
|
||||
function TestNode2(n) {}
|
||||
RED.nodes.registerType("test-node-multiple-1b",TestNode2);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<script type="text/x-red" data-template-name="nested-node-1"></script>
|
||||
<script type="text/x-red" data-help-name="nested-node-1"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('nested-node-1',{});</script>
|
||||
<style></style>
|
||||
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("nested-node-1",TestNode);
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 393 B |
@@ -0,0 +1,4 @@
|
||||
<script type="text/x-red" data-template-name="should-not-load-1"></script>
|
||||
<script type="text/x-red" data-help-name="should-not-load-1"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('should-not-load-1',{});</script>
|
||||
<style></style>
|
||||
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("should-not-load-1",TestNode);
|
||||
}
|
||||
4
test/unit/@node-red/registry/lib/resources/local/NestedDirectoryNode/NestedNode/node_modules/ShouldNotLoad.html
generated
vendored
Normal file
4
test/unit/@node-red/registry/lib/resources/local/NestedDirectoryNode/NestedNode/node_modules/ShouldNotLoad.html
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
<script type="text/x-red" data-template-name="should-not-load-2"></script>
|
||||
<script type="text/x-red" data-help-name="should-not-load-2"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('should-not-load-2',{});</script>
|
||||
<style></style>
|
||||
5
test/unit/@node-red/registry/lib/resources/local/NestedDirectoryNode/NestedNode/node_modules/ShouldNotLoad.js
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/NestedDirectoryNode/NestedNode/node_modules/ShouldNotLoad.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("should-not-load-2",TestNode);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<script type="text/x-red" data-template-name="should-not-load-3"></script>
|
||||
<script type="text/x-red" data-help-name="should-not-load-3"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('should-not-load-3',{});</script>
|
||||
<style></style>
|
||||
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("should-not-load-3",TestNode);
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<script type="text/x-red" data-template-name="test-node-1"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-1"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-1',{});</script>
|
||||
<style></style>
|
||||
<p>this should be filtered out</p>
|
||||
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("test-node-1",TestNode);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
<script type="text/x-red" data-template-name="test-node-2"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-2"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-2',{});</script>
|
||||
<style></style>
|
||||
@@ -0,0 +1,10 @@
|
||||
// A test node that exports a function which returns a resolving promise
|
||||
|
||||
var when = require("when");
|
||||
module.exports = function(RED) {
|
||||
return when.promise(function(resolve,reject) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("test-node-2",TestNode);
|
||||
resolve();
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<script type="text/x-red" data-template-name="test-node-3"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-3"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-3',{});</script>
|
||||
@@ -0,0 +1,8 @@
|
||||
// A test node that exports a function which returns a rejecting promise
|
||||
|
||||
var when = require("when");
|
||||
module.exports = function(RED) {
|
||||
return when.promise(function(resolve,reject) {
|
||||
reject("fail");
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<script type="text/x-red" data-template-name="test-node-3"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-3"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-3',{});</script>
|
||||
@@ -0,0 +1 @@
|
||||
throw new Error("fail to require");
|
||||
1
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/EmptyModule/file.txt
generated
vendored
Normal file
1
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/EmptyModule/file.txt
generated
vendored
Normal file
@@ -0,0 +1 @@
|
||||
This file exists just to ensure the parent directory is in the repository.
|
||||
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule.html
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule.html
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
<script type="text/x-red" data-template-name="test-node-mod-1"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-mod-1"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-mod-1',{});</script>
|
||||
<style></style>
|
||||
<p>this should be filtered out</p>
|
||||
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule.js
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("test-node-mod-1",TestNode);
|
||||
}
|
||||
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule2.html
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule2.html
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
<script type="text/x-red" data-template-name="test-node-mod-2"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-mod-2"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-mod-2',{});</script>
|
||||
<style></style>
|
||||
<p>this should be filtered out</p>
|
||||
4
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule2.js
generated
vendored
Normal file
4
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/TestNodeModule2.js
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
throw new Error("fail to load");
|
||||
}
|
||||
BIN
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/icons/arrow-in.png
generated
vendored
Normal file
BIN
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/icons/arrow-in.png
generated
vendored
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 393 B |
3
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/icons/file.txt
generated
vendored
Normal file
3
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/icons/file.txt
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This file exists just to ensure the 'icons' directory is in the repository.
|
||||
TODO: a future test needs to ensure the right icon files are loaded - this
|
||||
directory can be used for that
|
||||
11
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/package.json
generated
vendored
Normal file
11
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/TestNodeModule/package.json
generated
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
{
|
||||
"name" : "TestNodeModule",
|
||||
"version" : "0.0.1",
|
||||
"description" : "A test node module",
|
||||
"node-red" : {
|
||||
"nodes": {
|
||||
"TestNodeMod1": "TestNodeModule.js",
|
||||
"TestNodeMod2": "TestNodeModule2.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule.html
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule.html
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
<script type="text/x-red" data-template-name="test-node-mod-1"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-mod-1"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-mod-1',{});</script>
|
||||
<style></style>
|
||||
<p>this should be filtered out</p>
|
||||
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule.js
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule.js
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
function TestNode(n) {}
|
||||
RED.nodes.registerType("test-node-mod-1",TestNode);
|
||||
}
|
||||
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule2.html
generated
vendored
Normal file
5
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule2.html
generated
vendored
Normal file
@@ -0,0 +1,5 @@
|
||||
<script type="text/x-red" data-template-name="test-node-mod-2"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-mod-2"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-mod-2',{});</script>
|
||||
<style></style>
|
||||
<p>this should be filtered out</p>
|
||||
4
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule2.js
generated
vendored
Normal file
4
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/TestNodeModule2.js
generated
vendored
Normal file
@@ -0,0 +1,4 @@
|
||||
// A test node that exports a function
|
||||
module.exports = function(RED) {
|
||||
throw new Error("fail to load");
|
||||
}
|
||||
3
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/icons/file.txt
generated
vendored
Normal file
3
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/icons/file.txt
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This file exists just to ensure the 'icons' directory is in the repository.
|
||||
TODO: a future test needs to ensure the right icon files are loaded - this
|
||||
directory can be used for that
|
||||
12
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/package.json
generated
vendored
Normal file
12
test/unit/@node-red/registry/lib/resources/local/TestNodeModule/node_modules/VersionMismatchModule/package.json
generated
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"name" : "VersionMismatchModule",
|
||||
"version" : "0.0.1",
|
||||
"description" : "A test node module",
|
||||
"node-red" : {
|
||||
"version": "100.0.0",
|
||||
"nodes": {
|
||||
"VersionMismatchMod1": "TestNodeModule.js",
|
||||
"VersionMismatchMod2": "TestNodeModule2.js"
|
||||
}
|
||||
}
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 163 B |
@@ -0,0 +1,5 @@
|
||||
<script type="text/x-red" data-template-name="test-node-5"></script>
|
||||
<script type="text/x-red" data-help-name="test-node-5"></script>
|
||||
<script type="text/javascript">RED.nodes.registerType('test-node-5',{});</script>
|
||||
<style></style>
|
||||
<p>this should be filtered out</p>
|
||||
@@ -0,0 +1 @@
|
||||
throw new Error("fail to require");
|
||||
243
test/unit/@node-red/runtime/lib/api/comms_spec.js
Normal file
243
test/unit/@node-red/runtime/lib/api/comms_spec.js
Normal file
@@ -0,0 +1,243 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var comms = NR_TEST_UTILS.require("@node-red/runtime/lib/api/comms");
|
||||
|
||||
describe("runtime-api/comms", function() {
|
||||
describe("listens for events", function() {
|
||||
var messages = [];
|
||||
var clientConnection = {
|
||||
send: function(topic,data) {
|
||||
messages.push({topic,data})
|
||||
}
|
||||
}
|
||||
var eventHandlers = {};
|
||||
before(function(done) {
|
||||
comms.init({
|
||||
log: {
|
||||
trace: function(){}
|
||||
},
|
||||
events: {
|
||||
removeListener: function() {},
|
||||
on: function(evt,handler) {
|
||||
eventHandlers[evt] = handler;
|
||||
}
|
||||
}
|
||||
})
|
||||
comms.addConnection({client: clientConnection}).then(done);
|
||||
})
|
||||
after(function(done) {
|
||||
comms.removeConnection({client: clientConnection}).then(done);
|
||||
})
|
||||
afterEach(function() {
|
||||
messages = [];
|
||||
})
|
||||
|
||||
it('runtime events',function(){
|
||||
eventHandlers.should.have.property('runtime-event');
|
||||
eventHandlers['runtime-event']({
|
||||
id: "my-event",
|
||||
payload: "my-payload"
|
||||
})
|
||||
messages.should.have.length(1);
|
||||
messages[0].should.have.property("topic","notification/my-event");
|
||||
messages[0].should.have.property("data","my-payload")
|
||||
})
|
||||
it('status events',function(){
|
||||
eventHandlers.should.have.property('node-status');
|
||||
eventHandlers['node-status']({
|
||||
id: "my-event",
|
||||
status: "my-status"
|
||||
})
|
||||
messages.should.have.length(1);
|
||||
messages[0].should.have.property("topic","status/my-event");
|
||||
messages[0].should.have.property("data","my-status")
|
||||
})
|
||||
it('comms events',function(){
|
||||
eventHandlers.should.have.property('runtime-event');
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(1);
|
||||
messages[0].should.have.property("topic","my-topic");
|
||||
messages[0].should.have.property("data","my-payload")
|
||||
})
|
||||
});
|
||||
describe("manages connections", function() {
|
||||
var eventHandlers = {};
|
||||
var messages = [];
|
||||
var clientConnection1 = {
|
||||
send: function(topic,data) {
|
||||
messages.push({topic,data})
|
||||
}
|
||||
}
|
||||
var clientConnection2 = {
|
||||
send: function(topic,data) {
|
||||
messages.push({topic,data})
|
||||
}
|
||||
}
|
||||
before(function() {
|
||||
comms.init({
|
||||
log: {
|
||||
trace: function(){}
|
||||
},
|
||||
events: {
|
||||
removeListener: function() {},
|
||||
on: function(evt,handler) {
|
||||
eventHandlers[evt] = handler;
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
afterEach(function(done) {
|
||||
comms.removeConnection({client: clientConnection1}).then(function() {
|
||||
comms.removeConnection({client: clientConnection2}).then(done);
|
||||
});
|
||||
messages = [];
|
||||
})
|
||||
it('adds new connections',function(done){
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(0);
|
||||
comms.addConnection({client: clientConnection1}).then(function() {
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(1);
|
||||
comms.addConnection({client: clientConnection2}).then(function() {
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(3);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
it('removes connections',function(done){
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(0);
|
||||
comms.addConnection({client: clientConnection1}).then(function() {
|
||||
comms.addConnection({client: clientConnection2}).then(function() {
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(2);
|
||||
comms.removeConnection({client: clientConnection1}).then(function() {
|
||||
eventHandlers['comms']({
|
||||
topic: "my-topic",
|
||||
data: "my-payload"
|
||||
})
|
||||
messages.should.have.length(3);
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
})
|
||||
})
|
||||
|
||||
describe("subscriptions", function() {
|
||||
var messages = [];
|
||||
var clientConnection = {
|
||||
send: function(topic,data) {
|
||||
messages.push({topic,data})
|
||||
}
|
||||
}
|
||||
var clientConnection2 = {
|
||||
send: function(topic,data) {
|
||||
messages.push({topic,data})
|
||||
}
|
||||
}
|
||||
var eventHandlers = {};
|
||||
before(function() {
|
||||
comms.init({
|
||||
log: {
|
||||
trace: function(){}
|
||||
},
|
||||
events: {
|
||||
removeListener: function() {},
|
||||
on: function(evt,handler) {
|
||||
eventHandlers[evt] = handler;
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
afterEach(function(done) {
|
||||
messages = [];
|
||||
comms.removeConnection({client: clientConnection}).then(done);
|
||||
})
|
||||
|
||||
it('subscribe triggers retained messages',function(done){
|
||||
eventHandlers['comms']({
|
||||
topic: "my-event",
|
||||
data: "my-payload",
|
||||
retain: true
|
||||
})
|
||||
messages.should.have.length(0);
|
||||
comms.addConnection({client: clientConnection}).then(function() {
|
||||
return comms.subscribe({client: clientConnection, topic: "my-event"}).then(function() {
|
||||
messages.should.have.length(1);
|
||||
messages[0].should.have.property("topic","my-event");
|
||||
messages[0].should.have.property("data","my-payload");
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
})
|
||||
it('retained messages get cleared',function(done) {
|
||||
eventHandlers['comms']({
|
||||
topic: "my-event",
|
||||
data: "my-payload",
|
||||
retain: true
|
||||
})
|
||||
messages.should.have.length(0);
|
||||
comms.addConnection({client: clientConnection}).then(function() {
|
||||
return comms.subscribe({client: clientConnection, topic: "my-event"}).then(function() {
|
||||
messages.should.have.length(1);
|
||||
messages[0].should.have.property("topic","my-event");
|
||||
messages[0].should.have.property("data","my-payload");
|
||||
// Now we have a retained message, clear it
|
||||
eventHandlers['comms']({
|
||||
topic: "my-event",
|
||||
data: "my-payload-cleared"
|
||||
});
|
||||
messages.should.have.length(2);
|
||||
messages[1].should.have.property("topic","my-event");
|
||||
messages[1].should.have.property("data","my-payload-cleared");
|
||||
// Now add a second client and subscribe - no message should arrive
|
||||
return comms.addConnection({client: clientConnection2}).then(function() {
|
||||
return comms.subscribe({client: clientConnection2, topic: "my-event"}).then(function() {
|
||||
messages.should.have.length(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
})
|
||||
|
||||
});
|
||||
21
test/unit/@node-red/runtime/lib/api/context_spec.js
Normal file
21
test/unit/@node-red/runtime/lib/api/context_spec.js
Normal file
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("runtime-api/context", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
});
|
||||
414
test/unit/@node-red/runtime/lib/api/flows_spec.js
Normal file
414
test/unit/@node-red/runtime/lib/api/flows_spec.js
Normal file
@@ -0,0 +1,414 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/api/flows")
|
||||
|
||||
var mockLog = () => ({
|
||||
log: sinon.stub(),
|
||||
debug: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
metric: sinon.stub(),
|
||||
audit: sinon.stub(),
|
||||
_: function() { return "abc"}
|
||||
})
|
||||
|
||||
describe("runtime-api/flows", function() {
|
||||
describe("getFlows", function() {
|
||||
it("returns the current flow configuration", function(done) {
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
getFlows: function() { return [1,2,3] }
|
||||
}
|
||||
});
|
||||
flows.getFlows({}).then(function(result) {
|
||||
result.should.eql([1,2,3]);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setFlows", function() {
|
||||
var setFlows;
|
||||
var loadFlows;
|
||||
var reloadError = false;
|
||||
beforeEach(function() {
|
||||
setFlows = sinon.spy(function(flows,type) {
|
||||
if (flows[0] === "error") {
|
||||
var err = new Error("error");
|
||||
err.code = "error";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
return Promise.resolve("newRev");
|
||||
});
|
||||
loadFlows = sinon.spy(function() {
|
||||
if (!reloadError) {
|
||||
return Promise.resolve("newLoadRev");
|
||||
} else {
|
||||
var err = new Error("error");
|
||||
err.code = "error";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
})
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
getFlows: function() { return {rev:"currentRev",flows:[]} },
|
||||
setFlows: setFlows,
|
||||
loadFlows: loadFlows
|
||||
}
|
||||
})
|
||||
|
||||
})
|
||||
it("defaults to full deploy", function(done) {
|
||||
flows.setFlows({
|
||||
flows: {flows:[4,5,6]}
|
||||
}).then(function(result) {
|
||||
result.should.eql({rev:"newRev"});
|
||||
setFlows.called.should.be.true();
|
||||
setFlows.lastCall.args[0].should.eql([4,5,6]);
|
||||
setFlows.lastCall.args[1].should.eql("full");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("passes through other deploy types", function(done) {
|
||||
flows.setFlows({
|
||||
deploymentType: "nodes",
|
||||
flows: {flows:[4,5,6]}
|
||||
}).then(function(result) {
|
||||
result.should.eql({rev:"newRev"});
|
||||
setFlows.called.should.be.true();
|
||||
setFlows.lastCall.args[0].should.eql([4,5,6]);
|
||||
setFlows.lastCall.args[1].should.eql("nodes");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("triggers a flow reload", function(done) {
|
||||
flows.setFlows({
|
||||
deploymentType: "reload"
|
||||
}).then(function(result) {
|
||||
result.should.eql({rev:"newLoadRev"});
|
||||
setFlows.called.should.be.false();
|
||||
loadFlows.called.should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("allows update when revision matches", function(done) {
|
||||
flows.setFlows({
|
||||
deploymentType: "nodes",
|
||||
flows: {flows:[4,5,6],rev:"currentRev"}
|
||||
}).then(function(result) {
|
||||
result.should.eql({rev:"newRev"});
|
||||
setFlows.called.should.be.true();
|
||||
setFlows.lastCall.args[0].should.eql([4,5,6]);
|
||||
setFlows.lastCall.args[1].should.eql("nodes");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects update when revision does not match", function(done) {
|
||||
flows.setFlows({
|
||||
deploymentType: "nodes",
|
||||
flows: {flows:[4,5,6],rev:"notTheCurrentRev"}
|
||||
}).then(function(result) {
|
||||
done(new Error("Did not reject rev mismatch"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','version_mismatch');
|
||||
err.should.have.property('status',409);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when reload fails",function(done) {
|
||||
reloadError = true;
|
||||
flows.setFlows({
|
||||
deploymentType: "reload"
|
||||
}).then(function(result) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','error');
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when update fails",function(done) {
|
||||
flows.setFlows({
|
||||
deploymentType: "full",
|
||||
flows: {flows:["error",5,6]}
|
||||
}).then(function(result) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','error');
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("addFlow", function() {
|
||||
var addFlow;
|
||||
beforeEach(function() {
|
||||
addFlow = sinon.spy(function(flow) {
|
||||
if (flow === "error") {
|
||||
var err = new Error("error");
|
||||
err.code = "error";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
return Promise.resolve("newId");
|
||||
});
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
addFlow: addFlow
|
||||
}
|
||||
});
|
||||
})
|
||||
it("adds a flow", function(done) {
|
||||
flows.addFlow({flow:{a:"123"}}).then(function(id) {
|
||||
addFlow.called.should.be.true();
|
||||
addFlow.lastCall.args[0].should.eql({a:"123"});
|
||||
id.should.eql("newId");
|
||||
done()
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when add fails", function(done) {
|
||||
flows.addFlow({flow:"error"}).then(function(id) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','error');
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
describe("getFlow", function() {
|
||||
var getFlow;
|
||||
beforeEach(function() {
|
||||
getFlow = sinon.spy(function(flow) {
|
||||
if (flow === "unknown") {
|
||||
return null;
|
||||
}
|
||||
return [1,2,3];
|
||||
});
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
getFlow: getFlow
|
||||
}
|
||||
});
|
||||
})
|
||||
it("gets a flow", function(done) {
|
||||
flows.getFlow({id:"123"}).then(function(flow) {
|
||||
flow.should.eql([1,2,3]);
|
||||
done()
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when flow not found", function(done) {
|
||||
flows.getFlow({id:"unknown"}).then(function(flow) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','not_found');
|
||||
err.should.have.property('status',404);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("updateFlow", function() {
|
||||
var updateFlow;
|
||||
beforeEach(function() {
|
||||
updateFlow = sinon.spy(function(id,flow) {
|
||||
if (id === "unknown") {
|
||||
var err = new Error();
|
||||
// TODO: quirk of internal api - uses .code for .status
|
||||
err.code = 404;
|
||||
throw err;
|
||||
} else if (id === "error") {
|
||||
var err = new Error();
|
||||
// TODO: quirk of internal api - uses .code for .status
|
||||
err.code = "error";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
return Promise.resolve();
|
||||
});
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
updateFlow: updateFlow
|
||||
}
|
||||
});
|
||||
})
|
||||
it("updates a flow", function(done) {
|
||||
flows.updateFlow({id:"123",flow:[1,2,3]}).then(function(id) {
|
||||
id.should.eql("123");
|
||||
updateFlow.called.should.be.true();
|
||||
updateFlow.lastCall.args[0].should.eql("123");
|
||||
updateFlow.lastCall.args[1].should.eql([1,2,3]);
|
||||
done()
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when flow not found", function(done) {
|
||||
flows.updateFlow({id:"unknown"}).then(function(flow) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','not_found');
|
||||
err.should.have.property('status',404);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when update fails", function(done) {
|
||||
flows.updateFlow({id:"error"}).then(function(flow) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','error');
|
||||
err.should.have.property('status',400);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe("deleteFlow", function() {
|
||||
var removeFlow;
|
||||
beforeEach(function() {
|
||||
removeFlow = sinon.spy(function(flow) {
|
||||
if (flow === "unknown") {
|
||||
var err = new Error();
|
||||
// TODO: quirk of internal api - uses .code for .status
|
||||
err.code = 404;
|
||||
throw err;
|
||||
} else if (flow === "error") {
|
||||
var err = new Error();
|
||||
// TODO: quirk of internal api - uses .code for .status
|
||||
err.code = "error";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
return Promise.resolve();
|
||||
});
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
removeFlow: removeFlow
|
||||
}
|
||||
});
|
||||
})
|
||||
it("deletes a flow", function(done) {
|
||||
flows.deleteFlow({id:"123"}).then(function() {
|
||||
removeFlow.called.should.be.true();
|
||||
removeFlow.lastCall.args[0].should.eql("123");
|
||||
done()
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when flow not found", function(done) {
|
||||
flows.deleteFlow({id:"unknown"}).then(function(flow) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','not_found');
|
||||
err.should.have.property('status',404);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("rejects when delete fails", function(done) {
|
||||
flows.deleteFlow({id:"error"}).then(function(flow) {
|
||||
done(new Error("Did not return internal error"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','error');
|
||||
err.should.have.property('status',400);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe("getNodeCredentials", function() {
|
||||
beforeEach(function() {
|
||||
flows.init({
|
||||
log: mockLog(),
|
||||
nodes: {
|
||||
getCredentials: function(id) {
|
||||
if (id === "unknown") {
|
||||
return undefined;
|
||||
} else if (id === "known") {
|
||||
return {
|
||||
username: "abc",
|
||||
password: "123"
|
||||
}
|
||||
} else if (id === "known2") {
|
||||
return {
|
||||
username: "abc",
|
||||
password: ""
|
||||
}
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
},
|
||||
getCredentialDefinition: function(type) {
|
||||
if (type === "node") {
|
||||
return {
|
||||
username: {type:"text"},
|
||||
password: {type:"password"}
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
})
|
||||
it("returns an empty object for an unknown node", function(done) {
|
||||
flows.getNodeCredentials({id:"unknown", type:"node"}).then(function(result) {
|
||||
result.should.eql({});
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("gets the filtered credentials for a known node with password", function(done) {
|
||||
flows.getNodeCredentials({id:"known", type:"node"}).then(function(result) {
|
||||
result.should.eql({
|
||||
username: "abc",
|
||||
has_password: true
|
||||
});
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("gets the filtered credentials for a known node without password", function(done) {
|
||||
flows.getNodeCredentials({id:"known2", type:"node"}).then(function(result) {
|
||||
result.should.eql({
|
||||
username: "abc",
|
||||
has_password: false
|
||||
});
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it("gets the empty credentials for a known node without a registered definition", function(done) {
|
||||
flows.getNodeCredentials({id:"known2", type:"unknown-type"}).then(function(result) {
|
||||
result.should.eql({});
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
55
test/unit/@node-red/runtime/lib/api/index_spec.js
Normal file
55
test/unit/@node-red/runtime/lib/api/index_spec.js
Normal file
@@ -0,0 +1,55 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var index = NR_TEST_UTILS.require("@node-red/runtime/lib/api/index");
|
||||
|
||||
|
||||
describe("runtime-api/index", function() {
|
||||
before(function() {
|
||||
["comms","flows","nodes","settings","library","projects"].forEach(n => {
|
||||
sinon.stub(NR_TEST_UTILS.require(`@node-red/runtime/lib/api/${n}`),"init",()=>{});
|
||||
})
|
||||
});
|
||||
after(function() {
|
||||
["comms","flows","nodes","settings","library","projects"].forEach(n => {
|
||||
NR_TEST_UTILS.require(`@node-red/runtime/lib/api/${n}`).init.restore()
|
||||
})
|
||||
})
|
||||
it('isStarted', function(done) {
|
||||
index.init({
|
||||
isStarted: ()=>true
|
||||
});
|
||||
index.isStarted({}).then(function(started) {
|
||||
started.should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
})
|
||||
|
||||
it('isStarted', function(done) {
|
||||
index.init({
|
||||
version: ()=>"1.2.3.4"
|
||||
});
|
||||
index.version({}).then(function(version) {
|
||||
version.should.eql("1.2.3.4");
|
||||
done();
|
||||
}).catch(done);
|
||||
})
|
||||
|
||||
});
|
||||
539
test/unit/@node-red/runtime/lib/api/library_spec.js
Normal file
539
test/unit/@node-red/runtime/lib/api/library_spec.js
Normal file
@@ -0,0 +1,539 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var library = NR_TEST_UTILS.require("@node-red/runtime/lib/api/library")
|
||||
|
||||
var mockLog = {
|
||||
log: sinon.stub(),
|
||||
debug: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
metric: sinon.stub(),
|
||||
audit: sinon.stub(),
|
||||
_: function() { return "abc"}
|
||||
}
|
||||
|
||||
describe("runtime-api/library", function() {
|
||||
describe("getEntry", function() {
|
||||
before(function() {
|
||||
library.init({
|
||||
log: mockLog,
|
||||
library: {
|
||||
getEntry: function(type,path) {
|
||||
if (type === "known") {
|
||||
return Promise.resolve("known");
|
||||
} else if (type === "forbidden") {
|
||||
var err = new Error("forbidden");
|
||||
err.code = "forbidden";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
} else if (type === "not_found") {
|
||||
var err = new Error("forbidden");
|
||||
err.code = "not_found";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
} else if (type === "error") {
|
||||
var err = new Error("error");
|
||||
err.code = "unknown_error";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
} else if (type === "blank") {
|
||||
return Promise.reject();
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
it("returns a known entry", function(done) {
|
||||
library.getEntry({type: "known", path: "/abc"}).then(function(result) {
|
||||
result.should.eql("known")
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
it("rejects a forbidden entry", function(done) {
|
||||
library.getEntry({type: "forbidden", path: "/abc"}).then(function(result) {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("code","forbidden");
|
||||
err.should.have.property("status",403);
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
it("rejects an unknown entry", function(done) {
|
||||
library.getEntry({type: "not_found", path: "/abc"}).then(function(result) {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("code","not_found");
|
||||
err.should.have.property("status",404);
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
it("rejects a blank (unknown) entry", function(done) {
|
||||
library.getEntry({type: "blank", path: "/abc"}).then(function(result) {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("code","not_found");
|
||||
err.should.have.property("status",404);
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
it("rejects unexpected error", function(done) {
|
||||
library.getEntry({type: "error", path: "/abc"}).then(function(result) {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("status",400);
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
})
|
||||
describe("saveEntry", function() {
|
||||
var opts;
|
||||
before(function() {
|
||||
library.init({
|
||||
log: mockLog,
|
||||
library: {
|
||||
saveEntry: function(type,path,meta,body) {
|
||||
opts = {type,path,meta,body};
|
||||
if (type === "known") {
|
||||
return Promise.resolve();
|
||||
} else if (type === "forbidden") {
|
||||
var err = new Error("forbidden");
|
||||
err.code = "forbidden";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
} else if (type === "not_found") {
|
||||
var err = new Error("forbidden");
|
||||
err.code = "not_found";
|
||||
var p = Promise.reject(err);
|
||||
p.catch(()=>{});
|
||||
return p;
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
it("saves an entry", function(done) {
|
||||
library.saveEntry({type: "known", path: "/abc", meta: {a:1}, body:"123"}).then(function() {
|
||||
opts.should.have.property("type","known");
|
||||
opts.should.have.property("path","/abc");
|
||||
opts.should.have.property("meta",{a:1});
|
||||
opts.should.have.property("body","123");
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
it("rejects a forbidden entry", function(done) {
|
||||
library.saveEntry({type: "forbidden", path: "/abc", meta: {a:1}, body:"123"}).then(function() {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("code","forbidden");
|
||||
err.should.have.property("status",403);
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
it("rejects an unknown entry", function(done) {
|
||||
library.saveEntry({type: "not_found", path: "/abc", meta: {a:1}, body:"123"}).then(function() {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("status",400);
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
})
|
||||
describe("getEntries", function() {
|
||||
var opts;
|
||||
before(function() {
|
||||
library.init({
|
||||
log: mockLog,
|
||||
storage: {
|
||||
getAllFlows: function() {
|
||||
return Promise.resolve({a:1});
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
getNodeExampleFlows: function() {
|
||||
return {b:2};
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
it("returns all flows", function(done) {
|
||||
library.getEntries({type:"flows"}).then(function(result) {
|
||||
result.should.eql({a:1,d:{_examples_:{b:2}}});
|
||||
done();
|
||||
}).catch(done)
|
||||
});
|
||||
it("fails for non-flows (currently)", function(done) {
|
||||
library.getEntries({type:"functions"}).then(function(result) {
|
||||
done(new Error("did not reject"));
|
||||
}).catch(function(err) {
|
||||
done();
|
||||
}).catch(done)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
/*
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var fs = require("fs");
|
||||
var fspath = require('path');
|
||||
var request = require('supertest');
|
||||
var express = require('express');
|
||||
var bodyParser = require('body-parser');
|
||||
|
||||
var when = require('when');
|
||||
|
||||
var app;
|
||||
var library = require("../../../../red/api/editor/library");
|
||||
var auth = require("../../../../red/api/auth");
|
||||
|
||||
describe("api/editor/library", function() {
|
||||
|
||||
function initLibrary(_flows,_libraryEntries,_examples,_exampleFlowPathFunction) {
|
||||
var flows = _flows;
|
||||
var libraryEntries = _libraryEntries;
|
||||
library.init(app,{
|
||||
log:{audit:function(){},_:function(){},warn:function(){}},
|
||||
storage: {
|
||||
init: function() {
|
||||
return when.resolve();
|
||||
},
|
||||
getAllFlows: function() {
|
||||
return when.resolve(flows);
|
||||
},
|
||||
getFlow: function(fn) {
|
||||
if (flows[fn]) {
|
||||
return when.resolve(flows[fn]);
|
||||
} else if (fn.indexOf("..")!==-1) {
|
||||
var err = new Error();
|
||||
err.code = 'forbidden';
|
||||
return when.reject(err);
|
||||
} else {
|
||||
return when.reject();
|
||||
}
|
||||
},
|
||||
saveFlow: function(fn,data) {
|
||||
if (fn.indexOf("..")!==-1) {
|
||||
var err = new Error();
|
||||
err.code = 'forbidden';
|
||||
return when.reject(err);
|
||||
}
|
||||
flows[fn] = data;
|
||||
return when.resolve();
|
||||
},
|
||||
getLibraryEntry: function(type,path) {
|
||||
if (path.indexOf("..")!==-1) {
|
||||
var err = new Error();
|
||||
err.code = 'forbidden';
|
||||
return when.reject(err);
|
||||
}
|
||||
if (libraryEntries[type] && libraryEntries[type][path]) {
|
||||
return when.resolve(libraryEntries[type][path]);
|
||||
} else {
|
||||
return when.reject();
|
||||
}
|
||||
},
|
||||
saveLibraryEntry: function(type,path,meta,body) {
|
||||
if (path.indexOf("..")!==-1) {
|
||||
var err = new Error();
|
||||
err.code = 'forbidden';
|
||||
return when.reject(err);
|
||||
}
|
||||
libraryEntries[type][path] = body;
|
||||
return when.resolve();
|
||||
}
|
||||
},
|
||||
events: {
|
||||
on: function(){},
|
||||
removeListener: function(){}
|
||||
},
|
||||
nodes: {
|
||||
getNodeExampleFlows: function() {
|
||||
return _examples;
|
||||
},
|
||||
getNodeExampleFlowPath: _exampleFlowPathFunction
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
describe("flows", function() {
|
||||
before(function() {
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.get("/library/flows",library.getAll);
|
||||
app.post(new RegExp("/library/flows\/(.*)"),library.post);
|
||||
app.get(new RegExp("/library/flows\/(.*)"),library.get);
|
||||
app.response.sendFile = function (path) {
|
||||
app.response.json.call(this, {sendFile: path});
|
||||
};
|
||||
sinon.stub(fs,"statSync",function() { return true; });
|
||||
});
|
||||
after(function() {
|
||||
fs.statSync.restore();
|
||||
});
|
||||
it('returns empty result', function(done) {
|
||||
initLibrary({},{flows:{}});
|
||||
request(app)
|
||||
.get('/library/flows')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.not.have.property('f');
|
||||
res.body.should.not.have.property('d');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns 404 for non-existent entry', function(done) {
|
||||
initLibrary({},{flows:{}});
|
||||
request(app)
|
||||
.get('/library/flows/foo')
|
||||
.expect(404)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
|
||||
it('can store and retrieve item', function(done) {
|
||||
initLibrary({},{flows:{}});
|
||||
var flow = '[]';
|
||||
request(app)
|
||||
.post('/library/flows/foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(flow)
|
||||
.expect(204).end(function (err, res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
request(app)
|
||||
.get('/library/flows/foo')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.text.should.equal(flow);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('lists a stored item', function(done) {
|
||||
initLibrary({f:["bar"]});
|
||||
request(app)
|
||||
.get('/library/flows')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property('f');
|
||||
should.deepEqual(res.body.f,['bar']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns 403 for malicious get attempt', function(done) {
|
||||
initLibrary({});
|
||||
// without the userDir override the malicious url would be
|
||||
// http://127.0.0.1:1880/library/flows/../../package to
|
||||
// obtain package.json from the node-red root.
|
||||
request(app)
|
||||
.get('/library/flows/../../../../../package')
|
||||
.expect(403)
|
||||
.end(done);
|
||||
});
|
||||
it('returns 403 for malicious post attempt', function(done) {
|
||||
initLibrary({});
|
||||
// without the userDir override the malicious url would be
|
||||
// http://127.0.0.1:1880/library/flows/../../package to
|
||||
// obtain package.json from the node-red root.
|
||||
request(app)
|
||||
.post('/library/flows/../../../../../package')
|
||||
.expect(403)
|
||||
.end(done);
|
||||
});
|
||||
it('includes examples flows if set', function(done) {
|
||||
var examples = {"d":{"node-module":{"f":["example-one"]}}};
|
||||
initLibrary({},{},examples);
|
||||
request(app)
|
||||
.get('/library/flows')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property('d');
|
||||
res.body.d.should.have.property('_examples_');
|
||||
should.deepEqual(res.body.d._examples_,examples);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can retrieve an example flow', function(done) {
|
||||
var examples = {"d":{"node-module":{"f":["example-one"]}}};
|
||||
initLibrary({},{},examples,function(module,path) {
|
||||
return module + ':' + path
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows/_examples_/node-module/example-one')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property('sendFile',
|
||||
fspath.resolve('node-module') + ':example-one');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('can retrieve an example flow in an org scoped package', function(done) {
|
||||
var examples = {"d":{"@org_scope/node_package":{"f":["example-one"]}}};
|
||||
initLibrary({},{},examples,function(module,path) {
|
||||
return module + ':' + path
|
||||
});
|
||||
request(app)
|
||||
.get('/library/flows/_examples_/@org_scope/node_package/example-one')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.have.property('sendFile',
|
||||
fspath.resolve('@org_scope/node_package') +
|
||||
':example-one');
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("type", function() {
|
||||
before(function() {
|
||||
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
initLibrary({},{});
|
||||
auth.init({settings:{}});
|
||||
library.register("test");
|
||||
});
|
||||
|
||||
it('returns empty result', function(done) {
|
||||
initLibrary({},{'test':{"":[]}});
|
||||
request(app)
|
||||
.get('/library/test')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.body.should.not.have.property('f');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('returns 404 for non-existent entry', function(done) {
|
||||
initLibrary({},{});
|
||||
request(app)
|
||||
.get('/library/test/foo')
|
||||
.expect(404)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('can store and retrieve item', function(done) {
|
||||
initLibrary({},{'test':{}});
|
||||
var flow = {text:"test content"};
|
||||
request(app)
|
||||
.post('/library/test/foo')
|
||||
.set('Content-Type', 'application/json')
|
||||
.send(flow)
|
||||
.expect(204).end(function (err, res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
request(app)
|
||||
.get('/library/test/foo')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
res.text.should.equal(flow.text);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('lists a stored item', function(done) {
|
||||
initLibrary({},{'test':{'a':['abc','def']}});
|
||||
request(app)
|
||||
.get('/library/test/a')
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
// This response isn't strictly accurate - but it
|
||||
// verifies the api returns what storage gave it
|
||||
should.deepEqual(res.body,['abc','def']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('returns 403 for malicious access attempt', function(done) {
|
||||
request(app)
|
||||
.get('/library/test/../../../../../../../../../../etc/passwd')
|
||||
.expect(403)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('returns 403 for malicious access attempt', function(done) {
|
||||
request(app)
|
||||
.get('/library/test/..\\..\\..\\..\\..\\..\\..\\..\\..\\..\\etc\\passwd')
|
||||
.expect(403)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
it('returns 403 for malicious access attempt', function(done) {
|
||||
request(app)
|
||||
.post('/library/test/../../../../../../../../../../etc/passwd')
|
||||
.set('Content-Type', 'text/plain')
|
||||
.send('root:x:0:0:root:/root:/usr/bin/tclsh')
|
||||
.expect(403)
|
||||
.end(done);
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
*/
|
||||
1006
test/unit/@node-red/runtime/lib/api/nodes_spec.js
Normal file
1006
test/unit/@node-red/runtime/lib/api/nodes_spec.js
Normal file
File diff suppressed because it is too large
Load Diff
1110
test/unit/@node-red/runtime/lib/api/projects_spec.js
Normal file
1110
test/unit/@node-red/runtime/lib/api/projects_spec.js
Normal file
File diff suppressed because it is too large
Load Diff
671
test/unit/@node-red/runtime/lib/api/settings_spec.js
Normal file
671
test/unit/@node-red/runtime/lib/api/settings_spec.js
Normal file
@@ -0,0 +1,671 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var settings = NR_TEST_UTILS.require("@node-red/runtime/lib/api/settings")
|
||||
|
||||
var mockLog = () => ({
|
||||
log: sinon.stub(),
|
||||
debug: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
metric: sinon.stub(),
|
||||
audit: sinon.stub(),
|
||||
_: function() { return "abc"}
|
||||
})
|
||||
|
||||
describe("runtime-api/settings", function() {
|
||||
describe.skip("getRuntimeSettings", function() {});
|
||||
describe.skip("getUserSettings", function() {});
|
||||
describe.skip("updateUserSettings", function() {});
|
||||
describe.skip("getUserKeys", function() {});
|
||||
describe.skip("getUserKey", function() {});
|
||||
describe.skip("generateUserKey", function() {});
|
||||
describe.skip("removeUserKey", function() {});
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
||||
/*
|
||||
|
||||
before(function() {
|
||||
sinon.stub(theme,"settings",function() { return { test: 456 };});
|
||||
app = express();
|
||||
app.get("/settings",info.runtimeSettings);
|
||||
app.get("/settingsWithUser",function(req,res,next) {
|
||||
req.user = {
|
||||
username: "nick",
|
||||
permissions: "*",
|
||||
image: "http://example.com",
|
||||
anonymous: false,
|
||||
private: "secret"
|
||||
}
|
||||
next();
|
||||
},info.runtimeSettings);
|
||||
});
|
||||
after(function() {
|
||||
theme.settings.restore();
|
||||
});
|
||||
|
||||
|
||||
|
||||
it('returns the filtered settings', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
foo: 123,
|
||||
httpNodeRoot: "testHttpNodeRoot",
|
||||
version: "testVersion",
|
||||
paletteCategories :["red","blue","green"],
|
||||
exportNodeSettings: function(obj) {
|
||||
obj.testNodeSetting = "helloWorld";
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
paletteEditorEnabled: function() { return true; },
|
||||
getCredentialKeyType: function() { return "test-key-type"}
|
||||
},
|
||||
log: { error: console.error },
|
||||
storage: {}
|
||||
});
|
||||
request(app)
|
||||
.get("/settings")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("httpNodeRoot","testHttpNodeRoot");
|
||||
res.body.should.have.property("version","testVersion");
|
||||
res.body.should.have.property("paletteCategories",["red","blue","green"]);
|
||||
res.body.should.have.property("editorTheme",{test:456});
|
||||
res.body.should.have.property("testNodeSetting","helloWorld");
|
||||
res.body.should.not.have.property("foo",123);
|
||||
res.body.should.have.property("flowEncryptionType","test-key-type");
|
||||
res.body.should.not.have.property("user");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('returns the filtered user in settings', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
foo: 123,
|
||||
httpNodeRoot: "testHttpNodeRoot",
|
||||
version: "testVersion",
|
||||
paletteCategories :["red","blue","green"],
|
||||
exportNodeSettings: function(obj) {
|
||||
obj.testNodeSetting = "helloWorld";
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
paletteEditorEnabled: function() { return true; },
|
||||
getCredentialKeyType: function() { return "test-key-type"}
|
||||
},
|
||||
log: { error: console.error },
|
||||
storage: {}
|
||||
});
|
||||
request(app)
|
||||
.get("/settingsWithUser")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("user");
|
||||
res.body.user.should.have.property("username","nick");
|
||||
res.body.user.should.have.property("permissions","*");
|
||||
res.body.user.should.have.property("image","http://example.com");
|
||||
res.body.user.should.have.property("anonymous",false);
|
||||
res.body.user.should.not.have.property("private");
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('includes project settings if projects available', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
foo: 123,
|
||||
httpNodeRoot: "testHttpNodeRoot",
|
||||
version: "testVersion",
|
||||
paletteCategories :["red","blue","green"],
|
||||
exportNodeSettings: function(obj) {
|
||||
obj.testNodeSetting = "helloWorld";
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
paletteEditorEnabled: function() { return true; },
|
||||
getCredentialKeyType: function() { return "test-key-type"}
|
||||
},
|
||||
log: { error: console.error },
|
||||
storage: {
|
||||
projects: {
|
||||
getActiveProject: () => 'test-active-project',
|
||||
getFlowFilename: () => 'test-flow-file',
|
||||
getCredentialsFilename: () => 'test-creds-file',
|
||||
getGlobalGitUser: () => {return {name:'foo',email:'foo@example.com'}}
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get("/settings")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("project","test-active-project");
|
||||
res.body.should.not.have.property("files");
|
||||
res.body.should.have.property("git");
|
||||
res.body.git.should.have.property("globalUser",{name:'foo',email:'foo@example.com'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('includes existing files details if projects enabled but no active project and files exist', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
foo: 123,
|
||||
httpNodeRoot: "testHttpNodeRoot",
|
||||
version: "testVersion",
|
||||
paletteCategories :["red","blue","green"],
|
||||
exportNodeSettings: function(obj) {
|
||||
obj.testNodeSetting = "helloWorld";
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
paletteEditorEnabled: function() { return true; },
|
||||
getCredentialKeyType: function() { return "test-key-type"}
|
||||
},
|
||||
log: { error: console.error },
|
||||
storage: {
|
||||
projects: {
|
||||
flowFileExists: () => true,
|
||||
getActiveProject: () => false,
|
||||
getFlowFilename: () => 'test-flow-file',
|
||||
getCredentialsFilename: () => 'test-creds-file',
|
||||
getGlobalGitUser: () => {return {name:'foo',email:'foo@example.com'}}
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get("/settings")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.not.have.property("project");
|
||||
res.body.should.have.property("files");
|
||||
res.body.files.should.have.property("flow",'test-flow-file');
|
||||
res.body.files.should.have.property("credentials",'test-creds-file');
|
||||
res.body.should.have.property("git");
|
||||
res.body.git.should.have.property("globalUser",{name:'foo',email:'foo@example.com'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('does not include file details if projects enabled but no active project and files do not exist', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
foo: 123,
|
||||
httpNodeRoot: "testHttpNodeRoot",
|
||||
version: "testVersion",
|
||||
paletteCategories :["red","blue","green"],
|
||||
exportNodeSettings: function(obj) {
|
||||
obj.testNodeSetting = "helloWorld";
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
paletteEditorEnabled: function() { return true; },
|
||||
getCredentialKeyType: function() { return "test-key-type"}
|
||||
},
|
||||
log: { error: console.error },
|
||||
storage: {
|
||||
projects: {
|
||||
flowFileExists: () => false,
|
||||
getActiveProject: () => false,
|
||||
getFlowFilename: () => 'test-flow-file',
|
||||
getCredentialsFilename: () => 'test-creds-file',
|
||||
getGlobalGitUser: () => {return {name:'foo',email:'foo@example.com'}}
|
||||
}
|
||||
}
|
||||
});
|
||||
request(app)
|
||||
.get("/settings")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.not.have.property("project");
|
||||
res.body.should.not.have.property("files");
|
||||
res.body.should.have.property("git");
|
||||
res.body.git.should.have.property("globalUser",{name:'foo',email:'foo@example.com'});
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('overrides palette editable if runtime says it is disabled', function(done) {
|
||||
info.init({
|
||||
settings: {
|
||||
httpNodeRoot: "testHttpNodeRoot",
|
||||
version: "testVersion",
|
||||
paletteCategories :["red","blue","green"],
|
||||
exportNodeSettings: function() {}
|
||||
},
|
||||
nodes: {
|
||||
paletteEditorEnabled: function() { return false; },
|
||||
getCredentialKeyType: function() { return "test-key-type"}
|
||||
|
||||
},
|
||||
log: { error: console.error },
|
||||
storage: {}
|
||||
|
||||
});
|
||||
request(app)
|
||||
.get("/settings")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property("httpNodeRoot","testHttpNodeRoot");
|
||||
res.body.should.have.property("version","testVersion");
|
||||
res.body.should.have.property("paletteCategories",["red","blue","green"]);
|
||||
res.body.should.have.property("editorTheme");
|
||||
res.body.editorTheme.should.have.property("test",456);
|
||||
|
||||
res.body.editorTheme.should.have.property("palette",{editable:false});
|
||||
done();
|
||||
});
|
||||
})
|
||||
|
||||
*/
|
||||
/*
|
||||
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var request = require("supertest");
|
||||
var express = require("express");
|
||||
var editorApi = require("../../../../red/api/editor");
|
||||
var comms = require("../../../../red/api/editor/comms");
|
||||
var info = require("../../../../red/api/editor/settings");
|
||||
var auth = require("../../../../red/api/auth");
|
||||
var sshkeys = require("../../../../red/api/editor/sshkeys");
|
||||
var when = require("when");
|
||||
var bodyParser = require("body-parser");
|
||||
var fs = require("fs-extra");
|
||||
var fspath = require("path");
|
||||
|
||||
|
||||
describe("api/editor/sshkeys", function() {
|
||||
var app;
|
||||
var mockList = [
|
||||
'library','theme','locales','credentials','comms'
|
||||
]
|
||||
var isStarted = true;
|
||||
var errors = [];
|
||||
var session_data = {};
|
||||
|
||||
var mockRuntime = {
|
||||
settings:{
|
||||
httpNodeRoot: true,
|
||||
httpAdminRoot: true,
|
||||
disableEditor: false,
|
||||
exportNodeSettings:function(){},
|
||||
storage: {
|
||||
getSessions: function(){
|
||||
return when.resolve(session_data);
|
||||
},
|
||||
setSessions: function(_session) {
|
||||
session_data = _session;
|
||||
return when.resolve();
|
||||
}
|
||||
}
|
||||
},
|
||||
log:{audit:function(){},error:function(msg){errors.push(msg)},trace:function(){}},
|
||||
storage: {
|
||||
projects: {
|
||||
ssh: {
|
||||
init: function(){},
|
||||
listSSHKeys: function(){},
|
||||
getSSHKey: function(){},
|
||||
generateSSHKey: function(){},
|
||||
deleteSSHKey: function(){}
|
||||
}
|
||||
}
|
||||
},
|
||||
events:{on:function(){},removeListener:function(){}},
|
||||
isStarted: function() { return isStarted; },
|
||||
nodes: {paletteEditorEnabled: function() { return false }}
|
||||
};
|
||||
|
||||
before(function() {
|
||||
auth.init(mockRuntime);
|
||||
app = express();
|
||||
app.use(bodyParser.json());
|
||||
app.use(editorApi.init({},mockRuntime));
|
||||
});
|
||||
after(function() {
|
||||
})
|
||||
|
||||
beforeEach(function() {
|
||||
sinon.stub(mockRuntime.storage.projects.ssh, "listSSHKeys");
|
||||
sinon.stub(mockRuntime.storage.projects.ssh, "getSSHKey");
|
||||
sinon.stub(mockRuntime.storage.projects.ssh, "generateSSHKey");
|
||||
sinon.stub(mockRuntime.storage.projects.ssh, "deleteSSHKey");
|
||||
})
|
||||
afterEach(function() {
|
||||
mockRuntime.storage.projects.ssh.listSSHKeys.restore();
|
||||
mockRuntime.storage.projects.ssh.getSSHKey.restore();
|
||||
mockRuntime.storage.projects.ssh.generateSSHKey.restore();
|
||||
mockRuntime.storage.projects.ssh.deleteSSHKey.restore();
|
||||
})
|
||||
|
||||
it('GET /settings/user/keys --- return empty list', function(done) {
|
||||
mockRuntime.storage.projects.ssh.listSSHKeys.returns(Promise.resolve([]));
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('keys');
|
||||
res.body.keys.should.be.empty();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys --- return normal list', function(done) {
|
||||
var fileList = [
|
||||
'test_key01',
|
||||
'test_key02'
|
||||
];
|
||||
var retList = fileList.map(function(elem) {
|
||||
return {
|
||||
name: elem
|
||||
};
|
||||
});
|
||||
mockRuntime.storage.projects.ssh.listSSHKeys.returns(Promise.resolve(retList));
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('keys');
|
||||
for (var item of retList) {
|
||||
res.body.keys.should.containEql(item);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys --- return Error', function(done) {
|
||||
var errInstance = new Error("Messages here.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.listSSHKeys.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal(errInstance.code);
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return 404', function(done) {
|
||||
mockRuntime.storage.projects.ssh.getSSHKey.returns(Promise.resolve(null));
|
||||
request(app)
|
||||
.get("/settings/user/keys/NOT_REAL")
|
||||
.expect(404)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('GET /settings/user/keys --- return Unexpected Error', function(done) {
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.listSSHKeys.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys")
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.toString());
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return content', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var fileContent = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQD3a+sgtgzSbbliWxmOq5p6+H/mE+0gjWfLWrkIVmHENd1mifV4uCmIHAR2NfuadUYMQ3+bQ90kpmmEKTMYPsyentsKpHQZxTzG7wOCAIpJnbPTHDMxEJhVTaAwEjbVyMSIzTTPfnhoavWIBu0+uMgKDDlBm+RjlgkFlyhXyCN6UwFrIUUMH6Gw+eQHLiooKIl8ce7uDxIlt+9b7hFCU+sQ3kvuse239DZluu6+8buMWqJvrEHgzS9adRFKku8nSPAEPYn85vDi7OgVAcLQufknNgs47KHBAx9h04LeSrFJ/P5J1b//ItRpMOIme+O9d1BR46puzhvUaCHLdvO9czj+OmW+dIm+QIk6lZIOOMnppG72kZxtLfeKT16ur+2FbwAdL9ItBp4BI/YTlBPoa5mLMxpuWfmX1qHntvtGc9wEwS1P7YFfmF3XiK5apxalzrn0Qlr5UmDNbVIqJb1OlbC0w03Z0oktti1xT+R2DGOLWM4lBbpXDHV1BhQ7oYOvbUD8Cnof55lTP0WHHsOHlQc/BGDti1XA9aBX/OzVyzBUYEf0pkimsD0RYo6aqt7QwehJYdlz9x1NBguBffT0s4NhNb9IWr+ASnFPvNl2sw4XH/8U0J0q8ZkMpKkbLM1Zdp1Fv00GF0f5UNRokai6uM3w/ccantJ3WvZ6GtctqytWrw== \n";
|
||||
mockRuntime.storage.projects.ssh.getSSHKey.returns(Promise.resolve(fileContent));
|
||||
request(app)
|
||||
.get("/settings/user/keys/" + key_file_name)
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
mockRuntime.storage.projects.ssh.getSSHKey.called.should.be.true();
|
||||
res.body.should.be.deepEqual({ publickey: fileContent });
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.getSSHKey.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys/" + key_file_name)
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal(errInstance.code);
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('GET /settings/user/keys/<key_file_name> --- return Unexpected Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.getSSHKey.returns(p);
|
||||
request(app)
|
||||
.get("/settings/user/keys/" + key_file_name)
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.toString());
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- success', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
mockRuntime.storage.projects.ssh.generateSSHKey.returns(Promise.resolve(key_file_name));
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.send({ name: key_file_name })
|
||||
.expect(200)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- return parameter error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
mockRuntime.storage.projects.ssh.generateSSHKey.returns(Promise.resolve(key_file_name));
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal("You need to have body or body.name");
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- return Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.generateSSHKey.returns(p);
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.send({ name: key_file_name })
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("test_code");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('POST /settings/user/keys --- return Unexpected error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.generateSSHKey.returns(p);
|
||||
request(app)
|
||||
.post("/settings/user/keys")
|
||||
.send({ name: key_file_name })
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.toString());
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /settings/user/keys/<key_file_name> --- success', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
mockRuntime.storage.projects.ssh.deleteSSHKey.returns(Promise.resolve(true));
|
||||
request(app)
|
||||
.delete("/settings/user/keys/" + key_file_name)
|
||||
.expect(204)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.be.deepEqual({});
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /settings/user/keys/<key_file_name> --- return Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
errInstance.code = "test_code";
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.deleteSSHKey.returns(p);
|
||||
request(app)
|
||||
.delete("/settings/user/keys/" + key_file_name)
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("test_code");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.message);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('DELETE /settings/user/keys/<key_file_name> --- return Unexpected Error', function(done) {
|
||||
var key_file_name = "test_key";
|
||||
var errInstance = new Error("Messages.....");
|
||||
var p = Promise.reject(errInstance);
|
||||
p.catch(()=>{});
|
||||
mockRuntime.storage.projects.ssh.deleteSSHKey.returns(p);
|
||||
request(app)
|
||||
.delete("/settings/user/keys/" + key_file_name)
|
||||
.expect(400)
|
||||
.end(function(err,res) {
|
||||
if (err) {
|
||||
return done(err);
|
||||
}
|
||||
res.body.should.have.property('error');
|
||||
res.body.error.should.be.equal("unexpected_error");
|
||||
res.body.should.have.property('message');
|
||||
res.body.message.should.be.equal(errInstance.toString());
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
*/
|
||||
25
test/unit/@node-red/runtime/lib/events_spec.js
Normal file
25
test/unit/@node-red/runtime/lib/events_spec.js
Normal file
@@ -0,0 +1,25 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
var should = require("should");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("runtime/events", function() {
|
||||
it('can be required without errors', function() {
|
||||
NR_TEST_UTILS.require("@node-red/runtime/lib/events");
|
||||
});
|
||||
it.skip('more tests needed', function(){})
|
||||
});
|
||||
22
test/unit/@node-red/runtime/lib/exec_spec.js
Normal file
22
test/unit/@node-red/runtime/lib/exec_spec.js
Normal file
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var exec = NR_TEST_UTILS.require("@node-red/runtime/lib/exec");
|
||||
241
test/unit/@node-red/runtime/lib/index_spec.js
Normal file
241
test/unit/@node-red/runtime/lib/index_spec.js
Normal file
@@ -0,0 +1,241 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var path = require("path");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var api = NR_TEST_UTILS.require("@node-red/runtime/lib/api");
|
||||
var runtime = NR_TEST_UTILS.require("@node-red/runtime");
|
||||
|
||||
var redNodes = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes");
|
||||
var storage = NR_TEST_UTILS.require("@node-red/runtime/lib/storage");
|
||||
var settings = NR_TEST_UTILS.require("@node-red/runtime/lib/settings");
|
||||
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
|
||||
describe("runtime", function() {
|
||||
afterEach(function() {
|
||||
if (console.log.restore) {
|
||||
console.log.restore();
|
||||
}
|
||||
})
|
||||
|
||||
before(function() {
|
||||
process.env.NODE_RED_HOME = NR_TEST_UTILS.resolve("node-red");
|
||||
});
|
||||
after(function() {
|
||||
delete process.env.NODE_RED_HOME;
|
||||
});
|
||||
function mockUtil(metrics) {
|
||||
return {
|
||||
log:{
|
||||
log: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
metric: sinon.stub().returns(!!metrics),
|
||||
_: function() { return "abc"}
|
||||
},
|
||||
i18n: {
|
||||
registerMessageCatalog: function(){
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
describe("init", function() {
|
||||
beforeEach(function() {
|
||||
sinon.stub(log,"init",function() {});
|
||||
sinon.stub(settings,"init",function() {});
|
||||
sinon.stub(redNodes,"init",function() {})
|
||||
});
|
||||
afterEach(function() {
|
||||
log.init.restore();
|
||||
settings.init.restore();
|
||||
redNodes.init.restore();
|
||||
})
|
||||
|
||||
it("initialises components", function() {
|
||||
runtime.init({testSettings: true, httpAdminRoot:"/"},mockUtil());
|
||||
settings.init.called.should.be.true();
|
||||
redNodes.init.called.should.be.true();
|
||||
});
|
||||
|
||||
it("returns version", function() {
|
||||
runtime.init({testSettings: true, httpAdminRoot:"/"},mockUtil());
|
||||
return runtime.version().then(version => {
|
||||
/^\d+\.\d+\.\d+(-.*)?$/.test(version).should.be.true();
|
||||
});
|
||||
|
||||
|
||||
})
|
||||
});
|
||||
|
||||
describe("start",function() {
|
||||
var storageInit;
|
||||
var settingsLoad;
|
||||
var redNodesInit;
|
||||
var redNodesLoad;
|
||||
var redNodesCleanModuleList;
|
||||
var redNodesGetNodeList;
|
||||
var redNodesLoadFlows;
|
||||
var redNodesStartFlows;
|
||||
var redNodesLoadContextsPlugin;
|
||||
|
||||
beforeEach(function() {
|
||||
storageInit = sinon.stub(storage,"init",function(settings) {return Promise.resolve();});
|
||||
redNodesInit = sinon.stub(redNodes,"init", function() {});
|
||||
redNodesLoad = sinon.stub(redNodes,"load", function() {return Promise.resolve()});
|
||||
redNodesCleanModuleList = sinon.stub(redNodes,"cleanModuleList",function(){});
|
||||
redNodesLoadFlows = sinon.stub(redNodes,"loadFlows",function() {return Promise.resolve()});
|
||||
redNodesStartFlows = sinon.stub(redNodes,"startFlows",function() {});
|
||||
redNodesLoadContextsPlugin = sinon.stub(redNodes,"loadContextsPlugin",function() {return Promise.resolve()});
|
||||
});
|
||||
afterEach(function() {
|
||||
storageInit.restore();
|
||||
redNodesInit.restore();
|
||||
redNodesLoad.restore();
|
||||
redNodesGetNodeList.restore();
|
||||
redNodesCleanModuleList.restore();
|
||||
redNodesLoadFlows.restore();
|
||||
redNodesStartFlows.restore();
|
||||
redNodesLoadContextsPlugin.restore();
|
||||
});
|
||||
it("reports errored/missing modules",function(done) {
|
||||
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
||||
return [
|
||||
{ err:"errored",name:"errName" }, // error
|
||||
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]} // missing
|
||||
].filter(cb);
|
||||
});
|
||||
var util = mockUtil();
|
||||
runtime.init({testSettings: true, httpAdminRoot:"/", load:function() { return Promise.resolve();}},util);
|
||||
// sinon.stub(console,"log");
|
||||
runtime.start().then(function() {
|
||||
// console.log.restore();
|
||||
try {
|
||||
storageInit.calledOnce.should.be.true();
|
||||
redNodesInit.calledOnce.should.be.true();
|
||||
redNodesLoad.calledOnce.should.be.true();
|
||||
redNodesLoadFlows.calledOnce.should.be.true();
|
||||
|
||||
util.log.warn.calledWithMatch("Failed to register 1 node type");
|
||||
util.log.warn.calledWithMatch("Missing node modules");
|
||||
util.log.warn.calledWithMatch(" - module: typeA, typeB");
|
||||
redNodesCleanModuleList.calledOnce.should.be.true();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
}).catch(err=>{done(err)});
|
||||
});
|
||||
it("initiates load of missing modules",function(done) {
|
||||
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
||||
return [
|
||||
{ err:"errored",name:"errName" }, // error
|
||||
{ err:"errored",name:"errName" }, // error
|
||||
{ module:"module",enabled:true,loaded:false,types:["typeA","typeB"]}, // missing
|
||||
{ module:"node-red",enabled:true,loaded:false,types:["typeC","typeD"]} // missing
|
||||
].filter(cb);
|
||||
});
|
||||
var serverInstallModule = sinon.stub(redNodes,"installModule",function(name) { return Promise.resolve({nodes:[]});});
|
||||
var util = mockUtil();
|
||||
runtime.init({testSettings: true, autoInstallModules:true, httpAdminRoot:"/", load:function() { return Promise.resolve();}},util);
|
||||
sinon.stub(console,"log");
|
||||
runtime.start().then(function() {
|
||||
console.log.restore();
|
||||
try {
|
||||
util.log.warn.calledWithMatch("Failed to register 2 node types");
|
||||
util.log.warn.calledWithMatch("Missing node modules");
|
||||
util.log.warn.calledWithMatch(" - module: typeA, typeB");
|
||||
util.log.warn.calledWithMatch(" - node-red: typeC, typeD");
|
||||
redNodesCleanModuleList.calledOnce.should.be.false();
|
||||
serverInstallModule.calledOnce.should.be.true();
|
||||
serverInstallModule.calledWithMatch("module");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
} finally {
|
||||
serverInstallModule.restore();
|
||||
}
|
||||
}).catch(err=>{done(err)});
|
||||
});
|
||||
it("reports errored modules when verbose is enabled",function(done) {
|
||||
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function(cb) {
|
||||
return [
|
||||
{ err:"errored",name:"errName" } // error
|
||||
].filter(cb);
|
||||
});
|
||||
var util = mockUtil();
|
||||
runtime.init({testSettings: true, verbose:true, httpAdminRoot:"/", load:function() { return Promise.resolve();}},util);
|
||||
sinon.stub(console,"log");
|
||||
runtime.start().then(function() {
|
||||
console.log.restore();
|
||||
try {
|
||||
util.log.warn.neverCalledWithMatch("Failed to register 1 node type");
|
||||
util.log.warn.calledWithMatch("[errName] errored");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
}).catch(err=>{done(err)});
|
||||
});
|
||||
|
||||
it("reports runtime metrics",function(done) {
|
||||
var stopFlows = sinon.stub(redNodes,"stopFlows",function() { return Promise.resolve();} );
|
||||
redNodesGetNodeList = sinon.stub(redNodes,"getNodeList", function() {return []});
|
||||
var util = mockUtil(true);
|
||||
runtime.init({testSettings: true, runtimeMetricInterval:200, httpAdminRoot:"/", load:function() { return Promise.resolve();}},util);
|
||||
sinon.stub(console,"log");
|
||||
runtime.start().then(function() {
|
||||
console.log.restore();
|
||||
setTimeout(function() {
|
||||
try {
|
||||
util.log.log.args.should.have.lengthOf(3);
|
||||
util.log.log.args[0][0].should.have.property("event","runtime.memory.rss");
|
||||
util.log.log.args[1][0].should.have.property("event","runtime.memory.heapTotal");
|
||||
util.log.log.args[2][0].should.have.property("event","runtime.memory.heapUsed");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
} finally {
|
||||
runtime.stop();
|
||||
stopFlows.restore();
|
||||
}
|
||||
},300);
|
||||
}).catch(err=>{done(err)});
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
it("stops components", function(done) {
|
||||
var stopFlows = sinon.stub(redNodes,"stopFlows",function() { return Promise.resolve();} );
|
||||
var closeContextsPlugin = sinon.stub(redNodes,"closeContextsPlugin",function() { return Promise.resolve();} );
|
||||
runtime.stop().then(function(){
|
||||
stopFlows.called.should.be.true();
|
||||
closeContextsPlugin.called.should.be.true();
|
||||
stopFlows.restore();
|
||||
closeContextsPlugin.restore();
|
||||
done();
|
||||
}).catch(function(err){
|
||||
stopFlows.restore();
|
||||
closeContextsPlugin.restore();
|
||||
return done(err)
|
||||
});
|
||||
});
|
||||
});
|
||||
181
test/unit/@node-red/runtime/lib/library/index_spec.js
Normal file
181
test/unit/@node-red/runtime/lib/library/index_spec.js
Normal file
@@ -0,0 +1,181 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var fs = require("fs");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var library = NR_TEST_UTILS.require("@node-red/runtime/lib/library/index")
|
||||
|
||||
var mockLog = {
|
||||
log: sinon.stub(),
|
||||
debug: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
metric: sinon.stub(),
|
||||
audit: sinon.stub(),
|
||||
_: function() { return "abc"}
|
||||
}
|
||||
|
||||
|
||||
describe("runtime/library", function() {
|
||||
describe("register", function() {
|
||||
// it("throws error for duplicate type", function() {
|
||||
// library.init({});
|
||||
// library.register("unknown","/abc");
|
||||
// should(()=>{library.register("unknown","/abc")} ).throw();
|
||||
// })
|
||||
})
|
||||
describe("getEntry", function() {
|
||||
before(function() {
|
||||
library.init({
|
||||
log: mockLog,
|
||||
storage: {
|
||||
getLibraryEntry: function(type,path) {
|
||||
return Promise.resolve({type,path});
|
||||
},
|
||||
getFlow: function(path) {
|
||||
return Promise.resolve({path});
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
getNodeExampleFlowPath: function(module,entryPath) {
|
||||
if (module === "unknown") {
|
||||
return null;
|
||||
}
|
||||
return "/tmp/"+module+"/"+entryPath;
|
||||
}
|
||||
}
|
||||
});
|
||||
sinon.stub(fs,"readFile", function(path,opts,callback) {
|
||||
if (path === "/tmp/test-module/abc") {
|
||||
callback(null,"Example flow result");
|
||||
} else if (path === "/tmp/@scope/test-module/abc") {
|
||||
callback(null,"Example scope flow result");
|
||||
} else if (path === "/tmp/test-module/throw") {
|
||||
throw new Error("Instant error")
|
||||
} else {
|
||||
callback(new Error("Unexpected path:"+path))
|
||||
}
|
||||
})
|
||||
});
|
||||
after(function() {
|
||||
fs.readFile.restore();
|
||||
})
|
||||
it('throws error for unregistered type', function() {
|
||||
should(()=>{library.getEntry("unknown","/abc")} ).throw();
|
||||
});
|
||||
|
||||
it('returns a registered non-flow entry', function(done) {
|
||||
library.register("test-module","test-type");
|
||||
library.getEntry("test-type","/abc").then(function(result) {
|
||||
result.should.have.property("type","test-type")
|
||||
result.should.have.property("path","/abc")
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it ('returns a flow entry', function(done) {
|
||||
library.getEntry("flows","/abc").then(function(result) {
|
||||
result.should.have.property("path","/abc")
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it ('returns a flow example entry', function(done) {
|
||||
library.getEntry("flows","_examples_/test-module/abc").then(function(result) {
|
||||
result.should.eql("Example flow result");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it ('returns a flow example entry from scoped module', function(done) {
|
||||
library.getEntry("flows","_examples_/@scope/test-module/abc").then(function(result) {
|
||||
result.should.eql("Example scope flow result");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it ('returns an error for unknown flow example entry', function(done) {
|
||||
library.getEntry("flows","_examples_/unknown/abc").then(function(result) {
|
||||
done(new Error("No error thrown"))
|
||||
}).catch(function(err) {
|
||||
err.should.have.property("code","not_found");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it ('returns an error for file load error - async', function(done) {
|
||||
library.getEntry("flows","_examples_/test-module/unknown").then(function(result) {
|
||||
done(new Error("No error thrown"))
|
||||
}).catch(function(err) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it ('returns an error for file load error - sync', function(done) {
|
||||
library.getEntry("flows","_examples_/test-module/throw").then(function(result) {
|
||||
done(new Error("No error thrown"))
|
||||
}).catch(function(err) {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("saveEntry", function() {
|
||||
before(function() {
|
||||
library.init({
|
||||
log: mockLog,
|
||||
storage: {
|
||||
saveLibraryEntry: function(type, path, meta, body) {
|
||||
return Promise.resolve({type,path,meta,body})
|
||||
},
|
||||
saveFlow: function(path,body) {
|
||||
return Promise.resolve({path,body});
|
||||
}
|
||||
},
|
||||
nodes: {
|
||||
getNodeExampleFlowPath: function(module,entryPath) {
|
||||
if (module === "unknown") {
|
||||
return null;
|
||||
}
|
||||
return "/tmp/"+module+"/"+entryPath;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
it('throws error for unregistered type', function() {
|
||||
should(()=>{library.saveEntry("unknown","/abc",{id:"meta"},{id:"body"})} ).throw();
|
||||
});
|
||||
it('saves a flow entry', function(done) {
|
||||
library.saveEntry('flows','/abc',{id:"meta"},{id:"body"}).then(function(result) {
|
||||
result.should.have.property("path","/abc");
|
||||
result.should.have.property("body",{id:"body"});
|
||||
done();
|
||||
}).catch(done);
|
||||
})
|
||||
it('saves a non-flow entry', function(done) {
|
||||
library.register("test-module","test-type");
|
||||
library.saveEntry('test-type','/abc',{id:"meta"},{id:"body"}).then(function(result) {
|
||||
result.should.have.property("type","test-type");
|
||||
result.should.have.property("path","/abc");
|
||||
result.should.have.property("meta",{id:"meta"});
|
||||
result.should.have.property("body",{id:"body"});
|
||||
done();
|
||||
}).catch(done);
|
||||
})
|
||||
|
||||
});
|
||||
});
|
||||
548
test/unit/@node-red/runtime/lib/nodes/Node_spec.js
Normal file
548
test/unit/@node-red/runtime/lib/nodes/Node_spec.js
Normal file
@@ -0,0 +1,548 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require('sinon');
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var RedNode = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
|
||||
var Log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows");
|
||||
|
||||
describe('Node', function() {
|
||||
describe('#constructor',function() {
|
||||
it('is called with an id and a type',function() {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.not.have.property('name');
|
||||
n.wires.should.be.empty();
|
||||
});
|
||||
|
||||
it('is called with an id, a type and a name',function() {
|
||||
var n = new RedNode({id:'123',type:'abc',name:'barney'});
|
||||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.have.property('name','barney');
|
||||
n.wires.should.be.empty();
|
||||
});
|
||||
|
||||
it('is called with an id, a type and some wires',function() {
|
||||
var n = new RedNode({id:'123',type:'abc',wires:['123','456']});
|
||||
n.should.have.property('id','123');
|
||||
n.should.have.property('type','abc');
|
||||
n.should.not.have.property('name');
|
||||
n.wires.should.have.length(2);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#close', function() {
|
||||
it('emits close event when closed',function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('close',function() {
|
||||
done();
|
||||
});
|
||||
var p = n.close();
|
||||
should.not.exist(p);
|
||||
});
|
||||
|
||||
it('returns a promise when provided a callback with a done parameter',function(testdone) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('close',function(done) {
|
||||
setTimeout(function() {
|
||||
done();
|
||||
},50);
|
||||
});
|
||||
var p = n.close();
|
||||
should.exist(p);
|
||||
p.then(function() {
|
||||
testdone();
|
||||
});
|
||||
});
|
||||
it('accepts a callback with "removed" and "done" parameters', function(testdone) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var receivedRemoved;
|
||||
n.on('close',function(removed,done) {
|
||||
receivedRemoved = removed;
|
||||
setTimeout(function() {
|
||||
done();
|
||||
},50);
|
||||
});
|
||||
var p = n.close(true);
|
||||
should.exist(p);
|
||||
(receivedRemoved).should.be.true();
|
||||
p.then(function() {
|
||||
testdone();
|
||||
});
|
||||
})
|
||||
it('allows multiple close handlers to be registered',function(testdone) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var callbacksClosed = 0;
|
||||
n.on('close',function(done) {
|
||||
setTimeout(function() {
|
||||
callbacksClosed++;
|
||||
done();
|
||||
},50);
|
||||
});
|
||||
n.on('close',function(done) {
|
||||
setTimeout(function() {
|
||||
callbacksClosed++;
|
||||
done();
|
||||
},75);
|
||||
});
|
||||
n.on('close',function() {
|
||||
callbacksClosed++;
|
||||
});
|
||||
var p = n.close();
|
||||
should.exist(p);
|
||||
p.then(function() {
|
||||
callbacksClosed.should.eql(3);
|
||||
testdone();
|
||||
}).catch(function(e) {
|
||||
testdone(e);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
describe('#receive', function() {
|
||||
it('emits input event when called', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var message = {payload:"hello world"};
|
||||
n.on('input',function(msg) {
|
||||
should.deepEqual(msg,message);
|
||||
done();
|
||||
});
|
||||
n.receive(message);
|
||||
});
|
||||
|
||||
it('writes metric info with undefined msg', function(done){
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('input',function(msg) {
|
||||
(typeof msg).should.not.be.equal("undefined");
|
||||
(typeof msg._msgid).should.not.be.equal("undefined");
|
||||
done();
|
||||
});
|
||||
n.receive();
|
||||
});
|
||||
|
||||
it('writes metric info with null msg', function(done){
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
n.on('input',function(msg) {
|
||||
(typeof msg).should.not.be.equal("undefined");
|
||||
(typeof msg._msgid).should.not.be.equal("undefined");
|
||||
done();
|
||||
});
|
||||
n.receive(null);
|
||||
});
|
||||
|
||||
it('handles thrown errors', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
sinon.stub(n,"error",function(err,msg) {});
|
||||
var message = {payload:"hello world"};
|
||||
n.on('input',function(msg) {
|
||||
throw new Error("test error");
|
||||
});
|
||||
n.receive(message);
|
||||
n.error.called.should.be.true();
|
||||
n.error.firstCall.args[1].should.equal(message);
|
||||
done();
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('#send', function() {
|
||||
|
||||
it('emits a single message', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
|
||||
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"};
|
||||
|
||||
n2.on('input',function(msg) {
|
||||
// msg equals message, and is not a new copy
|
||||
should.deepEqual(msg,message);
|
||||
should.strictEqual(msg,message);
|
||||
flowGet.restore();
|
||||
done();
|
||||
});
|
||||
|
||||
n1.send(message);
|
||||
});
|
||||
|
||||
it('emits multiple messages on a single output', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
var flowGet = sinon.stub(flows,"get",function(id) {
|
||||
return {'n1':n1,'n2':n2}[id];
|
||||
});
|
||||
|
||||
var messages = [
|
||||
{payload:"hello world"},
|
||||
{payload:"hello world again"}
|
||||
];
|
||||
|
||||
var rcvdCount = 0;
|
||||
|
||||
n2.on('input',function(msg) {
|
||||
if (rcvdCount === 0) {
|
||||
// first msg sent, don't clone
|
||||
should.deepEqual(msg,messages[rcvdCount]);
|
||||
should.strictEqual(msg,messages[rcvdCount]);
|
||||
rcvdCount += 1;
|
||||
} else {
|
||||
// second msg sent, clone
|
||||
msg.payload.should.equal(messages[rcvdCount].payload);
|
||||
should.notStrictEqual(msg,messages[rcvdCount]);
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
});
|
||||
n1.send([messages]);
|
||||
});
|
||||
|
||||
it('emits messages to multiple outputs', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2'],['n3'],['n4','n5']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
var n3 = new RedNode({id:'n3',type:'abc'});
|
||||
var n4 = new RedNode({id:'n4',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 = [
|
||||
{payload:"hello world"},
|
||||
null,
|
||||
{payload:"hello world again"}
|
||||
];
|
||||
|
||||
var rcvdCount = 0;
|
||||
|
||||
// first message sent, don't clone
|
||||
// message uuids should match
|
||||
n2.on('input',function(msg) {
|
||||
should.deepEqual(msg,messages[0]);
|
||||
should.strictEqual(msg,messages[0]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 3) {
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
n3.on('input',function(msg) {
|
||||
should.fail(null,null,"unexpected message");
|
||||
});
|
||||
|
||||
// second message sent, clone
|
||||
// message uuids wont match since we've cloned
|
||||
n4.on('input',function(msg) {
|
||||
msg.payload.should.equal(messages[2].payload);
|
||||
should.notStrictEqual(msg,messages[2]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 3) {
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
// third message sent, clone
|
||||
// message uuids wont match since we've cloned
|
||||
n5.on('input',function(msg) {
|
||||
msg.payload.should.equal(messages[2].payload);
|
||||
should.notStrictEqual(msg,messages[2]);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 3) {
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
n1.send(messages);
|
||||
});
|
||||
|
||||
it('emits no messages', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n2']]});
|
||||
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) {
|
||||
should.fail(null,null,"unexpected message");
|
||||
});
|
||||
|
||||
setTimeout(function() {
|
||||
flowGet.restore();
|
||||
done();
|
||||
}, 200);
|
||||
|
||||
n1.send();
|
||||
});
|
||||
|
||||
it('emits messages ignoring non-existent nodes', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[['n9'],['n2']]});
|
||||
var n2 = new RedNode({id:'n2',type:'abc'});
|
||||
var flowGet = sinon.stub(flows,"get",function(id) {
|
||||
return {'n1':n1,'n2':n2}[id];
|
||||
});
|
||||
|
||||
var messages = [
|
||||
{payload:"hello world"},
|
||||
{payload:"hello world again"}
|
||||
];
|
||||
|
||||
// only one message sent, so no copy needed
|
||||
n2.on('input',function(msg) {
|
||||
should.deepEqual(msg,messages[1]);
|
||||
should.strictEqual(msg,messages[1]);
|
||||
flowGet.restore();
|
||||
done();
|
||||
});
|
||||
|
||||
n1.send(messages);
|
||||
});
|
||||
|
||||
it('emits messages without cloning req or res', function(done) {
|
||||
var n1 = new RedNode({id:'n1',type:'abc',wires:[[['n2'],['n3']]]});
|
||||
var n2 = new RedNode({id:'n2',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 res = {};
|
||||
var cloned = {};
|
||||
var message = {payload: "foo", cloned: cloned, req: req, res: res};
|
||||
|
||||
var rcvdCount = 0;
|
||||
|
||||
// first message to be sent, so should not be cloned
|
||||
n2.on('input',function(msg) {
|
||||
should.deepEqual(msg, message);
|
||||
msg.cloned.should.be.exactly(message.cloned);
|
||||
msg.req.should.be.exactly(message.req);
|
||||
msg.res.should.be.exactly(message.res);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 2) {
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
// second message to be sent, so should be cloned
|
||||
// message uuids wont match since we've cloned
|
||||
n3.on('input',function(msg) {
|
||||
msg.payload.should.equal(message.payload);
|
||||
msg.cloned.should.not.be.exactly(message.cloned);
|
||||
msg.req.should.be.exactly(message.req);
|
||||
msg.res.should.be.exactly(message.res);
|
||||
rcvdCount += 1;
|
||||
if (rcvdCount == 2) {
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
n1.send(message);
|
||||
});
|
||||
|
||||
it("logs the uuid for all messages sent", function(done) {
|
||||
var flowGet = sinon.stub(flows,"get",function(id) {
|
||||
return {'n1':sender,'n2':receiver1,'n3':receiver2}[id];
|
||||
});
|
||||
var logHandler = {
|
||||
messagesSent: 0,
|
||||
emit: function(event, msg) {
|
||||
if (msg.event == "node.abc.send" && msg.level == Log.METRIC) {
|
||||
this.messagesSent++;
|
||||
(typeof msg.msgid).should.not.be.equal("undefined");
|
||||
flowGet.restore();
|
||||
done();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Log.addHandler(logHandler);
|
||||
|
||||
var sender = new RedNode({id:'n1',type:'abc', wires:[['n2', 'n3']]});
|
||||
var receiver1 = new RedNode({id:'n2',type:'abc'});
|
||||
var receiver2 = new RedNode({id:'n3',type:'abc'});
|
||||
sender.send({"some": "message"});
|
||||
})
|
||||
});
|
||||
|
||||
|
||||
describe('#log', function() {
|
||||
it('produces a log message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc',z:'789'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
n.log("a log message");
|
||||
should.deepEqual({level:Log.INFO, id:n.id,
|
||||
type:n.type, msg:"a log message",z:'789'}, loginfo);
|
||||
Log.log.restore();
|
||||
done();
|
||||
});
|
||||
it('produces a log message with a name', function(done) {
|
||||
var n = new RedNode({id:'123', type:'abc', name:"barney", z:'789'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
n.log("a log message");
|
||||
should.deepEqual({level:Log.INFO, id:n.id, name: "barney",
|
||||
type:n.type, msg:"a log message",z:'789'}, loginfo);
|
||||
Log.log.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#warn', function() {
|
||||
it('produces a warning message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc',z:'789'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
n.warn("a warning");
|
||||
should.deepEqual({level:Log.WARN, id:n.id,
|
||||
type:n.type, msg:"a warning",z:'789'}, loginfo);
|
||||
Log.log.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#error', function() {
|
||||
it('handles a null error message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc',z:'789'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
sinon.stub(flows,"handleError", function(node,message,msg) {
|
||||
});
|
||||
|
||||
var message = {a:1};
|
||||
|
||||
n.error(null,message);
|
||||
should.deepEqual({level:Log.ERROR, id:n.id, type:n.type, msg:"",z:'789'}, loginfo);
|
||||
|
||||
flows.handleError.called.should.be.true();
|
||||
flows.handleError.args[0][0].should.eql(n);
|
||||
flows.handleError.args[0][1].should.eql("");
|
||||
flows.handleError.args[0][2].should.eql(message);
|
||||
|
||||
Log.log.restore();
|
||||
flows.handleError.restore();
|
||||
done();
|
||||
});
|
||||
|
||||
it('produces an error message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc',z:'789'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
sinon.stub(flows,"handleError", function(node,message,msg) {
|
||||
});
|
||||
|
||||
var message = {a:2};
|
||||
|
||||
n.error("This is an error",message);
|
||||
should.deepEqual({level:Log.ERROR, id:n.id, type:n.type, msg:"This is an error",z:'789'}, loginfo);
|
||||
|
||||
flows.handleError.called.should.be.true();
|
||||
flows.handleError.args[0][0].should.eql(n);
|
||||
flows.handleError.args[0][1].should.eql("This is an error");
|
||||
flows.handleError.args[0][2].should.eql(message);
|
||||
|
||||
Log.log.restore();
|
||||
flows.handleError.restore();
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#metric', function() {
|
||||
it('produces a metric message', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
var msg = {payload:"foo", _msgid:"987654321"};
|
||||
n.metric("test.metric",msg,"15mb");
|
||||
should.deepEqual({value:"15mb", level:Log.METRIC, nodeid:n.id,
|
||||
event:"node.abc.test.metric",msgid:"987654321"}, loginfo);
|
||||
Log.log.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#metric', function() {
|
||||
it('returns metric value if eventname undefined', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
var msg = {payload:"foo", _msgid:"987654321"};
|
||||
var m = n.metric(undefined,msg,"15mb");
|
||||
m.should.be.a.Boolean();
|
||||
Log.log.restore();
|
||||
done();
|
||||
});
|
||||
it('returns not defined if eventname defined', function(done) {
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var loginfo = {};
|
||||
sinon.stub(Log, 'log', function(msg) {
|
||||
loginfo = msg;
|
||||
});
|
||||
var msg = {payload:"foo", _msgid:"987654321"};
|
||||
var m = n.metric("info",msg,"15mb");
|
||||
should(m).be.undefined;
|
||||
Log.log.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#status', function() {
|
||||
it('publishes status', function(done) {
|
||||
sinon.stub(flows,"handleStatus", function(node,message,msg) {});
|
||||
var n = new RedNode({id:'123',type:'abc'});
|
||||
var status = {fill:"green",shape:"dot",text:"connected"};
|
||||
var topic;
|
||||
var message;
|
||||
var retain;
|
||||
|
||||
n.status(status);
|
||||
|
||||
flows.handleStatus.called.should.be.true();
|
||||
flows.handleStatus.args[0][0].should.eql(n);
|
||||
flows.handleStatus.args[0][1].should.eql(status);
|
||||
flows.handleStatus.restore();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
944
test/unit/@node-red/runtime/lib/nodes/context/index_spec.js
Normal file
944
test/unit/@node-red/runtime/lib/nodes/context/index_spec.js
Normal file
@@ -0,0 +1,944 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require('sinon');
|
||||
var path = require("path");
|
||||
var fs = require('fs-extra');
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var Context = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/context/index");
|
||||
|
||||
describe('context', function() {
|
||||
describe('local memory',function() {
|
||||
beforeEach(function() {
|
||||
Context.init({});
|
||||
Context.load();
|
||||
});
|
||||
afterEach(function() {
|
||||
Context.clean({allNodes:{}});
|
||||
return Context.close();
|
||||
});
|
||||
it('stores local property',function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
should.not.exist(context1.get("foo"));
|
||||
context1.set("foo","test");
|
||||
context1.get("foo").should.equal("test");
|
||||
});
|
||||
it('stores local property - creates parent properties',function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
context1.set("foo.bar","test");
|
||||
context1.get("foo").should.eql({bar:"test"});
|
||||
});
|
||||
it('deletes local property',function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
context1.set("foo.abc.bar1","test1");
|
||||
context1.set("foo.abc.bar2","test2");
|
||||
context1.get("foo.abc").should.eql({bar1:"test1",bar2:"test2"});
|
||||
context1.set("foo.abc.bar1",undefined);
|
||||
context1.get("foo.abc").should.eql({bar2:"test2"});
|
||||
context1.set("foo.abc",undefined);
|
||||
should.not.exist(context1.get("foo.abc"));
|
||||
context1.set("foo",undefined);
|
||||
should.not.exist(context1.get("foo"));
|
||||
});
|
||||
it('stores flow property',function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
should.not.exist(context1.flow.get("foo"));
|
||||
context1.flow.set("foo","test");
|
||||
context1.flow.get("foo").should.equal("test");
|
||||
});
|
||||
it('stores global property',function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
should.not.exist(context1.global.get("foo"));
|
||||
context1.global.set("foo","test");
|
||||
context1.global.get("foo").should.equal("test");
|
||||
});
|
||||
|
||||
it('keeps local context local', function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
var context2 = Context.get("2","flowA");
|
||||
|
||||
should.not.exist(context1.get("foo"));
|
||||
should.not.exist(context2.get("foo"));
|
||||
context1.set("foo","test");
|
||||
|
||||
context1.get("foo").should.equal("test");
|
||||
should.not.exist(context2.get("foo"));
|
||||
});
|
||||
it('flow context accessible to all flow nodes', function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
var context2 = Context.get("2","flowA");
|
||||
|
||||
should.not.exist(context1.flow.get("foo"));
|
||||
should.not.exist(context2.flow.get("foo"));
|
||||
|
||||
context1.flow.set("foo","test");
|
||||
context1.flow.get("foo").should.equal("test");
|
||||
context2.flow.get("foo").should.equal("test");
|
||||
});
|
||||
|
||||
it('flow context not shared to nodes on other flows', function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
var context2 = Context.get("2","flowB");
|
||||
|
||||
should.not.exist(context1.flow.get("foo"));
|
||||
should.not.exist(context2.flow.get("foo"));
|
||||
|
||||
context1.flow.set("foo","test");
|
||||
context1.flow.get("foo").should.equal("test");
|
||||
should.not.exist(context2.flow.get("foo"));
|
||||
});
|
||||
|
||||
it('global context shared to all nodes', function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
var context2 = Context.get("2","flowB");
|
||||
|
||||
should.not.exist(context1.global.get("foo"));
|
||||
should.not.exist(context2.global.get("foo"));
|
||||
|
||||
context1.global.set("foo","test");
|
||||
context1.global.get("foo").should.equal("test");
|
||||
context2.global.get("foo").should.equal("test");
|
||||
});
|
||||
|
||||
it('context.flow/global are not enumerable', function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
Object.keys(context1).length.should.equal(0);
|
||||
Object.keys(context1.flow).length.should.equal(0);
|
||||
Object.keys(context1.global).length.should.equal(0);
|
||||
})
|
||||
|
||||
it('context.flow/global cannot be deleted', function() {
|
||||
var context1 = Context.get("1","flowA");
|
||||
delete context1.flow;
|
||||
should.exist(context1.flow);
|
||||
delete context1.global;
|
||||
should.exist(context1.global);
|
||||
})
|
||||
|
||||
it('deletes context',function() {
|
||||
var context = Context.get("1","flowA");
|
||||
should.not.exist(context.get("foo"));
|
||||
context.set("foo","abc");
|
||||
context.get("foo").should.equal("abc");
|
||||
|
||||
return Context.delete("1","flowA").then(function(){
|
||||
context = Context.get("1","flowA");
|
||||
should.not.exist(context.get("foo"));
|
||||
});
|
||||
});
|
||||
|
||||
it('enumerates context keys - sync', function() {
|
||||
var context = Context.get("1","flowA");
|
||||
|
||||
var keys = context.keys();
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
context.set("foo","bar");
|
||||
keys = context.keys();
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.equal("foo");
|
||||
|
||||
context.set("abc.def","bar");
|
||||
keys = context.keys();
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.equal("abc");
|
||||
});
|
||||
|
||||
it('enumerates context keys - async', function(done) {
|
||||
var context = Context.get("1","flowA");
|
||||
|
||||
var keys = context.keys(function(err,keys) {
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
context.set("foo","bar");
|
||||
keys = context.keys(function(err,keys) {
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.equal("foo");
|
||||
|
||||
context.set("abc.def","bar");
|
||||
keys = context.keys(function(err,keys) {
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.equal("abc");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should enumerate only context keys when GlobalContext was given - sync', function() {
|
||||
Context.init({functionGlobalContext: {foo:"bar"}});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flowA");
|
||||
context.global.set("foo2","bar2");
|
||||
var keys = context.global.keys();
|
||||
keys.should.have.length(2);
|
||||
keys[0].should.equal("foo");
|
||||
keys[1].should.equal("foo2");
|
||||
});
|
||||
});
|
||||
|
||||
it('should enumerate only context keys when GlobalContext was given - async', function(done) {
|
||||
Context.init({functionGlobalContext: {foo:"bar"}});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flowA");
|
||||
context.global.set("foo2","bar2");
|
||||
context.global.keys(function(err,keys) {
|
||||
keys.should.have.length(2);
|
||||
keys[0].should.equal("foo");
|
||||
keys[1].should.equal("foo2");
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
|
||||
it('returns functionGlobalContext value if store value undefined', function() {
|
||||
Context.init({functionGlobalContext: {foo:"bar"}});
|
||||
return Context.load().then(function(){
|
||||
var context = Context.get("1","flowA");
|
||||
var v = context.global.get('foo');
|
||||
v.should.equal('bar');
|
||||
});
|
||||
})
|
||||
|
||||
it('returns functionGlobalContext sub-value if store value undefined', function() {
|
||||
Context.init({functionGlobalContext: {foo:{bar:123}}});
|
||||
return Context.load().then(function(){
|
||||
var context = Context.get("1","flowA");
|
||||
var v = context.global.get('foo.bar');
|
||||
should.equal(v,123);
|
||||
});
|
||||
})
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('external context storage',function() {
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
|
||||
var sandbox = sinon.sandbox.create();
|
||||
var stubGet = sandbox.stub();
|
||||
var stubSet = sandbox.stub();
|
||||
var stubKeys = sandbox.stub();
|
||||
var stubDelete = sandbox.stub().returns(Promise.resolve());
|
||||
var stubClean = sandbox.stub().returns(Promise.resolve());
|
||||
var stubOpen = sandbox.stub().returns(Promise.resolve());
|
||||
var stubClose = sandbox.stub().returns(Promise.resolve());
|
||||
var stubGet2 = sandbox.stub();
|
||||
var stubSet2 = sandbox.stub();
|
||||
var stubKeys2 = sandbox.stub();
|
||||
var stubDelete2 = sandbox.stub().returns(Promise.resolve());
|
||||
var stubClean2 = sandbox.stub().returns(Promise.resolve());
|
||||
var stubOpen2 = sandbox.stub().returns(Promise.resolve());
|
||||
var stubClose2 = sandbox.stub().returns(Promise.resolve());
|
||||
var testPlugin = function(config){
|
||||
function Test(){}
|
||||
Test.prototype.get = stubGet;
|
||||
Test.prototype.set = stubSet;
|
||||
Test.prototype.keys = stubKeys;
|
||||
Test.prototype.delete = stubDelete;
|
||||
Test.prototype.clean = stubClean;
|
||||
Test.prototype.open = stubOpen;
|
||||
Test.prototype.close = stubClose;
|
||||
return new Test(config);
|
||||
};
|
||||
var testPlugin2 = function(config){
|
||||
function Test2(){}
|
||||
Test2.prototype.get = stubGet2;
|
||||
Test2.prototype.set = stubSet2;
|
||||
Test2.prototype.keys = stubKeys2;
|
||||
Test2.prototype.delete = stubDelete2;
|
||||
Test2.prototype.clean = stubClean2;
|
||||
Test2.prototype.open = stubOpen2;
|
||||
Test2.prototype.close = stubClose2;
|
||||
return new Test2(config);
|
||||
};
|
||||
var contextStorage={
|
||||
test:{
|
||||
module: testPlugin,
|
||||
config:{}
|
||||
}
|
||||
};
|
||||
var contextDefaultStorage={
|
||||
default: {
|
||||
module: testPlugin2,
|
||||
config:{}
|
||||
},
|
||||
test:{
|
||||
module: testPlugin,
|
||||
config:{}
|
||||
}
|
||||
};
|
||||
var contextAlias={
|
||||
default: "test",
|
||||
test:{
|
||||
module: testPlugin,
|
||||
config:{}
|
||||
}
|
||||
};
|
||||
var memoryStorage ={
|
||||
memory:{
|
||||
module: "memory"
|
||||
}
|
||||
};
|
||||
|
||||
afterEach(function() {
|
||||
sandbox.reset();
|
||||
return Context.clean({allNodes:{}}).then(function(){
|
||||
return Context.close();
|
||||
}).then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
|
||||
describe('load modules',function(){
|
||||
it('should call open()', function() {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
stubOpen.called.should.be.true();
|
||||
stubOpen2.called.should.be.true();
|
||||
});
|
||||
});
|
||||
it('should load memory module', function() {
|
||||
Context.init({contextStorage:{memory:{module:"memory"}}});
|
||||
return Context.load();
|
||||
});
|
||||
it('should load localfilesystem module', function() {
|
||||
Context.init({contextStorage:{file:{module:"localfilesystem",config:{dir:resourcesDir}}}});
|
||||
return Context.load();
|
||||
});
|
||||
it('should ignore reserved storage name `_`', function(done) {
|
||||
Context.init({contextStorage:{_:{module:testPlugin}}});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){}
|
||||
context.set("foo","bar","_",cb);
|
||||
context.get("foo","_",cb);
|
||||
context.keys("_",cb);
|
||||
stubSet.called.should.be.false();
|
||||
stubGet.called.should.be.false();
|
||||
stubKeys.called.should.be.false();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should fail when using invalid store name', function(done) {
|
||||
Context.init({contextStorage:{'Invalid name':{module:testPlugin}}});
|
||||
Context.load().then(function(){
|
||||
done("An error was not thrown");
|
||||
}).catch(function(){
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should fail when using invalid sign character', function (done) {
|
||||
Context.init({ contextStorage:{'abc-123':{module:testPlugin}}});
|
||||
Context.load().then(function () {
|
||||
done("An error was not thrown");
|
||||
}).catch(function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should fail when using invalid default context', function(done) {
|
||||
Context.init({contextStorage:{default:"noexist"}});
|
||||
Context.load().then(function(){
|
||||
done("An error was not thrown");
|
||||
}).catch(function(){
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should fail for the storage with no module', function(done) {
|
||||
Context.init({ contextStorage: { test: {}}});
|
||||
Context.load().then(function(){
|
||||
done("An error was not thrown");
|
||||
}).catch(function(){
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should fail to load non-existent module', function(done) {
|
||||
Context.init({contextStorage:{ file:{module:"nonexistent"} }});
|
||||
Context.load().then(function(){
|
||||
done("An error was not thrown");
|
||||
}).catch(function(){
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('should fail to load invalid module', function (done) {
|
||||
Context.init({contextStorage: {
|
||||
test: {
|
||||
module: function (config) {
|
||||
throw new Error("invalid plugin was loaded.");
|
||||
}
|
||||
}
|
||||
}});
|
||||
Context.load().then(function () {
|
||||
done("An error was not thrown");
|
||||
}).catch(function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('close modules',function(){
|
||||
it('should call close()', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
return Context.close().then(function(){
|
||||
stubClose.called.should.be.true();
|
||||
stubClose2.called.should.be.true();
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('store context',function() {
|
||||
it('should store local property to external context storage',function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
var cb = function(){done("An error occurred")}
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set("foo","bar","test",cb);
|
||||
context.get("foo","test",cb);
|
||||
context.keys("test",cb);
|
||||
stubSet.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
||||
stubGet.calledWith("1:flow","foo").should.be.true();
|
||||
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('should store flow property to external context storage',function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.flow.set("foo","bar","test",cb);
|
||||
context.flow.get("foo","test",cb);
|
||||
context.flow.keys("test",cb);
|
||||
stubSet.calledWithExactly("flow","foo","bar",cb).should.be.true();
|
||||
stubGet.calledWith("flow","foo").should.be.true();
|
||||
stubKeys.calledWithExactly("flow",cb).should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('should store global property to external context storage',function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.global.set("foo","bar","test",cb);
|
||||
context.global.get("foo","test",cb);
|
||||
context.global.keys("test",cb);
|
||||
stubSet.calledWithExactly("global","foo","bar",cb).should.be.true();
|
||||
stubGet.calledWith("global","foo").should.be.true();
|
||||
stubKeys.calledWith("global").should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('should store data to the default context when non-existent context storage was specified', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.set("foo","bar","nonexist",cb);
|
||||
context.get("foo","nonexist",cb);
|
||||
context.keys("nonexist",cb);
|
||||
stubGet.called.should.be.false();
|
||||
stubSet.called.should.be.false();
|
||||
stubKeys.called.should.be.false();
|
||||
stubSet2.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
||||
stubGet2.calledWith("1:flow","foo").should.be.true();
|
||||
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('should use the default context', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.set("foo","bar","default",cb);
|
||||
context.get("foo","default",cb);
|
||||
context.keys("default",cb);
|
||||
stubGet.called.should.be.false();
|
||||
stubSet.called.should.be.false();
|
||||
stubKeys.called.should.be.false();
|
||||
stubSet2.calledWithExactly("1:flow","foo","bar",cb).should.be.true();
|
||||
stubGet2.calledWith("1:flow","foo").should.be.true();
|
||||
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('should use the alias of default context', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.set("foo","alias",cb);
|
||||
context.get("foo",cb);
|
||||
context.keys(cb);
|
||||
stubGet.called.should.be.false();
|
||||
stubSet.called.should.be.false();
|
||||
stubKeys.called.should.be.false();
|
||||
stubSet2.calledWithExactly("1:flow","foo","alias",cb).should.be.true();
|
||||
stubGet2.calledWith("1:flow","foo").should.be.true();
|
||||
stubKeys2.calledWithExactly("1:flow",cb).should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should allow the store name to be provide in the key', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.set("#:(test)::foo","bar");
|
||||
context.get("#:(test)::foo");
|
||||
stubGet2.called.should.be.false();
|
||||
stubSet2.called.should.be.false();
|
||||
stubSet.calledWithExactly("1:flow","foo","bar",undefined).should.be.true();
|
||||
stubGet.calledWith("1:flow","foo").should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
|
||||
it('should use default as the alias of other context', function(done) {
|
||||
Context.init({contextStorage:contextAlias});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.set("foo","alias",cb);
|
||||
context.get("foo",cb);
|
||||
context.keys(cb);
|
||||
stubSet.calledWithExactly("1:flow","foo","alias",cb).should.be.true();
|
||||
stubGet.calledWith("1:flow","foo").should.be.true();
|
||||
stubKeys.calledWithExactly("1:flow",cb).should.be.true();
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
it('should not throw an error using undefined storage for local context', function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.get("local","nonexist",cb);
|
||||
done()
|
||||
}).catch(done);
|
||||
});
|
||||
it('should throw an error using undefined storage for flow context', function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
var cb = function(){done("An error occurred")}
|
||||
context.flow.get("flow","nonexist",cb);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should return functionGlobalContext value as a default - synchronous', function(done) {
|
||||
var fGC = { "foo": 456 };
|
||||
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
|
||||
Context.load().then(function() {
|
||||
var context = Context.get("1","flow");
|
||||
// Get foo - should be value from fGC
|
||||
var v = context.global.get("foo");
|
||||
v.should.equal(456);
|
||||
|
||||
// Update foo - should not touch fGC object
|
||||
context.global.set("foo","new value");
|
||||
fGC.foo.should.equal(456);
|
||||
|
||||
// Get foo - should be the updated value
|
||||
v = context.global.get("foo");
|
||||
v.should.equal("new value");
|
||||
done();
|
||||
}).catch(done);
|
||||
})
|
||||
|
||||
it('should return functionGlobalContext value as a default - async', function(done) {
|
||||
var fGC = { "foo": 456 };
|
||||
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
|
||||
Context.load().then(function() {
|
||||
var context = Context.get("1","flow");
|
||||
// Get foo - should be value from fGC
|
||||
context.global.get("foo", function(err, v) {
|
||||
if (err) {
|
||||
done(err)
|
||||
} else {
|
||||
v.should.equal(456);
|
||||
// Update foo - should not touch fGC object
|
||||
context.global.set("foo","new value", function(err) {
|
||||
if (err) {
|
||||
done(err)
|
||||
} else {
|
||||
fGC.foo.should.equal(456);
|
||||
// Get foo - should be the updated value
|
||||
context.global.get("foo", function(err, v) {
|
||||
if (err) {
|
||||
done(err)
|
||||
} else {
|
||||
v.should.equal("new value");
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}).catch(done);
|
||||
})
|
||||
|
||||
it('should return multiple values if key is an array', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set("foo1","bar1","memory");
|
||||
context.set("foo2","bar2","memory");
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
foo1.should.be.equal("bar1");
|
||||
foo2.should.be.equal("bar2");
|
||||
should.not.exist(foo3);
|
||||
done();
|
||||
}
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
|
||||
it('should return multiple functionGlobalContext values if key is an array', function(done) {
|
||||
var fGC = { "foo1": 456, "foo2": {"bar":789} };
|
||||
Context.init({contextStorage:memoryStorage, functionGlobalContext:fGC });
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.global.get(["foo1","foo2.bar","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
should.equal(foo1, 456);
|
||||
should.equal(foo2, 789);
|
||||
should.not.exist(foo3);
|
||||
done();
|
||||
}
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
|
||||
it('should return an error if an error occurs in getting multiple store values', function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
stubGet.onFirstCall().callsArgWith(2, "error2", "bar1");
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.global.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err === "error2") {
|
||||
done();
|
||||
} else {
|
||||
done("An error occurred");
|
||||
}
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
|
||||
it('should return a first error if some errors occur in getting multiple store values', function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
stubGet.onFirstCall().callsArgWith(2, "error1");
|
||||
stubGet.onSecondCall().callsArgWith(2, null, "bar2");
|
||||
stubGet.onThirdCall().callsArgWith(2, "error3");
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err === "error1") {
|
||||
done();
|
||||
} else {
|
||||
done("An error occurred");
|
||||
}
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
|
||||
it('should store multiple properties if key and value are arrays', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
foo1.should.be.equal("bar1");
|
||||
foo2.should.be.equal("bar2");
|
||||
foo3.should.be.equal("bar3");
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should deletes multiple properties', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
foo1.should.be.equal("bar1");
|
||||
foo2.should.be.equal("bar2");
|
||||
foo3.should.be.equal("bar3");
|
||||
context.set(["foo1","foo2","foo3"], new Array(3), "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
should.not.exist(foo1);
|
||||
should.not.exist(foo2);
|
||||
should.not.exist(foo3);
|
||||
done();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should use null for missing values if the value array is shorter than the key array', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2"], "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
context.keys(function(err, keys){
|
||||
keys.should.have.length(3);
|
||||
keys.should.eql(["foo1","foo2","foo3"]);
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
foo1.should.be.equal("bar1");
|
||||
foo2.should.be.equal("bar2");
|
||||
should(foo3).be.null();
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should use null for missing values if the value is not array', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], "bar1", "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
context.keys(function(err, keys){
|
||||
keys.should.have.length(3);
|
||||
keys.should.eql(["foo1","foo2","foo3"]);
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
foo1.should.be.equal("bar1");
|
||||
should(foo2).be.null();
|
||||
should(foo3).be.null();
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should ignore the extra values if the value array is longer than the key array', function(done) {
|
||||
Context.init({contextStorage:memoryStorage});
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3","ignored"], "memory", function(err){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
context.keys(function(err, keys){
|
||||
keys.should.have.length(3);
|
||||
keys.should.eql(["foo1","foo2","foo3"]);
|
||||
context.get(["foo1","foo2","foo3"], "memory", function(err,foo1,foo2,foo3){
|
||||
if (err) {
|
||||
done(err);
|
||||
} else {
|
||||
foo1.should.be.equal("bar1");
|
||||
foo2.should.be.equal("bar2");
|
||||
foo3.should.be.equal("bar3");
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error if an error occurs in storing multiple values', function(done) {
|
||||
Context.init({contextStorage:contextStorage});
|
||||
stubSet.onFirstCall().callsArgWith(3, "error2");
|
||||
Context.load().then(function(){
|
||||
var context = Context.get("1","flow");
|
||||
context.set(["foo1","foo2","foo3"], ["bar1","bar2","bar3"], "memory", function(err){
|
||||
if (err === "error2") {
|
||||
done();
|
||||
} else {
|
||||
done("An error occurred");
|
||||
}
|
||||
});
|
||||
}).catch(function(err){ done(err); });
|
||||
});
|
||||
|
||||
it('should throw an error if callback of context.get is not a function', function (done) {
|
||||
Context.init({ contextStorage: memoryStorage });
|
||||
Context.load().then(function () {
|
||||
var context = Context.get("1", "flow");
|
||||
context.get("foo", "memory", "callback");
|
||||
done("should throw an error.");
|
||||
}).catch(function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not throw an error if callback of context.get is not specified', function (done) {
|
||||
Context.init({ contextStorage: memoryStorage });
|
||||
Context.load().then(function () {
|
||||
var context = Context.get("1", "flow");
|
||||
context.get("foo", "memory");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should throw an error if callback of context.set is not a function', function (done) {
|
||||
Context.init({ contextStorage: memoryStorage });
|
||||
Context.load().then(function () {
|
||||
var context = Context.get("1", "flow");
|
||||
context.set("foo", "bar", "memory", "callback");
|
||||
done("should throw an error.");
|
||||
}).catch(function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not throw an error if callback of context.set is not specified', function (done) {
|
||||
Context.init({ contextStorage: memoryStorage });
|
||||
Context.load().then(function () {
|
||||
var context = Context.get("1", "flow");
|
||||
context.set("foo", "bar", "memory");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should throw an error if callback of context.keys is not a function', function (done) {
|
||||
Context.init({ contextStorage: memoryStorage });
|
||||
Context.load().then(function () {
|
||||
var context = Context.get("1", "flow");
|
||||
context.keys("memory", "callback");
|
||||
done("should throw an error.");
|
||||
}).catch(function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should not throw an error if callback of context.keys is not specified', function (done) {
|
||||
Context.init({ contextStorage: memoryStorage });
|
||||
Context.load().then(function () {
|
||||
var context = Context.get("1", "flow");
|
||||
context.keys("memory");
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('listStores', function () {
|
||||
it('should list context storages', function (done) {
|
||||
Context.init({ contextStorage: contextDefaultStorage });
|
||||
Context.load().then(function () {
|
||||
var list = Context.listStores();
|
||||
list.default.should.equal("default");
|
||||
list.stores.should.eql(["default", "test"]);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
|
||||
it('should list context storages without default storage', function (done) {
|
||||
Context.init({ contextStorage: contextStorage });
|
||||
Context.load().then(function () {
|
||||
var list = Context.listStores();
|
||||
list.default.should.equal("test");
|
||||
list.stores.should.eql(["test"]);
|
||||
done();
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('delete context',function(){
|
||||
it('should not call delete() when external context storage is used', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
Context.get("flowA");
|
||||
return Context.delete("flowA").then(function(){
|
||||
stubDelete.called.should.be.false();
|
||||
stubDelete2.called.should.be.false();
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('clean context',function(){
|
||||
it('should call clean()', function(done) {
|
||||
Context.init({contextStorage:contextDefaultStorage});
|
||||
Context.load().then(function(){
|
||||
return Context.clean({allNodes:{}}).then(function(){
|
||||
stubClean.calledWithExactly([]).should.be.true();
|
||||
stubClean2.calledWithExactly([]).should.be.true();
|
||||
done();
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,883 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require('should');
|
||||
var fs = require('fs-extra');
|
||||
var path = require("path");
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var LocalFileSystem = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/context/localfilesystem");
|
||||
|
||||
var resourcesDir = path.resolve(path.join(__dirname,"..","resources","context"));
|
||||
|
||||
var defaultContextBase = "context";
|
||||
|
||||
describe('localfilesystem',function() {
|
||||
|
||||
before(function() {
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
|
||||
describe('#get/set',function() {
|
||||
var context;
|
||||
beforeEach(function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: false});
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close();
|
||||
}).then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
|
||||
it('should store property',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
if (err) { return done(err); }
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo","test",function(err){
|
||||
if (err) { return done(err); }
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
if (err) { return done(err); }
|
||||
value.should.be.equal("test");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store property - creates parent properties',function(done) {
|
||||
context.set("nodeX","foo.bar","test",function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.eql({bar:"test"});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store local scope property', function (done) {
|
||||
context.set("abc:def", "foo.bar", "test", function (err) {
|
||||
context.get("abc:def", "foo", function (err, value) {
|
||||
value.should.be.eql({ bar: "test" });
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete property',function(done) {
|
||||
context.set("nodeX","foo.abc.bar1","test1",function(err){
|
||||
context.set("nodeX","foo.abc.bar2","test2",function(err){
|
||||
context.get("nodeX","foo.abc",function(err, value){
|
||||
value.should.be.eql({bar1:"test1",bar2:"test2"});
|
||||
context.set("nodeX","foo.abc.bar1",undefined,function(err){
|
||||
context.get("nodeX","foo.abc",function(err, value){
|
||||
value.should.be.eql({bar2:"test2"});
|
||||
context.set("nodeX","foo.abc",undefined,function(err){
|
||||
context.get("nodeX","foo.abc",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",undefined,function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should not shared context with other scope', function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.get("nodeY","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo","testX",function(err){
|
||||
context.set("nodeY","foo","testY",function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.equal("testX");
|
||||
context.get("nodeY","foo",function(err, value){
|
||||
value.should.be.equal("testY");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store string',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo","bar",function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.String();
|
||||
value.should.be.equal("bar");
|
||||
context.set("nodeX","foo","1",function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.String();
|
||||
value.should.be.equal("1");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store number',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",1,function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Number();
|
||||
value.should.be.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store null',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",null,function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should(value).be.null();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store boolean',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",true,function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Boolean().and.true();
|
||||
context.set("nodeX","foo",false,function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Boolean().and.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store object',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",{obj:"bar"},function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Object();
|
||||
value.should.eql({obj:"bar"});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store array',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",["a","b","c"],function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Array();
|
||||
value.should.eql(["a","b","c"]);
|
||||
context.get("nodeX","foo[1]",function(err, value){
|
||||
value.should.be.String();
|
||||
value.should.equal("b");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store array of arrays',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",[["a","b","c"],[1,2,3,4],[true,false]],function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Array();
|
||||
value.should.have.length(3);
|
||||
value[0].should.have.length(3);
|
||||
value[1].should.have.length(4);
|
||||
value[2].should.have.length(2);
|
||||
context.get("nodeX","foo[1]",function(err, value){
|
||||
value.should.be.Array();
|
||||
value.should.have.length(4);
|
||||
value.should.be.eql([1,2,3,4]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should store array of objects',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo",[{obj:"bar1"},{obj:"bar2"},{obj:"bar3"}],function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.Array();
|
||||
value.should.have.length(3);
|
||||
value[0].should.be.Object();
|
||||
value[1].should.be.Object();
|
||||
value[2].should.be.Object();
|
||||
context.get("nodeX","foo[1]",function(err, value){
|
||||
value.should.be.Object();
|
||||
value.should.be.eql({obj:"bar2"});
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should set/get multiple values', function(done) {
|
||||
context.set("nodeX",["one","two","three"],["test1","test2","test3"], function(err) {
|
||||
context.get("nodeX",["one","two"], function() {
|
||||
Array.prototype.slice.apply(arguments).should.eql([undefined,"test1","test2"])
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
it('should set/get multiple values - get unknown', function(done) {
|
||||
context.set("nodeX",["one","two","three"],["test1","test2","test3"], function(err) {
|
||||
context.get("nodeX",["one","two","unknown"], function() {
|
||||
Array.prototype.slice.apply(arguments).should.eql([undefined,"test1","test2",undefined])
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
it('should set/get multiple values - single value providd', function(done) {
|
||||
context.set("nodeX",["one","two","three"],"test1", function(err) {
|
||||
context.get("nodeX",["one","two"], function() {
|
||||
Array.prototype.slice.apply(arguments).should.eql([undefined,"test1",null])
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
it('should throw error if bad key included in multiple keys - get', function(done) {
|
||||
context.set("nodeX",["one","two","three"],["test1","test2","test3"], function(err) {
|
||||
context.get("nodeX",["one",".foo","three"], function(err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
})
|
||||
|
||||
it('should throw error if bad key included in multiple keys - set', function(done) {
|
||||
context.set("nodeX",["one",".foo","three"],["test1","test2","test3"], function(err) {
|
||||
should.exist(err);
|
||||
// Check 'one' didn't get set as a result
|
||||
context.get("nodeX","one",function(err,one) {
|
||||
should.not.exist(one);
|
||||
done();
|
||||
})
|
||||
});
|
||||
})
|
||||
|
||||
it('should throw an error when getting a value with invalid key', function (done) {
|
||||
context.set("nodeX","foo","bar",function(err) {
|
||||
context.get("nodeX"," ",function(err,value) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when setting a value with invalid key',function (done) {
|
||||
context.set("nodeX"," ","bar",function (err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when callback of get() is not a function',function (done) {
|
||||
try {
|
||||
context.get("nodeX","foo","callback");
|
||||
done("should throw an error.");
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error when callback of get() is not specified',function (done) {
|
||||
try {
|
||||
context.get("nodeX","foo");
|
||||
done("should throw an error.");
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error when callback of set() is not a function',function (done) {
|
||||
try {
|
||||
context.set("nodeX","foo","bar","callback");
|
||||
done("should throw an error.");
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should not throw an error when callback of set() is not specified', function (done) {
|
||||
try {
|
||||
context.set("nodeX"," ","bar");
|
||||
done();
|
||||
} catch (err) {
|
||||
done("should not throw an error.");
|
||||
}
|
||||
});
|
||||
|
||||
it('should handle empty context file', function (done) {
|
||||
fs.outputFile(path.join(resourcesDir,defaultContextBase,"nodeX","flow.json"),"",function(){
|
||||
context.get("nodeX", "foo", function (err, value) {
|
||||
should.not.exist(value);
|
||||
context.set("nodeX", "foo", "test", function (err) {
|
||||
context.get("nodeX", "foo", function (err, value) {
|
||||
value.should.be.equal("test");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when reading corrupt context file', function (done) {
|
||||
fs.outputFile(path.join(resourcesDir, defaultContextBase, "nodeX", "flow.json"),"{abc",function(){
|
||||
context.get("nodeX", "foo", function (err, value) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#keys',function() {
|
||||
var context;
|
||||
beforeEach(function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: false});
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close();
|
||||
}).then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
|
||||
it('should enumerate context keys', function(done) {
|
||||
context.keys("nodeX",function(err, value){
|
||||
value.should.be.an.Array();
|
||||
value.should.be.empty();
|
||||
context.set("nodeX","foo","bar",function(err){
|
||||
context.keys("nodeX",function(err, value){
|
||||
value.should.have.length(1);
|
||||
value[0].should.equal("foo");
|
||||
context.set("nodeX","abc.def","bar",function(err){
|
||||
context.keys("nodeX",function(err, value){
|
||||
value.should.have.length(2);
|
||||
value[1].should.equal("abc");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should enumerate context keys in each scopes', function(done) {
|
||||
context.keys("nodeX",function(err, value){
|
||||
value.should.be.an.Array();
|
||||
value.should.be.empty();
|
||||
context.keys("nodeY",function(err, value){
|
||||
value.should.be.an.Array();
|
||||
value.should.be.empty();
|
||||
context.set("nodeX","foo","bar",function(err){
|
||||
context.set("nodeY","hoge","piyo",function(err){
|
||||
context.keys("nodeX",function(err, value){
|
||||
value.should.have.length(1);
|
||||
value[0].should.equal("foo");
|
||||
context.keys("nodeY",function(err, value){
|
||||
value.should.have.length(1);
|
||||
value[0].should.equal("hoge");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw an error when callback of keys() is not a function', function (done) {
|
||||
try {
|
||||
context.keys("nodeX", "callback");
|
||||
done("should throw an error.");
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
|
||||
it('should throw an error when callback of keys() is not specified', function (done) {
|
||||
try {
|
||||
context.keys("nodeX");
|
||||
done("should throw an error.");
|
||||
} catch (err) {
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('#delete',function() {
|
||||
var context;
|
||||
beforeEach(function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: false});
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close();
|
||||
}).then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
|
||||
it('should delete context',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.get("nodeY","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo","testX",function(err){
|
||||
context.set("nodeY","foo","testY",function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.be.equal("testX");
|
||||
context.get("nodeY","foo",function(err, value){
|
||||
value.should.be.equal("testY");
|
||||
context.delete("nodeX").then(function(){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.get("nodeY","foo",function(err, value){
|
||||
value.should.be.equal("testY");
|
||||
done();
|
||||
});
|
||||
});
|
||||
}).catch(done);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#clean',function() {
|
||||
var context;
|
||||
var contextGet;
|
||||
var contextSet;
|
||||
beforeEach(function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: false});
|
||||
contextGet = function(scope,key) {
|
||||
return new Promise((res,rej) => {
|
||||
context.get(scope,key, function(err,value) {
|
||||
if (err) {
|
||||
rej(err);
|
||||
} else {
|
||||
res(value);
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
contextSet = function(scope,key,value) {
|
||||
return new Promise((res,rej) => {
|
||||
context.set(scope,key,value, function(err) {
|
||||
if (err) {
|
||||
rej(err);
|
||||
} else {
|
||||
res();
|
||||
}
|
||||
})
|
||||
});
|
||||
}
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close().then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
});
|
||||
it('should clean unnecessary context',function(done) {
|
||||
contextSet("global","foo","testGlobal").then(function() {
|
||||
return contextSet("nodeX:flow1","foo","testX");
|
||||
}).then(function() {
|
||||
return contextSet("nodeY:flow2","foo","testY");
|
||||
}).then(function() {
|
||||
return contextGet("nodeX:flow1","foo");
|
||||
}).then(function(value) {
|
||||
value.should.be.equal("testX");
|
||||
}).then(function() {
|
||||
return contextGet("nodeY:flow2","foo");
|
||||
}).then(function(value) {
|
||||
value.should.be.equal("testY");
|
||||
}).then(function() {
|
||||
return context.clean([])
|
||||
}).then(function() {
|
||||
return contextGet("nodeX:flow1","foo");
|
||||
}).then(function(value) {
|
||||
should.not.exist(value);
|
||||
}).then(function() {
|
||||
return contextGet("nodeY:flow2","foo");
|
||||
}).then(function(value) {
|
||||
should.not.exist(value);
|
||||
}).then(function() {
|
||||
return contextGet("global","foo");
|
||||
}).then(function(value) {
|
||||
value.should.eql("testGlobal");
|
||||
}).then(done).catch(done);
|
||||
});
|
||||
|
||||
it('should not clean active context',function(done) {
|
||||
contextSet("global","foo","testGlobal").then(function() {
|
||||
return contextSet("nodeX:flow1","foo","testX");
|
||||
}).then(function() {
|
||||
return contextSet("nodeY:flow2","foo","testY");
|
||||
}).then(function() {
|
||||
return contextGet("nodeX:flow1","foo");
|
||||
}).then(function(value) {
|
||||
value.should.be.equal("testX");
|
||||
}).then(function() {
|
||||
return contextGet("nodeY:flow2","foo");
|
||||
}).then(function(value) {
|
||||
value.should.be.equal("testY");
|
||||
}).then(function() {
|
||||
return context.clean(["flow1","nodeX"])
|
||||
}).then(function() {
|
||||
return contextGet("nodeX:flow1","foo");
|
||||
}).then(function(value) {
|
||||
value.should.be.equal("testX");
|
||||
}).then(function() {
|
||||
return contextGet("nodeY:flow2","foo");
|
||||
}).then(function(value) {
|
||||
should.not.exist(value);
|
||||
}).then(function() {
|
||||
return contextGet("global","foo");
|
||||
}).then(function(value) {
|
||||
value.should.eql("testGlobal");
|
||||
}).then(done).catch(done);
|
||||
});
|
||||
});
|
||||
|
||||
describe('#if cache is enabled',function() {
|
||||
|
||||
var context;
|
||||
beforeEach(function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: false});
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close();
|
||||
}).then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
it('should load contexts into the cache',function() {
|
||||
var globalData = {key:"global"};
|
||||
var flowData = {key:"flow"};
|
||||
var nodeData = {key:"node"};
|
||||
return Promise.all([
|
||||
fs.outputFile(path.join(resourcesDir,defaultContextBase,"global","global.json"), JSON.stringify(globalData,null,4), "utf8"),
|
||||
fs.outputFile(path.join(resourcesDir,defaultContextBase,"flow","flow.json"), JSON.stringify(flowData,null,4), "utf8"),
|
||||
fs.outputFile(path.join(resourcesDir,defaultContextBase,"flow","node.json"), JSON.stringify(nodeData,null,4), "utf8")
|
||||
]).then(function(){
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: true});
|
||||
return context.open();
|
||||
}).then(function(){
|
||||
return Promise.all([
|
||||
fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json")),
|
||||
fs.remove(path.join(resourcesDir,defaultContextBase,"flow","flow.json")),
|
||||
fs.remove(path.join(resourcesDir,defaultContextBase,"flow","node.json"))
|
||||
]);
|
||||
}).then(function(){
|
||||
context.get("global","key").should.be.equal("global");
|
||||
context.get("flow","key").should.be.equal("flow");
|
||||
context.get("node:flow","key").should.be.equal("node");
|
||||
});
|
||||
});
|
||||
|
||||
it('should store property to the cache',function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 1});
|
||||
return context.open().then(function(){
|
||||
return new Promise(function(resolve, reject){
|
||||
context.set("global","foo","bar",function(err){
|
||||
if(err){
|
||||
reject(err);
|
||||
} else {
|
||||
fs.readJson(path.join(resourcesDir,defaultContextBase,"global","global.json")).then(function(data) {
|
||||
// File should not exist as flush hasn't happened
|
||||
reject("File global/global.json should not exist");
|
||||
}).catch(function(err) {
|
||||
setTimeout(function() {
|
||||
fs.readJson(path.join(resourcesDir,defaultContextBase,"global","global.json")).then(function(data) {
|
||||
data.should.eql({foo:'bar'});
|
||||
resolve();
|
||||
}).catch(function(err) {
|
||||
reject(err);
|
||||
});
|
||||
},1100)
|
||||
})
|
||||
}
|
||||
});
|
||||
});
|
||||
}).then(function(){
|
||||
return fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json"));
|
||||
}).then(function(){
|
||||
context.get("global","foo").should.be.equal("bar");
|
||||
})
|
||||
});
|
||||
|
||||
it('should enumerate context keys in the cache',function() {
|
||||
var globalData = {foo:"bar"};
|
||||
return fs.outputFile(path.join(resourcesDir,defaultContextBase,"global","global.json"), JSON.stringify(globalData,null,4), "utf8").then(function(){
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 2});
|
||||
return context.open()
|
||||
}).then(function(){
|
||||
return fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json"));
|
||||
}).then(function(){
|
||||
var keys = context.keys("global");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.equal("foo");
|
||||
return new Promise(function(resolve, reject){
|
||||
context.set("global","foo2","bar2",function(err){
|
||||
if(err){
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}).then(function(){
|
||||
return fs.remove(path.join(resourcesDir,defaultContextBase,"global","global.json"));
|
||||
}).then(function(){
|
||||
var keys = context.keys("global");
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.equal("foo2");
|
||||
})
|
||||
});
|
||||
|
||||
it('should delete context in the cache',function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 2});
|
||||
return context.open().then(function(){
|
||||
return new Promise(function(resolve, reject){
|
||||
context.set("global","foo","bar",function(err){
|
||||
if(err){
|
||||
reject(err);
|
||||
} else {
|
||||
resolve();
|
||||
}
|
||||
});
|
||||
});
|
||||
}).then(function(){
|
||||
context.get("global","foo").should.be.equal("bar");
|
||||
return context.delete("global");
|
||||
}).then(function(){
|
||||
should.not.exist(context.get("global","foo"))
|
||||
})
|
||||
});
|
||||
|
||||
it('should clean unnecessary context in the cache',function() {
|
||||
var flowAData = {key:"flowA"};
|
||||
var flowBData = {key:"flowB"};
|
||||
return Promise.all([
|
||||
fs.outputFile(path.join(resourcesDir,defaultContextBase,"flowA","flow.json"), JSON.stringify(flowAData,null,4), "utf8"),
|
||||
fs.outputFile(path.join(resourcesDir,defaultContextBase,"flowB","flow.json"), JSON.stringify(flowBData,null,4), "utf8")
|
||||
]).then(function(){
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: true, flushInterval: 2});
|
||||
return context.open();
|
||||
}).then(function(){
|
||||
context.get("flowA","key").should.be.equal("flowA");
|
||||
context.get("flowB","key").should.be.equal("flowB");
|
||||
return context.clean(["flowA"]);
|
||||
}).then(function(){
|
||||
context.get("flowA","key").should.be.equal("flowA");
|
||||
should.not.exist(context.get("flowB","key"));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Configuration', function () {
|
||||
var context;
|
||||
beforeEach(function() {
|
||||
context = LocalFileSystem({dir: resourcesDir, cache: false});
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close();
|
||||
}).then(function(){
|
||||
return fs.remove(resourcesDir);
|
||||
});
|
||||
});
|
||||
it('should change a base directory', function (done) {
|
||||
var differentBaseContext = LocalFileSystem({
|
||||
base: "contexts2",
|
||||
dir: resourcesDir,
|
||||
cache: false
|
||||
});
|
||||
differentBaseContext.open().then(function () {
|
||||
differentBaseContext.set("node2", "foo2", "bar2", function (err) {
|
||||
differentBaseContext.get("node2", "foo2", function (err, value) {
|
||||
value.should.be.equal("bar2");
|
||||
context.get("node2", "foo2", function(err, value) {
|
||||
should.not.exist(value);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should use userDir', function (done) {
|
||||
var userDirContext = LocalFileSystem({
|
||||
base: "contexts2",
|
||||
cache: false,
|
||||
settings: {
|
||||
userDir: resourcesDir
|
||||
}
|
||||
});
|
||||
userDirContext.open().then(function () {
|
||||
userDirContext.set("node2", "foo2", "bar2", function (err) {
|
||||
userDirContext.get("node2", "foo2", function (err, value) {
|
||||
value.should.be.equal("bar2");
|
||||
context.get("node2", "foo2", function (err, value) {
|
||||
should.not.exist(value);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should use NODE_RED_HOME', function (done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
process.env.NODE_RED_HOME = resourcesDir;
|
||||
fs.ensureDirSync(resourcesDir);
|
||||
fs.writeFileSync(path.join(resourcesDir,".config.json"),"");
|
||||
var nrHomeContext = LocalFileSystem({
|
||||
base: "contexts2",
|
||||
cache: false
|
||||
});
|
||||
try {
|
||||
nrHomeContext.open().then(function () {
|
||||
nrHomeContext.set("node2", "foo2", "bar2", function (err) {
|
||||
nrHomeContext.get("node2", "foo2", function (err, value) {
|
||||
value.should.be.equal("bar2");
|
||||
context.get("node2", "foo2", function (err, value) {
|
||||
should.not.exist(value);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
}
|
||||
});
|
||||
|
||||
it('should use HOME_PATH', function (done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
var oldHOMEPATH = process.env.HOMEPATH;
|
||||
process.env.NODE_RED_HOME = resourcesDir;
|
||||
process.env.HOMEPATH = resourcesDir;
|
||||
var homePath = path.join(resourcesDir, ".node-red");
|
||||
fs.outputFile(path.join(homePath, ".config.json"),"",function(){
|
||||
var homeContext = LocalFileSystem({
|
||||
base: "contexts2",
|
||||
cache: false
|
||||
});
|
||||
try {
|
||||
homeContext.open().then(function () {
|
||||
homeContext.set("node2", "foo2", "bar2", function (err) {
|
||||
homeContext.get("node2", "foo2", function (err, value) {
|
||||
value.should.be.equal("bar2");
|
||||
context.get("node2", "foo2", function (err, value) {
|
||||
should.not.exist(value);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
process.env.HOMEPATH = oldHOMEPATH;
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should use HOME_PATH', function (done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
var oldHOMEPATH = process.env.HOMEPATH;
|
||||
var oldHOME = process.env.HOME;
|
||||
process.env.NODE_RED_HOME = resourcesDir;
|
||||
process.env.HOMEPATH = resourcesDir;
|
||||
process.env.HOME = resourcesDir;
|
||||
var homeContext = LocalFileSystem({
|
||||
base: "contexts2",
|
||||
cache: false
|
||||
});
|
||||
try {
|
||||
homeContext.open().then(function () {
|
||||
homeContext.set("node2", "foo2", "bar2", function (err) {
|
||||
homeContext.get("node2", "foo2", function (err, value) {
|
||||
value.should.be.equal("bar2");
|
||||
context.get("node2", "foo2", function (err, value) {
|
||||
should.not.exist(value);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
process.env.HOMEPATH = oldHOMEPATH;
|
||||
process.env.HOME = oldHOME;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
321
test/unit/@node-red/runtime/lib/nodes/context/memory_spec.js
Normal file
321
test/unit/@node-red/runtime/lib/nodes/context/memory_spec.js
Normal file
@@ -0,0 +1,321 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require('should');
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var Memory = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/context/memory");
|
||||
|
||||
describe('memory',function() {
|
||||
var context;
|
||||
|
||||
beforeEach(function() {
|
||||
context = Memory({});
|
||||
return context.open();
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
return context.clean([]).then(function(){
|
||||
return context.close();
|
||||
});
|
||||
});
|
||||
|
||||
describe('#get/set',function() {
|
||||
describe('sync',function() {
|
||||
it('should store property',function() {
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
context.set("nodeX","foo","test");
|
||||
context.get("nodeX","foo").should.equal("test");
|
||||
});
|
||||
|
||||
it('should store property - creates parent properties',function() {
|
||||
context.set("nodeX","foo.bar","test");
|
||||
context.get("nodeX","foo").should.eql({bar:"test"});
|
||||
});
|
||||
|
||||
it('should delete property',function() {
|
||||
context.set("nodeX","foo.abc.bar1","test1");
|
||||
context.set("nodeX","foo.abc.bar2","test2");
|
||||
context.get("nodeX","foo.abc").should.eql({bar1:"test1",bar2:"test2"});
|
||||
context.set("nodeX","foo.abc.bar1",undefined);
|
||||
context.get("nodeX","foo.abc").should.eql({bar2:"test2"});
|
||||
context.set("nodeX","foo.abc",undefined);
|
||||
should.not.exist(context.get("nodeX","foo.abc"));
|
||||
context.set("nodeX","foo",undefined);
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
});
|
||||
|
||||
it('should not shared context with other scope', function() {
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
should.not.exist(context.get("nodeY","foo"));
|
||||
context.set("nodeX","foo","testX");
|
||||
context.set("nodeY","foo","testY");
|
||||
|
||||
context.get("nodeX","foo").should.equal("testX");
|
||||
context.get("nodeY","foo").should.equal("testY");
|
||||
});
|
||||
|
||||
it('should throw the error if the error occurs', function() {
|
||||
try{
|
||||
context.set("nodeX",".foo","test");
|
||||
should.fail("Error was not thrown");
|
||||
}catch(err){
|
||||
should.exist(err);
|
||||
try{
|
||||
context.get("nodeX",".foo");
|
||||
should.fail("Error was not thrown");
|
||||
}catch(err){
|
||||
should.exist(err);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
it('should get multiple values - all known', function() {
|
||||
context.set("nodeX","one","test1");
|
||||
context.set("nodeX","two","test2");
|
||||
context.set("nodeX","three","test3");
|
||||
context.set("nodeX","four","test4");
|
||||
|
||||
var values = context.get("nodeX",["one","two","four"]);
|
||||
values.should.eql(["test1","test2","test4"])
|
||||
})
|
||||
it('should get multiple values - include unknown', function() {
|
||||
context.set("nodeX","one","test1");
|
||||
context.set("nodeX","two","test2");
|
||||
context.set("nodeX","three","test3");
|
||||
context.set("nodeX","four","test4");
|
||||
|
||||
var values = context.get("nodeX",["one","unknown.with.multiple.levels"]);
|
||||
values.should.eql(["test1",undefined])
|
||||
})
|
||||
it('should throw error if bad key included in multiple keys', function() {
|
||||
context.set("nodeX","one","test1");
|
||||
context.set("nodeX","two","test2");
|
||||
context.set("nodeX","three","test3");
|
||||
context.set("nodeX","four","test4");
|
||||
|
||||
try{
|
||||
var values = context.get("nodeX",["one",".foo","three"]);
|
||||
should.fail("Error was not thrown");
|
||||
}catch(err){
|
||||
should.exist(err);
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('async',function() {
|
||||
it('should store property',function(done) {
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
should.not.exist(value);
|
||||
context.set("nodeX","foo","test",function(err){
|
||||
context.get("nodeX","foo",function(err, value){
|
||||
value.should.equal("test");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should pass the error to callback if the error occurs',function(done) {
|
||||
context.set("nodeX",".foo","test",function(err, value){
|
||||
should.exist(err);
|
||||
context.get("nodeX",".foo",function(err){
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
it('should get multiple values - all known', function(done) {
|
||||
context.set("nodeX","one","test1");
|
||||
context.set("nodeX","two","test2");
|
||||
context.set("nodeX","three","test3");
|
||||
context.set("nodeX","four","test4");
|
||||
|
||||
context.get("nodeX",["one","two","four"],function() {
|
||||
Array.prototype.slice.apply(arguments).should.eql([undefined,"test1","test2","test4"])
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('should get multiple values - include unknown', function(done) {
|
||||
context.set("nodeX","one","test1");
|
||||
context.set("nodeX","two","test2");
|
||||
context.set("nodeX","three","test3");
|
||||
context.set("nodeX","four","test4");
|
||||
|
||||
context.get("nodeX",["one","unknown"],function() {
|
||||
Array.prototype.slice.apply(arguments).should.eql([undefined,"test1",undefined])
|
||||
done();
|
||||
});
|
||||
})
|
||||
it('should throw error if bad key included in multiple keys', function(done) {
|
||||
context.set("nodeX","one","test1");
|
||||
context.set("nodeX","two","test2");
|
||||
context.set("nodeX","three","test3");
|
||||
context.set("nodeX","four","test4");
|
||||
|
||||
context.get("nodeX",["one",".foo","three"], function(err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
||||
|
||||
describe('#keys',function() {
|
||||
describe('sync',function() {
|
||||
it('should enumerate context keys', function() {
|
||||
var keys = context.keys("nodeX");
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
context.set("nodeX","foo","bar");
|
||||
keys = context.keys("nodeX");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.equal("foo");
|
||||
|
||||
context.set("nodeX","abc.def","bar");
|
||||
keys = context.keys("nodeX");
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.equal("abc");
|
||||
});
|
||||
|
||||
it('should enumerate context keys in each scopes', function() {
|
||||
var keysX = context.keys("nodeX");
|
||||
keysX.should.be.an.Array();
|
||||
keysX.should.be.empty();
|
||||
|
||||
var keysY = context.keys("nodeY");
|
||||
keysY.should.be.an.Array();
|
||||
keysY.should.be.empty();
|
||||
|
||||
context.set("nodeX","foo","bar");
|
||||
context.set("nodeY","hoge","piyo");
|
||||
keysX = context.keys("nodeX");
|
||||
keysX.should.have.length(1);
|
||||
keysX[0].should.equal("foo");
|
||||
|
||||
keysY = context.keys("nodeY");
|
||||
keysY.should.have.length(1);
|
||||
keysY[0].should.equal("hoge");
|
||||
});
|
||||
|
||||
it('should enumerate global context keys', function () {
|
||||
var keys = context.keys("global");
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
|
||||
context.set("global", "foo", "bar");
|
||||
keys = context.keys("global");
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.equal("foo");
|
||||
|
||||
context.set("global", "abc.def", "bar");
|
||||
keys = context.keys("global");
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.equal("abc");
|
||||
});
|
||||
|
||||
it('should not return specific keys as global context keys', function () {
|
||||
var keys = context.keys("global");
|
||||
|
||||
context.set("global", "set", "bar");
|
||||
context.set("global", "get", "bar");
|
||||
context.set("global", "keys", "bar");
|
||||
keys = context.keys("global");
|
||||
keys.should.have.length(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe('async',function() {
|
||||
it('should enumerate context keys', function(done) {
|
||||
context.keys("nodeX", function(err, keys) {
|
||||
keys.should.be.an.Array();
|
||||
keys.should.be.empty();
|
||||
context.set("nodeX", "foo", "bar", function(err) {
|
||||
context.keys("nodeX", function(err, keys) {
|
||||
keys.should.have.length(1);
|
||||
keys[0].should.equal("foo");
|
||||
context.set("nodeX","abc.def","bar",function(err){
|
||||
context.keys("nodeX",function(err, keys){
|
||||
keys.should.have.length(2);
|
||||
keys[1].should.equal("abc");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#delete',function() {
|
||||
it('should delete context',function() {
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
should.not.exist(context.get("nodeY","foo"));
|
||||
context.set("nodeX","foo","abc");
|
||||
context.set("nodeY","foo","abc");
|
||||
context.get("nodeX","foo").should.equal("abc");
|
||||
context.get("nodeY","foo").should.equal("abc");
|
||||
|
||||
return context.delete("nodeX").then(function(){
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
should.exist(context.get("nodeY","foo"));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#clean',function() {
|
||||
it('should clean unnecessary context',function() {
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
should.not.exist(context.get("nodeY","foo"));
|
||||
context.set("nodeX","foo","abc");
|
||||
context.set("nodeY","foo","abc");
|
||||
context.get("nodeX","foo").should.equal("abc");
|
||||
context.get("nodeY","foo").should.equal("abc");
|
||||
|
||||
return context.clean([]).then(function(){
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
should.not.exist(context.get("nodeY","foo"));
|
||||
});
|
||||
});
|
||||
it('should not clean active context',function() {
|
||||
should.not.exist(context.get("nodeX","foo"));
|
||||
should.not.exist(context.get("nodeY","foo"));
|
||||
context.set("nodeX","foo","abc");
|
||||
context.set("nodeY","foo","abc");
|
||||
context.get("nodeX","foo").should.equal("abc");
|
||||
context.get("nodeY","foo").should.equal("abc");
|
||||
|
||||
return context.clean(["nodeX"]).then(function(){
|
||||
should.exist(context.get("nodeX","foo"));
|
||||
should.not.exist(context.get("nodeY","foo"));
|
||||
});
|
||||
});
|
||||
it('should not clean global context', function () {
|
||||
context.set("global", "foo", "abc");
|
||||
context.get("global", "foo").should.equal("abc");
|
||||
|
||||
return context.clean(["global"]).then(function () {
|
||||
should.exist(context.get("global", "foo"));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
478
test/unit/@node-red/runtime/lib/nodes/credentials_spec.js
Normal file
478
test/unit/@node-red/runtime/lib/nodes/credentials_spec.js
Normal file
@@ -0,0 +1,478 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var when = require("when");
|
||||
var util = require("util");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var index = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/index");
|
||||
var credentials = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/credentials");
|
||||
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
|
||||
|
||||
describe('red/runtime/nodes/credentials', function() {
|
||||
|
||||
var encryptionDisabledSettings = {
|
||||
get: function(key) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
afterEach(function() {
|
||||
index.clearRegistry();
|
||||
});
|
||||
|
||||
it('loads provided credentials',function(done) {
|
||||
credentials.init({
|
||||
log: log,
|
||||
settings: encryptionDisabledSettings
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('adds a new credential',function(done) {
|
||||
credentials.init({
|
||||
log: log,
|
||||
settings: encryptionDisabledSettings
|
||||
});
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
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"},
|
||||
password2:{type:"password"},
|
||||
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();
|
||||
});
|
||||
});
|
||||
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();
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','credentials_load_failed');
|
||||
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();
|
||||
}).catch(function(err) {
|
||||
err.should.have.property('code','credentials_load_failed');
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
})
|
||||
})
|
||||
1108
test/unit/@node-red/runtime/lib/nodes/flows/Flow_spec.js
Normal file
1108
test/unit/@node-red/runtime/lib/nodes/flows/Flow_spec.js
Normal file
File diff suppressed because it is too large
Load Diff
583
test/unit/@node-red/runtime/lib/nodes/flows/index_spec.js
Normal file
583
test/unit/@node-red/runtime/lib/nodes/flows/index_spec.js
Normal file
@@ -0,0 +1,583 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var when = require("when");
|
||||
var clone = require("clone");
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows");
|
||||
var RedNode = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
|
||||
var RED = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes");
|
||||
var events = NR_TEST_UTILS.require("@node-red/runtime/lib/events");
|
||||
var credentials = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/credentials");
|
||||
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry")
|
||||
var Flow = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows/Flow");
|
||||
|
||||
describe('flows/index', function() {
|
||||
|
||||
var storage;
|
||||
var eventsOn;
|
||||
var credentialsClean;
|
||||
var credentialsLoad;
|
||||
|
||||
var flowCreate;
|
||||
var getType;
|
||||
|
||||
var mockLog = {
|
||||
log: sinon.stub(),
|
||||
debug: sinon.stub(),
|
||||
trace: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
info: sinon.stub(),
|
||||
metric: sinon.stub(),
|
||||
_: function() { return "abc"}
|
||||
}
|
||||
|
||||
|
||||
before(function() {
|
||||
getType = sinon.stub(typeRegistry,"get",function(type) {
|
||||
return type.indexOf('missing') === -1;
|
||||
});
|
||||
});
|
||||
after(function() {
|
||||
getType.restore();
|
||||
});
|
||||
|
||||
|
||||
beforeEach(function() {
|
||||
eventsOn = sinon.spy(events,"on");
|
||||
credentialsClean = sinon.stub(credentials,"clean",function(conf) {
|
||||
conf.forEach(function(n) {
|
||||
delete n.credentials;
|
||||
});
|
||||
return when.resolve();
|
||||
});
|
||||
credentialsLoad = sinon.stub(credentials,"load",function() {
|
||||
return when.resolve();
|
||||
});
|
||||
flowCreate = sinon.stub(Flow,"create",function(global, flow) {
|
||||
var id;
|
||||
if (typeof flow === 'undefined') {
|
||||
flow = global;
|
||||
id = '_GLOBAL_';
|
||||
} else {
|
||||
id = flow.id;
|
||||
}
|
||||
flowCreate.flows[id] = {
|
||||
flow: flow,
|
||||
global: global,
|
||||
start: sinon.spy(),
|
||||
update: sinon.spy(),
|
||||
stop: sinon.spy(),
|
||||
getActiveNodes: function() {
|
||||
return flow.nodes||{};
|
||||
},
|
||||
handleError: sinon.spy(),
|
||||
handleStatus: sinon.spy()
|
||||
|
||||
}
|
||||
return flowCreate.flows[id];
|
||||
});
|
||||
flowCreate.flows = {};
|
||||
|
||||
storage = {
|
||||
saveFlows: function(conf) {
|
||||
storage.conf = conf;
|
||||
return when.resolve();
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
afterEach(function(done) {
|
||||
eventsOn.restore();
|
||||
credentialsClean.restore();
|
||||
credentialsLoad.restore();
|
||||
flowCreate.restore();
|
||||
|
||||
flows.stopFlows().then(done);
|
||||
|
||||
});
|
||||
// describe('#init',function() {
|
||||
// it('registers the type-registered handler', function() {
|
||||
// flows.init({},{});
|
||||
// eventsOn.calledOnce.should.be.true();
|
||||
// });
|
||||
// });
|
||||
|
||||
describe('#setFlows', function() {
|
||||
it('sets the full flow', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.setFlows(originalConfig).then(function() {
|
||||
credentialsClean.called.should.be.true();
|
||||
storage.hasOwnProperty('conf').should.be.true();
|
||||
flows.getFlows().flows.should.eql(originalConfig);
|
||||
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"}
|
||||
];
|
||||
var loadStorage = {
|
||||
saveFlows: function(conf) {
|
||||
loadStorage.conf = conf;
|
||||
return when.resolve(456);
|
||||
},
|
||||
getFlows: function() {
|
||||
return when.resolve({flows:originalConfig,rev:123})
|
||||
}
|
||||
}
|
||||
flows.init({log:mockLog, settings:{},storage:loadStorage});
|
||||
flows.setFlows(originalConfig,"load").then(function() {
|
||||
credentialsClean.called.should.be.false();
|
||||
// 'load' type does not trigger a save
|
||||
loadStorage.hasOwnProperty('conf').should.be.false();
|
||||
flows.getFlows().flows.should.eql(originalConfig);
|
||||
done();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('extracts credentials from the full flow', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[],credentials:{"a":1}},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.setFlows(originalConfig).then(function() {
|
||||
credentialsClean.called.should.be.true();
|
||||
storage.hasOwnProperty('conf').should.be.true();
|
||||
var cleanedFlows = flows.getFlows();
|
||||
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();
|
||||
});
|
||||
});
|
||||
|
||||
it('updates existing flows with partial deployment - nodes', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
var newConfig = clone(originalConfig);
|
||||
newConfig.push({id:"t1-2",x:10,y:10,z:"t1",type:"test",wires:[]});
|
||||
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({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
flows.setFlows(newConfig,"nodes").then(function() {
|
||||
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();
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
|
||||
it('updates existing flows with partial deployment - flows', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
var newConfig = clone(originalConfig);
|
||||
newConfig.push({id:"t1-2",x:10,y:10,z:"t1",type:"test",wires:[]});
|
||||
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({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
flows.setFlows(newConfig,"nodes").then(function() {
|
||||
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();
|
||||
flows.stopFlows().then(done);
|
||||
})
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('#load', function() {
|
||||
it('loads the flow config', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
credentialsLoad.called.should.be.true();
|
||||
// 'load' type does not trigger a save
|
||||
storage.hasOwnProperty('conf').should.be.false();
|
||||
flows.getFlows().flows.should.eql(originalConfig);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#startFlows', function() {
|
||||
it('starts the loaded config', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
Object.keys(flowCreate.flows).should.eql(['_GLOBAL_','t1']);
|
||||
done();
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
it('does not start if nodes missing', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
flowCreate.called.should.be.false();
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('starts when missing nodes registered', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},
|
||||
{id:"t1-3",x:10,y:10,z:"t1",type:"missing2",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
flowCreate.called.should.be.false();
|
||||
|
||||
events.emit("type-registered","missing");
|
||||
setTimeout(function() {
|
||||
flowCreate.called.should.be.false();
|
||||
events.emit("type-registered","missing2");
|
||||
setTimeout(function() {
|
||||
flowCreate.called.should.be.true();
|
||||
done();
|
||||
},10);
|
||||
},10);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe.skip('#get',function() {
|
||||
|
||||
});
|
||||
|
||||
describe('#eachNode', function() {
|
||||
it('iterates the flow nodes', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
var c = 0;
|
||||
flows.eachNode(function(node) {
|
||||
c++
|
||||
})
|
||||
c.should.equal(2);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#stopFlows', function() {
|
||||
|
||||
});
|
||||
describe('#handleError', function() {
|
||||
it('passes error to correct flow', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
flows.handleError(originalConfig[0],"message",{});
|
||||
flowCreate.flows['t1'].handleError.called.should.be.true();
|
||||
done();
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
it('passes error to flows that use the originating global config', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"configNode",type:"test"},
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",config:"configNode",wires:[]},
|
||||
{id:"t2",type:"tab"},
|
||||
{id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]},
|
||||
{id:"t3",type:"tab"},
|
||||
{id:"t3-1",x:10,y:10,z:"t3",type:"test",config:"configNode",wires:[]}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
flows.handleError(originalConfig[0],"message",{});
|
||||
try {
|
||||
flowCreate.flows['t1'].handleError.called.should.be.true();
|
||||
flowCreate.flows['t2'].handleError.called.should.be.false();
|
||||
flowCreate.flows['t3'].handleError.called.should.be.true();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe('#handleStatus', function() {
|
||||
it('passes status to correct flow', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
flows.handleStatus(originalConfig[0],"message");
|
||||
flowCreate.flows['t1'].handleStatus.called.should.be.true();
|
||||
done();
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
|
||||
it('passes status to flows that use the originating global config', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"configNode",type:"test"},
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",config:"configNode",wires:[]},
|
||||
{id:"t2",type:"tab"},
|
||||
{id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]},
|
||||
{id:"t3",type:"tab"},
|
||||
{id:"t3-1",x:10,y:10,z:"t3",type:"test",config:"configNode",wires:[]}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
|
||||
events.once('nodes-started',function() {
|
||||
flows.handleStatus(originalConfig[0],"message");
|
||||
try {
|
||||
flowCreate.flows['t1'].handleStatus.called.should.be.true();
|
||||
flowCreate.flows['t2'].handleStatus.called.should.be.false();
|
||||
flowCreate.flows['t3'].handleStatus.called.should.be.true();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.startFlows();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#checkTypeInUse', function() {
|
||||
|
||||
before(function() {
|
||||
sinon.stub(typeRegistry,"getNodeInfo",function(id) {
|
||||
if (id === 'unused-module') {
|
||||
return {types:['one','two','three']}
|
||||
} else {
|
||||
return {types:['one','test','three']}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
after(function() {
|
||||
typeRegistry.getNodeInfo.restore();
|
||||
});
|
||||
|
||||
it('returns cleanly if type not is use', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.setFlows(originalConfig).then(function() {
|
||||
flows.checkTypeInUse("unused-module");
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('throws error if type is in use', function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.setFlows(originalConfig).then(function() {
|
||||
/*jshint immed: false */
|
||||
try {
|
||||
flows.checkTypeInUse("used-module");
|
||||
done("type_in_use error not thrown");
|
||||
} catch(err) {
|
||||
err.code.should.eql("type_in_use");
|
||||
done();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('#addFlow', function() {
|
||||
it("rejects duplicate node id",function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
flows.addFlow({
|
||||
label:'new flow',
|
||||
nodes:[
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]}
|
||||
]
|
||||
}).then(function() {
|
||||
done(new Error('failed to reject duplicate node id'));
|
||||
}).catch(function(err) {
|
||||
done();
|
||||
})
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it("addFlow",function(done) {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
storage.getFlows = function() {
|
||||
return when.resolve({flows:originalConfig});
|
||||
}
|
||||
storage.setFlows = function() {
|
||||
return when.resolve();
|
||||
}
|
||||
flows.init({log:mockLog, settings:{},storage:storage});
|
||||
flows.load().then(function() {
|
||||
return flows.startFlows();
|
||||
}).then(function() {
|
||||
flows.addFlow({
|
||||
label:'new flow',
|
||||
nodes:[
|
||||
{id:"t2-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t2-2",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t2-3",z:"t1",type:"test"}
|
||||
]
|
||||
}).then(function(id) {
|
||||
flows.getFlows().flows.should.have.lengthOf(6);
|
||||
var createdFlows = Object.keys(flowCreate.flows);
|
||||
createdFlows.should.have.lengthOf(3);
|
||||
createdFlows[2].should.eql(id);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
})
|
||||
});
|
||||
|
||||
});
|
||||
})
|
||||
describe('#updateFlow', function() {
|
||||
it.skip("updateFlow");
|
||||
})
|
||||
describe('#removeFlow', function() {
|
||||
it.skip("removeFlow");
|
||||
})
|
||||
describe('#disableFlow', function() {
|
||||
it.skip("disableFlow");
|
||||
})
|
||||
describe('#enableFlow', function() {
|
||||
it.skip("enableFlow");
|
||||
})
|
||||
});
|
||||
738
test/unit/@node-red/runtime/lib/nodes/flows/util_spec.js
Normal file
738
test/unit/@node-red/runtime/lib/nodes/flows/util_spec.js
Normal file
@@ -0,0 +1,738 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var sinon = require("sinon");
|
||||
var when = require("when");
|
||||
var clone = require("clone");
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var flowUtil = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows/util");
|
||||
var typeRegistry = NR_TEST_UTILS.require("@node-red/registry");
|
||||
var redUtil = NR_TEST_UTILS.require("@node-red/util").util;
|
||||
|
||||
describe('flows/util', function() {
|
||||
var getType;
|
||||
|
||||
before(function() {
|
||||
getType = sinon.stub(typeRegistry,"get",function(type) {
|
||||
return type!=='missing';
|
||||
});
|
||||
});
|
||||
after(function() {
|
||||
getType.restore();
|
||||
});
|
||||
|
||||
describe('#mapEnvVarProperties',function() {
|
||||
before(function() {
|
||||
process.env.foo1 = "bar1";
|
||||
process.env.foo2 = "bar2";
|
||||
process.env.foo3 = "bar3";
|
||||
})
|
||||
after(function() {
|
||||
delete process.env.foo1;
|
||||
delete process.env.foo2;
|
||||
delete process.env.foo3;
|
||||
})
|
||||
it('handles ENV substitutions in an object - $()', function() {
|
||||
var foo = {a:"$(foo1)",b:"$(foo2)",c:{d:"$(foo3)"}};
|
||||
for (var p in foo) {
|
||||
if (foo.hasOwnProperty(p)) {
|
||||
flowUtil.mapEnvVarProperties(foo,p);
|
||||
}
|
||||
}
|
||||
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
|
||||
});
|
||||
it('handles ENV substitutions in an object - ${}', function() {
|
||||
var foo = {a:"${foo1}",b:"${foo2}",c:{d:"${foo3}"}};
|
||||
for (var p in foo) {
|
||||
if (foo.hasOwnProperty(p)) {
|
||||
flowUtil.mapEnvVarProperties(foo,p);
|
||||
}
|
||||
}
|
||||
foo.should.eql({ a: 'bar1', b: 'bar2', c: { d: 'bar3' } } );
|
||||
});
|
||||
});
|
||||
|
||||
describe('#diffNodes',function() {
|
||||
it('handles a null old node', function() {
|
||||
flowUtil.diffNodes(null,{}).should.be.true();
|
||||
});
|
||||
it('ignores x/y changes', function() {
|
||||
flowUtil.diffNodes({x:10,y:10},{x:20,y:10}).should.be.false();
|
||||
flowUtil.diffNodes({x:10,y:10},{x:10,y:20}).should.be.false();
|
||||
});
|
||||
it('ignores wiring changes', function() {
|
||||
flowUtil.diffNodes({wires:[]},{wires:[1,2,3]}).should.be.false();
|
||||
});
|
||||
it('spots existing property change - string', function() {
|
||||
flowUtil.diffNodes({a:"foo"},{a:"bar"}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - number', function() {
|
||||
flowUtil.diffNodes({a:0},{a:1}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - boolean', function() {
|
||||
flowUtil.diffNodes({a:true},{a:false}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - truthy', function() {
|
||||
flowUtil.diffNodes({a:true},{a:1}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - falsey', function() {
|
||||
flowUtil.diffNodes({a:false},{a:0}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - array', function() {
|
||||
flowUtil.diffNodes({a:[0,1,2]},{a:[0,2,3]}).should.be.true();
|
||||
});
|
||||
it('spots existing property change - object', function() {
|
||||
flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{a:[0,2,3]}}).should.be.true();
|
||||
flowUtil.diffNodes({a:{a:[0,1,2]}},{a:{b:[0,1,2]}}).should.be.true();
|
||||
});
|
||||
it('spots added property', function() {
|
||||
flowUtil.diffNodes({a:"foo"},{a:"foo",b:"bar"}).should.be.true();
|
||||
});
|
||||
it('spots removed property', function() {
|
||||
flowUtil.diffNodes({a:"foo",b:"bar"},{a:"foo"}).should.be.true();
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('#parseConfig',function() {
|
||||
|
||||
it('parses a single-tab flow', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]},"t1":{"id":"t1","type":"tab"}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]}}}},"missingTypes":[]};
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a single-tab flow with global config node', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",foo:"cn", wires:[]},
|
||||
{id:"cn",type:"test"},
|
||||
{id:"t1",type:"tab"}
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]},"cn":{"id":"cn","type":"test"},"t1":{"id":"t1","type":"tab"}},"subflows":{},"configs":{"cn":{"id":"cn","type":"test","_users":["t1-1"]}},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]}}}},"missingTypes":[]};
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a multi-tab flow', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",wires:[]},
|
||||
{id:"t2",type:"tab"},
|
||||
{id:"t2-1",x:10,y:10,z:"t2",type:"test",wires:[]}
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]},"t2":{"id":"t2","type":"tab"},"t2-1":{"id":"t2-1","x":10,"y":10,"z":"t2","type":"test","wires":[]}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","wires":[]}}},"t2":{"id":"t2","type":"tab","subflows":{},"configs":{},"nodes":{"t2-1":{"id":"t2-1","x":10,"y":10,"z":"t2","type":"test","wires":[]}}}},"missingTypes":[]};
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a subflow flow', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"subflow:sf1",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf1-1",x:10,y:10,z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[]},"sf1":{"id":"sf1","type":"subflow"},"sf1-1":{"id":"sf1-1","x":10,"y":10,"z":"sf1","type":"test","wires":[]}},"subflows":{"sf1":{"id":"sf1","type":"subflow","configs":{},"nodes":{"sf1-1":{"id":"sf1-1","x":10,"y":10,"z":"sf1","type":"test","wires":[]}},"instances":[{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[],"subflow":"sf1"}]}},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"subflow:sf1","wires":[],"subflow":"sf1"}}}},"missingTypes":[]};
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
it('parses a flow with a missing type', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1",type:"tab"},
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"sf1",wires:[]},
|
||||
{id:"t1-2",x:10,y:10,z:"t1",type:"missing",wires:[]},
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
parsedConfig.missingTypes.should.eql(['missing']);
|
||||
var expectedConfig = {"allNodes":{"t1":{"id":"t1","type":"tab"},"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"sf1","wires":[]},"t1-2":{"id":"t1-2","x":10,"y":10,"z":"t1","type":"missing","wires":[]}},"subflows":{},"configs":{},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"sf1","wires":[]},'t1-2': { id: 't1-2', x: 10, y: 10, z: 't1', type: 'missing', wires: [] }}}},"missingTypes":["missing"]};
|
||||
redUtil.compareObjects(parsedConfig,expectedConfig).should.be.true();
|
||||
});
|
||||
|
||||
it('parses a flow with a missing flow', function() {
|
||||
var originalConfig = [
|
||||
{id:"t1-1",x:10,y:10,z:"t1",type:"test",foo:"cn", wires:[]},
|
||||
{id:"cn",type:"test"},
|
||||
];
|
||||
var parsedConfig = flowUtil.parseConfig(originalConfig);
|
||||
var expectedConfig = {"allNodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]},"cn":{"id":"cn","type":"test"}},"subflows":{},"configs":{"cn":{"id":"cn","type":"test","_users":["t1-1"]}},"flows":{"t1":{"id":"t1","type":"tab","subflows":{},"configs":{},"nodes":{"t1-1":{"id":"t1-1","x":10,"y":10,"z":"t1","type":"test","foo":"cn","wires":[]}}}},"missingTypes":[]};
|
||||
parsedConfig.should.eql(expectedConfig);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
describe('#diffConfigs', function() {
|
||||
|
||||
it('handles an identical configuration', function() {
|
||||
var config = [{id:"123",type:"test",foo:"a",wires:[]}];
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(clone(config));
|
||||
var changedConfig = flowUtil.parseConfig(clone(config));
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.should.have.length(0);
|
||||
});
|
||||
|
||||
it('identifies nodes with changed properties, including downstream linked', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[[1]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[0].foo = "b";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.eql(["1"]);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.should.eql(["2"]);
|
||||
|
||||
});
|
||||
it('identifies nodes with changed properties, including upstream linked', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[1].bar = "c";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.eql(["2"]);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.should.eql(["1"]);
|
||||
});
|
||||
|
||||
it('identifies nodes with changed credentials, including downstream linked', function() {
|
||||
var config = [{id:"1",type:"test",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[0].credentials = {};
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.eql(["1"]);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.should.eql(["2"]);
|
||||
});
|
||||
|
||||
it('identifies nodes with changed wiring', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[1].wires[0][0] = "3";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.eql(["2"]);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('identifies nodes with changed wiring - second connection added', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"2",type:"test",bar:"b",wires:[["1"]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[1].wires[0].push("1");
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.eql(["2"]);
|
||||
diffResult.linked.sort().should.eql(["1"]);
|
||||
});
|
||||
|
||||
it('identifies nodes with changed wiring - node connected', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[[]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[1].wires.push("3");
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.eql(["2"]);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('identifies new nodes', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig.push({id:"2",type:"test",bar:"b",wires:[["1"]]});
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.eql(["2"]);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1"]);
|
||||
});
|
||||
|
||||
it('identifies deleted nodes', function() {
|
||||
var config = [{id:"1",type:"test",foo:"a",wires:[["2"]]},{id:"2",type:"test",bar:"b",wires:[["3"]]},{id:"3",type:"test",foo:"a",wires:[]}];
|
||||
var newConfig = clone(config);
|
||||
newConfig.splice(1,1);
|
||||
newConfig[0].wires = [];
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.eql(["2"]);
|
||||
diffResult.rewired.should.eql(["1"]);
|
||||
diffResult.linked.sort().should.eql(["3"]);
|
||||
});
|
||||
|
||||
it('identifies config nodes changes, node->config', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",foo:"configNode",wires:[["2"]]},
|
||||
{id:"2",type:"test",bar:"b",wires:[["3"]]},
|
||||
{id:"3",type:"test",foo:"a",wires:[]},
|
||||
{id:"configNode",type:"testConfig"}
|
||||
];
|
||||
var newConfig = clone(config);
|
||||
newConfig[3].foo = "bar";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(["1","configNode"]);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["2","3"]);
|
||||
});
|
||||
|
||||
it('identifies config nodes changes, node->config->config', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",foo:"configNode1",wires:[["2"]]},
|
||||
{id:"2",type:"test",bar:"b",wires:[["3"]]},
|
||||
{id:"3",type:"test",foo:"a",wires:[]},
|
||||
{id:"configNode1",foo:"configNode2",type:"testConfig"},
|
||||
{id:"configNode2",type:"testConfig"}
|
||||
];
|
||||
var newConfig = clone(config);
|
||||
newConfig[4].foo = "bar";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(["1","configNode1","configNode2"]);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["2","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal property change', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf1-1",z:"sf1",type:"test",foo:"a",wires:[]},
|
||||
{id:"4",type:"subflow:sf1",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[4].foo = "b";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', '4', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
|
||||
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal wiring change', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[4].wires = [["sf1-2"]];
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal node add', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig.push({id:"sf1-3",z:"sf1",type:"test",wires:[]});
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal node delete', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig.splice(5,1);
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(1);
|
||||
diffResult.removed.sort().should.eql(['sf1-2']);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal subflow wiring change - input removed', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[3].in[0].wires = [];
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal subflow wiring change - input added', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[3].in[0].wires.push({"id":"sf1-2"});
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal subflow wiring change - output added', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[3].out[0].wires.push({"id":"sf1-2","port":0});
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal subflow wiring change - output removed', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow","in": [{"wires": [{"id": "sf1-1"}]}],"out": [{"wires": [{"id": "sf1-2","port": 0}]}]},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[3].out[0].wires = [];
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for a global config node change', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf1-1",z:"sf1",prop:"configNode",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"test",wires:[]},
|
||||
{id:"configNode",a:"foo",type:"test",wires:[]}
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[6].a = "bar";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', "configNode", 'sf1']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
it('marks a parent subflow as changed for an internal subflow instance change', function() {
|
||||
var config = [
|
||||
{id:"1",type:"test",wires:[["2"]]},
|
||||
{id:"2",type:"subflow:sf1",wires:[["3"]]},
|
||||
{id:"3",type:"test",wires:[]},
|
||||
{id:"sf1",type:"subflow"},
|
||||
{id:"sf2",type:"subflow"},
|
||||
{id:"sf1-1",z:"sf1",type:"test",wires:[]},
|
||||
{id:"sf1-2",z:"sf1",type:"subflow:sf2",wires:[]},
|
||||
{id:"sf2-1",z:"sf2",type:"test",wires:[]},
|
||||
{id:"sf2-2",z:"sf2",type:"test",wires:[]},
|
||||
];
|
||||
|
||||
var newConfig = clone(config);
|
||||
newConfig[8].a = "bar";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.sort().should.eql(['2', 'sf1', 'sf2']);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
diffResult.linked.sort().should.eql(["1","3"]);
|
||||
});
|
||||
|
||||
|
||||
it('ignores tab changes that are immaterial', function() {
|
||||
var config = [{id:"1",type:"tab",label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[0].label = "barney";
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
});
|
||||
|
||||
|
||||
it('marks a deleted tab as removed', function() {
|
||||
var config = [{id:"f1",type:"tab",label:"fred"},{id:"n1",type:"test",bar:"b",wires:[["1"]],z:"f1"},
|
||||
{id:"f2",type:"tab",label:"fred"},{id:"n2",type:"test",bar:"b",wires:[["1"]],z:"f2"}];
|
||||
var newConfig = clone(config);
|
||||
newConfig = newConfig.slice(0,2);
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.sort().should.eql(['f2', 'n2']);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
});
|
||||
|
||||
it('marks all nodes as added when tab state changes disabled to enabled', function() {
|
||||
var config = [{id:"1",type:"tab",disabled:true,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[0].disabled = false;
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
|
||||
diffResult.added.should.have.length(2);
|
||||
diffResult.added.sort().should.eql(["1","2"]);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(0);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
});
|
||||
it('marks all nodes as removed when tab state changes enabled to disabled', function() {
|
||||
var config = [{id:"1",type:"tab",disabled:false,label:"fred"},{id:"2",type:"test",bar:"b",wires:[["1"]],z:"1"},{id:"3",type:"test"}];
|
||||
var newConfig = clone(config);
|
||||
newConfig[0].disabled = true;
|
||||
|
||||
var originalConfig = flowUtil.parseConfig(config);
|
||||
var changedConfig = flowUtil.parseConfig(newConfig);
|
||||
|
||||
originalConfig.missingTypes.should.have.length(0);
|
||||
|
||||
var diffResult = flowUtil.diffConfigs(originalConfig,changedConfig);
|
||||
|
||||
diffResult.added.should.have.length(0);
|
||||
diffResult.changed.should.have.length(0);
|
||||
diffResult.removed.should.have.length(2);
|
||||
diffResult.removed.sort().should.eql(["1","2"]);
|
||||
diffResult.rewired.should.have.length(0);
|
||||
});
|
||||
});
|
||||
});
|
||||
404
test/unit/@node-red/runtime/lib/nodes/index_spec.js
Normal file
404
test/unit/@node-red/runtime/lib/nodes/index_spec.js
Normal file
@@ -0,0 +1,404 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
var when = require("when");
|
||||
var sinon = require('sinon');
|
||||
var inherits = require("util").inherits;
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var index = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/index");
|
||||
var flows = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/flows");
|
||||
var registry = NR_TEST_UTILS.require("@node-red/registry")
|
||||
var Node = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/Node");
|
||||
|
||||
describe("red/nodes/index", function() {
|
||||
before(function() {
|
||||
sinon.stub(index,"startFlows");
|
||||
process.env.NODE_RED_HOME = NR_TEST_UTILS.resolve("node-red");
|
||||
});
|
||||
after(function() {
|
||||
index.startFlows.restore();
|
||||
delete process.env.NODE_RED_HOME;
|
||||
});
|
||||
|
||||
afterEach(function() {
|
||||
index.clearRegistry();
|
||||
});
|
||||
|
||||
process.env.foo="bar";
|
||||
var testFlows = [{"type":"test","id":"tab1","label":"Sheet 1"}];
|
||||
var testCredentials = {"tab1":{"b":1, "c":"2", "d":"$(foo)"}};
|
||||
var storage = {
|
||||
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 },
|
||||
get: function() { return false }
|
||||
};
|
||||
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var runtime = {
|
||||
settings: settings,
|
||||
storage: storage,
|
||||
log: {debug:function() {}, warn:function() {}},
|
||||
events: new EventEmitter()
|
||||
};
|
||||
|
||||
function TestNode(n) {
|
||||
index.createNode(this, n);
|
||||
var node = this;
|
||||
this.on("log", function() {
|
||||
// do nothing
|
||||
});
|
||||
}
|
||||
|
||||
it('nodes are initialised with credentials',function(done) {
|
||||
index.init(runtime);
|
||||
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);
|
||||
testnode.credentials.should.have.property('c',"2");
|
||||
testnode.credentials.should.have.property('d',"bar");
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('flows should be initialised',function(done) {
|
||||
index.init(runtime);
|
||||
index.loadFlows().then(function() {
|
||||
// console.log(testFlows);
|
||||
// console.log(index.getFlows());
|
||||
should.deepEqual(testFlows, index.getFlows().flows);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
|
||||
});
|
||||
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);
|
||||
});
|
||||
});
|
||||
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);
|
||||
|
||||
index.registerType('node-set','node-type2',TestNodeConstructor);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Node);
|
||||
TestNodeConstructor.prototype.should.be.an.instanceOf(Foo);
|
||||
});
|
||||
});
|
||||
describe("register credentials definition", function() {
|
||||
var http = require('http');
|
||||
var express = require('express');
|
||||
var app = express();
|
||||
var runtime = NR_TEST_UTILS.require("@node-red/runtime");
|
||||
var credentials = NR_TEST_UTILS.require("@node-red/runtime/lib/nodes/credentials");
|
||||
var localfilesystem = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem");
|
||||
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
var RED = NR_TEST_UTILS.require("node-red/lib/red.js");
|
||||
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
before(function(done) {
|
||||
sinon.stub(log,"log",function(){});
|
||||
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();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
after(function(done) {
|
||||
fs.remove(userDir,function() {
|
||||
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) {
|
||||
return when.resolve(randomNodeInfo);
|
||||
});
|
||||
});
|
||||
afterEach(function() {
|
||||
registry.getNodeInfo.restore();
|
||||
registry.disableNode.restore();
|
||||
});
|
||||
|
||||
it('allows an unused node type to be disabled',function(done) {
|
||||
index.init(runtime);
|
||||
index.registerType('test-node-set','test', TestNode);
|
||||
index.loadFlows().then(function() {
|
||||
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();
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('prevents disabling a node type that is in use',function(done) {
|
||||
index.init(runtime);
|
||||
index.registerType('test-node-set','test', TestNode);
|
||||
index.loadFlows().then(function() {
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
index.disabledNode("test");
|
||||
}).should.throw();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('prevents disabling a node type that is unknown',function(done) {
|
||||
index.init(runtime);
|
||||
index.registerType('test-node-set','test', TestNode);
|
||||
index.loadFlows().then(function() {
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
index.disableNode("doesnotexist");
|
||||
}).should.throw();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('allows modules to be removed from the registry', function() {
|
||||
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;
|
||||
}
|
||||
});
|
||||
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) {
|
||||
index.init(runtime);
|
||||
index.registerType('test-node-set','test', TestNode);
|
||||
index.loadFlows().then(function() {
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
index.removeModule("node-red");
|
||||
}).should.throw();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('prevents removing a module that is unknown',function(done) {
|
||||
index.init(runtime);
|
||||
index.registerType('test-node-set','test', TestNode);
|
||||
index.loadFlows().then(function() {
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
index.removeModule("doesnotexist");
|
||||
}).should.throw();
|
||||
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 393 B |
3
test/unit/@node-red/runtime/lib/nodes/resources/local/TestNodeModule/node_modules/TestNodeModule/icons/arrow-in.png
generated
vendored
Normal file
3
test/unit/@node-red/runtime/lib/nodes/resources/local/TestNodeModule/node_modules/TestNodeModule/icons/arrow-in.png
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
This file exists just to ensure the 'icons' directory is in the repository.
|
||||
TODO: a future test needs to ensure the right icon files are loaded - this
|
||||
directory can be used for that
|
||||
243
test/unit/@node-red/runtime/lib/settings_spec.js
Normal file
243
test/unit/@node-red/runtime/lib/settings_spec.js
Normal file
@@ -0,0 +1,243 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
var should = require("should");
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var settings = NR_TEST_UTILS.require("@node-red/runtime/lib/settings");
|
||||
|
||||
|
||||
describe("red/settings", function() {
|
||||
|
||||
afterEach(function() {
|
||||
settings.reset();
|
||||
});
|
||||
|
||||
it('wraps the user settings as read-only properties', function() {
|
||||
var userSettings = {
|
||||
a: 123,
|
||||
b: "test",
|
||||
c: [1,2,3]
|
||||
}
|
||||
settings.init(userSettings);
|
||||
|
||||
settings.available().should.be.false();
|
||||
|
||||
settings.a.should.equal(123);
|
||||
settings.b.should.equal("test");
|
||||
settings.c.should.be.an.Array();
|
||||
settings.c.should.have.lengthOf(3);
|
||||
|
||||
settings.get("a").should.equal(123);
|
||||
settings.get("b").should.equal("test");
|
||||
settings.get("c").should.be.an.Array();
|
||||
settings.get("c").should.have.lengthOf(3);
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
settings.a = 456;
|
||||
}).should.throw();
|
||||
|
||||
settings.c.push(5);
|
||||
settings.c.should.be.an.Array();
|
||||
settings.c.should.have.lengthOf(4);
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
settings.set("a",456);
|
||||
}).should.throw();
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
settings.set("a",456);
|
||||
}).should.throw();
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
settings.get("unknown");
|
||||
}).should.throw();
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
settings.set("unknown",456);
|
||||
}).should.throw();
|
||||
|
||||
});
|
||||
|
||||
it('loads global settings from storage', function(done) {
|
||||
var userSettings = {
|
||||
a: 123,
|
||||
b: "test",
|
||||
c: [1,2,3]
|
||||
}
|
||||
var savedSettings = null;
|
||||
var saveCount = 0;
|
||||
var storage = {
|
||||
getSettings: function() {
|
||||
return Promise.resolve({globalA:789});
|
||||
},
|
||||
saveSettings: function(settings) {
|
||||
saveCount++;
|
||||
savedSettings = settings;
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
settings.init(userSettings);
|
||||
|
||||
settings.available().should.be.false();
|
||||
|
||||
/*jshint immed: false */
|
||||
(function() {
|
||||
settings.get("unknown");
|
||||
}).should.throw();
|
||||
settings.load(storage).then(function() {
|
||||
settings.available().should.be.true();
|
||||
settings.get("globalA").should.equal(789);
|
||||
settings.set("globalA","abc").then(function() {
|
||||
savedSettings.globalA.should.equal("abc");
|
||||
saveCount.should.equal(1);
|
||||
settings.set("globalA","abc").then(function() {
|
||||
savedSettings.globalA.should.equal("abc");
|
||||
// setting to existing value should not trigger save
|
||||
saveCount.should.equal(1);
|
||||
done();
|
||||
});
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('removes persistent settings when reset', function() {
|
||||
var userSettings = {
|
||||
a: 123,
|
||||
b: "test",
|
||||
c: [1,2,3]
|
||||
}
|
||||
settings.init(userSettings);
|
||||
|
||||
settings.available().should.be.false();
|
||||
|
||||
settings.should.have.property("a",123);
|
||||
settings.should.have.property("b","test");
|
||||
settings.c.should.be.an.Array();
|
||||
settings.c.should.have.lengthOf(3);
|
||||
|
||||
settings.reset();
|
||||
|
||||
settings.should.not.have.property("a");
|
||||
settings.should.not.have.property("d");
|
||||
settings.should.not.have.property("c");
|
||||
|
||||
});
|
||||
|
||||
it('registers node settings and exports them', function() {
|
||||
var userSettings = {};
|
||||
settings.init(userSettings);
|
||||
settings.registerNodeSettings("inject", {injectColor:{value:"red", exportable:true}, injectSize:{value:"100", exportable:true}} );
|
||||
settings.registerNodeSettings("mqtt", {mqttColor:{value:"purple", exportable:false}, mqttSize:{value:"50", exportable:true}} );
|
||||
settings.registerNodeSettings("http request", {httpRequest1:{value:"a1", exportable:true}} );
|
||||
settings.registerNodeSettings(" http--request<> ", {httpRequest2:{value:"a2", exportable:true}} );
|
||||
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.should.have.property("injectColor", "red");
|
||||
safeSettings.should.have.property("injectSize", "100");
|
||||
safeSettings.should.not.have.property("mqttColor");
|
||||
safeSettings.should.have.property("mqttSize", "50");
|
||||
safeSettings.should.have.property("httpRequest1", "a1");
|
||||
safeSettings.should.have.property("httpRequest2", "a2");
|
||||
safeSettings.should.have.property("httpRequest3", "a3");
|
||||
safeSettings.should.have.property("mQtTColor", "purple");
|
||||
safeSettings.should.have.property("abc123", "def456");
|
||||
|
||||
safeSettings.should.have.property("noValueHasValue", "123");
|
||||
safeSettings.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.should.not.have.property("color");
|
||||
safeSettings.should.not.have.property("ab1Color", "blue");
|
||||
safeSettings.should.not.have.property("AB2Color");
|
||||
safeSettings.should.not.have.property("abcColor");
|
||||
});
|
||||
|
||||
it('overwrites node settings with user settings', function() {
|
||||
var userSettings = {
|
||||
injectColor: "green",
|
||||
mqttColor: "yellow",
|
||||
abColor: [1,2,3]
|
||||
}
|
||||
settings.init(userSettings);
|
||||
settings.registerNodeSettings("inject", {injectColor:{value:"red", exportable:true}} );
|
||||
settings.registerNodeSettings("ab", {abColor:{value:"red", exportable:false}} );
|
||||
var safeSettings = {};
|
||||
settings.exportNodeSettings(safeSettings);
|
||||
safeSettings.should.have.property("injectColor", "green");
|
||||
safeSettings.should.not.have.property("mqttColor");
|
||||
safeSettings.should.not.have.property("abColor");
|
||||
});
|
||||
|
||||
it('disables/enables node settings', function() {
|
||||
var userSettings = {};
|
||||
settings.init(userSettings);
|
||||
|
||||
var safeSettings = {};
|
||||
settings.registerNodeSettings("inject", {injectColor:{value:"red", exportable:true}} );
|
||||
settings.registerNodeSettings("mqtt", {mqttColor:{value:"purple", exportable:true}} );
|
||||
settings.registerNodeSettings("http request", {httpRequestColor:{value:"yellow", exportable:true}} );
|
||||
settings.exportNodeSettings(safeSettings);
|
||||
safeSettings.should.have.property("injectColor", "red");
|
||||
safeSettings.should.have.property("mqttColor", "purple");
|
||||
safeSettings.should.have.property("httpRequestColor", "yellow");
|
||||
|
||||
safeSettings = {};
|
||||
var types = ["inject", "mqtt"];
|
||||
settings.disableNodeSettings(types);
|
||||
settings.exportNodeSettings(safeSettings);
|
||||
safeSettings.should.not.have.property("injectColor");
|
||||
safeSettings.should.not.have.property("mqttColor");
|
||||
safeSettings.should.have.property("httpRequestColor", "yellow");
|
||||
|
||||
safeSettings = {};
|
||||
types = ["inject"];
|
||||
settings.enableNodeSettings(types);
|
||||
settings.exportNodeSettings(safeSettings);
|
||||
safeSettings.should.have.property("injectColor", "red");
|
||||
safeSettings.should.not.have.property("mqttColor");
|
||||
safeSettings.should.have.property("httpRequestColor", "yellow");
|
||||
});
|
||||
|
||||
});
|
||||
272
test/unit/@node-red/runtime/lib/storage/index_spec.js
Normal file
272
test/unit/@node-red/runtime/lib/storage/index_spec.js
Normal file
@@ -0,0 +1,272 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
var when = require("when");
|
||||
var should = require("should");
|
||||
var paff = require('path');
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var storage = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/index");
|
||||
|
||||
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() {
|
||||
var one = 1;
|
||||
var zero = 0;
|
||||
try {
|
||||
zero.should.equal(one, "The initialization promise should never get resolved");
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
}).catch(function(e) {
|
||||
done(); //successfully rejected promise
|
||||
});
|
||||
});
|
||||
|
||||
it('non-string storage module', function(done) {
|
||||
var initSetsMeToTrue = false;
|
||||
|
||||
var moduleWithBooleanSettingInit = {
|
||||
init : function() {
|
||||
initSetsMeToTrue = true;
|
||||
}
|
||||
};
|
||||
|
||||
var setsBooleanModule = {
|
||||
settings: {
|
||||
storageModule : moduleWithBooleanSettingInit
|
||||
}
|
||||
};
|
||||
|
||||
storage.init(setsBooleanModule);
|
||||
initSetsMeToTrue.should.be.true();
|
||||
done();
|
||||
});
|
||||
|
||||
it('respects storage interface', function(done) {
|
||||
var calledFlagGetFlows = false;
|
||||
var calledFlagGetCredentials = false;
|
||||
var calledFlagGetAllFlows = false;
|
||||
var calledInit = false;
|
||||
var calledFlagGetSettings = false;
|
||||
var calledFlagGetSessions = false;
|
||||
|
||||
var interfaceCheckerModule = {
|
||||
init : function (settings) {
|
||||
settings.should.be.an.Object();
|
||||
calledInit = true;
|
||||
},
|
||||
getFlows : function() {
|
||||
calledFlagGetFlows = true;
|
||||
return when.resolve([]);
|
||||
},
|
||||
saveFlows : function (flows) {
|
||||
flows.should.be.an.Array();
|
||||
flows.should.have.lengthOf(0);
|
||||
return when.resolve("");
|
||||
},
|
||||
getCredentials : function() {
|
||||
calledFlagGetCredentials = true;
|
||||
return when.resolve({});
|
||||
},
|
||||
saveCredentials : function(credentials) {
|
||||
credentials.should.be.true();
|
||||
},
|
||||
getSettings : function() {
|
||||
calledFlagGetSettings = true;
|
||||
},
|
||||
saveSettings : function(settings) {
|
||||
settings.should.be.true();
|
||||
},
|
||||
getSessions : function() {
|
||||
calledFlagGetSessions = true;
|
||||
},
|
||||
saveSessions : function(sessions) {
|
||||
sessions.should.be.true();
|
||||
},
|
||||
getAllFlows : function() {
|
||||
calledFlagGetAllFlows = true;
|
||||
},
|
||||
getFlow : function(fn) {
|
||||
fn.should.equal("name");
|
||||
},
|
||||
saveFlow : function(fn, data) {
|
||||
fn.should.equal("name");
|
||||
data.should.be.true();
|
||||
},
|
||||
getLibraryEntry : function(type, path) {
|
||||
type.should.be.true();
|
||||
path.should.equal("name");
|
||||
},
|
||||
saveLibraryEntry : function(type, path, meta, body) {
|
||||
type.should.be.true();
|
||||
path.should.equal("name");
|
||||
meta.should.be.true();
|
||||
body.should.be.true();
|
||||
}
|
||||
};
|
||||
|
||||
var moduleToLoad = {
|
||||
settings: {
|
||||
storageModule : interfaceCheckerModule
|
||||
}
|
||||
};
|
||||
|
||||
var promises = [];
|
||||
storage.init(moduleToLoad);
|
||||
promises.push(storage.getFlows());
|
||||
promises.push(storage.saveFlows({flows:[],credentials:{}}));
|
||||
storage.getSettings();
|
||||
storage.saveSettings(true);
|
||||
storage.getSessions();
|
||||
storage.saveSessions(true);
|
||||
storage.getAllFlows();
|
||||
storage.getFlow("name");
|
||||
storage.saveFlow("name", true);
|
||||
storage.getLibraryEntry(true, "name");
|
||||
storage.saveLibraryEntry(true, "name", true, true);
|
||||
|
||||
when.settle(promises).then(function() {
|
||||
try {
|
||||
calledInit.should.be.true();
|
||||
calledFlagGetFlows.should.be.true();
|
||||
calledFlagGetCredentials.should.be.true();
|
||||
calledFlagGetAllFlows.should.be.true();
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('respects deprecated flow library functions', function() {
|
||||
|
||||
var savePath;
|
||||
var saveContent;
|
||||
var saveMeta;
|
||||
var saveType;
|
||||
|
||||
var interfaceCheckerModule = {
|
||||
init : function (settings) {
|
||||
settings.should.be.an.Object();
|
||||
},
|
||||
getLibraryEntry : function(type, path) {
|
||||
if (type === "flows") {
|
||||
if (path === "/" || path === "\\") {
|
||||
return when.resolve(["a",{fn:"test.json"}]);
|
||||
} else if (path == "/a" || path == "\\a") {
|
||||
return when.resolve([{fn:"test2.json"}]);
|
||||
} else if (path == paff.join("","a","test2.json")) {
|
||||
return when.resolve("test content");
|
||||
}
|
||||
}
|
||||
},
|
||||
saveLibraryEntry : function(type, path, meta, body) {
|
||||
saveType = type;
|
||||
savePath = path;
|
||||
saveContent = body;
|
||||
saveMeta = meta;
|
||||
return when.resolve();
|
||||
}
|
||||
};
|
||||
|
||||
var moduleToLoad = {
|
||||
settings: {
|
||||
storageModule : interfaceCheckerModule
|
||||
}
|
||||
};
|
||||
before(function() {
|
||||
storage.init(moduleToLoad);
|
||||
});
|
||||
it('getAllFlows',function(done) {
|
||||
storage.getAllFlows().then(function (res) {
|
||||
try {
|
||||
res.should.eql({ d: { a: { f: ['test2'] } }, f: [ 'test' ] });
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('getFlow',function(done) {
|
||||
storage.getFlow(paff.join("a","test2.json")).then(function(res) {
|
||||
try {
|
||||
res.should.eql("test content");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it ('saveFlow', function (done) {
|
||||
storage.saveFlow(paff.join("a","test2.json"),"new content").then(function(res) {
|
||||
try {
|
||||
savePath.should.eql(paff.join("a","test2.json"));
|
||||
saveContent.should.eql("new content");
|
||||
saveMeta.should.eql({});
|
||||
saveType.should.eql("flows");
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
}
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
||||
describe('handles missing settings/sessions interface', function() {
|
||||
before(function() {
|
||||
var interfaceCheckerModule = {
|
||||
init : function () {}
|
||||
};
|
||||
storage.init({settings:{storageModule: interfaceCheckerModule}});
|
||||
});
|
||||
|
||||
it('defaults missing getSettings',function(done) {
|
||||
storage.getSettings().then(function(settings) {
|
||||
should.not.exist(settings);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('defaults missing saveSettings',function(done) {
|
||||
storage.saveSettings({}).then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('defaults missing getSessions',function(done) {
|
||||
storage.getSessions().then(function(settings) {
|
||||
should.not.exist(settings);
|
||||
done();
|
||||
});
|
||||
});
|
||||
it('defaults missing saveSessions',function(done) {
|
||||
storage.saveSessions({}).then(function() {
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
@@ -0,0 +1,477 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
var sinon = require('sinon');
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var localfilesystem = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem");
|
||||
var log = NR_TEST_UTILS.require("@node-red/util").log;
|
||||
|
||||
describe('storage/localfilesystem', function() {
|
||||
var mockRuntime = {
|
||||
log:{
|
||||
_:function() { return "placeholder message"},
|
||||
info: function() { },
|
||||
warn: function() { },
|
||||
trace: function() {}
|
||||
}
|
||||
};
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
var testFlow = [{"type":"tab","id":"d8be2a6d.2741d8","label":"Sheet 1"}];
|
||||
beforeEach(function(done) {
|
||||
fs.remove(userDir,function(err) {
|
||||
fs.mkdir(userDir,done);
|
||||
});
|
||||
});
|
||||
afterEach(function(done) {
|
||||
fs.remove(userDir,done);
|
||||
});
|
||||
|
||||
it('should initialise the user directory',function(done) {
|
||||
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
||||
fs.existsSync(path.join(userDir,"lib")).should.be.true();
|
||||
fs.existsSync(path.join(userDir,"lib",'flows')).should.be.true();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should set userDir to NRH if .config.json presents',function(done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
|
||||
fs.mkdirSync(process.env.NODE_RED_HOME);
|
||||
fs.writeFileSync(path.join(process.env.NODE_RED_HOME,".config.json"),"{}","utf8");
|
||||
var settings = {};
|
||||
localfilesystem.init(settings, mockRuntime).then(function() {
|
||||
try {
|
||||
fs.existsSync(path.join(process.env.NODE_RED_HOME,"lib")).should.be.true();
|
||||
fs.existsSync(path.join(process.env.NODE_RED_HOME,"lib",'flows')).should.be.true();
|
||||
settings.userDir.should.equal(process.env.NODE_RED_HOME);
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
}
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should set userDir to HOMEPATH/.node-red if .config.json presents',function(done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
|
||||
var oldHOMEPATH = process.env.HOMEPATH;
|
||||
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
|
||||
fs.mkdirSync(process.env.HOMEPATH);
|
||||
fs.mkdirSync(path.join(process.env.HOMEPATH,".node-red"));
|
||||
fs.writeFileSync(path.join(process.env.HOMEPATH,".node-red",".config.json"),"{}","utf8");
|
||||
var settings = {};
|
||||
localfilesystem.init(settings, mockRuntime).then(function() {
|
||||
try {
|
||||
fs.existsSync(path.join(process.env.HOMEPATH,".node-red","lib")).should.be.true();
|
||||
fs.existsSync(path.join(process.env.HOMEPATH,".node-red","lib",'flows')).should.be.true();
|
||||
settings.userDir.should.equal(path.join(process.env.HOMEPATH,".node-red"));
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
process.env.NODE_HOMEPATH = oldHOMEPATH;
|
||||
}
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should set userDir to HOME/.node-red',function(done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
|
||||
var oldHOME = process.env.HOME;
|
||||
process.env.HOME = path.join(userDir,"HOME");
|
||||
var oldHOMEPATH = process.env.HOMEPATH;
|
||||
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
|
||||
|
||||
fs.mkdirSync(process.env.HOME);
|
||||
var settings = {};
|
||||
localfilesystem.init(settings, mockRuntime).then(function() {
|
||||
try {
|
||||
fs.existsSync(path.join(process.env.HOME,".node-red","lib")).should.be.true();
|
||||
fs.existsSync(path.join(process.env.HOME,".node-red","lib",'flows')).should.be.true();
|
||||
settings.userDir.should.equal(path.join(process.env.HOME,".node-red"));
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
process.env.HOME = oldHOME;
|
||||
process.env.HOMEPATH = oldHOMEPATH;
|
||||
}
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should set userDir to USERPROFILE/.node-red',function(done) {
|
||||
var oldNRH = process.env.NODE_RED_HOME;
|
||||
process.env.NODE_RED_HOME = path.join(userDir,"NRH");
|
||||
var oldHOME = process.env.HOME;
|
||||
process.env.HOME = "";
|
||||
var oldHOMEPATH = process.env.HOMEPATH;
|
||||
process.env.HOMEPATH = path.join(userDir,"HOMEPATH");
|
||||
var oldUSERPROFILE = process.env.USERPROFILE;
|
||||
process.env.USERPROFILE = path.join(userDir,"USERPROFILE");
|
||||
|
||||
fs.mkdirSync(process.env.USERPROFILE);
|
||||
var settings = {};
|
||||
localfilesystem.init(settings, mockRuntime).then(function() {
|
||||
try {
|
||||
fs.existsSync(path.join(process.env.USERPROFILE,".node-red","lib")).should.be.true();
|
||||
fs.existsSync(path.join(process.env.USERPROFILE,".node-red","lib",'flows')).should.be.true();
|
||||
settings.userDir.should.equal(path.join(process.env.USERPROFILE,".node-red"));
|
||||
done();
|
||||
} catch(err) {
|
||||
done(err);
|
||||
} finally {
|
||||
process.env.NODE_RED_HOME = oldNRH;
|
||||
process.env.HOME = oldHOME;
|
||||
process.env.HOMEPATH = oldHOMEPATH;
|
||||
process.env.USERPROFILE = oldUSERPROFILE;
|
||||
}
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle missing flow file',function(done) {
|
||||
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
||||
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
fs.existsSync(flowFilePath).should.be.false();
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty flow file, no backup',function(done) {
|
||||
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
||||
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
|
||||
fs.closeSync(fs.openSync(flowFilePath, 'w'));
|
||||
fs.existsSync(flowFilePath).should.be.true();
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle empty flow file, restores backup',function(done) {
|
||||
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
||||
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
|
||||
fs.closeSync(fs.openSync(flowFilePath, 'w'));
|
||||
fs.existsSync(flowFilePath).should.be.true();
|
||||
fs.existsSync(flowFileBackupPath).should.be.false();
|
||||
fs.writeFileSync(flowFileBackupPath,JSON.stringify(testFlow));
|
||||
fs.existsSync(flowFileBackupPath).should.be.true();
|
||||
setTimeout(function() {
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
},50);
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should save flows to the default file',function(done) {
|
||||
localfilesystem.init({userDir:userDir}, mockRuntime).then(function() {
|
||||
var flowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
|
||||
fs.existsSync(flowFilePath).should.be.false();
|
||||
fs.existsSync(flowFileBackupPath).should.be.false();
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.existsSync(flowFilePath).should.be.true();
|
||||
fs.existsSync(flowFileBackupPath).should.be.false();
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should save flows to the specified file',function(done) {
|
||||
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false();
|
||||
fs.existsSync(flowFilePath).should.be.false();
|
||||
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false();
|
||||
fs.existsSync(flowFilePath).should.be.true();
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should format the flows file when flowFilePretty specified',function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath,flowFilePretty:true}, mockRuntime).then(function() {
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
var content = fs.readFileSync(flowFilePath,"utf8");
|
||||
content.split("\n").length.should.be.above(1);
|
||||
localfilesystem.getFlows().then(function(flows) {
|
||||
flows.should.eql(testFlow);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should fsync the flows file',function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
localfilesystem.init({editorTheme:{projects:{enabled:false}},userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
sinon.spy(fs,"fsync");
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.fsync.callCount.should.be.greaterThan(0);
|
||||
fs.fsync.restore();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
fs.fsync.restore();
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should log fsync errors and continue',function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
sinon.stub(fs,"fsync", function(fd, cb) {
|
||||
cb(new Error());
|
||||
});
|
||||
sinon.spy(log,"warn");
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.fsync.callCount.should.be.greaterThan(0);
|
||||
log.warn.restore();
|
||||
fs.fsync.callCount.should.be.greaterThan(0);
|
||||
fs.fsync.restore();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should backup the flows file', function(done) {
|
||||
var defaultFlowFile = 'flows_'+require('os').hostname()+'.json';
|
||||
var defaultFlowFilePath = path.join(userDir,defaultFlowFile);
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var flowFileBackupPath = path.join(userDir,"."+flowFile+".backup");
|
||||
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false();
|
||||
fs.existsSync(flowFilePath).should.be.false();
|
||||
fs.existsSync(flowFileBackupPath).should.be.false();
|
||||
|
||||
localfilesystem.saveFlows(testFlow).then(function() {
|
||||
fs.existsSync(flowFileBackupPath).should.be.false();
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false();
|
||||
fs.existsSync(flowFilePath).should.be.true();
|
||||
var content = fs.readFileSync(flowFilePath,'utf8');
|
||||
var testFlow2 = [{"type":"tab","id":"bc5672ad.2741d8","label":"Sheet 2"}];
|
||||
|
||||
localfilesystem.saveFlows(testFlow2).then(function() {
|
||||
fs.existsSync(flowFileBackupPath).should.be.true();
|
||||
fs.existsSync(defaultFlowFilePath).should.be.false();
|
||||
fs.existsSync(flowFilePath).should.be.true();
|
||||
var backupContent = fs.readFileSync(flowFileBackupPath,'utf8');
|
||||
content.should.equal(backupContent);
|
||||
var content2 = fs.readFileSync(flowFilePath,'utf8');
|
||||
content2.should.not.equal(backupContent);
|
||||
done();
|
||||
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
|
||||
|
||||
});
|
||||
|
||||
it('should handle missing credentials', function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var credFile = path.join(userDir,"test_cred.json");
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
fs.existsSync(credFile).should.be.false();
|
||||
|
||||
localfilesystem.getCredentials().then(function(creds) {
|
||||
creds.should.eql({});
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should handle credentials', function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var credFile = path.join(userDir,"test_cred.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
|
||||
fs.existsSync(credFile).should.be.false();
|
||||
|
||||
var credentials = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystem.saveCredentials(credentials).then(function() {
|
||||
fs.existsSync(credFile).should.be.true();
|
||||
localfilesystem.getCredentials().then(function(creds) {
|
||||
creds.should.eql(credentials);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
it('should backup existing credentials', function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var credFile = path.join(userDir,"test_cred.json");
|
||||
var credFileBackup = path.join(userDir,".test_cred.json.backup");
|
||||
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath}, mockRuntime).then(function() {
|
||||
|
||||
fs.writeFileSync(credFile,"{}","utf8");
|
||||
|
||||
fs.existsSync(credFile).should.be.true();
|
||||
fs.existsSync(credFileBackup).should.be.false();
|
||||
|
||||
var credentials = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystem.saveCredentials(credentials).then(function() {
|
||||
fs.existsSync(credFile).should.be.true();
|
||||
fs.existsSync(credFileBackup).should.be.true();
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should format the creds file when flowFilePretty specified',function(done) {
|
||||
var flowFile = 'test.json';
|
||||
var flowFilePath = path.join(userDir,flowFile);
|
||||
var credFile = path.join(userDir,"test_cred.json");
|
||||
|
||||
localfilesystem.init({userDir:userDir, flowFile:flowFilePath, flowFilePretty:true}, mockRuntime).then(function() {
|
||||
|
||||
fs.existsSync(credFile).should.be.false();
|
||||
|
||||
var credentials = {"abc":{"type":"creds"}};
|
||||
|
||||
localfilesystem.saveCredentials(credentials).then(function() {
|
||||
fs.existsSync(credFile).should.be.true();
|
||||
var content = fs.readFileSync(credFile,"utf8");
|
||||
content.split("\n").length.should.be.above(1);
|
||||
localfilesystem.getCredentials().then(function(creds) {
|
||||
creds.should.eql(credentials);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var should = require("should");
|
||||
var fs = require('fs-extra');
|
||||
var path = require('path');
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
var localfilesystemLibrary = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem/library");
|
||||
|
||||
describe('storage/localfilesystem/library', function() {
|
||||
var userDir = path.join(__dirname,".testUserHome");
|
||||
beforeEach(function(done) {
|
||||
fs.remove(userDir,function(err) {
|
||||
fs.mkdir(userDir,done);
|
||||
});
|
||||
});
|
||||
afterEach(function(done) {
|
||||
fs.remove(userDir,done);
|
||||
});
|
||||
|
||||
it('should return an empty list of library objects',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an empty list of library objects (path=/)',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('object','/').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return an error for a non-existent library object',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('object','A/B').then(function(flows) {
|
||||
should.fail(null,null,"non-existent flow");
|
||||
}).catch(function(err) {
|
||||
should.exist(err);
|
||||
done();
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
function createObjectLibrary(type) {
|
||||
type = type ||"object";
|
||||
var objLib = path.join(userDir,"lib",type);
|
||||
try {
|
||||
fs.mkdirSync(objLib);
|
||||
} catch(err) {
|
||||
}
|
||||
fs.mkdirSync(path.join(objLib,"A"));
|
||||
fs.mkdirSync(path.join(objLib,"B"));
|
||||
fs.mkdirSync(path.join(objLib,"B","C"));
|
||||
if (type === "functions" || type === "object") {
|
||||
fs.writeFileSync(path.join(objLib,"file1.js"),"// abc: def\n// not a metaline \n\n Hi",'utf8');
|
||||
fs.writeFileSync(path.join(objLib,"B","file2.js"),"// ghi: jkl\n// not a metaline \n\n Hi",'utf8');
|
||||
}
|
||||
if (type === "flows" || type === "object") {
|
||||
fs.writeFileSync(path.join(objLib,"B","flow.json"),"Hi",'utf8');
|
||||
}
|
||||
}
|
||||
|
||||
it('should return a directory listing of library objects',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary();
|
||||
|
||||
localfilesystemLibrary.getLibraryEntry('object','').then(function(flows) {
|
||||
flows.should.eql([ 'A', 'B', { abc: 'def', fn: 'file1.js' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('object','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' }, { fn: 'flow.json' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('object','B/C').then(function(flows) {
|
||||
flows.should.eql([]);
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should load a flow library object with .json unspecified', function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("flows");
|
||||
localfilesystemLibrary.getLibraryEntry('flows','B/flow').then(function(flows) {
|
||||
flows.should.eql("Hi");
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
it('should return a library object',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary();
|
||||
localfilesystemLibrary.getLibraryEntry('object','B/file2.js').then(function(body) {
|
||||
body.should.eql("// not a metaline \n\n Hi");
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library function',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("functions");
|
||||
localfilesystemLibrary.getLibraryEntry('functions','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', { ghi: 'jkl', fn: 'file2.js' } ]);
|
||||
var ft = path.join("B","D","file3.js");
|
||||
localfilesystemLibrary.saveLibraryEntry('functions',ft,{mno:'pqr'},"// another non meta line\n\n Hi There").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('functions',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.js' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('functions',ft).then(function(body) {
|
||||
body.should.eql("// another non meta line\n\n Hi There");
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
})
|
||||
}, 50);
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
|
||||
it('should return a newly saved library flow',function(done) {
|
||||
localfilesystemLibrary.init({userDir:userDir}).then(function() {
|
||||
createObjectLibrary("flows");
|
||||
localfilesystemLibrary.getLibraryEntry('flows','B').then(function(flows) {
|
||||
flows.should.eql([ 'C', {fn:'flow.json'} ]);
|
||||
var ft = path.join("B","D","file3");
|
||||
localfilesystemLibrary.saveLibraryEntry('flows',ft,{mno:'pqr'},"Hi").then(function() {
|
||||
setTimeout(function() {
|
||||
localfilesystemLibrary.getLibraryEntry('flows',path.join("B","D")).then(function(flows) {
|
||||
flows.should.eql([ { mno: 'pqr', fn: 'file3.json' } ]);
|
||||
localfilesystemLibrary.getLibraryEntry('flows',ft+".json").then(function(body) {
|
||||
body.should.eql("Hi");
|
||||
done();
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
})
|
||||
}, 50);
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
}).catch(function(err) {
|
||||
done(err);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,21 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
|
||||
describe("storage/localfilesystem/projects/Project", function() {
|
||||
it.skip("NEEDS TESTS WRITING",function() {});
|
||||
})
|
||||
@@ -0,0 +1,64 @@
|
||||
/**
|
||||
* Copyright JS Foundation and other contributors, http://js.foundation
|
||||
*
|
||||
* 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.
|
||||
**/
|
||||
|
||||
|
||||
var should = require("should");
|
||||
var NR_TEST_UTILS = require("nr-test-utils");
|
||||
var defaultFileSet = NR_TEST_UTILS.require("@node-red/runtime/lib/storage/localfilesystem/projects/defaultFileSet");
|
||||
|
||||
describe('storage/localfilesystem/projects/defaultFileSet', function() {
|
||||
var runtime = {
|
||||
i18n: {
|
||||
"_": function(name) {
|
||||
return name;
|
||||
}
|
||||
}
|
||||
};
|
||||
it('generates package.json for a project', function() {
|
||||
var generated = defaultFileSet["package.json"]({
|
||||
name: "A TEST NAME",
|
||||
summary: "A TEST SUMMARY",
|
||||
files: {
|
||||
flow: "MY FLOW FILE",
|
||||
credentials: "MY CREDENTIALS FILE"
|
||||
}
|
||||
}, runtime);
|
||||
|
||||
var parsed = JSON.parse(generated);
|
||||
parsed.should.have.property('name',"A TEST NAME");
|
||||
parsed.should.have.property('description',"A TEST SUMMARY");
|
||||
parsed.should.have.property('node-red');
|
||||
parsed['node-red'].should.have.property('settings');
|
||||
parsed['node-red'].settings.should.have.property('flowFile',"MY FLOW FILE");
|
||||
parsed['node-red'].settings.should.have.property('credentialsFile',"MY CREDENTIALS FILE");
|
||||
});
|
||||
|
||||
it('generates README.md for a project', function() {
|
||||
var generated = defaultFileSet["README.md"]({
|
||||
name: "A TEST NAME",
|
||||
summary: "A TEST SUMMARY"
|
||||
}, runtime);
|
||||
generated.should.match(/A TEST NAME/);
|
||||
generated.should.match(/A TEST SUMMARY/);
|
||||
});
|
||||
it('generates .gitignore for a project', function() {
|
||||
var generated = defaultFileSet[".gitignore"]({
|
||||
name: "A TEST NAME",
|
||||
summary: "A TEST SUMMARY"
|
||||
}, runtime);
|
||||
generated.length.should.be.greaterThan(0);
|
||||
});
|
||||
});
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user