UNPKG

6.82 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 connections = new Array(4);
9let contentType = 'application/vnd.oada.yield.1+json';
10let connectTime = 30 * 1000; // seconds to click through oauth
11
12describe('~~~~~~ TESTING BASIC API - 1) cache+ws, 2) cache only, 3) ws only, 4) neither~~~~~~~', function() {
13
14 it('Connect with metadata. Browser popup must be used to login within 30s.', function() {
15 this.timeout(connectTime);
16 let connection = oada.connect({
17 domain,
18 options: {
19 redirect: 'http://localhost:8000/oauth2/redirect.html',
20 metadata: 'eyJqa3UiOiJodHRwczovL2lkZW50aXR5Lm9hZGEtZGV2LmNvbS9jZXJ0cyIsImtpZCI6ImtqY1NjamMzMmR3SlhYTEpEczNyMTI0c2ExIiwidHlwIjoiSldUIiwiYWxnIjoiUlMyNTYifQ.eyJyZWRpcmVjdF91cmlzIjpbImh0dHA6Ly92aXAzLmVjbi5wdXJkdWUuZWR1OjgwMDAvb2F1dGgyL3JlZGlyZWN0Lmh0bWwiLCJodHRwOi8vbG9jYWxob3N0OjgwMDAvb2F1dGgyL3JlZGlyZWN0Lmh0bWwiXSwidG9rZW5fZW5kcG9pbnRfYXV0aF9tZXRob2QiOiJ1cm46aWV0ZjpwYXJhbXM6b2F1dGg6Y2xpZW50LWFzc2VydGlvbi10eXBlOmp3dC1iZWFyZXIiLCJncmFudF90eXBlcyI6WyJpbXBsaWNpdCJdLCJyZXNwb25zZV90eXBlcyI6WyJ0b2tlbiIsImlkX3Rva2VuIiwiaWRfdG9rZW4gdG9rZW4iXSwiY2xpZW50X25hbWUiOiJPcGVuQVRLIiwiY2xpZW50X3VyaSI6Imh0dHBzOi8vdmlwMy5lY24ucHVyZHVlLmVkdSIsImNvbnRhY3RzIjpbIlNhbSBOb2VsIDxzYW5vZWxAcHVyZHVlLmVkdT4iXSwic29mdHdhcmVfaWQiOiIxZjc4NDc3Zi0zNTQxLTQxM2ItOTdiNi04NjQ0YjRhZjViYjgiLCJyZWdpc3RyYXRpb25fcHJvdmlkZXIiOiJodHRwczovL2lkZW50aXR5Lm9hZGEtZGV2LmNvbSIsImlhdCI6MTUxMjAwNjc2MX0.AJSjNlWX8UKfVh-h1ebCe0MEGqKzArNJ6x0nmta0oFMcWMyR6Cn2saR-oHvU8WrtUMEr-w020mAjvhfYav4EdT3GOGtaFgnbVkIs73iIMtr8Z-Y6mDEzqRzNzVRMLghj7CyWRCNJEk0jwWjOuC8FH4UsfHmtw3ouMFomjwsNLY0',
21 scope: 'oada.yield:all',
22 },
23 noCache: true
24 }).then((result) => {
25 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
26 expect(result.cache).to.equal(undefined);
27 expect(result.socket).to.not.equal(undefined);
28 })
29 })
30
31 this.timeout(connectTime);
32
33 it('Making connection 1', function() {
34 this.timeout(connectTime);
35 return oada.connect({
36 cache: {name: 'testOne'},
37 domain,
38 token: 'def',
39 }).then((result) => {
40 connections[0] = result;
41 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
42 expect(result.cache).to.not.equal(undefined);
43 expect(result.socket).to.not.equal(undefined);
44 })
45 })
46
47 it('Making connection 2', function() {
48 this.timeout(connectTime);
49 return oada.connect({
50 cache: {name: 'testTwo'},
51 domain,
52 token: 'def',
53 noWebsocket: true,
54 }).then((result) => {
55 connections[1] = result;
56 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
57 expect(result.cache).to.not.equal(undefined);
58 expect(result.socket).to.equal(undefined);
59 })
60 })
61
62 it('Making connection 3', function() {
63 this.timeout(connectTime);
64 return oada.connect({
65 domain,
66 token: 'def',
67 cache: false,
68 }).then((result) => {
69 connections[2] = result;
70 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
71 expect(result.cache).to.equal(undefined);
72 expect(result.socket).to.not.equal(undefined);
73 })
74 })
75
76 it('Making connection 4', function() {
77 this.timeout(connectTime);
78 return oada.connect({
79 domain,
80 token: 'def',
81 noWebsocket: true,
82 cache: false,
83 }).then((result) => {
84 connections[3] = result;
85 expect(result).to.have.keys(['token', 'cache', 'socket', 'disconnect', 'get', 'put', 'post', 'delete', 'resetCache'])
86 expect(result.cache).to.equal(undefined);
87 expect(result.socket).to.equal(undefined);
88 })
89 })
90})
91
92for (let i = 0; i < connections.length; i++) {
93 describe(`Testing connection ${i+1}`, () => {
94 it('GET using a path', () => {
95 return connections[i].get({
96 path: '/bookmarks',
97 }).then((response) => {
98 expect(response.status).to.equal(200)
99 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
100 })
101 })
102
103 it('GET using a url', () => {
104 return connections[i].get({
105 url: domain+'/bookmarks',
106 }).then((response) => {
107 expect(response.status).to.equal(200)
108 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
109 expect(response.data).to.include.keys(['_id', '_rev'])
110 })
111 })
112
113 it('PUT using a path', ()=> {
114 return connections[i].put({
115 path: '/bookmarks/test1',
116 type: contentType,
117 data:'123'
118 }).then((response) => {
119 expect(response.status).to.equal(204)
120 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
121 })
122 })
123
124 it('PUT using a url', ()=> {
125 return connections[i].put({
126 url: domain+'/bookmarks/test',
127 type: contentType,
128 data:'{}'
129 }).then((response) => {
130 expect(response.status).to.equal(204)
131 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
132 })
133 })
134
135 it('POST using a path', ()=> {
136 return connections[i].post({
137 path: '/bookmarks/test',
138 type: contentType,
139 data:'123'
140 }).then((response) => {
141 expect(response.status).to.equal(204)
142 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
143 })
144 })
145
146 it('POST using a url', ()=> {
147 return connections[i].post({
148 url: domain+'/bookmarks/test',
149 type: contentType,
150 data:'123'
151 }).then((response) => {
152 expect(response.status).to.equal(204)
153 expect(response.headers).to.include.keys(['content-location', 'x-oada-rev'])
154 })
155 })
156
157 it('DELETE using a path', ()=> {
158 return connections[i].delete({
159 path: '/bookmarks/test1',
160 }).then((response) => {
161 expect(response.status).to.equal(204)
162 return connections[i].get({
163 path: '/bookmarks/test1'
164 }).catch((err) => {
165 expect(err.response.status).to.equal(404)
166 })
167 })
168 })
169
170 it('DELETE using a url', ()=> {
171 return connections[i].delete({
172 url: domain+'/bookmarks/test',
173 }).then((response) => {
174 expect(response.status).to.equal(204)
175 return connections[i].get({
176 path: '/bookmarks/test'
177 }).catch((err) => {
178 expect(err.response.status).to.equal(404)
179 })
180 })
181 })
182
183 it('Clean up.', () => {
184 return connections[i].disconnect();
185 })
186
187 })
188}