@@ -39,6 +39,7 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
39
39
const sandbox = sinon . sandbox . create ( ) ;
40
40
const now = Date . now ( ) ;
41
41
const expiredDate = new Date ( now - ONE_DAY ) ;
42
+ const FAKE_SUBSCRIPTION = makeFakeSubscription ( ) ;
42
43
43
44
const EXAMPLE_FCM_TOKEN = 'ExampleFCMToken1337' ;
44
45
const EXAMPLE_SENDER_ID = '1234567890' ;
@@ -52,7 +53,9 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
52
53
const EXAMPLE_TOKEN_DETAILS_DEFAULT_VAPID = {
53
54
swScope : '/example-scope' ,
54
55
vapidKey : DEFAULT_VAPID_KEY ,
55
- subscription : makeFakeSubscription ( ) ,
56
+ endpoint : FAKE_SUBSCRIPTION . endpoint ,
57
+ auth : arrayBufferToBase64 ( FAKE_SUBSCRIPTION [ 'getKey' ] ( 'auth' ) ) ,
58
+ p256dh : arrayBufferToBase64 ( FAKE_SUBSCRIPTION [ 'getKey' ] ( 'p256dh' ) ) ,
56
59
fcmSenderId : EXAMPLE_SENDER_ID ,
57
60
fcmToken : 'qwerty1' ,
58
61
fcmPushSet : '87654321' ,
@@ -61,7 +64,9 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
61
64
const EXAMPLE_TOKEN_DETAILS_CUSTOM_VAPID = {
62
65
swScope : '/example-scope' ,
63
66
vapidKey : CUSTOM_VAPID_KEY ,
64
- subscription : makeFakeSubscription ( ) ,
67
+ endpoint : FAKE_SUBSCRIPTION . endpoint ,
68
+ auth : arrayBufferToBase64 ( FAKE_SUBSCRIPTION [ 'getKey' ] ( 'auth' ) ) ,
69
+ p256dh : arrayBufferToBase64 ( FAKE_SUBSCRIPTION [ 'getKey' ] ( 'p256dh' ) ) ,
65
70
fcmSenderId : EXAMPLE_SENDER_ID ,
66
71
fcmToken : 'qwerty2' ,
67
72
fcmPushSet : '7654321' ,
@@ -70,7 +75,9 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
70
75
const EXAMPLE_EXPIRED_TOKEN_DETAILS = {
71
76
swScope : '/example-scope' ,
72
77
vapidKey : DEFAULT_VAPID_KEY ,
73
- subscription : makeFakeSubscription ( ) ,
78
+ endpoint : FAKE_SUBSCRIPTION . endpoint ,
79
+ auth : arrayBufferToBase64 ( FAKE_SUBSCRIPTION [ 'getKey' ] ( 'auth' ) ) ,
80
+ p256dh : arrayBufferToBase64 ( FAKE_SUBSCRIPTION [ 'getKey' ] ( 'p256dh' ) ) ,
74
81
fcmSenderId : EXAMPLE_SENDER_ID ,
75
82
fcmToken : 'qwerty3' ,
76
83
fcmPushSet : '654321' ,
@@ -399,6 +406,83 @@ describe('Firebase Messaging > *Controller.getToken()', function() {
399
406
assert . equal ( tokenModelArgs . fcmPushSet , TOKEN_DETAILS [ 'pushSet' ] ) ;
400
407
} ) ;
401
408
} ) ;
409
+
410
+ it ( 'should get a new token in ${ServiceClass.name} if PushSubscription details have changed' , function ( ) {
411
+ // Stubs
412
+ const deleteTokenStub = sandbox . stub (
413
+ TokenDetailsModel . prototype ,
414
+ 'deleteToken'
415
+ ) ;
416
+ const saveTokenDetailsStub = sandbox . stub (
417
+ TokenDetailsModel . prototype ,
418
+ 'saveTokenDetails'
419
+ ) ;
420
+ saveTokenDetailsStub . callsFake ( async ( ) => { } ) ;
421
+
422
+ sandbox
423
+ . stub ( ControllerInterface . prototype , 'getNotificationPermission_' )
424
+ . callsFake ( ( ) => NotificationPermission . granted ) ;
425
+
426
+ const existingTokenDetails = VapidSetup [ 'details' ] ;
427
+ let vapidKeyToUse = FCMDetails . DEFAULT_PUBLIC_VAPID_KEY ;
428
+ if ( VapidSetup [ 'name' ] === 'custom' ) {
429
+ vapidKeyToUse = base64ToArrayBuffer ( CUSTOM_VAPID_KEY ) ;
430
+ }
431
+ sandbox
432
+ . stub ( ServiceClass . prototype , 'getPublicVapidKey_' )
433
+ . callsFake ( ( ) => Promise . resolve ( vapidKeyToUse ) ) ;
434
+
435
+ sandbox
436
+ . stub ( VapidDetailsModel . prototype , 'saveVapidDetails' )
437
+ . callsFake ( async ( ) => { } ) ;
438
+
439
+ const GET_TOKEN_RESPONSE = {
440
+ token : 'new-token' ,
441
+ pushSet : 'new-pushSet'
442
+ } ;
443
+ sandbox
444
+ . stub ( IIDModel . prototype , 'getToken' )
445
+ . callsFake ( ( ) => Promise . resolve ( GET_TOKEN_RESPONSE ) ) ;
446
+ sandbox
447
+ . stub ( IIDModel . prototype , 'deleteToken' )
448
+ . callsFake ( async ( ) => { } ) ;
449
+
450
+ const registration = generateFakeReg ( Promise . resolve ( null ) ) ;
451
+ mockGetReg ( Promise . resolve ( registration ) ) ;
452
+
453
+ const options = {
454
+ endpoint : 'https://different-push-endpoint.com/' ,
455
+ auth : 'another-auth-secret' ,
456
+ p256dh : 'another-user-public-key'
457
+ } ;
458
+ const newPS = makeFakeSubscription ( options ) ;
459
+
460
+ deleteTokenStub . callsFake ( token => {
461
+ assert . equal ( token , existingTokenDetails . fcmToken ) ;
462
+ return Promise . resolve ( existingTokenDetails ) ;
463
+ } ) ;
464
+ // The push subscription has changed since we saved the token.
465
+ sandbox
466
+ . stub ( ServiceClass . prototype , 'getPushSubscription' )
467
+ . callsFake ( ( ) => Promise . resolve ( newPS ) ) ;
468
+ sandbox
469
+ . stub ( TokenDetailsModel . prototype , 'getTokenDetailsFromSWScope' )
470
+ . callsFake ( ( ) => Promise . resolve ( existingTokenDetails ) ) ;
471
+
472
+ const serviceInstance = new ServiceClass ( app ) ;
473
+ return serviceInstance . getToken ( ) . then ( token => {
474
+ // make sure we call getToken and retrieve the new token.
475
+ assert . equal ( 'new-token' , token ) ;
476
+ // make sure the existing token is deleted.
477
+ assert . equal ( deleteTokenStub . callCount , 1 ) ;
478
+ // make sure the new details are saved.
479
+ assert . equal ( saveTokenDetailsStub . callCount , 1 ) ;
480
+ assert . equal (
481
+ VapidDetailsModel . prototype . saveVapidDetails [ 'callCount' ] ,
482
+ 1
483
+ ) ;
484
+ } ) ;
485
+ } ) ;
402
486
} ) ;
403
487
} ) ;
404
488
0 commit comments