mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Increase test coverage
This commit is contained in:
@@ -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)
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
});
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user