This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 121
/
push.js
772 lines (708 loc) · 24.2 KB
/
push.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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
'use strict'
const ROOT_DIR = '../..'
const assert = require('insist')
var proxyquire = require('proxyquire')
var sinon = require('sinon')
var ajv = require('ajv')()
var fs = require('fs')
var path = require('path')
const P = require(`${ROOT_DIR}/lib/promise`)
const mocks = require('../mocks')
const mockLog = mocks.mockLog
var mockUid = Buffer.from('foo')
var mockConfig = {}
const PUSH_PAYLOADS_SCHEMA_PATH = `${ROOT_DIR}/docs/pushpayloads.schema.json`
var TTL = '42'
const pushModulePath = `${ROOT_DIR}/lib/push`
var mockDbEmpty = {
devices: function () {
return P.resolve([])
}
}
var mockDevices = [
{
'id': '0f7aa00356e5416e82b3bef7bc409eef',
'isCurrentDevice': true,
'lastAccessTime': 1449235471335,
'name': 'My Phone',
'type': 'mobile',
'pushCallback': 'https://updates.push.services.mozilla.com/update/abcdef01234567890abcdefabcdef01234567890abcdef',
'pushPublicKey': mocks.MOCK_PUSH_KEY,
'pushAuthKey': 'w3b14Zjc-Afj2SDOLOyong=='
},
{
'id': '3a45e6d0dae543qqdKyqjuvAiEupsnOd',
'isCurrentDevice': false,
'lastAccessTime': 1417699471335,
'name': 'My Desktop',
'type': null,
'pushCallback': 'https://updates.push.services.mozilla.com/update/d4c5b1e3f5791ef83896c27519979b93a45e6d0da34c75',
'pushPublicKey': mocks.MOCK_PUSH_KEY,
'pushAuthKey': 'w3b14Zjc-Afj2SDOLOyong=='
}
]
var mockDbResult = {
devices: function (/* uid */) {
return P.resolve(mockDevices)
}
}
describe('push', () => {
it(
'pushToDevices throws on device not found',
() => {
var push = require(pushModulePath)(mockLog(), mockDbEmpty, mockConfig)
sinon.spy(push, 'sendPush')
return push.pushToDevices([mockUid], 'bogusid').then(function () {
assert(false, 'must throw')
}, function(err) {
assert(! push.sendPush.called)
})
}
)
it(
'pushToAllDevices does not throw on empty device result',
() => {
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.success') {
assert.fail('must not call push.success')
}
}
})
var push = require(pushModulePath)(thisMockLog, mockDbEmpty, mockConfig)
return push.pushToAllDevices(mockUid, 'accountVerify')
}
)
it(
'pushToAllDevices does not send notification to an excluded device',
() => {
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(mockLog(), mockDbResult, mockConfig)
var options = { excludedDeviceIds: [mockDevices[0].id] }
return push.pushToAllDevices(mockUid, 'accountVerify', options)
}
)
it(
'pushToAllDevices calls sendPush',
() => {
var push = require(pushModulePath)(mockLog(), mockDbResult, mockConfig)
sinon.stub(push, 'sendPush')
var excluded = [mockDevices[0].id]
var data = Buffer.from('foobar')
var options = { data: data, excludedDeviceIds: excluded, TTL: TTL }
return push.pushToAllDevices(mockUid, 'deviceConnected', options)
.then(function() {
assert.ok(push.sendPush.calledOnce, 'push was called')
assert.equal(push.sendPush.getCall(0).args[0], mockUid)
assert.deepEqual(push.sendPush.getCall(0).args[1], [mockDevices[1]])
assert.equal(push.sendPush.getCall(0).args[2], 'deviceConnected')
assert.deepEqual(push.sendPush.getCall(0).args[3], { data: data, TTL: TTL })
push.sendPush.restore()
})
}
)
it(
'pushToDevices calls sendPush',
() => {
var push = require(pushModulePath)(mockLog(), mockDbResult, mockConfig)
sinon.stub(push, 'sendPush')
var data = Buffer.from('foobar')
var options = { data: data, TTL: TTL }
return push.pushToDevices(mockUid, [mockDevices[0].id], 'deviceConnected', options)
.then(function() {
assert.ok(push.sendPush.calledOnce, 'push was called')
assert.equal(push.sendPush.getCall(0).args[0], mockUid)
assert.deepEqual(push.sendPush.getCall(0).args[1], [mockDevices[0]])
assert.equal(push.sendPush.getCall(0).args[2], 'deviceConnected')
assert.deepEqual(push.sendPush.getCall(0).args[3], { data: data, TTL: TTL })
push.sendPush.restore()
})
}
)
it(
'pushToDevice calls pushToDevices',
() => {
var push = require(pushModulePath)(mockLog(), mockDbResult, mockConfig)
sinon.stub(push, 'pushToDevices')
var data = Buffer.from('foobar')
var options = { data: data, TTL: TTL }
push.pushToDevice(mockUid, mockDevices[0].id, 'deviceConnected', options)
assert.ok(push.pushToDevices.calledOnce, 'pushToDevices was called')
assert.equal(push.pushToDevices.getCall(0).args[0], mockUid)
assert.deepEqual(push.pushToDevices.getCall(0).args[1], [mockDevices[0].id])
assert.equal(push.pushToDevices.getCall(0).args[2], 'deviceConnected')
assert.deepEqual(push.pushToDevices.getCall(0).args[3], { data: data, TTL: TTL })
push.pushToDevices.restore()
}
)
it(
'sendPush sends notifications with a TTL of 0',
() => {
var successCalled = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.success') {
// notification sent
successCalled++
}
}
})
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
assert.equal(options.TTL, '0', 'sends the proper ttl header')
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDbResult, mockConfig)
return push.sendPush(mockUid, mockDevices, 'accountVerify')
.then(() => {
assert.equal(successCalled, 2)
})
}
)
it(
'sendPush sends notifications with user-defined TTL',
() => {
var successCalled = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.success') {
// notification sent
successCalled++
}
}
})
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
assert.equal(options.TTL, TTL, 'sends the proper ttl header')
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDbResult, mockConfig)
var options = { TTL: TTL }
return push.sendPush(mockUid, mockDevices, 'accountVerify', options)
.then(() => {
assert.equal(successCalled, 2)
})
}
)
it(
'sendPush sends data',
() => {
var count = 0
var data = Buffer.from('foobar')
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
count++
assert.ok(sub.keys.p256dh)
assert.ok(sub.keys.auth)
assert.deepEqual(payload, data)
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(mockLog(), mockDbResult, mockConfig)
var options = { data: data }
return push.sendPush(mockUid, mockDevices, 'accountVerify', options)
.then(() => {
assert.equal(count, 2)
})
}
)
it(
'push fails if data is present but both keys are not present',
() => {
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.data_but_no_keys') {
// data detected but device had no keys
count++
}
}
})
var devices = [{
'id': 'foo',
'name': 'My Phone',
'pushCallback': 'https://updates.push.services.mozilla.com/update/abcdef01234567890abcdefabcdef01234567890abcdef',
'pushAuthKey': 'bogus'
}]
var push = require(pushModulePath)(thisMockLog, mockDbResult, mockConfig)
var options = { data: Buffer.from('foobar') }
return push.sendPush(mockUid, devices, 'accountVerify', options)
.then(() => {
assert.equal(count, 1)
})
}
)
it(
'push catches devices with no push callback',
() => {
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.no_push_callback') {
// device had no push callback
count++
}
}
})
var devices = [{
'id': 'foo',
'name': 'My Phone'
}]
var push = require(pushModulePath)(thisMockLog, mockDbResult, mockConfig)
return push.sendPush(mockUid, devices, 'accountVerify')
.then(() => {
assert.equal(count, 1)
})
}
)
it(
'push reports errors when web-push fails',
() => {
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.failed') {
// web-push failed
count++
}
}
})
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
return P.reject(new Error('Failed'))
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDbResult, mockConfig)
return push.sendPush(mockUid, [mockDevices[0]], 'accountVerify')
.then(() => {
assert.equal(count, 1)
})
}
)
it(
'push logs an error when asked to send to more than 200 devices',
() => {
var thisMockLog = mockLog({
error: sinon.spy()
})
var devices = []
for (var i = 0; i < 200; i++) {
devices.push(mockDevices[0])
}
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
assert.equal(options.TTL, '0', 'sends the proper ttl header')
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDbResult, mockConfig)
return push.sendPush(mockUid, devices, 'accountVerify').then(function () {
assert.equal(thisMockLog.error.callCount, 0, 'log.error was not called')
devices.push(mockDevices[0])
return push.sendPush(mockUid, devices, 'accountVerify')
}).then(function () {
assert.equal(thisMockLog.error.callCount, 1, 'log.error was called')
var arg = thisMockLog.error.getCall(0).args[0]
assert.equal(arg.op, 'push.pushToDevices')
assert.equal(arg.err.message, 'Too many devices connected to account')
})
}
)
it(
'push resets device push data when push server responds with a 400 level error',
() => {
var mockDb = {
updateDevice: sinon.spy(function () {
return P.resolve()
})
}
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.reset_settings') {
// web-push failed
assert.equal(mockDb.updateDevice.callCount, 1, 'db.updateDevice was called once')
var args = mockDb.updateDevice.args[0]
assert.equal(args.length, 3, 'db.updateDevice was passed three arguments')
assert.equal(args[1], null, 'sessionTokenId argument was null')
count++
}
}
})
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
var err = new Error('Failed')
err.statusCode = 410
return P.reject(err)
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDb, mockConfig)
// Careful, the argument gets modified in-place.
var device = JSON.parse(JSON.stringify(mockDevices[0]))
return push.sendPush(mockUid, [device], 'accountVerify')
.then(() => {
assert.equal(count, 1)
})
}
)
it(
'push resets device push data when a failure is caused by bad encryption keys',
() => {
var mockDb = {
updateDevice: sinon.spy(function () {
return P.resolve()
})
}
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.reset_settings') {
// web-push failed
assert.equal(mockDb.updateDevice.callCount, 1, 'db.updateDevice was called once')
var args = mockDb.updateDevice.args[0]
assert.equal(args.length, 3, 'db.updateDevice was passed three arguments')
assert.equal(args[1], null, 'sessionTokenId argument was null')
count++
}
}
})
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
var err = new Error('Failed')
return P.reject(err)
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDb, mockConfig)
// Careful, the argument gets modified in-place.
var device = JSON.parse(JSON.stringify(mockDevices[0]))
device.pushPublicKey = 'E' + device.pushPublicKey.substring(1) // make the key invalid
return push.sendPush(mockUid, [device], 'accountVerify')
.then(() => {
assert.equal(count, 1)
})
}
)
it(
'push does not reset device push data after an unexpected failure',
() => {
var mockDb = {
updateDevice: sinon.spy(function () {
return P.resolve()
})
}
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.failed') {
// web-push failed
assert.equal(mockDb.updateDevice.callCount, 0, 'db.updateDevice was not called')
count++
}
}
})
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
var err = new Error('Failed')
return P.reject(err)
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDb, mockConfig)
return push.sendPush(mockUid, [mockDevices[0]], 'accountVerify')
.then(() => {
assert.equal(count, 1)
})
}
)
it(
'notifyUpdate calls pushToAllDevices',
() => {
var push = require(pushModulePath)(mockLog(), mockDbEmpty, mockConfig)
sinon.spy(push, 'pushToAllDevices')
return push.notifyUpdate(mockUid, 'passwordReset')
.then(function() {
assert.ok(push.pushToAllDevices.calledOnce, 'pushToAllDevices was called')
assert.equal(push.pushToAllDevices.getCall(0).args[0], mockUid)
assert.equal(push.pushToAllDevices.getCall(0).args[1], 'passwordReset')
push.pushToAllDevices.restore()
})
}
)
it(
'notifyUpdate without a 2nd arg calls pushToAllDevices with a accountVerify reason',
() => {
var push = require(pushModulePath)(mockLog(), mockDbEmpty, mockConfig)
sinon.spy(push, 'pushToAllDevices')
return push.notifyUpdate(mockUid)
.then(function() {
assert.ok(push.pushToAllDevices.calledOnce, 'pushToAllDevices was called')
assert.equal(push.pushToAllDevices.getCall(0).args[0], mockUid)
assert.equal(push.pushToAllDevices.getCall(0).args[1], 'accountVerify')
push.pushToAllDevices.restore()
})
}
)
it(
'notifyDeviceConnected calls pushToAllDevices',
() => {
var push = require(pushModulePath)(mockLog(), mockDbEmpty, mockConfig)
sinon.spy(push, 'pushToAllDevices')
var deviceId = 'gjfkd5434jk5h5fd'
var deviceName = 'My phone'
var expectedData = {
version: 1,
command: 'fxaccounts:device_connected',
data: {
deviceName: deviceName
}
}
return push.notifyDeviceConnected(mockUid, deviceName, deviceId).catch(function (err) {
assert.fail('must not throw')
throw err
})
.then(function() {
assert.ok(push.pushToAllDevices.calledOnce, 'pushToAllDevices was called')
assert.equal(push.pushToAllDevices.getCall(0).args[0], mockUid)
assert.equal(push.pushToAllDevices.getCall(0).args[1], 'deviceConnected')
var options = push.pushToAllDevices.getCall(0).args[2]
var payload = JSON.parse(options.data.toString('utf8'))
assert.deepEqual(payload, expectedData)
var schemaPath = path.resolve(__dirname, PUSH_PAYLOADS_SCHEMA_PATH)
var schema = JSON.parse(fs.readFileSync(schemaPath))
assert.ok(ajv.validate(schema, payload))
assert.deepEqual(options.excludedDeviceIds, [deviceId])
push.pushToAllDevices.restore()
})
}
)
it(
'notifyDeviceDisconnected calls pushToAllDevices',
() => {
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(mockLog(), mockDbResult, mockConfig)
sinon.spy(push, 'pushToAllDevices')
var idToDisconnect = mockDevices[0].id
var expectedData = {
version: 1,
command: 'fxaccounts:device_disconnected',
data: {
id: idToDisconnect
}
}
return push.notifyDeviceDisconnected(mockUid, idToDisconnect).catch(function (err) {
assert.fail('must not throw')
throw err
})
.then(function() {
assert.ok(push.pushToAllDevices.calledOnce, 'pushToAllDevices was called')
assert.equal(push.pushToAllDevices.getCall(0).args[0], mockUid)
assert.equal(push.pushToAllDevices.getCall(0).args[1], 'deviceDisconnected')
var options = push.pushToAllDevices.getCall(0).args[2]
var payload = JSON.parse(options.data.toString('utf8'))
assert.deepEqual(payload, expectedData)
var schemaPath = path.resolve(__dirname, PUSH_PAYLOADS_SCHEMA_PATH)
var schema = JSON.parse(fs.readFileSync(schemaPath))
assert.ok(ajv.validate(schema, payload))
assert.ok(options.TTL, 'TTL should be set')
push.pushToAllDevices.restore()
})
}
)
it(
'notifyPasswordChanged calls sendPush',
() => {
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(mockLog(), mockDbResult, mockConfig)
sinon.spy(push, 'sendPush')
var expectedData = {
version: 1,
command: 'fxaccounts:password_changed'
}
return push.notifyPasswordChanged(mockUid, mockDevices).catch(function (err) {
assert.fail('must not throw')
throw err
})
.then(function() {
assert.ok(push.sendPush.calledOnce, 'sendPush was called')
assert.equal(push.sendPush.getCall(0).args[0], mockUid)
assert.equal(push.sendPush.getCall(0).args[1], mockDevices)
assert.equal(push.sendPush.getCall(0).args[2], 'passwordChange')
var options = push.sendPush.getCall(0).args[3]
var payload = JSON.parse(options.data.toString('utf8'))
assert.deepEqual(payload, expectedData)
var schemaPath = path.resolve(__dirname, PUSH_PAYLOADS_SCHEMA_PATH)
var schema = JSON.parse(fs.readFileSync(schemaPath))
assert.ok(ajv.validate(schema, payload))
push.sendPush.restore()
})
}
)
it(
'notifyPasswordReset calls sendPush',
() => {
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(mockLog(), mockDbEmpty, mockConfig)
sinon.spy(push, 'sendPush')
var expectedData = {
version: 1,
command: 'fxaccounts:password_reset'
}
return push.notifyPasswordReset(mockUid, mockDevices).catch(function (err) {
assert.fail('must not throw')
throw err
})
.then(function() {
assert.ok(push.sendPush.calledOnce, 'sendPush was called')
assert.equal(push.sendPush.getCall(0).args[0], mockUid)
assert.equal(push.sendPush.getCall(0).args[1], mockDevices)
assert.equal(push.sendPush.getCall(0).args[2], 'passwordReset')
var options = push.sendPush.getCall(0).args[3]
var payload = JSON.parse(options.data.toString('utf8'))
assert.deepEqual(payload, expectedData)
var schemaPath = path.resolve(__dirname, PUSH_PAYLOADS_SCHEMA_PATH)
var schema = JSON.parse(fs.readFileSync(schemaPath))
assert.ok(ajv.validate(schema, payload))
push.sendPush.restore()
})
}
)
it(
'notifyAccountDestroyed calls sendPush',
() => {
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(mockLog(), mockDbEmpty, mockConfig)
sinon.spy(push, 'sendPush')
var expectedData = {
version: 1,
command: 'fxaccounts:account_destroyed',
data: {
uid: mockUid.toString('hex')
}
}
return push.notifyAccountDestroyed(mockUid, mockDevices).catch(function (err) {
assert.fail('must not throw')
throw err
})
.then(function() {
assert.ok(push.sendPush.calledOnce, 'sendPush was called')
assert.equal(push.sendPush.getCall(0).args[0], mockUid)
assert.equal(push.sendPush.getCall(0).args[1], mockDevices)
assert.equal(push.sendPush.getCall(0).args[2], 'accountDestroyed')
var options = push.sendPush.getCall(0).args[3]
var payload = JSON.parse(options.data.toString('utf8'))
assert.deepEqual(payload, expectedData)
var schemaPath = path.resolve(__dirname, PUSH_PAYLOADS_SCHEMA_PATH)
var schema = JSON.parse(fs.readFileSync(schemaPath))
assert.ok(ajv.validate(schema, payload))
push.sendPush.restore()
})
}
)
it(
'sendPush includes VAPID identification if it is configured',
() => {
let count = 0
var thisMockLog = mockLog({
info: function (log) {
if (log.name === 'push.account_verify.success') {
count++
}
}
})
var mockConfig = {
publicUrl: 'https://example.com',
vapidKeysFile: path.join(__dirname, '../config/mock-vapid-keys.json')
}
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
assert.ok(options.vapidDetails, 'sends the VAPID params object')
assert.equal(options.vapidDetails.subject, mockConfig.publicUrl, 'sends the correct VAPID subject')
assert.equal(options.vapidDetails.privateKey, 'private', 'sends the correct VAPID privkey')
assert.equal(options.vapidDetails.publicKey, 'public', 'sends the correct VAPID pubkey')
return P.resolve()
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDbResult, mockConfig)
return push.sendPush(mockUid, mockDevices, 'accountVerify')
.then(() => {
assert.equal(count, 2)
})
}
)
it(
'sendPush errors out cleanly if given an unknown reason argument',
() => {
var thisMockLog = mockLog()
var mockConfig = {}
var mocks = {
'web-push': {
sendNotification: function (sub, payload, options) {
assert.fail('should not have called sendNotification')
return P.reject('Should not have called sendNotification')
}
}
}
var push = proxyquire(pushModulePath, mocks)(thisMockLog, mockDbResult, mockConfig)
return push.sendPush(mockUid, mockDevices, 'anUnknownReasonString').then(
function () {
assert(false, 'calling sendPush should have failed')
},
function (err) {
assert.equal(err, 'Unknown push reason: anUnknownReasonString')
}
)
}
)
})