22
33const assert = require ( 'assert' ) ;
44const urlBase64 = require ( 'urlsafe-base64' ) ;
5+ const sinon = require ( 'sinon' ) ;
6+ const crypto = require ( 'crypto' ) ;
57const webPush = require ( '../src/index' ) ;
68const vapidHelper = require ( '../src/vapid-helper' ) ;
79
@@ -13,6 +15,16 @@ const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
1315const VALID_EXPIRATION = Math . floor ( Date . now ( ) / 1000 ) + ( 60 * 60 * 12 ) ;
1416
1517suite ( 'Test Vapid Helpers' , function ( ) {
18+ const sandbox = sinon . sandbox . create ( ) ;
19+
20+ beforeEach ( function ( ) {
21+ sandbox . restore ( ) ;
22+ } ) ;
23+
24+ after ( function ( ) {
25+ sandbox . restore ( ) ;
26+ } ) ;
27+
1628 test ( 'is defined' , function ( ) {
1729 assert ( webPush . generateVAPIDKeys ) ;
1830 assert ( webPush . getVapidHeaders ) ;
@@ -30,6 +42,26 @@ suite('Test Vapid Helpers', function() {
3042 assert . equal ( urlBase64 . decode ( keys . publicKey ) . length , 65 ) ;
3143 } ) ;
3244
45+ test ( 'generate vapid keys with padding' , function ( ) {
46+ sandbox . stub ( crypto , 'createECDH' ) . callsFake ( ( ) => {
47+ return {
48+ generateKeys : ( ) => { } ,
49+ getPublicKey : ( ) => new Buffer ( 60 ) ,
50+ getPrivateKey : ( ) => new Buffer ( 27 )
51+ } ;
52+ } ) ;
53+
54+ const keys = webPush . generateVAPIDKeys ( ) ;
55+ assert ( keys . privateKey ) ;
56+ assert ( keys . publicKey ) ;
57+
58+ assert . equal ( typeof keys . privateKey , 'string' ) ;
59+ assert . equal ( typeof keys . publicKey , 'string' ) ;
60+
61+ assert . equal ( urlBase64 . decode ( keys . privateKey ) . length , 32 ) ;
62+ assert . equal ( urlBase64 . decode ( keys . publicKey ) . length , 65 ) ;
63+ } ) ;
64+
3365 test ( 'generate new vapid keys between calls' , function ( ) {
3466 const keys = webPush . generateVAPIDKeys ( ) ;
3567 assert ( keys . privateKey ) ;
0 commit comments