1 | process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
|
2 | import oada from "../src/index";
|
3 | import chai from "chai";
|
4 | var expect = chai.expect;
|
5 | const status = require("http-status");
|
6 |
|
7 | let token = "def";
|
8 |
|
9 | let domain = "https://localhost";
|
10 | let connections = new Array(4);
|
11 | let contentType = "application/vnd.oada.yield.1+json";
|
12 | let connectTime = 30 * 1000;
|
13 | let nTests = 5;
|
14 |
|
15 | const _ = require("lodash");
|
16 | const config = require("./config.js");
|
17 | const { tree, getConnections } = require("./utils.js");
|
18 |
|
19 | let connection;
|
20 |
|
21 | let connectionParameters = {
|
22 | domain,
|
23 | options: {
|
24 | redirect: "http://localhost:8000/oauth2/redirect.html",
|
25 | metadata:
|
26 | "eyJqa3UiOiJodHRwczovL2lkZW50aXR5Lm9hZGEtZGV2LmNvbS9jZXJ0cyIsImtpZCI6ImtqY1NjamMzMmR3SlhYTEpEczNyMTI0c2ExIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJyZWRpcmVjdF91cmlzIjpbImh0dHA6Ly92aXAzLmVjbi5wdXJkdWUuZWR1OjgwMDAvb2F1dGgyL3JlZGlyZWN0Lmh0bWwiLCJodHRwOi8vbG9jYWxob3N0OjgwMDAvb2F1dGgyL3JlZGlyZWN0Lmh0bWwiXSwidG9rZW5fZW5kcG9pbnRfYXV0aF9tZXRob2QiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6Y2xpZW50LWFzc2VydGlvbi10eXBlOmp3dC1iZWFyZXIiLCJncmFudF90eXBlcyI6WyJpbXBsaWNpdCJdLCJyZXNwb25zZV90eXBlcyI6WyJ0b2tlbiIsImlkX3Rva2VuIiwiaWRfdG9rZW4gdG9rZW4iXSwiY2xpZW50X25hbWUiOiJPcGVuQVRLIiwiY2xpZW50X3VyaSI6Imh0dHBzOi8vdmlwMy5lY24ucHVyZHVlLmVkdSIsImNvbnRhY3RzIjpbIlNhbSBOb2VsIDxzYW5vZWxAcHVyZHVlLmVkdT4iXSwic29mdHdhcmVfaWQiOiIxZjc4NDc3Zi0zNTQxLTQxM2ItOTdiNi04NjQ0YjRhZjViYjgiLCJyZWdpc3RyYXRpb25fcHJvdmlkZXIiOiJodHRwczovL2lkZW50aXR5Lm9hZGEtZGV2LmNvbSIsImlhdCI6MTUxMjAwNjc2MX0.AJSjNlWX8UKfVh-h1ebCe0MEGqKzArNJ6x0nmta0oFMcWMyR6Cn2saR-oHvU8WrtUMEr-w020mAjvhfYav4EdT3GOGtaFgnbVkIs73iIMtr8Z-Y6mDEzqRzNzVRMLghj7CyWRCNJEk0jwWjOuC8FH4UsfHmtw3ouMFomjwsNLY0",
|
27 | scope: "oada.yield:all"
|
28 | }
|
29 | };
|
30 |
|
31 | describe("~~~~~~ Testing Connect() -> Disconnect() -> Connect() ~~~~~~~", function() {
|
32 | it("#1 - Should make a connection with websocket and cache", async function() {
|
33 | this.timeout(connectTime);
|
34 | connections[0] = await oada.connect({
|
35 | domain,
|
36 | token: "def",
|
37 | options: connectionParameters.options
|
38 | });
|
39 | expect(connections[0]).to.have.keys([
|
40 | "token",
|
41 | "disconnect",
|
42 | "reconnect",
|
43 | "get",
|
44 | "put",
|
45 | "post",
|
46 | "delete",
|
47 | "resetCache",
|
48 | "cache",
|
49 | "websocket"
|
50 | ]);
|
51 | expect(connections[0].cache).to.equal(true);
|
52 | expect(connections[0].websocket).to.equal(true);
|
53 | expect(connections[0].get).to.satisfy(member => {
|
54 | return typeof member === "function";
|
55 | });
|
56 | expect(connections[0].put).to.satisfy(member => {
|
57 | return typeof member === "function";
|
58 | });
|
59 | expect(connections[0].post).to.satisfy(member => {
|
60 | return typeof member === "function";
|
61 | });
|
62 | expect(connections[0].delete).to.satisfy(member => {
|
63 | return typeof member === "function";
|
64 | });
|
65 | expect(connections[0].resetCache).to.satisfy(member => {
|
66 | return typeof member === "function";
|
67 | });
|
68 | expect(connections[0].disconnect).to.satisfy(member => {
|
69 | return typeof member === "function";
|
70 | });
|
71 | });
|
72 |
|
73 | describe(`Disconnecting connection 0`, function() {
|
74 | it("Should disconnect connection with websocket and cache", async () => {
|
75 | connections[0].disconnect();
|
76 | });
|
77 | });
|
78 |
|
79 | it("#2 - Should make a connection with websocket and cache", async function() {
|
80 | this.timeout(connectTime);
|
81 | connections[0] = await oada.connect({
|
82 | domain,
|
83 | token: "def",
|
84 | options: connectionParameters.options
|
85 | });
|
86 | expect(connections[0]).to.have.keys([
|
87 | "token",
|
88 | "disconnect",
|
89 | "reconnect",
|
90 | "get",
|
91 | "put",
|
92 | "post",
|
93 | "delete",
|
94 | "resetCache",
|
95 | "cache",
|
96 | "websocket"
|
97 | ]);
|
98 | expect(connections[0].cache).to.equal(true);
|
99 | expect(connections[0].websocket).to.equal(true);
|
100 | expect(connections[0].get).to.satisfy(member => {
|
101 | return typeof member === "function";
|
102 | });
|
103 | expect(connections[0].put).to.satisfy(member => {
|
104 | return typeof member === "function";
|
105 | });
|
106 | expect(connections[0].post).to.satisfy(member => {
|
107 | return typeof member === "function";
|
108 | });
|
109 | expect(connections[0].delete).to.satisfy(member => {
|
110 | return typeof member === "function";
|
111 | });
|
112 | expect(connections[0].resetCache).to.satisfy(member => {
|
113 | return typeof member === "function";
|
114 | });
|
115 | expect(connections[0].disconnect).to.satisfy(member => {
|
116 | return typeof member === "function";
|
117 | });
|
118 | });
|
119 | });
|
120 |
|
121 | describe("~~~~connect() -> disconnect() -> connect() -> puts ~~~~~~~", () => {
|
122 | var connections;
|
123 | var conn;
|
124 | before(
|
125 | "First, make the connection. Cache + websockets enabled.",
|
126 | async function() {
|
127 | connection = await oada.connect({
|
128 | domain,
|
129 | token: "def"
|
130 | });
|
131 | conn = connection;
|
132 | }
|
133 | );
|
134 |
|
135 | before("Disconnecting - First time", function() {
|
136 | conn.disconnect();
|
137 | });
|
138 |
|
139 | before(
|
140 | "Second, make the connection. Cache + websockets enabled.",
|
141 | async function() {
|
142 | connection = await oada.connect({
|
143 | domain,
|
144 | token: "def"
|
145 | });
|
146 | conn = connection;
|
147 | }
|
148 | );
|
149 |
|
150 | it(`Should error when neither 'url' nor 'path' are supplied`, async function() {
|
151 | try {
|
152 | var response = await conn.put({
|
153 | data: `"123"`,
|
154 | tree,
|
155 | type: "application/json"
|
156 | });
|
157 | } catch (error) {
|
158 | expect(error.message).to.equal("Either path or url must be specified.");
|
159 | }
|
160 | });
|
161 |
|
162 | it(`Shouldn't error when 'data' contains a _type key.`, async function() {
|
163 | try {
|
164 | var response = await conn.put({
|
165 | path: "/bookmarks/testA/sometest",
|
166 | data: { _type: "application/json" }
|
167 | });
|
168 | expect(response.status).to.equal(status.NO_CONTENT);
|
169 | } catch (error) {
|
170 | console.log("data _type", error);
|
171 | }
|
172 | });
|
173 |
|
174 | it(`Shouldn't error when 'type' is specified.`, async function() {
|
175 | var response = await conn.put({
|
176 | path: "/bookmarks/testA/somethingnew",
|
177 | data: `"abc123"`,
|
178 | type: "application/json"
|
179 | });
|
180 | expect(response.status).to.equal(status.NO_CONTENT);
|
181 | });
|
182 |
|
183 | it(`Shouldn't error when 'Content-Type' header is specified.`, async function() {
|
184 | var response = await conn.put({
|
185 | path: "/bookmarks/testA/somethingnew",
|
186 | data: `"abc123"`,
|
187 | headers: { "Content-Type": "application/json" }
|
188 | });
|
189 | expect(response.status).to.equal(status.NO_CONTENT);
|
190 | });
|
191 |
|
192 | it("Should provide expected response status and headers when no tree is supplied.", async function() {
|
193 | var response = await conn.put({
|
194 | path:
|
195 | "/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ddd/index-three/eee/test/123",
|
196 | type: "application/vnd.oada.as-harvested.yield-moisture.dataset.1+json",
|
197 | data: `"some test"`
|
198 | });
|
199 | expect(response.status).to.equal(204);
|
200 | expect(response.headers).to.include.keys([
|
201 | "content-location",
|
202 | "x-oada-rev",
|
203 | "location"
|
204 | ]);
|
205 | });
|
206 |
|
207 | it(`Should create the data PUT in the previous test.`, async function() {
|
208 | var response = await conn.get({
|
209 | path: "/bookmarks/test"
|
210 | });
|
211 | expect(response.status).to.equal(200);
|
212 | expect(response.headers).to.include.keys([
|
213 | "content-location",
|
214 | "x-oada-rev"
|
215 | ]);
|
216 | });
|
217 |
|
218 | it("retrieving previous data: aaa", async function() {
|
219 | var response = await conn.get({
|
220 | path: "/bookmarks/test/aaa"
|
221 | });
|
222 | expect(response.status).to.equal(200);
|
223 | expect(response.headers).to.include.keys([
|
224 | "content-location",
|
225 | "x-oada-rev"
|
226 | ]);
|
227 | });
|
228 |
|
229 | it("retrieving previous data: bbb", async function() {
|
230 | var response = await conn.get({
|
231 | path: "/bookmarks/test/aaa/bbb"
|
232 | });
|
233 | expect(response.status).to.equal(200);
|
234 | expect(response.headers).to.include.keys([
|
235 | "content-location",
|
236 | "x-oada-rev"
|
237 | ]);
|
238 | });
|
239 |
|
240 | it("retrieving previous data: index-one", async function() {
|
241 | var response = await conn.get({
|
242 | path: "/bookmarks/test/aaa/bbb/index-one"
|
243 | });
|
244 | expect(response.status).to.equal(200);
|
245 | expect(response.headers).to.include.keys([
|
246 | "content-location",
|
247 | "x-oada-rev"
|
248 | ]);
|
249 | });
|
250 |
|
251 | it("retrieving previous data: ccc", async function() {
|
252 | var response = await conn.get({
|
253 | path: "/bookmarks/test/aaa/bbb/index-one/ccc"
|
254 | });
|
255 | expect(response.status).to.equal(200);
|
256 | expect(response.headers).to.include.keys([
|
257 | "content-location",
|
258 | "x-oada-rev"
|
259 | ]);
|
260 | });
|
261 |
|
262 | it("retrieving previous data: index-two", async function() {
|
263 | var response = await conn.get({
|
264 | path: "/bookmarks/test/aaa/bbb/index-one/ccc/index-two"
|
265 | });
|
266 | expect(response.status).to.equal(200);
|
267 | expect(response.headers).to.include.keys([
|
268 | "content-location",
|
269 | "x-oada-rev"
|
270 | ]);
|
271 | });
|
272 |
|
273 | it("retrieving previous data: ddd", async function() {
|
274 | var response = await conn.get({
|
275 | path: "/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ddd"
|
276 | });
|
277 | expect(response.status).to.equal(200);
|
278 | expect(response.headers).to.include.keys([
|
279 | "content-location",
|
280 | "x-oada-rev"
|
281 | ]);
|
282 | });
|
283 |
|
284 | it("retrieving previous data: test", async function() {
|
285 | var response = await conn.get({
|
286 | path: "/bookmarks/test"
|
287 | });
|
288 | expect(response.status).to.equal(200);
|
289 | expect(response.headers).to.include.keys([
|
290 | "content-location",
|
291 | "x-oada-rev"
|
292 | ]);
|
293 | });
|
294 |
|
295 | it("retrieving previous data: aaa", async function() {
|
296 | var response = await conn.get({
|
297 | path: "/bookmarks/test/aaa"
|
298 | });
|
299 | expect(response.status).to.equal(200);
|
300 | expect(response.headers).to.include.keys([
|
301 | "content-location",
|
302 | "x-oada-rev"
|
303 | ]);
|
304 | });
|
305 |
|
306 | it("retrieving previous data: bbb", async function() {
|
307 | var response = await conn.get({
|
308 | path: "/bookmarks/test/aaa/bbb"
|
309 | });
|
310 | expect(response.status).to.equal(200);
|
311 | expect(response.headers).to.include.keys([
|
312 | "content-location",
|
313 | "x-oada-rev"
|
314 | ]);
|
315 | });
|
316 |
|
317 | it("retrieving previous data: index-one", async function() {
|
318 | var response = await conn.get({
|
319 | path: "/bookmarks/test/aaa/bbb/index-one"
|
320 | });
|
321 | expect(response.status).to.equal(200);
|
322 | expect(response.headers).to.include.keys([
|
323 | "content-location",
|
324 | "x-oada-rev"
|
325 | ]);
|
326 | });
|
327 |
|
328 | it("retrieving previous data: ccc", async function() {
|
329 | var response = await conn.get({
|
330 | path: "/bookmarks/test/aaa/bbb/index-one/ccc"
|
331 | });
|
332 | expect(response.status).to.equal(200);
|
333 | expect(response.headers).to.include.keys([
|
334 | "content-location",
|
335 | "x-oada-rev"
|
336 | ]);
|
337 | });
|
338 |
|
339 |
|
340 | it("Should set watches on previous data: /bookmarks/test", async function() {
|
341 | try {
|
342 | var response = await conn.get({
|
343 | path: "/bookmarks/test",
|
344 | watch: payload => {
|
345 | console.log("handlinh watch valid", payload);
|
346 | }
|
347 | });
|
348 | expect(response.status).to.equal(status.OK);
|
349 | expect(response.headers).to.include.keys([
|
350 | "content-location",
|
351 | "x-oada-rev"
|
352 | ]);
|
353 | } catch (error) {
|
354 | console.log("Catch error", error);
|
355 | }
|
356 | });
|
357 |
|
358 |
|
359 | it("Should fail setting watches on non existent data: yyy", async function() {
|
360 | try {
|
361 | var response = await conn.get({
|
362 | path: "/bookmarks/test/aaa/bbb/index-one/yyy",
|
363 | watch: payload => {
|
364 | console.log("handlinng watch invalid", payload);
|
365 | }
|
366 |
|
367 | });
|
368 | } catch (error) {
|
369 |
|
370 |
|
371 | }
|
372 | });
|
373 |
|
374 | it("Now clean up", async function() {
|
375 | await conn.resetCache();
|
376 | await conn.delete({path: '/bookmarks/test', tree})
|
377 | });
|
378 | });
|
379 |
|
380 | describe("~~~~connect() -> disconnect() -> connect() -> disconnect() -> puts ~~~~~~~", () => {
|
381 | var connections;
|
382 | var conn;
|
383 | before(
|
384 | "First, make the connection. Cache + websockets enabled.",
|
385 | async function() {
|
386 | connection = await oada.connect({
|
387 | domain,
|
388 | token: "def"
|
389 | });
|
390 | conn = connection;
|
391 | }
|
392 | );
|
393 |
|
394 | before("Disconnecting - First time", function() {
|
395 | conn.disconnect();
|
396 | });
|
397 |
|
398 | before(
|
399 | "Second, make the connection. Cache + websockets enabled.",
|
400 | async function() {
|
401 | connection = await oada.connect({
|
402 | domain,
|
403 | token: "def"
|
404 | });
|
405 | conn = connection;
|
406 | }
|
407 | );
|
408 |
|
409 | before("Disconnecting - Second time", function() {
|
410 | conn.disconnect();
|
411 | });
|
412 |
|
413 | it(`Should error when there is no active connection and neither 'url' nor 'path' are supplied`, async function() {
|
414 | try {
|
415 | var response = await conn.put({
|
416 | data: `"123"`,
|
417 | tree,
|
418 | type: "application/json"
|
419 | });
|
420 | } catch (error) {
|
421 | expect(error.message).to.equal("Either path or url must be specified.");
|
422 | }
|
423 | });
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 |
|
434 |
|
435 |
|
436 |
|
437 |
|
438 |
|
439 |
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 |
|
449 |
|
450 |
|
451 |
|
452 |
|
453 |
|
454 |
|
455 |
|
456 |
|
457 |
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 |
|
469 |
|
470 |
|
471 |
|
472 |
|
473 |
|
474 |
|
475 |
|
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 | });
|
489 |
|
490 |
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 |
|
497 |
|
498 |
|
499 |
|
500 |
|
501 |
|
502 |
|
503 |
|
504 |
|
505 |
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 |
|
515 |
|
516 |
|
517 |
|
518 |
|
519 |
|
520 |
|