UNPKG

12.1 kBJavaScriptView Raw
1process.env.NODE_TLS_REJECT_UNAUTHORIZED=0
2import oada from '../src/index'
3import chai from 'chai';
4var expect = chai.expect;
5
6let token = 'def';
7let domain = 'https://vip3.ecn.purdue.edu';
8let connection;
9let contentType = 'application/vnd.oada.yield.1+json';
10let connectTime = 30 * 1000; // seconds to click through oauth
11let tree = {
12 'bookmarks': {
13 '_type': 'application/vnd.oada.bookmarks.1+json',
14 '_rev': '0-0',
15 'test': {
16 '_type': 'application/vnd.oada.harvest.1+json',
17 '_rev': '0-0',
18 'aaa': {
19 '_type': 'application/vnd.oada.as-harvested.1+json',
20 '_rev': '0-0',
21 'bbb': {
22 '_type': 'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
23 '_rev': '0-0',
24 'index-one': {
25 '*': {
26 '_type': 'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
27 '_rev': '0-0',
28 'index-two': {
29 '*': {
30 '_type': 'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
31 '_rev': '0-0',
32 'index-three': {
33 '*': {
34 '_type': 'application/vnd.oada.as-harvested.yield-moisture-dataset.1+json',
35 'test': {}
36 }
37 }
38 }
39 }
40 }
41 }
42 }
43 }
44 }
45 }
46}
47
48describe('~~~~IMPORT SCRIPT TEST - ENSURE TREE METHOD~~~~~~~', () => {
49
50 it('First, make the connection. Cache + websockets enabled.', function() {
51 return oada.connect({
52 domain,
53 token: 'def',
54 cache: {name: 'testDb'}
55 }).then((result) => {
56 connection = result;
57 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
58 expect(result.cache).to.not.equal(undefined);
59 expect(result.socket).to.not.equal(undefined);
60 return connection.resetCache();
61 })
62 })
63
64 it('PUT using a path', ()=> {
65 return connection.put({
66 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ddd/index-three/eee/test/123',
67 type: 'application/vnd.oada.as-harvested.yield-moisture.dataset.1+json',
68 data: `"some test"`,
69 tree,
70 }).then((response) => {
71 expect(response.status).to.equal(204)
72 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev', 'location'])
73 })
74 })
75
76 it('Now test what weve created: test', () => {
77 return connection.get({
78 path: '/bookmarks/test',
79 }).then((response) => {
80 expect(response.cached).to.equal(true)
81 expect(response.status).to.equal(200)
82 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
83 expect(response.data).to.include.keys(['_id', '_rev', 'aaa'])
84 expect(response.data.aaa).to.have.keys(['_id', '_rev'])
85 expect(response.data.aaa).to.not.include.keys(['bbb'])
86 })
87 })
88
89 it('Now test what weve created: aaa', () => {
90 return connection.get({
91 path: '/bookmarks/test/aaa',
92 }).then((response) => {
93 expect(response.status).to.equal(200)
94 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
95 expect(response.data).to.include.keys(['_id', '_rev', 'bbb'])
96 expect(response.data.bbb).to.have.keys(['_id', '_rev'])
97 expect(response.data.bbb).to.not.include.keys(['index-one'])
98 })
99 })
100
101 it('Now test what weve created: bbb', () => {
102 return connection.get({
103 path: '/bookmarks/test/aaa/bbb',
104 }).then((response) => {
105 expect(response.status).to.equal(200)
106 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
107 expect(response.data).to.include.keys(['_id', '_rev', 'index-one'])
108 expect(response.data['index-one']).to.not.include.keys(['_id', '_rev'])
109 expect(response.data['index-one']).to.include.keys(['ccc'])
110 })
111 })
112
113 it('Now test what weve created: index-one', () => {
114 return connection.get({
115 path: '/bookmarks/test/aaa/bbb/index-one',
116 }).then((response) => {
117 expect(response.status).to.equal(200)
118 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
119 expect(response.data).to.not.include.keys(['_id', '_rev'])
120 expect(response.data).to.include.keys(['ccc'])
121 expect(response.data.ccc).to.have.keys(['_id', '_rev'])
122 })
123 })
124
125 it('Now test what weve created: ccc', () => {
126 return connection.get({
127 path: '/bookmarks/test/aaa/bbb/index-one/ccc',
128 }).then((response) => {
129 expect(response.status).to.equal(200)
130 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
131 expect(response.data).to.include.keys(['_id', '_rev', 'index-two'])
132 expect(response.data['index-two']).to.not.include.keys(['_id', '_rev'])
133 })
134 })
135
136 it('Now test what weve created: index-two', () => {
137 return connection.get({
138 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two',
139 }).then((response) => {
140 expect(response.status).to.equal(200)
141 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
142 expect(response.data).to.not.include.keys(['_id', '_rev'])
143 expect(response.data).to.include.keys(['ddd'])
144 expect(response.data['ddd']).to.have.keys(['_id', '_rev'])
145 })
146 })
147
148 it('Now test what weve created: ddd', () => {
149 return connection.get({
150 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ddd',
151 }).then((response) => {
152 expect(response.status).to.equal(200)
153 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
154 expect(response.data).to.include.keys(['_id', '_rev', 'index-three'])
155 expect(response.data['index-three']).to.not.include.keys(['_id', '_rev'])
156 expect(response.data['index-three']).to.include.keys(['eee'])
157 })
158 })
159
160 it('Now test what weve created: index-three', () => {
161 return connection.get({
162 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ddd/index-three',
163 }).then((response) => {
164 expect(response.status).to.equal(200)
165 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
166 expect(response.data).to.not.include.keys(['_id', '_rev'])
167 expect(response.data).to.include.keys(['eee'])
168 expect(response.data['eee']).to.have.keys(['_id'])
169 expect(response.data['eee']).to.not.have.keys(['_rev'])
170 })
171 })
172
173 it('Now test what weve created: eee', () => {
174 return connection.get({
175 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ddd/index-three/eee',
176 }).then((response) => {
177 expect(response.status).to.equal(200)
178 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
179 expect(response.data).to.include.keys(['_id', '_rev', 'test'])
180 expect(response.data['test']).to.not.include.keys(['_id', '_rev'])
181 expect(response.data['test']).to.include.keys(['123'])
182 })
183 })
184
185 it('PUT to a different path using the same tree.', ()=> {
186 return connection.put({
187 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ggg/index-three/hhh/test/123',
188 type: 'application/vnd.oada.as-harvested.yield-moisture.dataset.1+json',
189 data: `"some test"`,
190 tree,
191 }).then((response) => {
192 expect(response.status).to.equal(204)
193 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev', 'location'])
194 })
195 })
196
197 it('Now test what weve created: test', () => {
198 return connection.get({
199 path: '/bookmarks/test',
200 }).then((response) => {
201 expect(response.status).to.equal(200)
202 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
203 expect(response.data).to.include.keys(['_id', '_rev', 'aaa'])
204 expect(response.data.aaa).to.have.keys(['_id', '_rev'])
205 expect(response.data.aaa).to.not.include.keys(['bbb'])
206 })
207 })
208
209 it('Now test what weve created: aaa', () => {
210 return connection.get({
211 path: '/bookmarks/test/aaa',
212 }).then((response) => {
213 expect(response.status).to.equal(200)
214 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
215 expect(response.data).to.include.keys(['_id', '_rev', 'bbb'])
216 expect(response.data.bbb).to.have.keys(['_id', '_rev'])
217 expect(response.data.bbb).to.not.include.keys(['index-one'])
218 })
219 })
220
221 it('Now test what weve created: bbb', () => {
222 return connection.get({
223 path: '/bookmarks/test/aaa/bbb',
224 }).then((response) => {
225 expect(response.status).to.equal(200)
226 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
227 expect(response.data).to.include.keys(['_id', '_rev', 'index-one'])
228 expect(response.data['index-one']).to.not.include.keys(['_id', '_rev'])
229 expect(response.data['index-one']).to.include.keys(['ccc'])
230 })
231 })
232
233 it('Now test what weve created: index-one', () => {
234 return connection.get({
235 path: '/bookmarks/test/aaa/bbb/index-one',
236 }).then((response) => {
237 expect(response.status).to.equal(200)
238 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
239 expect(response.data).to.not.include.keys(['_id', '_rev'])
240 expect(response.data).to.include.keys(['ccc'])
241 expect(response.data.ccc).to.have.keys(['_id', '_rev'])
242 })
243 })
244
245 it('Now test what weve created: ccc', () => {
246 return connection.get({
247 path: '/bookmarks/test/aaa/bbb/index-one/ccc',
248 }).then((response) => {
249 expect(response.status).to.equal(200)
250 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
251 expect(response.data).to.include.keys(['_id', '_rev', 'index-two'])
252 expect(response.data['index-two']).to.not.include.keys(['_id', '_rev'])
253 })
254 })
255
256 it('Now test what weve created: index-two', () => {
257 return connection.get({
258 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two',
259 }).then((response) => {
260 expect(response.status).to.equal(200)
261 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
262 expect(response.data).to.not.include.keys(['_id', '_rev'])
263 expect(response.data).to.include.keys(['ggg'])
264 expect(response.data['ggg']).to.have.keys(['_id', '_rev'])
265 })
266 })
267
268 it('Now test what weve created: ggg', () => {
269 return connection.get({
270 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ggg',
271 }).then((response) => {
272 expect(response.status).to.equal(200)
273 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
274 expect(response.data).to.include.keys(['_id', '_rev', 'index-three'])
275 expect(response.data['index-three']).to.not.include.keys(['_id', '_rev'])
276 expect(response.data['index-three']).to.include.keys(['hhh'])
277 })
278 })
279
280 it('Now test what weve created: index-three', () => {
281 return connection.get({
282 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ggg/index-three',
283 }).then((response) => {
284 expect(response.status).to.equal(200)
285 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
286 expect(response.data).to.not.include.keys(['_id', '_rev'])
287 expect(response.data).to.include.keys(['hhh'])
288 expect(response.data['hhh']).to.have.keys(['_id'])
289 expect(response.data['hhh']).to.not.have.keys(['_rev'])
290 })
291 })
292
293 it('Now test what weve created: hhh', () => {
294 return connection.get({
295 path: '/bookmarks/test/aaa/bbb/index-one/ccc/index-two/ggg/index-three/hhh',
296 }).then((response) => {
297 expect(response.status).to.equal(200)
298 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
299 expect(response.data).to.include.keys(['_id', '_rev', 'test'])
300 expect(response.data['test']).to.not.include.keys(['_id', '_rev'])
301 expect(response.data['test']).to.include.keys(['123'])
302 })
303 })
304
305 it('Now clean up', () => {
306 return connection.delete({
307 path: '/bookmarks/test',
308 }).then(() => {
309 return connection.disconnect();
310 })
311 })
312})