1 | process.env.NODE_TLS_REJECT_UNAUTHORIZED=0
|
2 | const oada = require('../build/index').default
|
3 | const _ = require('lodash');
|
4 | const uuid = require('uuid');
|
5 | const chai = require('chai');
|
6 | var chaiAsPromised = require("chai-as-promised");
|
7 | chai.use(chaiAsPromised);
|
8 | var expect = chai.expect;
|
9 | const axios = require('axios');
|
10 | const { token, domain }= require('./config')
|
11 | const {tree, putResource, getConnections} = require('./utils.js');
|
12 |
|
13 | var connections;
|
14 | var resources = [];
|
15 |
|
16 |
|
17 | describe(`------------GET-----------------`, async function() {
|
18 | before(`Create connection types`, async function() {
|
19 | connections = await getConnections({
|
20 | domain,
|
21 | token: 'def',
|
22 | })
|
23 | connections = Object.values(connections)
|
24 | })
|
25 |
|
26 | for (let i = 0; i < 4; i++) {
|
27 | describe(`Testing connection ${i+1}`, async function() {
|
28 |
|
29 | it(`Should allow for a basic GET request without tree parameter`, async function() {
|
30 | console.log(`Cache: ${connections[i].cache ? true : false}; Websocket: ${connections[i].websocket ? true : false}`)
|
31 | this.timeout(4000);
|
32 | await putResource({
|
33 | _type: 'application/vnd.oada.notes.1+json',
|
34 | sometest: 'abc'
|
35 | },
|
36 | domain+'/bookmarks/test')
|
37 |
|
38 | await putResource({'somethingelse': 'okay'}, domain+'/bookmarks/test/aaa')
|
39 | await putResource({'b': 'b'}, domain+'/bookmarks/test/aaa/bbb')
|
40 | await putResource({'c': 'c'}, domain+'/bookmarks/test/aaa/bbb/index-one/ccc')
|
41 | await putResource({'d': 'd'}, domain+'/bookmarks/test/aaa/bbb/index-one/ccc/index-two/bob')
|
42 | await putResource({'e': 'e'}, domain+'/bookmarks/test/aaa/bbb/index-one/ccc/index-two/bob/index-three/2018')
|
43 |
|
44 | var test = await connections[i].get({
|
45 | path: '/bookmarks/test'
|
46 | })
|
47 | expect(test.data).to.include.key('aaa')
|
48 | expect(test.data).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
49 | })
|
50 |
|
51 | it(`Should allow you to get a resource directly`, async function() {
|
52 | var response = await connections[i].get({
|
53 | path: '/resources/default:resources_bookmarks_321',
|
54 | })
|
55 | expect(response.data).to.include.keys(['_id', '_rev', '_meta'])
|
56 | })
|
57 |
|
58 | it(`Should error when the root path of a 'tree' GET doesn't exist`, async function() {
|
59 | return expect(connections[i].get({
|
60 | path: '/bookmarks/test/testTwo',
|
61 | tree
|
62 | })).to.be.rejectedWith(Error, 'Request failed with status code 404');
|
63 | })
|
64 |
|
65 | it(`Should handle when the cache only contains part of the tree which is on the server`, async function() {
|
66 | if (connections[i].cache) {
|
67 | await connections[i].resetCache()
|
68 | var subTree = _.cloneDeep(tree);
|
69 | delete subTree.bookmarks.test.aaa.bbb['index-one']['*']
|
70 |
|
71 | var first = await connections[i].get({
|
72 | path: '/bookmarks/test',
|
73 | tree: subTree
|
74 | })
|
75 | expect(first.data).to.include.key('aaa')
|
76 | expect(first.data['aaa']).to.include.key('bbb')
|
77 | expect(first.data['aaa']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
78 | expect(first.data['aaa']['bbb']).to.include.key('index-one')
|
79 | expect(first.data['aaa']['bbb']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
80 | expect(first.data['aaa']['bbb']['index-one']).to.not.include.keys(['_id', '_meta', '_type', '_rev'])
|
81 | expect(first.data['aaa']['bbb']['index-one']).to.include.key('ccc')
|
82 | expect(first.data['aaa']['bbb']['index-one']['ccc']).to.have.keys(['_id','_rev'])
|
83 |
|
84 |
|
85 | var second = await connections[i].get({
|
86 | path: '/bookmarks/test',
|
87 | tree
|
88 | })
|
89 | expect(second.data).to.include.key('aaa')
|
90 | expect(second.data['aaa']).to.include.key('bbb')
|
91 | expect(second.data['aaa']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
92 | expect(second.data['aaa']['bbb']).to.include.key('index-one')
|
93 | expect(second.data['aaa']['bbb']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
94 | expect(second.data['aaa']['bbb']['index-one']).to.not.include.keys(['_id', '_meta', '_type', '_rev'])
|
95 | expect(second.data['aaa']['bbb']['index-one']).to.include.key('ccc')
|
96 | expect(second.data['aaa']['bbb']['index-one']['ccc']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
97 | expect(second.data['aaa']['bbb']['index-one']['ccc']['index-two']['bob']['index-three']['2018']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
98 | expect(second.cached).to.equal(false)
|
99 | }
|
100 | })
|
101 |
|
102 | it(`Should handle fully cached tree`, async function() {
|
103 | if (connections[i].cache) {
|
104 | var test = await connections[i].get({
|
105 | path: '/bookmarks/test',
|
106 | tree
|
107 | })
|
108 | expect(test.data).to.include.key('aaa')
|
109 | expect(test.data['aaa']).to.include.key('bbb')
|
110 | expect(test.data['aaa']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
111 | expect(test.data['aaa']['bbb']).to.include.key('index-one')
|
112 | expect(test.data['aaa']['bbb']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
113 | expect(test.data['aaa']['bbb']['index-one']).to.not.include.keys(['_id', '_meta', '_type', '_rev'])
|
114 | expect(test.data['aaa']['bbb']['index-one']).to.include.key('ccc')
|
115 | expect(test.data['aaa']['bbb']['index-one']['ccc']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
116 | expect(test.cached).to.equal(true)
|
117 | }
|
118 | })
|
119 |
|
120 | it(`Should only return the part of the tree prescribed by the given 'tree' when the server has more data`, async function() {
|
121 | this.timeout(4000)
|
122 | await connections[i].put({
|
123 | path: '/bookmarks/test/aaa/bbb/index-one/hhh',
|
124 | data: {foo: "bar"},
|
125 | tree
|
126 | })
|
127 |
|
128 | await connections[i].put({
|
129 | path: '/bookmarks/test/aaa/bbb/index-one/hhh/index-two/bob/index-three/2014',
|
130 | type: 'application/vnd.oada.yield.1+json',
|
131 | data: {bar: "foo"},
|
132 | tree
|
133 | })
|
134 |
|
135 | await axios({
|
136 | method: 'put',
|
137 | url: domain+'/bookmarks/test/aaa/bbb/extraKey',
|
138 | headers: {'Authorization': 'Bearer def', 'Content-Type': 'application/vnd.oada.yield.1+json'},
|
139 | data: {hello: "world"},
|
140 | })
|
141 |
|
142 | await axios({
|
143 | method: 'put',
|
144 | url: domain+'/resources/7656401651',
|
145 | headers: {'Authorization': 'Bearer def', 'Content-Type': 'application/vnd.oada.yield.1+json'},
|
146 | data: {foobar: 'foobar'},
|
147 | })
|
148 |
|
149 | await connections[i].put({
|
150 | path: '/bookmarks/test/aaa/bbb/index-one/hhh/index-two/joe/somethingElse',
|
151 | type: 'application/vnd.oada.yield.1+json',
|
152 | data: {_id: "resources/7656401651"},
|
153 | tree
|
154 | })
|
155 |
|
156 | var first = await connections[i].get({
|
157 | path: '/bookmarks/test',
|
158 | tree
|
159 | })
|
160 |
|
161 | expect(first.data).to.include.key('aaa')
|
162 | expect(first.data['aaa']).to.include.key('bbb')
|
163 | expect(first.data['aaa']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
164 | expect(first.data['aaa']['bbb']).to.include.key('index-one')
|
165 | expect(first.data['aaa']['bbb']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
166 | expect(first.data['aaa']['bbb']['index-one']).to.not.include.keys(['_id', '_meta', '_type', '_rev'])
|
167 | expect(first.data['aaa']['bbb']['index-one']).to.include.key('ccc')
|
168 | expect(first.data['aaa']['bbb']['index-one']['hhh']).to.include.keys(['_id', '_meta', '_type', '_rev'])
|
169 | expect(first.data['aaa']['bbb']['index-one']['hhh']['index-two']).to.include.keys(['bob', 'joe'])
|
170 | expect(first.data['aaa']['bbb']['index-one']['hhh']['index-two']['joe']).to.include.keys(['somethingElse'])
|
171 | expect(first.data['aaa']['bbb']['index-one']['hhh']['index-two']['joe']['somethingElse']).to.not.include.keys(['foobar'])
|
172 | })
|
173 |
|
174 | |
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 |
|
185 | it('clean up', async function() {
|
186 | this.timeout(5000);
|
187 | await connections[i].resetCache();
|
188 | try {
|
189 | await connections[i].delete({path:'/bookmarks/test', tree});
|
190 | } catch (error) {
|
191 | console.log(error)
|
192 | }
|
193 | })
|
194 | })
|
195 | }
|
196 | })
|