Remove known unused files

This commit is contained in:
andrew.greene
2021-12-08 18:01:31 -07:00
parent 419a81034c
commit b1daa8932a
367 changed files with 1 additions and 75791 deletions

View File

@@ -1,234 +0,0 @@
/**
* 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 context = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/context");
describe("api/admin/context", function () {
var app = undefined;
before(function () {
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);
app.delete("/context/:scope(global)/*", context.delete);
app.delete("/context/:scope(node|flow)/:id/*", context.delete);
});
describe("get", function () {
var gContext = {
default: { abc: { msg: '111', format: 'number' } },
file: { abc: { msg: '222', format: 'number' } }
};
var fContext = {
default: { bool: { msg: 'true', format: 'boolean' } },
file: { string: { msg: 'aaaa', format: 'string[7]' } }
};
var nContext = { msg: "1", format: "number" };
var stub = sinon.stub();
before(function () {
context.init({
context: {
getValue: stub
}
});
});
afterEach(function () {
stub.reset();
});
it('should call context.getValue to get global contexts', function (done) {
stub.returns(Promise.resolve(gContext));
request(app)
.get('/context/global')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'global');
stub.args[0][0].should.have.property('id', undefined);
stub.args[0][0].should.have.property('key', undefined);
stub.args[0][0].should.have.property('store', undefined);
var body = res.body;
body.should.eql(gContext);
done();
});
});
it('should call context.getValue to get flow contexts', function (done) {
stub.returns(Promise.resolve(fContext));
request(app)
.get('/context/flow/1234/')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'flow');
stub.args[0][0].should.have.property('id', '1234');
stub.args[0][0].should.have.property('key', undefined);
stub.args[0][0].should.have.property('store', undefined);
var body = res.body;
body.should.eql(fContext);
done();
});
});
it('should call context.getValue to get a node context', function (done) {
stub.returns(Promise.resolve(nContext));
request(app)
.get('/context/node/5678/foo?store=file')
.set('Accept', 'application/json')
.expect(200)
.end(function (err, res) {
if (err) {
return done(err);
}
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'node');
stub.args[0][0].should.have.property('id', '5678');
stub.args[0][0].should.have.property('key', 'foo');
stub.args[0][0].should.have.property('store', 'file');
var body = res.body;
body.should.eql(nContext);
done();
});
});
it('should handle error which context.getValue causes', function (done) {
var stubbedResult = Promise.reject('error');
stubbedResult.catch(function() {});
stub.returns(stubbedResult);
request(app)
.get('/context/global')
.set('Accept', 'application/json')
.expect(400)
.end(function (err, res) {
if (err) {
return done(err);
}
res.body.should.has.a.property('code', 'unexpected_error');
res.body.should.has.a.property('message', 'error');
done();
});
});
});
describe("delete", function () {
var stub = sinon.stub();
before(function () {
context.init({
context: {
delete: stub
}
});
});
afterEach(function () {
stub.reset();
});
it('should call context.delete to delete a global context', function (done) {
stub.returns(Promise.resolve());
request(app)
.delete('/context/global/abc?store=default')
.expect(204)
.end(function (err, res) {
if (err) {
return done(err);
}
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'global');
stub.args[0][0].should.have.property('id', undefined);
stub.args[0][0].should.have.property('key', 'abc');
stub.args[0][0].should.have.property('store', 'default');
done();
});
});
it('should call context.delete to delete a flow context', function (done) {
stub.returns(Promise.resolve());
request(app)
.delete('/context/flow/1234/abc?store=file')
.expect(204)
.end(function (err, res) {
if (err) {
return done(err);
}
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'flow');
stub.args[0][0].should.have.property('id', '1234');
stub.args[0][0].should.have.property('key', 'abc');
stub.args[0][0].should.have.property('store', 'file');
done();
});
});
it('should call context.delete to delete a node context', function (done) {
stub.returns(Promise.resolve());
request(app)
.delete('/context/node/5678/foo?store=file')
.expect(204)
.end(function (err, res) {
if (err) {
return done(err);
}
stub.args[0][0].should.have.property('user', undefined);
stub.args[0][0].should.have.property('scope', 'node');
stub.args[0][0].should.have.property('id', '5678');
stub.args[0][0].should.have.property('key', 'foo');
stub.args[0][0].should.have.property('store', 'file');
done();
});
});
it('should handle error which context.delete causes', function (done) {
var stubbedResult = Promise.reject('error');
stubbedResult.catch(function() {});
stub.returns(stubbedResult);
request(app)
.delete('/context/global/abc?store=default')
.expect(400)
.end(function (err, res) {
if (err) {
return done(err);
}
res.body.should.has.a.property('code', 'unexpected_error');
res.body.should.has.a.property('message', 'error');
done();
});
});
});
});

View File

@@ -1,248 +0,0 @@
/**
* 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 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();
});
})
})
});

View File

@@ -1,211 +0,0 @@
/**
* 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();
});
});
});

View File

@@ -1,458 +0,0 @@
/**
* 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");
var context = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/context");
/**
* 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,context
];
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").callsFake(function(){});
});
sinon.stub(auth,"needsPermission").callsFake(function(permission) {
return function(req,res,next) {
permissionChecks[permission] = (permissionChecks[permission]||0)+1;
next();
};
});
sinon.stub(flows,"get").callsFake(stubApp);
sinon.stub(flows,"post").callsFake(stubApp);
sinon.stub(flow,"get").callsFake(stubApp);
sinon.stub(flow,"post").callsFake(stubApp);
sinon.stub(flow,"delete").callsFake(stubApp);
sinon.stub(flow,"put").callsFake(stubApp);
sinon.stub(nodes,"getAll").callsFake(stubApp);
sinon.stub(nodes,"post").callsFake(stubApp);
sinon.stub(nodes,"getModule").callsFake(stubApp);
sinon.stub(nodes,"putModule").callsFake(stubApp);
sinon.stub(nodes,"delete").callsFake(stubApp);
sinon.stub(nodes,"getSet").callsFake(stubApp);
sinon.stub(nodes,"putSet").callsFake(stubApp);
sinon.stub(nodes,"getModuleCatalog").callsFake(stubApp);
sinon.stub(nodes,"getModuleCatalogs").callsFake(stubApp);
sinon.stub(context,"get").callsFake(stubApp);
sinon.stub(context,"delete").callsFake(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();
context.get.restore();
context.delete.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();
});
});
it('GET /context/global', function(done) {
request(app).get("/context/global").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.read',1);
lastRequest.params.should.have.property('scope','global');
done();
});
});
it('GET /context/global/key?store=memory', function(done) {
request(app).get("/context/global/key?store=memory").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.read',1);
lastRequest.params.should.have.property('scope','global');
lastRequest.params.should.have.property(0,'key');
lastRequest.query.should.have.property('store','memory');
done();
});
});
it('GET /context/flow/1234', function(done) {
request(app).get("/context/flow/1234").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.read',1);
lastRequest.params.should.have.property('scope','flow');
lastRequest.params.should.have.property('id','1234');
done();
});
});
it('GET /context/flow/1234/key?store=memory', function(done) {
request(app).get("/context/flow/1234/key?store=memory").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.read',1);
lastRequest.params.should.have.property('scope','flow');
lastRequest.params.should.have.property('id','1234');
lastRequest.params.should.have.property(0,'key');
lastRequest.query.should.have.property('store','memory');
done();
});
});
it('GET /context/node/5678', function(done) {
request(app).get("/context/node/5678").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.read',1);
lastRequest.params.should.have.property('scope','node');
lastRequest.params.should.have.property('id','5678');
done();
});
});
it('GET /context/node/5678/foo?store=memory', function(done) {
request(app).get("/context/node/5678/foo?store=memory").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.read',1);
lastRequest.params.should.have.property('scope','node');
lastRequest.params.should.have.property('id','5678');
lastRequest.params.should.have.property(0,'foo');
lastRequest.query.should.have.property('store','memory');
done();
});
});
it('DELETE /context/global/key?store=memory', function(done) {
request(app).del("/context/global/key?store=memory").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.write',1);
lastRequest.params.should.have.property('scope','global');
lastRequest.params.should.have.property(0,'key');
lastRequest.query.should.have.property('store','memory');
done();
});
});
it('DELETE /context/flow/1234/key?store=memory', function(done) {
request(app).del("/context/flow/1234/key?store=memory").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.write',1);
lastRequest.params.should.have.property('scope','flow');
lastRequest.params.should.have.property('id','1234');
lastRequest.params.should.have.property(0,'key');
lastRequest.query.should.have.property('store','memory');
done();
});
});
it('DELETE /context/node/5678/foo?store=memory', function(done) {
request(app).del("/context/node/5678/foo?store=memory").expect(200).end(function(err,res) {
if (err) {
return done(err);
}
permissionChecks.should.have.property('context.write',1);
lastRequest.params.should.have.property('scope','node');
lastRequest.params.should.have.property('id','5678');
lastRequest.params.should.have.property(0,'foo');
lastRequest.query.should.have.property('store','memory');
done();
});
});
});
});

View File

@@ -1,497 +0,0 @@
/**
* 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 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").callsFake(function() {
return "en-US";
});
});
after(function() {
apiUtil.determineLangFromHeaders.restore();
})
describe('get nodes', function() {
it('returns node list', function(done) {
nodes.init({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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",url:"https://example/foo-1.2.3.tgz"})
.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");
opts.should.have.property("url","https://example/foo-1.2.3.tgz");
done();
});
});
it('returns error', function(done) {
nodes.init({
settings: {},
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",url:"https://example/foo-1.2.3.tgz"})
.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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
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({
settings: {},
nodes:{
getModuleCatalog: function(opts) {
return Promise.resolve({a:123});
}
}
});
request(app)
.get('/nodes/module/set/messages')
.expect(200)
.end(function(err,res) {
if (err) {
throw err;
}
res.body.should.eql({a:123});
done();
});
});
it('returns all node catalogs', function(done) {
nodes.init({
settings: {},
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();
});
});
})
});

View File

@@ -1,111 +0,0 @@
const should = require("should");
const request = require('supertest');
const express = require('express');
const bodyParser = require("body-parser");
var app;
var NR_TEST_UTILS = require("nr-test-utils");
var plugins = NR_TEST_UTILS.require("@node-red/editor-api/lib/admin/plugins");
describe("api/editor/plugins", function() {
const pluginList = [
{
"id": "test-module/test-set",
"enabled": true,
"local": false,
"plugins": [
{
"type": "foo",
"id": "a-plugin",
"module": "test-module"
},
{
"type": "bar",
"id": "a-plugin2",
"module": "test-module"
},
{
"type": "foo",
"id": "a-plugin3",
"module": "test-module"
}
]
},
{
"id": "test-module/test-disabled-set",
"enabled": false,
"local": false,
"plugins": []
}
];
const pluginConfigs = `
<!-- --- [red-plugin:test-module/test-set] --- -->
test-module-config`;
const pluginCatalogs = { "test-module": {"foo": "bar"}};
before(function() {
app = express();
app.use(bodyParser.json());
app.get("/plugins",plugins.getAll);
app.get("/plugins/messages",plugins.getCatalogs);
plugins.init({
plugins: {
getPluginList: async function() { return pluginList },
getPluginConfigs: async function() { return pluginConfigs },
getPluginCatalogs: async function() { return pluginCatalogs }
}
})
});
it('returns the list of plugins', function(done) {
request(app)
.get("/plugins")
.set('Accept', 'application/json')
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
try {
JSON.stringify(res.body).should.eql(JSON.stringify(pluginList));
done();
} catch(err) {
done(err)
}
});
});
it('returns the plugin configs', function(done) {
request(app)
.get("/plugins")
.set('Accept', 'text/html')
.expect(200)
.expect(pluginConfigs)
.end(function(err,res) {
if (err) {
return done(err);
}
done();
});
});
it('returns the plugin catalogs', function(done) {
request(app)
.get("/plugins/messages")
.set('Accept', 'application/json')
.expect(200)
.end(function(err,res) {
if (err) {
return done(err);
}
try {
JSON.stringify(res.body).should.eql(JSON.stringify(pluginCatalogs));
done();
} catch(err) {
done(err)
}
});
});
});

View File

@@ -1,93 +0,0 @@
/**
* 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/admin/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").callsFake(function() { return { existing: 123, test: 456 };});
app = express();
app.use(bodyParser.json());
app.get("/settings",info.runtimeSettings);
});
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,
editorTheme: { existing: 789 }
})
}
}
});
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",{existing: 789, test:456});
done();
});
});
it('returns the runtime settings - disableEditor true', function(done) {
info.init({disableEditor: true},{
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);
// no editorTheme if disabledEditor true
res.body.should.not.have.property("editorTheme");
done();
});
});
});