|
14 | 14 | * limitations under the License. |
15 | 15 | **/ |
16 | 16 |
|
| 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 | + |
17 | 34 | 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 | + |
19 | 198 | }); |
20 | 199 |
|
21 | 200 |
|
|
0 commit comments