From 9a972b0b8a47277608176d11c1ba86b316940fcd Mon Sep 17 00:00:00 2001 From: Nick O'Leary Date: Tue, 24 Apr 2018 22:13:35 +0100 Subject: [PATCH] Increase test coverage --- red/runtime-api/library.js | 5 +- test/red/api/auth/strategies_spec.js | 5 +- test/red/api/auth/users_spec.js | 5 +- test/red/runtime-api/library_spec.js | 181 ++++++++++++++++++++++++++- 4 files changed, 189 insertions(+), 7 deletions(-) diff --git a/red/runtime-api/library.js b/red/runtime-api/library.js index fb0658554..f6d521b10 100644 --- a/red/runtime-api/library.js +++ b/red/runtime-api/library.js @@ -53,7 +53,7 @@ var api = module.exports = { runtime.log.audit({event: "library.get",type:opts.type,path:opts.path,error:err.code}); return reject(err); } - runtime.log.audit({event: "library.get",type:type,error:"not_found"}); + runtime.log.audit({event: "library.get",type:opts.type,error:"not_found"}); var error = new Error(); error.code = "not_found"; error.status = 404; @@ -80,14 +80,13 @@ var api = module.exports = { return resolve(); }).catch(function(err) { runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()})); - if (err.code === 'forbidden') { + if (err.code === 'forbidden') { runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"}); err.status = 403; return reject(err); } runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()}); var error = new Error(); - error.code = "not_found"; error.status = 400; return reject(error); }); diff --git a/test/red/api/auth/strategies_spec.js b/test/red/api/auth/strategies_spec.js index 5002e8b4b..5f3e88471 100644 --- a/test/red/api/auth/strategies_spec.js +++ b/test/red/api/auth/strategies_spec.js @@ -105,7 +105,6 @@ describe("api/auth/strategies", function() { user.should.equal("anon"); strategies.anonymousStrategy.success = strategies.anonymousStrategy._success; delete strategies.anonymousStrategy._success; - userDefault.restore(); done(); }; strategies.anonymousStrategy.authenticate({}); @@ -119,11 +118,13 @@ describe("api/auth/strategies", function() { err.should.equal(401); strategies.anonymousStrategy.fail = strategies.anonymousStrategy._fail; delete strategies.anonymousStrategy._fail; - userDefault.restore(); done(); }; strategies.anonymousStrategy.authenticate({}); }); + afterEach(function() { + Users.default.restore(); + }) }); describe("Bearer Strategy", function() { diff --git a/test/red/api/auth/users_spec.js b/test/red/api/auth/users_spec.js index 23c34001d..9502a4f93 100644 --- a/test/red/api/auth/users_spec.js +++ b/test/red/api/auth/users_spec.js @@ -21,6 +21,9 @@ var sinon = require('sinon'); var Users = require("../../../../red/api/auth/users"); describe("api/auth/users", function() { + after(function() { + Users.init({}); + }) describe('Initalised with a credentials object, no anon',function() { before(function() { Users.init({ @@ -214,7 +217,7 @@ describe("api/auth/users", function() { }); describe('#default',function() { it('handles api.default being a function',function(done) { - Users.should.have.property('default').which.is.a.Function; + Users.should.have.property('default').which.is.a.Function(); (Users.default()).should.equal("Done"); done(); }); diff --git a/test/red/runtime-api/library_spec.js b/test/red/runtime-api/library_spec.js index 7c41a32de..502750ea7 100644 --- a/test/red/runtime-api/library_spec.js +++ b/test/red/runtime-api/library_spec.js @@ -14,8 +14,187 @@ * limitations under the License. **/ + +var should = require("should"); +var sinon = require("sinon"); + +var library = require("../../../red/runtime-api/library") + +var mockLog = { + log: sinon.stub(), + debug: sinon.stub(), + trace: sinon.stub(), + warn: sinon.stub(), + info: sinon.stub(), + metric: sinon.stub(), + audit: sinon.stub(), + _: function() { return "abc"} +} + describe("runtime-api/library", function() { - it.skip('more tests needed', function(){}) + describe("getEntry", function() { + before(function() { + library.init({ + log: mockLog, + library: { + getEntry: function(type,path) { + if (type === "known") { + return Promise.resolve("known"); + } else if (type === "forbidden") { + var err = new Error("forbidden"); + err.code = "forbidden"; + var p = Promise.reject(err); + p.catch(()=>{}); + return p; + } else if (type === "not_found") { + var err = new Error("forbidden"); + err.code = "not_found"; + var p = Promise.reject(err); + p.catch(()=>{}); + return p; + } else if (type === "error") { + var err = new Error("error"); + err.code = "unknown_error"; + var p = Promise.reject(err); + p.catch(()=>{}); + return p; + } else if (type === "blank") { + return Promise.reject(); + } + } + } + }) + }) + it("returns a known entry", function(done) { + library.getEntry({type: "known", path: "/abc"}).then(function(result) { + result.should.eql("known") + done(); + }).catch(done) + }) + it("rejects a forbidden entry", function(done) { + library.getEntry({type: "forbidden", path: "/abc"}).then(function(result) { + done(new Error("did not reject")); + }).catch(function(err) { + err.should.have.property("code","forbidden"); + err.should.have.property("status",403); + done(); + }).catch(done) + }) + it("rejects an unknown entry", function(done) { + library.getEntry({type: "not_found", path: "/abc"}).then(function(result) { + done(new Error("did not reject")); + }).catch(function(err) { + err.should.have.property("code","not_found"); + err.should.have.property("status",404); + done(); + }).catch(done) + }) + it("rejects a blank (unknown) entry", function(done) { + library.getEntry({type: "blank", path: "/abc"}).then(function(result) { + done(new Error("did not reject")); + }).catch(function(err) { + err.should.have.property("code","not_found"); + err.should.have.property("status",404); + done(); + }).catch(done) + }) + it("rejects unexpected error", function(done) { + library.getEntry({type: "error", path: "/abc"}).then(function(result) { + done(new Error("did not reject")); + }).catch(function(err) { + err.should.have.property("status",400); + done(); + }).catch(done) + }) + }) + describe("saveEntry", function() { + var opts; + before(function() { + library.init({ + log: mockLog, + library: { + saveEntry: function(type,path,meta,body) { + opts = {type,path,meta,body}; + if (type === "known") { + return Promise.resolve(); + } else if (type === "forbidden") { + var err = new Error("forbidden"); + err.code = "forbidden"; + var p = Promise.reject(err); + p.catch(()=>{}); + return p; + } else if (type === "not_found") { + var err = new Error("forbidden"); + err.code = "not_found"; + var p = Promise.reject(err); + p.catch(()=>{}); + return p; + } + } + } + }) + }) + + it("saves an entry", function(done) { + library.saveEntry({type: "known", path: "/abc", meta: {a:1}, body:"123"}).then(function() { + opts.should.have.property("type","known"); + opts.should.have.property("path","/abc"); + opts.should.have.property("meta",{a:1}); + opts.should.have.property("body","123"); + done(); + }).catch(done) + }) + it("rejects a forbidden entry", function(done) { + library.saveEntry({type: "forbidden", path: "/abc", meta: {a:1}, body:"123"}).then(function() { + done(new Error("did not reject")); + }).catch(function(err) { + err.should.have.property("code","forbidden"); + err.should.have.property("status",403); + done(); + }).catch(done) + }) + it("rejects an unknown entry", function(done) { + library.saveEntry({type: "not_found", path: "/abc", meta: {a:1}, body:"123"}).then(function() { + done(new Error("did not reject")); + }).catch(function(err) { + err.should.have.property("status",400); + done(); + }).catch(done) + }) + }) + describe("getEntries", function() { + var opts; + before(function() { + library.init({ + log: mockLog, + storage: { + getAllFlows: function() { + return Promise.resolve({a:1}); + } + }, + nodes: { + getNodeExampleFlows: function() { + return {b:2}; + } + } + }); + }); + it("returns all flows", function(done) { + library.getEntries({type:"flows"}).then(function(result) { + result.should.eql({a:1,d:{_examples_:{b:2}}}); + done(); + }).catch(done) + }); + it("fails for non-flows (currently)", function(done) { + library.getEntries({type:"functions"}).then(function(result) { + done(new Error("did not reject")); + }).catch(function(err) { + done(); + }).catch(done) + }) + }) + + });