From 12a06cacce22c0ee20e510744d0571e1434e37cf Mon Sep 17 00:00:00 2001 From: zobalogh Date: Thu, 24 Jul 2014 14:11:39 +0100 Subject: [PATCH 1/2] Adding UI unit tests --- test/red/ui_spec.js | 151 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 147 insertions(+), 4 deletions(-) diff --git a/test/red/ui_spec.js b/test/red/ui_spec.js index a3cff7e6d..e73057994 100644 --- a/test/red/ui_spec.js +++ b/test/red/ui_spec.js @@ -13,10 +13,153 @@ * 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"); -describe("red/ui", function() { - it('can be required without errors', function() { - require("../../red/ui"); +describe("red/ui icon handler", function() { + it('returns the default icon when getting an unknown icon', function(done) { + var app = require("../../red/ui")(); + request(app) + .get("/icons/youwonthaveme.png") + .expect('Content-Type', /image\/png/) + .expect(200) + .end(function(err, res){ + if (err){ + return done(err); + } + done(); + }); + }); + + it('returns an icon from disk', function(done) { + var app = require("../../red/ui")(); + request(app) + .get("/icons/arduino.png") + .expect('Content-Type', /image\/png/) + .expect(200) + .end(function(err, res){ + if (err){ + return done(err); + } + done(); + }); + }); +}); + +describe("icon cache handler", function() { + var fs = require("fs"); + var events = require("../../red/events"); + + var tempDir = "/tmp/"; + var cachedFakePNG = tempDir + "cacheMe.png"; + + beforeEach(function(done) { + fs.writeFileSync(cachedFakePNG, "Hello PNG\n"); + done(); + }); + afterEach(function(done) { + fs.exists(cachedFakePNG, function(exists) { + if(exists) { + fs.unlinkSync(cachedFakePNG); + done(); + } else { + done(); + } + }) + }); + + /* + * This test case test that: + * 1) any directory can be added to the path lookup (such as /tmp) by + * calling the right event + * 2) that a file we know exists gets cached so that the lookup/verification + * of actual existence doesn't occur again when a subsequent request comes in + * + * The second point verifies that the cache works. If the cache wouldn't work + * the default PNG would be served + */ + it('returns an icon using icon cache', function(done) { + var app = require("../../red/ui")(); + events.emit("node-icon-dir", "/tmp/"); + request(app) + .get("/icons/cacheMe.png") + .expect('Content-Type', /image\/png/) + .expect(200) + .end(function(err, res){ + if (err){ + return done(err); + } + fs.unlink(cachedFakePNG, function(err) { + if(err) { + return done(err); + } + request(app) + .get("/icons/cacheMe.png") + .expect('Content-Type', /text\/html/) + .expect(404) + .end(function(err, res){ + if (err){ + return done(err); + } + done(); + }); + }); + }); + }); +}); + +describe("red/ui settings handler", function() { + it('returns the provided settings', function(done) { + var settings = { + httpNodeRoot: "testHttpNodeRoot", + version: "testVersion", + }; + var app = require("../../red/ui")(settings); + + request(app) + .get("/settings") + .expect('Content-Type', /application\/json/) + .expect(200, "{\n \"httpNodeRoot\": \"testHttpNodeRoot\",\n \"version\": \"testVersion\"\n}") + .end(function(err, res){ + if (err){ + return done(err); + } + done(); + }); + + }); +}); + +describe("red/ui root handler", function() { + it('server up the main page', function(done) { + var app = require("../../red/ui")(); + + request(app) + .get("/") + .expect('Content-Type', /text\/html/) + .expect(200) + .end(function(err, res){ + if (err){ + return done(err); + } + done(); + }); + + }); + + it('redirects to path ending with /', function(done) { + var app = express().use('/root', require("../../red/ui")()); + + request(app) + .get("/root") + .expect('Content-Type', /text\/plain/) + .expect(302) + .end(function(err, res){ + if (err){ + return done(err); + } + done(); + }); + }); }); From 6c093eef998f8cc38f24f0ab0889ac68553493a1 Mon Sep 17 00:00:00 2001 From: zobalogh Date: Thu, 24 Jul 2014 14:31:48 +0100 Subject: [PATCH 2/2] Fixing settings usage in UI --- red/ui.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/red/ui.js b/red/ui.js index 52b98112f..93bdfaf71 100644 --- a/red/ui.js +++ b/red/ui.js @@ -14,6 +14,8 @@ * limitations under the License. **/ var express = require('express'); +var util = require('util'); +var crypto = require('crypto'); var fs = require("fs"); var app = express(); var events = require("./events"); @@ -21,13 +23,16 @@ var path = require("path"); var icon_paths = [path.resolve(__dirname + '/../public/icons')]; +var settings; // settings has to be global, otherwise variable not in scope for express + events.on("node-icon-dir",function(dir) { icon_paths.push(path.resolve(dir)); }); -// TODO: nothing here uses settings... so does this need to be a function? -function setupUI(settings) { +function setupUI(_settings) { + + settings = _settings; // TODO confirm if settings are needed // Need to ensure the url ends with a '/' so the static serving works // with relative paths @@ -45,7 +50,7 @@ function setupUI(settings) { app.get("/icons/:icon",function(req,res) { if (iconCache[req.params.icon]) { - res.sendfile(iconCache[req.params.icon]); + res.sendfile(iconCache[req.params.icon]); // if not found, express prints this to the console and serves 404 } else { for (var p=0;p