-
Notifications
You must be signed in to change notification settings - Fork 10
/
test.js
436 lines (428 loc) · 15.6 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
"use strict"
var sinon = require('sinon')
, logger = require('winston')
, assert = require('assert')
, mockery = require('mockery')
, _ = require('lodash')
, request = require('request')
, nock = require('nock')
, http = require('http')
mockery.enable({
warnOnReplace: false
, warnOnUnregistered: false
, useCleanCache: true
});
var HERCULES_BASE_URL = 'https://api.integrator.io';
if (process.env.NODE_ENV !== 'production') {
HERCULES_BASE_URL = 'http://api.localhost.io:5000'
}
var utils = require('./utils.js')
describe('utils.js integratorRestClient function unit test cases', function() {
it('should throw no auth token error', function(done) {
utils.integratorRestClient({
bearerToken: "",
resourcetype: 'integrations'
}, function(err, response, body) {
if (err) {
assert.equal(err.message, 'No Auth Token is given!')
done()
}
})
})
it('should throw no resourceType error', function(done) {
utils.integratorRestClient({
bearerToken: 'TestToken',
resourcetype: ''
}, function(err, response, body) {
if(err) {
assert.equal(err.message, 'No resourcetype is given!')
done()
}
}
)})
it('should use GET if no id and data is provided', function(done) {
var api = nock(HERCULES_BASE_URL)
.get("/v1/integrations")
.reply(200, "GET http method has been used.");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations'
}, function(err, response, body) {
assert.equal(body, "GET http method has been used.")
done()
})
})
it('should use POST if no id and but data is provided', function(done) {
var api = nock(HERCULES_BASE_URL)
.post("/v1/integrations")
.reply(200, "POST http method has been used.");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
data: "sample Data"
}, function(err, response, body) {
assert.equal(body, "POST http method has been used.")
done()
})
})
it('should use PUT if id(externally) but no data is provided', function(done) {
var api = nock(HERCULES_BASE_URL)
.put("/v1/integrations/4389578hi8443jhj4")
.reply(200, "PUT http method has been used.");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
id: "4389578hi8443jhj4",
data: {}
}, function(err, response, body) {
assert.equal(body, "PUT http method has been used.")
done()
})
})
it('should use PUT if id(internally) but no data is provided', function(done) {
var api = nock(HERCULES_BASE_URL)
.put("/v1/integrations/4389578hi8443jhj4")
.reply(200, "PUT http method has been used.");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
data: {"data" : "dummy data",
"_id" : "4389578hi8443jhj4"
}
}, function(err, response, body) {
assert.equal(body, "PUT http method has been used.")
done()
})
})
it('distributed should be added to url , with http method GET ', function(done) {
var api = nock(HERCULES_BASE_URL)
.get("/v1/integrations/4389578hi8443jhj4/distributed")
.reply(200, "distributed has been added to url");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
id: "4389578hi8443jhj4",
distributed: true
}, function(err, response, body) {
assert.equal(body, "distributed has been added to url")
done()
})
})
it('distributed should be added to url , with http method PUT, with id provided externally', function(done) {
var api = nock(HERCULES_BASE_URL)
.put("/v1/integrations/4389578hi8443jhj41/distributed")
.reply(200, "distributed has been added to url");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
id: "4389578hi8443jhj41",
distributed: true,
data: "sample data"
}, function(err, response, body) {
assert.equal(body, "distributed has been added to url")
done()
})
})
it('distributed should be added to url , with http method PUT, with id provided internally', function(done) {
var api = nock(HERCULES_BASE_URL)
.put("/v1/integrations/4389578hi8443jhj42/distributed")
.reply(200, "distributed has been added to url");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
distributed: true,
data: {"data" : "sample data",
"_id" : "4389578hi8443jhj42"}
}, function(err, response, body) {
assert.equal(body, "distributed has been added to url")
done()
})
})
it('distributed should be added to url , with http method POST, with no id', function(done) {
var api = nock(HERCULES_BASE_URL)
.post("/v1/integrations/distributed")
.reply(200, "distributed has been added to url");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations',
distributed: true,
data: {"data" : "sample data"}
}, function(err, response, body) {
assert.equal(body, "distributed has been added to url")
done()
})
})
it('should return incorrect response error!', function(done) {
var api = nock(HERCULES_BASE_URL)
.get("/v1/integrations")
.reply(401, "Hello World");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations'
}, function(err, response, body) {
if (err) {
assert.equal(err.message, 'Unable to verify response')
done()
}
})
})
it('should not return incorrect response error!', function(done) {
var api = nock(HERCULES_BASE_URL)
.get("/v1/integrations")
.reply(201, "Hello World");
utils.integratorRestClient({
bearerToken: "dkskwqyei8767876",
resourcetype: 'integrations'
}, function(err, response, body) {
if (err) {
if(err.message !== 'Unable to verify response')
done()
}
else {
done()
}
})
})
})
describe('integratorApiIdentifierClient function unit test cases!', function() {
beforeEach(function() {
})
afterEach(function() {
})
it('should throw no auth token error', function(done) {
utils.integratorApiIdentifierClient({
bearerToken: "",
apiIdentifier: 'brv75984365865'
}, function(err, response, body) {
if (err) {
assert.equal(err.message, 'No Auth Token was provided!')
done()
}
})
})
it('should throw no apiIdentifier error', function(done) {
utils.integratorApiIdentifierClient({
bearerToken: "kdjfk85748kjkjfjk84n",
apiIdentifier: ""
}, function(err, response, body) {
if (err) {
assert.equal(err.message, 'No apiIdentifier was provided!')
done()
}
})
})
it('should return incorrect response error.', function(done) {
var api = nock(HERCULES_BASE_URL)
.post("/brv75984365865")
.reply(401, "POST http method has been used.");
utils.integratorApiIdentifierClient({
bearerToken: "dhkjsh876jhr3894kj8",
apiIdentifier: 'brv75984365865',
data: {"field1": "value1"}
}, function(err, response, body) {
assert.equal(err.message, 'Unable to verify response', 'Incorrect response from server was the expected result, but that does seem like case here')
done()
})
})
})
describe('createRecordsInOrder function unit test cases!', function() {
it('loadJSON has been called once', function(done) {
var metaData = require('./testData/allrecordsmeta.json')
try {
utils.createRecordsInOrder(metaData, { bearerToken : 'jshdkjsdh7567djd68b'}, function(error, success) {
}) }catch(e) {
assert.equal(e.code, "MODULE_NOT_FOUND")
done()
}
})
it('should return true since all dependencies are marked resolved', function(done) {
var metaData = require('./testData/allrecordsmeta.json')
_.each(metaData, function(record) {
record.resolved = true,
record.isLoaded = true
})
utils.createRecordsInOrder(metaData, { bearerToken : 'jshdkjsdh7567djd68b'}, function(error, success) {
assert.equal(error, null)
done()
})
})
it('should throw cyclic dependency error!', function(done) {
var circularJson = require('./testData/allRecordsMetaDataCircular.json')
utils.createRecordsInOrder(circularJson, null, function(error, succss) {
if(error) {
assert.equal(error.message, 'The recordsArray has cyclic refreneces')
done()
}
})
})
it('should throw no location error !', function(done) {
var noLocationJson = require('./testData/allRecordsMetaDataNoLocation.json')
utils.createRecordsInOrder(noLocationJson, null, function(error, success) {
if(error) {
var index = (error.message).search('Config Error: no filelocation given in record : ');
assert.notEqual(index, -1)
done()
}
})
})
})
describe('loadJSON function test cases!', function() {
it('should throw MODULE_NOT_FOUND error', function(done) {
try {
utils.loadJSON('./testData/allrecordsmeta.json')
} catch (e) {
assert.equal(e.code, "MODULE_NOT_FOUND")
done()
}
})
//TODO: need to do proper mocking for require statement
it.skip('should not throw MODULE_NOT_FOUND error if already loaded', function(done) {
console.log(require.cache[require.resolve('./testData/allrecordsmeta.json')])
if(utils.loadJSON('./testData/allrecordsmeta.json') !== undefined)
done()
})
})
describe('makeAsyncCalls function unit test cases.', function(){
it('if make async calls has been called', function(done){
var api = nock(HERCULES_BASE_URL)
.post("/jhdjhs7dh67db6")
.reply(402, "POST http method has been used.");
var metaData = require('./testData/allrecordsmeta.json')
_.each(metaData, function(record) {
record.isLoaded = true,
record.resolved = false,
record.info = {"apiIdentifier" : true},
record.info.data = { "apiIdentifier" : "jhdjhs7dh67db6",
"bearerToken" : "jshdkjsdh7567djd68b"},
record.info.bearerToken = "jshdkjsdh7567djd68b"
})
utils.createRecordsInOrder(metaData, { bearerToken : 'jshdkjsdh7567djd68b'}, function(error, success) {
if(error) {
console.log(error)
assert.equal(error.message, 'Unable to verify response')
done()
} else {
console.log('no error!')
}
})
})
it('second block in makeAsyncCalls(integratorRestClient call)', function(done){
var api = nock(HERCULES_BASE_URL)
.get("/v1/connections")
.reply(402, "Hello World");
var metaData = require('./testData/allrecordsmeta.json')
_.each(metaData, function(record) {
record.isLoaded = true,
record.resolved = false,
record.info = {"resourcetype" : "connections",
"bearerToken": "dkskwqyei8767876"}
})
utils.createRecordsInOrder(metaData, { bearerToken : 'dkskwqyei8767876'}, function(error, success) {
if(error) {
console.log(error)
assert.equal(error.message, 'Unable to verify response')
done()
} else {
console.log('no error!')
}
})
})
it('should get ** The method is set GET but there is no _id in data** error', function(done){
var metaData = require('./testData/allrecordsmeta.json')
_.each(metaData, function(record) {
record.isLoaded = true,
record.resolved = false,
record.info = { "apiIdentifier1" : false,
"method" : "GET"},
record.info.data = { "bearerToken" : "jshdkjsdh7567djd68b"},
record.info.bearerToken = "jshdkjsdh7567djd68b"
})
utils.createRecordsInOrder(metaData, { bearerToken : 'jshdkjsdh7567djd68b'}, function(error, success) {
if(error) {
assert.equal(error.message, 'The method is set GET but there is no _id in data')
done()
} else {
console.log('no error!')
}
})
})
it('should call makeAsyncCalls at the end.', function(done) {
var metaData = require('./testData/allRecordsMetaData.json')
var api = nock(HERCULES_BASE_URL)
.post("/v1/connections")
.reply(201, "First Time POST");
var api = nock(HERCULES_BASE_URL)
.post("/v1/exports")
.reply(201, "Second Time POST");
_.each(metaData, function(record) {
if(record.name === "connection-shopify")
{
record.isLoaded = true,
record.resolved = false,
record.info = {"resourcetype" : "connections",
"bearerToken": "dhskdh7djsd57dn",
"data": {"f1" : "v1"}}
} else {
record.isLoaded = true,
record.resolved = false,
record.info = {"resourcetype" : "exports",
"bearerToken": "dkskwqyei8767876",
"data": {"f2": "v2"}}
}
})
utils.createRecordsInOrder(metaData, {}, function(error, success) {
assert.equal((metaData["connection-shopify"].resolved), true, 'value of resolved for connection-shopify record was supposed to be true , but is is not.')
assert.equal((metaData["export-order"].resolved), true, 'value of resolved for export-order record was supposed to be true , but is is not.')
done()
})
})
it('should use GET http method(as it provided inside info block) and delete data inside info', function(done) {
var metaData = require('./testData/allRecordsMetaDataSingleRecord.json')
var api = nock(HERCULES_BASE_URL)
.get("/v1/connections/vjsdsd8sjdhj9sdj8")
.reply(401, "GET http method has been used.")
_.each(metaData, function(record) {
if(record.name === "connection-shopify")
{
record.isLoaded = true,
record.resolved = false,
record.info = {"method": "GET",
"resourcetype" : "connections",
"bearerToken": "dhskdh7djsd57dn",
"data": {"_id" : "vjsdsd8sjdhj9sdj8"}}
}
})
utils.createRecordsInOrder(metaData, {}, function(error, success) {
assert.equal(error.message, 'Unable to verify response', 'is supposed to use GET http method using id provided inside info block, but seems like that is not the case here.')
done()
})
})
})
describe('utils.js integratorProxyCall function unit test cases', function() {
it('should throw no auth token error', function(done) {
utils.integratorProxyCall({
'bearerToken': ""
, 'connectionId': 1234
, 'method': 'GET'
, 'relativeURI': '/admin/orders/' + 234 + '/transactions'
}, function(err, response, body) {
if (err) {
assert.equal(err.message, 'No Auth Token is given!')
done()
}
})
})
it('should throw Proxy request headers are not in correct format error', function(done) {
utils.integratorProxyCall({
'bearerToken': "testToken"
, 'connectionId': "1234"
, 'method': 'GET'
, 'relativeURI': ''
}, function(err, response, body) {
if (err) {
assert.equal(err.message, 'Proxy request headers are not in correct format')
done()
}
})
})
})