Fixup all the tests

This commit is contained in:
Nick O'Leary
2018-04-24 15:01:49 +01:00
parent 34832d5942
commit 5d064aa1d7
50 changed files with 3480 additions and 1762 deletions

View File

@@ -15,6 +15,7 @@
**/
var runtimeAPI;
var apiUtils = require("../util");
module.exports = {
init: function(_runtimeAPI) {
@@ -23,7 +24,7 @@ module.exports = {
get: function(req,res) {
var version = req.get("Node-RED-API-Version")||"v1";
if (!/^v[12]$/.test(version)) {
return res.status(500).json({code:"invalid_api_version", message:"Invalid API Version requested"});
return res.status(400).json({code:"invalid_api_version", message:"Invalid API Version requested"});
}
var opts = {
user: req.user
@@ -41,7 +42,7 @@ module.exports = {
post: function(req,res) {
var version = req.get("Node-RED-API-Version")||"v1";
if (!/^v[12]$/.test(version)) {
return res.status(500).json({code:"invalid_api_version", message:"Invalid API Version requested"});
return res.status(400).json({code:"invalid_api_version", message:"Invalid API Version requested"});
}
var opts = {
user: req.user,

View File

@@ -56,7 +56,7 @@ module.exports = {
user: req.user,
module: req.params[0]
}
runtimeAPI.nodes.removeModule(opts).then(function(info) {
runtimeAPI.nodes.removeModule(opts).then(function() {
res.status(204).end();
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
@@ -77,7 +77,7 @@ module.exports = {
} else {
opts.lang = apiUtils.determineLangFromHeaders(req.acceptsLanguages());
runtimeAPI.nodes.getNodeConfig(opts).then(function(result) {
return res.json(result);
return res.send(result);
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);
})
@@ -100,7 +100,7 @@ module.exports = {
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
res.status(400).json({error:"invalid_request", message:"Invalid request"});
res.status(400).json({code:"invalid_request", message:"Invalid request"});
return;
}
var opts = {
@@ -119,7 +119,7 @@ module.exports = {
var body = req.body;
if (!body.hasOwnProperty("enabled")) {
// log.audit({event: "nodes.module.set",error:"invalid_request"},req);
res.status(400).json({error:"invalid_request", message:"Invalid request"});
res.status(400).json({code:"invalid_request", message:"Invalid request"});
return;
}
var opts = {

View File

@@ -28,6 +28,8 @@ var runtimeAPI;
var wsServer;
var activeConnections = [];
var anonymousUser;
var retained = {};
var heartbeatTimer;
@@ -173,7 +175,8 @@ CommsConnection.prototype.subscribe = function(topic) {
function start() {
if (!settings.disableEditor) {
Users.default().then(function(anonymousUser) {
Users.default().then(function(_anonymousUser) {
anonymousUser = _anonymousUser;
var webSocketKeepAliveTime = settings.webSocketKeepAliveTime || 15000;
var path = settings.httpAdminRoot || "/";
path = (path.slice(0,1) != "/" ? "/":"") + path + (path.slice(-1) == "/" ? "":"/") + "comms";

View File

@@ -89,7 +89,6 @@ module.exports = {
// Library
var library = require("./library");
library.init(runtimeAPI);
editorApp.get("/library/flows",needsPermission("library.read"),library.getAll,apiUtil.errorHandler);
editorApp.get(/library\/([^\/]+)(?:$|\/(.*))/,needsPermission("library.read"),library.getEntry);
editorApp.post(/library\/([^\/]+)\/(.*)/,needsPermission("library.write"),library.saveEntry);
@@ -107,7 +106,7 @@ module.exports = {
// User Settings
editorApp.post("/settings/user",needsPermission("settings.write"),info.updateUserSettings,apiUtil.errorHandler);
// SSH keys
editorApp.use("/settings/user/keys",info.sshkeys());
editorApp.use("/settings/user/keys",needsPermission("settings.write"),info.sshkeys());
return editorApp;
}

View File

@@ -14,9 +14,9 @@
* limitations under the License.
**/
var apiUtils = require("../util");
var express = require("express");
var runtimeAPI;
var needsPermission = require("../auth").needsPermission;
function getUsername(userObj) {
var username = '__default';
@@ -34,7 +34,7 @@ module.exports = {
var app = express();
// List all SSH keys
app.get("/", needsPermission("settings.read"), function(req,res) {
app.get("/", function(req,res) {
var opts = {
user: req.user
}
@@ -48,7 +48,7 @@ module.exports = {
});
// Get SSH key detail
app.get("/:id", needsPermission("settings.read"), function(req,res) {
app.get("/:id", function(req,res) {
var opts = {
user: req.user,
id: req.params.id
@@ -63,11 +63,17 @@ module.exports = {
});
// Generate a SSH key
app.post("/", needsPermission("settings.write"), function(req,res) {
app.post("/", function(req,res) {
var opts = {
user: req.user,
id: req.params.id
}
// TODO: validate params
opts.name = req.body.name;
opts.password = req.body.password;
opts.comment = req.body.comment;
opts.size = req.body.size;
runtimeAPI.settings.generateUserKey(opts).then(function(name) {
res.json({
name: name
@@ -78,12 +84,12 @@ module.exports = {
});
// Delete a SSH key
app.delete("/:id", needsPermission("settings.write"), function(req,res) {
app.delete("/:id", function(req,res) {
var opts = {
user: req.user,
id: req.params.id
}
runtimeAPI.settings.generateUserKey(opts).then(function(name) {
runtimeAPI.settings.removeUserKey(opts).then(function(name) {
res.status(204).end();
}).catch(function(err) {
apiUtils.rejectHandler(req,res,err);

View File

@@ -89,23 +89,9 @@ module.exports = {
init: init,
start: start,
stop: stop,
library: {
register: function(type) {
if (editor) {
editor.registerLibrary(type);
}
}
},
auth: {
needsPermission: auth.needsPermission
},
comms: {
publish: function(topic,data,retain) {
if (editor) {
editor.publish(topic,data,retain);
}
}
},
get adminApp() { return adminApp; },
get server() { return server; }
};

View File

@@ -21,7 +21,6 @@ var i18n = require("../util").i18n; // TODO: separate module
module.exports = {
errorHandler: function(err,req,res,next) {
console.error(err.stack);
if (err.message === "request entity too large") {
log.error(err);
} else {
@@ -40,13 +39,9 @@ module.exports = {
return lang;
},
rejectHandler: function(req,res,err) {
res.status(err.status||500);
if (err.code || err.message) {
res.json({
code: err.code||"unexpected_error",
message: err.message
})
}
res.end();
res.status(err.status||500).json({
code: err.code||"unexpected_error",
message: err.message||err.toString()
});
}
}

View File

@@ -111,9 +111,20 @@ module.exports = {
util: runtime.util,
version: runtime.version,
events: runtime.events,
comms: api.comms,
library: api.library,
comms: {
publish: function(topic,data,retain) {
runtime.events.emit("comms",{
topic: topic,
data: data,
retain: retain
})
}
},
library: {
register: function(type) {
return runtime.library.register(null,type);
}
},
auth: api.auth,
get app() { console.log("Deprecated use of RED.app - use RED.httpAdmin instead"); return runtime.app },

View File

@@ -49,13 +49,15 @@ function publish(topic,data,retain) {
} else {
delete retained[topic];
}
connections.forEach(connection => connection.send(topic,data,retain))
connections.forEach(connection => connection.send(topic,data))
}
var api = module.exports = {
init: function(_runtime) {
runtime = _runtime;
connections = [];
retained = {};
runtime.events.removeListener("node-status",handleStatusEvent);
runtime.events.on("node-status",handleStatusEvent);
runtime.events.removeListener("runtime-event",handleRuntimeEvent);

View File

@@ -135,6 +135,7 @@ var api = module.exports = {
} else {
runtime.log.audit({event: "flow.get",id:opts.id,error:"not_found"});
var err = new Error();
err.code = "not_found";
err.status = 404;
return reject(err);
}

View File

@@ -42,9 +42,6 @@ var stubbedExpressApp = {
delete: function() {}
}
var adminApi = {
library: {
register: function() {}
},
auth: {
needsPermission: function() {}
},

View File

@@ -29,17 +29,20 @@ function init(_runtime) {
}
function registerType(id,type) {
if (knownTypes.hasOwnProperty(type)) {
throw new Error(`Library type '${type}' already registerd by ${id}'`)
}
// TODO: would like to enforce this, but currently the tests register the same type multiple
// times and have no way to remove themselves.
// if (knownTypes.hasOwnProperty(type)) {
// throw new Error(`Library type '${type}' already registered by ${id}'`)
// }
knownTypes[type] = id;
}
function getAllEntries(type) {
if (!knownTypes.hasOwnProperty(type)) {
throw new Error(`Unknown library type '${type}'`);
}
}
// function getAllEntries(type) {
// if (!knownTypes.hasOwnProperty(type)) {
// throw new Error(`Unknown library type '${type}'`);
// }
// }
function getEntry(type,path) {
if (type !== 'flows') {
if (!knownTypes.hasOwnProperty(type)) {
@@ -67,12 +70,11 @@ function getEntry(type,path) {
return reject(err);
}
}
} else {
// IF we get here, we didn't find the file
var error = new Error("not_found");
error.code = "not_found";
return reject(error);
}
// IF we get here, we didn't find the file
var error = new Error("not_found");
error.code = "not_found";
return reject(error);
} else {
resolve(storage.getFlow(path));
}
@@ -92,8 +94,8 @@ function saveEntry(type,path,meta,body) {
module.exports = {
init: init,
registerType: registerType,
getAllEntries: getAllEntries,
register: registerType,
// getAllEntries: getAllEntries,
getEntry: getEntry,
saveEntry: saveEntry

View File

@@ -79,6 +79,11 @@ function createNodeApi(node) {
retain: retain
})
}
},
library: {
register: function(type) {
return runtime.library.register(node.id,type);
}
}
}
copyObjectProperties(runtime.nodes,red.nodes,["createNode","getNode","eachNode","addCredentials","getCredentials","deleteCredentials" ]);
@@ -88,11 +93,6 @@ function createNodeApi(node) {
copyObjectProperties(runtime.log,red.log,null,["init"]);
copyObjectProperties(runtime.settings,red.settings,null,["init","load","reset"]);
if (runtime.adminApi) {
red.library = {
register: function(type) {
return runtime.library.registerType(node.id,type);
}
};
red.auth = runtime.adminApi.auth;
red.httpAdmin = runtime.adminApi.adminApp;
red.httpNode = runtime.nodeApp;
@@ -100,12 +100,6 @@ function createNodeApi(node) {
} else {
//TODO: runtime.adminApi is always stubbed if not enabled, so this block
// is unused - but may be needed for the unit tests
red.comms = {
publish: function() {}
};
red.library = {
register: function() {}
};
red.auth = {
needsPermission: function() {}
};