Skip to content

Commit 9a972b0

Browse files
committed
Increase test coverage
1 parent e6aeeea commit 9a972b0

4 files changed

Lines changed: 189 additions & 7 deletions

File tree

red/runtime-api/library.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var api = module.exports = {
5353
runtime.log.audit({event: "library.get",type:opts.type,path:opts.path,error:err.code});
5454
return reject(err);
5555
}
56-
runtime.log.audit({event: "library.get",type:type,error:"not_found"});
56+
runtime.log.audit({event: "library.get",type:opts.type,error:"not_found"});
5757
var error = new Error();
5858
error.code = "not_found";
5959
error.status = 404;
@@ -80,14 +80,13 @@ var api = module.exports = {
8080
return resolve();
8181
}).catch(function(err) {
8282
runtime.log.warn(runtime.log._("api.library.error-save-entry",{path:opts.path,message:err.toString()}));
83-
if (err.code === 'forbidden') {
83+
if (err.code === 'forbidden') {
8484
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"forbidden"});
8585
err.status = 403;
8686
return reject(err);
8787
}
8888
runtime.log.audit({event: "library.set",type:opts.type,path:opts.path,error:"unexpected_error",message:err.toString()});
8989
var error = new Error();
90-
error.code = "not_found";
9190
error.status = 400;
9291
return reject(error);
9392
});

test/red/api/auth/strategies_spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ describe("api/auth/strategies", function() {
105105
user.should.equal("anon");
106106
strategies.anonymousStrategy.success = strategies.anonymousStrategy._success;
107107
delete strategies.anonymousStrategy._success;
108-
userDefault.restore();
109108
done();
110109
};
111110
strategies.anonymousStrategy.authenticate({});
@@ -119,11 +118,13 @@ describe("api/auth/strategies", function() {
119118
err.should.equal(401);
120119
strategies.anonymousStrategy.fail = strategies.anonymousStrategy._fail;
121120
delete strategies.anonymousStrategy._fail;
122-
userDefault.restore();
123121
done();
124122
};
125123
strategies.anonymousStrategy.authenticate({});
126124
});
125+
afterEach(function() {
126+
Users.default.restore();
127+
})
127128
});
128129

129130
describe("Bearer Strategy", function() {

test/red/api/auth/users_spec.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ var sinon = require('sinon');
2121
var Users = require("../../../../red/api/auth/users");
2222

2323
describe("api/auth/users", function() {
24+
after(function() {
25+
Users.init({});
26+
})
2427
describe('Initalised with a credentials object, no anon',function() {
2528
before(function() {
2629
Users.init({
@@ -214,7 +217,7 @@ describe("api/auth/users", function() {
214217
});
215218
describe('#default',function() {
216219
it('handles api.default being a function',function(done) {
217-
Users.should.have.property('default').which.is.a.Function;
220+
Users.should.have.property('default').which.is.a.Function();
218221
(Users.default()).should.equal("Done");
219222
done();
220223
});

test/red/runtime-api/library_spec.js

Lines changed: 180 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,187 @@
1414
* limitations under the License.
1515
**/
1616

17+
18+
var should = require("should");
19+
var sinon = require("sinon");
20+
21+
var library = require("../../../red/runtime-api/library")
22+
23+
var mockLog = {
24+
log: sinon.stub(),
25+
debug: sinon.stub(),
26+
trace: sinon.stub(),
27+
warn: sinon.stub(),
28+
info: sinon.stub(),
29+
metric: sinon.stub(),
30+
audit: sinon.stub(),
31+
_: function() { return "abc"}
32+
}
33+
1734
describe("runtime-api/library", function() {
18-
it.skip('more tests needed', function(){})
35+
describe("getEntry", function() {
36+
before(function() {
37+
library.init({
38+
log: mockLog,
39+
library: {
40+
getEntry: function(type,path) {
41+
if (type === "known") {
42+
return Promise.resolve("known");
43+
} else if (type === "forbidden") {
44+
var err = new Error("forbidden");
45+
err.code = "forbidden";
46+
var p = Promise.reject(err);
47+
p.catch(()=>{});
48+
return p;
49+
} else if (type === "not_found") {
50+
var err = new Error("forbidden");
51+
err.code = "not_found";
52+
var p = Promise.reject(err);
53+
p.catch(()=>{});
54+
return p;
55+
} else if (type === "error") {
56+
var err = new Error("error");
57+
err.code = "unknown_error";
58+
var p = Promise.reject(err);
59+
p.catch(()=>{});
60+
return p;
61+
} else if (type === "blank") {
62+
return Promise.reject();
63+
}
64+
}
65+
}
66+
})
67+
})
68+
it("returns a known entry", function(done) {
69+
library.getEntry({type: "known", path: "/abc"}).then(function(result) {
70+
result.should.eql("known")
71+
done();
72+
}).catch(done)
73+
})
74+
it("rejects a forbidden entry", function(done) {
75+
library.getEntry({type: "forbidden", path: "/abc"}).then(function(result) {
76+
done(new Error("did not reject"));
77+
}).catch(function(err) {
78+
err.should.have.property("code","forbidden");
79+
err.should.have.property("status",403);
80+
done();
81+
}).catch(done)
82+
})
83+
it("rejects an unknown entry", function(done) {
84+
library.getEntry({type: "not_found", path: "/abc"}).then(function(result) {
85+
done(new Error("did not reject"));
86+
}).catch(function(err) {
87+
err.should.have.property("code","not_found");
88+
err.should.have.property("status",404);
89+
done();
90+
}).catch(done)
91+
})
92+
it("rejects a blank (unknown) entry", function(done) {
93+
library.getEntry({type: "blank", path: "/abc"}).then(function(result) {
94+
done(new Error("did not reject"));
95+
}).catch(function(err) {
96+
err.should.have.property("code","not_found");
97+
err.should.have.property("status",404);
98+
done();
99+
}).catch(done)
100+
})
101+
it("rejects unexpected error", function(done) {
102+
library.getEntry({type: "error", path: "/abc"}).then(function(result) {
103+
done(new Error("did not reject"));
104+
}).catch(function(err) {
105+
err.should.have.property("status",400);
106+
done();
107+
}).catch(done)
108+
})
109+
})
110+
describe("saveEntry", function() {
111+
var opts;
112+
before(function() {
113+
library.init({
114+
log: mockLog,
115+
library: {
116+
saveEntry: function(type,path,meta,body) {
117+
opts = {type,path,meta,body};
118+
if (type === "known") {
119+
return Promise.resolve();
120+
} else if (type === "forbidden") {
121+
var err = new Error("forbidden");
122+
err.code = "forbidden";
123+
var p = Promise.reject(err);
124+
p.catch(()=>{});
125+
return p;
126+
} else if (type === "not_found") {
127+
var err = new Error("forbidden");
128+
err.code = "not_found";
129+
var p = Promise.reject(err);
130+
p.catch(()=>{});
131+
return p;
132+
}
133+
}
134+
}
135+
})
136+
})
137+
138+
it("saves an entry", function(done) {
139+
library.saveEntry({type: "known", path: "/abc", meta: {a:1}, body:"123"}).then(function() {
140+
opts.should.have.property("type","known");
141+
opts.should.have.property("path","/abc");
142+
opts.should.have.property("meta",{a:1});
143+
opts.should.have.property("body","123");
144+
done();
145+
}).catch(done)
146+
})
147+
it("rejects a forbidden entry", function(done) {
148+
library.saveEntry({type: "forbidden", path: "/abc", meta: {a:1}, body:"123"}).then(function() {
149+
done(new Error("did not reject"));
150+
}).catch(function(err) {
151+
err.should.have.property("code","forbidden");
152+
err.should.have.property("status",403);
153+
done();
154+
}).catch(done)
155+
})
156+
it("rejects an unknown entry", function(done) {
157+
library.saveEntry({type: "not_found", path: "/abc", meta: {a:1}, body:"123"}).then(function() {
158+
done(new Error("did not reject"));
159+
}).catch(function(err) {
160+
err.should.have.property("status",400);
161+
done();
162+
}).catch(done)
163+
})
164+
})
165+
describe("getEntries", function() {
166+
var opts;
167+
before(function() {
168+
library.init({
169+
log: mockLog,
170+
storage: {
171+
getAllFlows: function() {
172+
return Promise.resolve({a:1});
173+
}
174+
},
175+
nodes: {
176+
getNodeExampleFlows: function() {
177+
return {b:2};
178+
}
179+
}
180+
});
181+
});
182+
it("returns all flows", function(done) {
183+
library.getEntries({type:"flows"}).then(function(result) {
184+
result.should.eql({a:1,d:{_examples_:{b:2}}});
185+
done();
186+
}).catch(done)
187+
});
188+
it("fails for non-flows (currently)", function(done) {
189+
library.getEntries({type:"functions"}).then(function(result) {
190+
done(new Error("did not reject"));
191+
}).catch(function(err) {
192+
done();
193+
}).catch(done)
194+
})
195+
})
196+
197+
19198
});
20199

21200

0 commit comments

Comments
 (0)