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();
}