Skip to content

Commit 826d6d8

Browse files
author
Matt Gaunt
committed
Removing comments and adding few things to tests
1 parent 5be867e commit 826d6d8

File tree

6 files changed

+70
-33
lines changed

6 files changed

+70
-33
lines changed

src/encryption-helper.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,28 @@ const encrypt = function(userPublicKey, userAuth, payload) {
1010
}
1111

1212
if (typeof userPublicKey !== 'string') {
13-
throw new Error('User auth must be a string.');
13+
throw new Error('The subscription p256dh value must be a string.');
14+
}
15+
16+
if (urlBase64.decode(userPublicKey).length !== 65) {
17+
throw new Error('The subscription p256dh value should be 65 bytes long.');
1418
}
1519

1620
if (!userAuth) {
1721
throw new Error('No user auth provided for encryption.');
1822
}
1923

2024
if (typeof userAuth !== 'string') {
21-
throw new Error('User auth must be a string.');
25+
throw new Error('The subscirption auth key must be a string.');
26+
}
27+
28+
if (urlBase64.decode(userAuth).length < 16) {
29+
throw new Error('The subscription auth key should be at least 16 ' +
30+
'bytes long');
2231
}
2332

24-
if (userAuth.length < 22) {
25-
throw new Error('User auth should be 22 bytes or more.');
33+
if (typeof payload !== 'string' && !Buffer.isBuffer(payload)) {
34+
throw new Error('Payload must be either a string or a Node Buffer.');
2635
}
2736

2837
if (typeof payload === 'string' || payload instanceof String) {

src/vapid-helper.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ function generateVAPIDKeys() {
3939

4040
/**
4141
* This method takes the required VAPID parameters and returns the required
42-
* header to added to a Web Push Protocol Request.
43-
* @param {string} audience This must be the origni of the push service.
42+
* header to be added to a Web Push Protocol Request.
43+
* @param {string} audience This must be the origin of the push service.
4444
* @param {string} subject This should be a URL or a 'mailto:' email
4545
* address.
4646
* @param {Buffer} publicKey The VAPID public key.

src/web-push-lib.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,33 +116,6 @@ WebPushLib.prototype.sendNotification =
116116
}
117117
}
118118

119-
// Test subscription is an Object
120-
//
121-
// Test subscription has an endpoint string with length
122-
//
123-
// Test subscription only has endpoint keys and auth / p256dh keys
124-
//
125-
// Test keys
126-
// if (userPublicKey) {
127-
// if (typeof userPublicKey !== 'string') {
128-
// throw new Error('userPublicKey should be a base64-encoded string.');
129-
// } else if (urlBase64.decode(userPublicKey).length !== 65) {
130-
// throw new Error('userPublicKey should be 65 bytes long.');
131-
// }
132-
// }
133-
//
134-
// if (userAuth) {
135-
// if (typeof userAuth !== 'string') {
136-
// throw new Error('userAuth should be a base64-encoded string.');
137-
// } else if (urlBase64.decode(userAuth).length < 16) {
138-
// throw new Error('userAuth should be at least 16 bytes long');
139-
// }
140-
// }
141-
//
142-
// Test payload is a string
143-
//
144-
// Test options for gcm api key, vapid details and ttl
145-
146119
if (typeof timeToLive === 'undefined') {
147120
timeToLive = DEFAULT_TTL;
148121
}
@@ -156,6 +129,15 @@ WebPushLib.prototype.sendNotification =
156129
let requestPayload = null;
157130

158131
if (payload) {
132+
if (!subscription.keys ||
133+
typeof subscription !== 'object' ||
134+
!subscription.keys.p256dh ||
135+
!subscription.keys.auth) {
136+
return Promise.reject(new Error('Unable to send a message with ' +
137+
'payload to this subscription since it doesn\'t have the ' +
138+
'required encryption keys'));
139+
}
140+
159141
try {
160142
const encrypted = encryptionHelper.encrypt(
161143
subscription.keys.p256dh, subscription.keys.auth, payload);

test/test-encryption-helper.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ suite('Test Encryption Helpers', function() {
5858
function() {
5959
// Invalid auth size
6060
webPush.encrypt(VALID_PUBLIC_KEY, 'Fake', 'Example');
61+
},
62+
function() {
63+
// Invalid auth size
64+
webPush.encrypt(VALID_PUBLIC_KEY, VALID_AUTH, []);
6165
}
6266
];
6367

test/testSelenium.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
// We need geckodriver on the path
2525
require('geckodriver');
26+
require('chromedriver');
2627
/* eslint-enable global-require */
2728

2829
const PUSH_TEST_TIMEOUT = 120 * 1000;

test/testSendNotification.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,37 @@ suite('sendNotification', function() {
442442
},
443443
message: 'hello'
444444
}
445+
}, {
446+
testTitle: 'Payload provided with invalid keys',
447+
requestOptions: {
448+
subscription: {
449+
endpoint: true,
450+
keys: 'silly example'
451+
},
452+
message: 'hello'
453+
}
454+
}, {
455+
testTitle: 'Payload provided with only p256dh keys',
456+
requestOptions: {
457+
subscription: {
458+
endpoint: true,
459+
keys: {
460+
p256dh: urlBase64.encode(userPublicKey)
461+
}
462+
},
463+
message: 'hello'
464+
}
465+
}, {
466+
testTitle: 'Payload provided with only auth keys',
467+
requestOptions: {
468+
subscription: {
469+
endpoint: true,
470+
keys: {
471+
auth: urlBase64.encode(userAuth)
472+
}
473+
},
474+
message: 'hello'
475+
}
445476
}, {
446477
testTitle: 'userPublicKey argument isn\'t a string',
447478
requestOptions: {
@@ -512,6 +543,16 @@ suite('sendNotification', function() {
512543
},
513544
addEndpoint: true,
514545
serverFlags: ['statusCode=404']
546+
}, {
547+
testTitle: 'rejects when payload isn\'t a string or buffer',
548+
requestOptions: {
549+
subscription: {
550+
keys: VALID_KEYS
551+
},
552+
message: []
553+
},
554+
addEndpoint: true,
555+
serverFlags: ['statusCode=404']
515556
}
516557
];
517558

0 commit comments

Comments
 (0)