Add unit tests for refactored API modules

This commit is contained in:
Nick O'Leary
2014-11-04 17:05:29 +00:00
parent 72f9471f2b
commit e7eb02fcb7
16 changed files with 1352 additions and 318 deletions

View File

@@ -17,6 +17,7 @@ var express = require('express');
var fs = require("fs");
var events = require("../events");
var path = require("path");
var util = require("util");
var redNodes = require("../nodes");
var settings = require("../settings");

View File

@@ -60,6 +60,8 @@ function init(adminApp) {
adminApp.get("/library/flows",library.getAll);
adminApp.get(new RegExp("/library/flows\/(.*)"),library.get);
// Error Handler
adminApp.use(errorHandler);
}

View File

@@ -20,48 +20,49 @@ var redApp = null;
var storage = require("../storage");
function createLibrary(type) {
redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
var path = req.params[1]||"";
storage.getLibraryEntry(type,path).then(function(result) {
if (typeof result === "string") {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(result);
res.end();
} else {
res.json(result);
}
}).otherwise(function(err) {
if (err) {
util.log("[red] Error loading library entry '"+path+"' : "+err);
if (err.message.indexOf('forbidden') === 0) {
res.send(403);
return;
if (redApp) {
redApp.get(new RegExp("/library/"+type+"($|\/(.*))"),function(req,res) {
var path = req.params[1]||"";
storage.getLibraryEntry(type,path).then(function(result) {
if (typeof result === "string") {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.write(result);
res.end();
} else {
res.json(result);
}
}
res.send(404);
});
});
redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
var path = req.params[0];
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
res.send(204);
}).otherwise(function(err) {
util.log("[red] Error saving library entry '"+path+"' : "+err);
}).otherwise(function(err) {
if (err) {
util.log("[red] Error loading library entry '"+path+"' : "+err);
if (err.message.indexOf('forbidden') === 0) {
res.send(403);
return;
}
res.send(500);
});
}
res.send(404);
});
});
});
redApp.post(new RegExp("/library/"+type+"\/(.*)"),function(req,res) {
var path = req.params[0];
var fullBody = '';
req.on('data', function(chunk) {
fullBody += chunk.toString();
});
req.on('end', function() {
storage.saveLibraryEntry(type,path,req.query,fullBody).then(function() {
res.send(204);
}).otherwise(function(err) {
util.log("[red] Error saving library entry '"+path+"' : "+err);
if (err.message.indexOf('forbidden') === 0) {
res.send(403);
return;
}
res.send(500);
});
});
});
}
}
module.exports = {
init: function(app) {

View File

@@ -55,7 +55,7 @@ module.exports = {
return;
}
promise.then(function(info) {
res.json(info);
res.json(info);
}).otherwise(function(err) {
if (err.code === 404) {
res.send(404);
@@ -90,7 +90,6 @@ module.exports = {
promise.then(function(removedNodes) {
res.json(removedNodes);
}).otherwise(function(err) {
console.log(err.stack);
res.send(400,err.toString());
});
} catch(err) {

View File

@@ -32,7 +32,7 @@ events.on("node-icon-dir",function(dir) {
module.exports = {
ensureSlash: function(req,res,next) {
if (req.originalUrl.slice(-1) != "/") {
res.redirect(req.originalUrl+"/");
res.redirect(301,req.originalUrl+"/");
} else {
next();
}

View File

@@ -17,7 +17,7 @@
var express = require('express');
var util = require('util');
var when = require('when');
var exec = require('child_process').exec;
var child_process = require('child_process');
var redNodes = require("./nodes");
var comms = require("./comms");
@@ -28,7 +28,7 @@ var nodeApp = null;
var server = null;
var settings = null;
function createServer(_server,_settings) {
function init(_server,_settings) {
server = _server;
settings = _settings;
@@ -37,7 +37,6 @@ function createServer(_server,_settings) {
nodeApp = express();
app = express();
if (settings.httpAdminRoot !== false) {
require("./api").init(app);
}
@@ -147,7 +146,7 @@ function installModule(module) {
return;
}
util.log("[red] Installing module: "+module);
var child = exec('npm install --production '+module, function(err, stdin, stdout) {
var child = child_process.exec('npm install --production '+module, function(err, stdin, stdout) {
if (err) {
var lookFor404 = new RegExp(" 404 .*"+module+"$","m");
if (lookFor404.test(stdout)) {
@@ -171,14 +170,14 @@ function installModule(module) {
}
function uninstallModule(module) {
var list = redNodes.removeModule(module);
return when.promise(function(resolve,reject) {
if (/[\s;]/.test(module)) {
reject(new Error("Invalid module name"));
return;
}
var list = redNodes.removeModule(module);
util.log("[red] Removing module: "+module);
var child = exec('npm remove '+module, function(err, stdin, stdout) {
var child = child_process.exec('npm remove '+module, function(err, stdin, stdout) {
if (err) {
util.log("[red] Removal of module "+module+" failed:");
util.log("------------------------------------------");
@@ -202,7 +201,7 @@ function stop() {
}
module.exports = {
init: createServer,
init: init,
start: start,
stop: stop,

View File

@@ -75,9 +75,17 @@ var persistentSettings = {
},
reset: function() {
for (var i in userSettings) {
if (userSettings.hasOwnProperty(i)) {
delete persistentSettings[i];
}
}
userSettings = null;
globalSettings = null;
storage = null;
}
}