-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mondo.spec.js
495 lines (348 loc) · 13 KB
/
Mondo.spec.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
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
import Mondo, {ENDPOINTS, API, oauth2} from './Mondo';
import chai, {expect} from 'chai';
import sinon from 'sinon';
import _ from 'lodash';
import nock from 'nock';
import moment from 'moment'
import Promise from 'promise';
import config from './config';
import './nocks.js';
let mondo, clock;
before(() => {
// Clear terminal between each test.
process.stdout.write('\x1B[2J\x1B[0f');
console.log(new Date() + '\n');
// Don't make any real remote requests.
nock.disableNetConnect();
});
after(() => {
// Clean up and enable remote requests again.
nock.cleanAll().enableNetConnect();
})
describe('Mondo', () => {
describe('constructor', () => {
it('should throw an error if you don\'t pass in a client id and secret', () => {
expect(() => {
return new Mondo();
}).to.throw(Error);
expect(() => {
return new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
}).to.not.throw(Error);
});
it('should use a default api if none are provided', () => {
let stub = sinon.stub(Mondo.prototype, '__oauthSet', function () {
this.oauth2 = (config) => {
expect(config.site).to.equal(API);
}
});
new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
Mondo.prototype.__oauthSet.restore();
});
it('should allow users to override the api if one is provided', () => {
const CUSTOM_API = 'https://example.com';
let stub = sinon.stub(Mondo.prototype, '__oauthSet', function () {
this.oauth2 = (config) => {
expect(config.site).to.equal(CUSTOM_API);
}
});
new Mondo(config.CLIENT_ID, config.CLIENT_SECRET, CUSTOM_API);
Mondo.prototype.__oauthSet.restore();
});
it('should set an internal oauth instance containing client ids, secrets and endpoints', () => {
let stub = sinon.stub(Mondo.prototype, '__oauthSet', function () {
this.oauth2 = (oauthConfig) => {
expect(oauthConfig).to.deep.equal({
clientID: config.CLIENT_ID,
clientSecret: config.CLIENT_SECRET,
site: API,
tokenPath: API+ENDPOINTS.AUTHENTICATION.OAUTH2,
authorizationPath: API+ENDPOINTS.AUTHENTICATION.OAUTH2
});
}
});
new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
Mondo.prototype.__oauthSet.restore();
});
});
describe('auth', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if you don\'t provide a username and password', () => {
expect(mondo.auth).to.throw(Error);
expect(mondo.auth.bind(mondo, config.USERNAME, config.PASSWORD)).to.not.throw(Error);
});
it('should request a token with the username and password provided', () => {
let stub = sinon.stub(mondo.oauth.password, 'getToken', function ({username, password}) {
expect(username).to.equal(config.USERNAME);
expect(password).to.equal(config.PASSWORD);
return new Promise((resolve, reject) => {
resolve();
})
});
mondo.auth(config.USERNAME, config.PASSWORD);
});
it('should store successful token requests on the instance', (done) => {
mondo.auth(config.USERNAME, config.PASSWORD).then(function (token) {
expect(mondo.token.token.access_token).to.equal(config.ACCESS_TOKEN);
expect(mondo.token.token.client_id).to.equal(config.CLIENT_ID);
expect(mondo.token.token.expires_in).to.equal(config.EXPIRES_IN);
expect(mondo.token.token.refresh_token).to.equal(config.REFRESH_TOKEN);
expect(mondo.token.token.user_id).to.equal(config.USER_ID);
// due to it's async nature, this may be 1 millisecond out
// so testing between two values instead of one with a fairly
// high tolerance.
expect(mondo.token.token.expires_at.getTime()).to.be.within(moment().add(config.EXPIRES_IN, 'seconds').toDate().getTime()-5, moment().add(config.EXPIRES_IN, 'seconds').toDate().getTime()+5);
done();
});
});
});
describe('expired', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if there is no token stored on the instance', () => {
expect(mondo.expired).to.throw(Error);
})
/**
* This one is a bit tricky: We must first make a token and test it's
* not expired. Then, after that make another one, and tick time past
* the expiry and then test again. It has to happen synchronously
* or the timer will cause incorrect results. Putting two promises in
* the same it function causes race conditions.
*/
it('should return whether or not the token is expired', (done) => {
mondo.auth(config.USERNAME, config.PASSWORD).then(function() {
expect(mondo.expired()).to.be.false;
clock = sinon.useFakeTimers();
mondo.auth(config.USERNAME, config.PASSWORD).then(function(){
clock.tick((config.EXPIRES_IN+1)*1000);
expect(mondo.expired()).to.be.true;
clock.restore();
done();
});
});
});
});
describe('refresh', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if no token is stored on the instance', () => {
expect(mondo.refresh).to.throw(Error);
});
});
// Not yet implemented server side.
describe('revoke', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.revoke).to.throw(Error);
});
it('should trigger a console warn when using revoke', () => {
let stub = sinon.stub(console, 'warn', () => {});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.revoke();
expect(stub.calledOnce).to.be.true;
stub.restore();
});
});
it('should always return a rejected promise', (done) => {
let stub = sinon.stub(console, 'warn', () => {});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.revoke().catch(() => {
done();
stub.restore();
});
});
});
// it('should revoke both the access_token and the refresh_token');
});
describe('accounts', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.accounts).to.throw(Error);
});
it('should request the accounts endpoint with the instances\' token', (done) => {
let spy = sinon.stub(mondo.oauth, 'api', (method, endpoint, options) => {
expect(method).to.equal('GET');
expect(endpoint).to.equal(ENDPOINTS.ACCOUNTS);
expect(options).to.deep.equal({
access_token: config.ACCESS_TOKEN
});
done();
});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.accounts();
});
});
});
describe('balance', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.balance.bind(mondo, config.ACCOUNT_ID)).to.throw(Error);
});
it('should throw an error if an account id isn\'t passed', () => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.balance).to.throw(Error);
});
});
it('should request the balance endpoint with the instances\' token', (done) => {
let stub = sinon.stub(mondo.oauth, 'api', (method, endpoint, options) => {
expect(method).to.equal('GET');
expect(endpoint).to.equal(_.template(ENDPOINTS.BALANCE)({accountId: config.ACCOUNT_ID}));
expect(options).to.deep.equal({
access_token: config.ACCESS_TOKEN
});
done();
});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.balance(config.ACCOUNT_ID)
});
});
});
describe('transactions', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.transactions.bind(this, config.TRANSACTION_ID)).to.throw(Error);
});
it('should throw an error if no account id is provided', () => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.transactions).to.throw(Error);
});
})
it('should request the transactions endpoint with the instances\' token and the provided account id', (done) => {
let stub = sinon.stub(mondo.oauth, 'api', (method, endpoint, options) => {
expect(method).to.equal('GET');
expect(endpoint).to.equal(ENDPOINTS.TRANSACTIONS);
expect(options).to.deep.equal({
account_id: config.ACCOUNT_ID,
access_token: config.ACCESS_TOKEN
});
done();
});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.transactions(config.ACCOUNT_ID)
});
});
});
describe('transaction', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.transaction.bind(mondo, config.TRANSACTION_ID)).to.throw(Error);
});
it('should throw an error if no transaction id is provided', () => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.transaction).to.throw(Error)
});
});
it('should request the balance endpoint with the instances\' token and provided transaction id', (done) => {
let stub = sinon.stub(mondo.oauth, 'api', function(method, endpoint, options) {
expect(method).to.equal('GET');
expect(endpoint).to.equal(_.template(ENDPOINTS.TRANSACTION)({transactionId: config.TRANSACTION_ID}));
expect(options.access_token).to.equal(config.ACCESS_TOKEN);
done();
});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.transaction(config.TRANSACTION_ID);
});
});
});
describe('annotate', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.annotate).to.throw(Error);
});
it('should throw an error if no transaction id is provided', () => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.annotate).to.throw(Error);
expect(mondo.annotate.bind(mondo, null, {
lorem: 'ipsum'
})).to.throw(Error);
});
});
it('should throw an error if no annotation hash is provided', () => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.annotate).to.throw(Error);
expect(mondo.annotate.bind(mondo, config.TRANSACTION_ID, null)).to.throw(Error);
});
});
it('should patch the transaction endpoint with the instances\' token and provided annotation hash in correctly formed headers', () => {
let stub = sinon.stub(mondo.oauth, 'api', (method, endpoint, options) => {
expect(method).to.equal('PATCH');
expect(endpoint).to.equal(_.template(ENDPOINTS.TRANSACTION)({transactionId: config.TRANSACTION_ID}))
expect(options).to.deep.equal({
access_token: config.ACCESS_TOKEN,
'metadata[lorem]': 'ipsum'
});
});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.annotate(config.TRANSACTION_ID, {
lorem: 'ipsum'
})
});
});
});
describe('feed', () => {
beforeEach(() => {
mondo = new Mondo(config.CLIENT_ID, config.CLIENT_SECRET);
});
it('should throw an error if a token isn\'t stored on the instance', () => {
expect(mondo.feed).to.throw(Error);
});
it('should throw an error if no account id is provided', (done) => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.feed.bind(mondo, null, {
'params[title]': 'Test, lorem ipsum',
'params[image_url]': 'https://www.cloudbees.com/sites/default/files/blogger_importer/s1600/jenkinsLogo1.png'
})).to.throw(Error);
done()
});
});
it('should throw an error if no annotation hash is provided', () => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.feed.bind(mondo, config.ACCOUNT_ID)).to.throw(Error);
});
})
it('should throw an error the annotation hash donesn\'t contain the required fields', (done) => {
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
expect(mondo.feed.bind(mondo, config.ACCOUNT_ID), {
'params[title]': 'Test, lorem ipsum',
}).to.throw(Error);
expect(mondo.feed.bind(mondo, config.ACCOUNT_ID), {
'params[image_url]': 'Test, lorem ipsum',
}).to.throw(Error);
done()
});
})
it('should post the feed endpoint with the instances\' token and provided hash in correctly formed headers', (done) => {
let title = 'Lorem';
let imageUrl = 'https://example.com/cat.gif';
let stub = sinon.stub(mondo.oauth, 'api', (method, endpoint, options) => {
expect(method).to.equal('POST');
expect(endpoint).to.equal(ENDPOINTS.FEED);
expect(options.access_token).to.equal(config.ACCESS_TOKEN)
expect(options['params[title]']).to.equal(title)
expect(options['params[image_url]']).to.equal(imageUrl)
done();
});
mondo.auth(config.USERNAME, config.PASSWORD).then(() => {
mondo.feed(config.ACCOUNT_ID, {
'params[title]': title,
'params[image_url]': imageUrl
})
});
});
});
});