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

Support at sign in module examples path for flows endpoint

This commit allows an example from an npm package that has
an org scoped name (which includes an @ character) to be retrieved and
loaded properly through the flows endpoint.
This commit is contained in:
Jeremy Barlow 2018-01-29 15:23:19 -08:00
parent 771e43583a
commit 3938550ea8
2 changed files with 50 additions and 3 deletions

View File

@ -95,7 +95,7 @@ module.exports = {
}, },
get: function(req,res) { get: function(req,res) {
if (req.params[0].indexOf("_examples_/") === 0) { if (req.params[0].indexOf("_examples_/") === 0) {
var m = /^_examples_\/([^\/]+)\/(.*)$/.exec(req.params[0]); var m = /^_examples_\/(@.*?\/[^\/]+|[^\/]+)\/(.*)$/.exec(req.params[0]);
if (m) { if (m) {
var module = m[1]; var module = m[1];
var path = m[2]; var path = m[2];

View File

@ -15,6 +15,9 @@
**/ **/
var should = require("should"); var should = require("should");
var sinon = require("sinon");
var fs = require("fs");
var fspath = require('path');
var request = require('supertest'); var request = require('supertest');
var express = require('express'); var express = require('express');
var bodyParser = require('body-parser'); var bodyParser = require('body-parser');
@ -27,7 +30,7 @@ var auth = require("../../../../red/api/auth");
describe("api/editor/library", function() { describe("api/editor/library", function() {
function initLibrary(_flows,_libraryEntries,_examples) { function initLibrary(_flows,_libraryEntries,_examples,_exampleFlowPathFunction) {
var flows = _flows; var flows = _flows;
var libraryEntries = _libraryEntries; var libraryEntries = _libraryEntries;
library.init(app,{ library.init(app,{
@ -88,7 +91,8 @@ describe("api/editor/library", function() {
nodes: { nodes: {
getNodeExampleFlows: function() { getNodeExampleFlows: function() {
return _examples; return _examples;
} },
getNodeExampleFlowPath: _exampleFlowPathFunction
} }
}); });
} }
@ -100,6 +104,13 @@ describe("api/editor/library", function() {
app.get("/library/flows",library.getAll); app.get("/library/flows",library.getAll);
app.post(new RegExp("/library/flows\/(.*)"),library.post); app.post(new RegExp("/library/flows\/(.*)"),library.post);
app.get(new RegExp("/library/flows\/(.*)"),library.get); 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) { it('returns empty result', function(done) {
initLibrary({},{flows:{}}); initLibrary({},{flows:{}});
@ -200,6 +211,42 @@ describe("api/editor/library", function() {
done(); 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',
'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',
'@org_scope/node_package:example-one');
done();
});
});
}); });
describe("type", function() { describe("type", function() {