Skip to content

Commit da7cd2b

Browse files
author
Matt Gaunt
committed
Adding tests for setVapidDetails
1 parent 4ec6ea2 commit da7cd2b

File tree

2 files changed

+149
-0
lines changed

2 files changed

+149
-0
lines changed

src/web-push-lib.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,36 @@ WebPushLib.prototype.setVapidDetails =
5555
return;
5656
}
5757

58+
if (typeof subject !== 'string' || subject.length === 0) {
59+
throw new Error('The subject should be a URL or mailto:');
60+
}
61+
62+
if (subject.indexOf('mailto:') !== 0) {
63+
// Must be a url if it doesn't have mailto at the start
64+
const subjectParseResult = url.parse(subject);
65+
if (!subjectParseResult.hostname) {
66+
throw new Error('Vapid subject is not a url or mailto url. ' + subject);
67+
}
68+
}
69+
70+
if (typeof publicKey !== 'string') {
71+
throw new Error('The vapid public key must be a url safe Base 64 ' +
72+
'encoded string.');
73+
}
74+
75+
if (urlBase64.decode(publicKey).length !== 65) {
76+
throw new Error('The vapid public key must be 65 bytes once decoded.');
77+
}
78+
79+
if (typeof privateKey !== 'string') {
80+
throw new Error('The vapid private key must be a url safe Base 64 ' +
81+
'encoded string.');
82+
}
83+
84+
if (urlBase64.decode(privateKey).length !== 32) {
85+
throw new Error('The vapid private key must be 65 bytes once decoded.');
86+
}
87+
5888
vapidDetails = {
5989
subject: subject,
6090
publicKey: publicKey,

test/test-set-vapid-details.js

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const urlBase64 = require('urlsafe-base64');
5+
const webPush = require('../src/index');
6+
7+
const VALID_SUBJECT_MAILTO = 'mailto: [email protected]';
8+
const VALID_SUBJECT_URL = 'https://exampe.com/contact';
9+
const VALID_PUBLIC_KEY = urlBase64.encode(new Buffer(65));
10+
const VALID_PRIVATE_KEY = urlBase64.encode(new Buffer(32));
11+
12+
suite('setVapidDetails()', function() {
13+
test('is defined', function() {
14+
assert(webPush.setVapidDetails);
15+
});
16+
17+
test('Valid URL input', function() {
18+
assert.doesNotThrow(function() {
19+
webPush.setVapidDetails(VALID_SUBJECT_URL, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY);
20+
});
21+
});
22+
23+
test('Valid mailto: input', function() {
24+
assert.doesNotThrow(function() {
25+
webPush.setVapidDetails(VALID_SUBJECT_MAILTO, VALID_PUBLIC_KEY, VALID_PRIVATE_KEY);
26+
});
27+
});
28+
29+
test('reset Vapid Details with null', function() {
30+
assert.doesNotThrow(function() {
31+
webPush.setVapidDetails(null);
32+
});
33+
});
34+
35+
const invalidInputs = [
36+
{
37+
subject: '',
38+
publicKey: VALID_PUBLIC_KEY,
39+
privateKey: VALID_PRIVATE_KEY
40+
},
41+
{
42+
subject: 'This is not a valid subject',
43+
publicKey: VALID_PUBLIC_KEY,
44+
privateKey: VALID_PRIVATE_KEY
45+
},
46+
{
47+
subject: {},
48+
publicKey: VALID_PUBLIC_KEY,
49+
privateKey: VALID_PRIVATE_KEY
50+
},
51+
{
52+
subject: true,
53+
publicKey: VALID_PUBLIC_KEY,
54+
privateKey: VALID_PRIVATE_KEY
55+
},
56+
{
57+
subject: VALID_SUBJECT_URL,
58+
publicKey: urlBase64.encode(new Buffer(60)),
59+
privateKey: VALID_PRIVATE_KEY
60+
},
61+
{
62+
subject: VALID_SUBJECT_URL,
63+
publicKey: 'This is invalid',
64+
privateKey: VALID_PRIVATE_KEY
65+
},
66+
{
67+
subject: VALID_SUBJECT_URL,
68+
publicKey: '',
69+
privateKey: VALID_PRIVATE_KEY
70+
},
71+
{
72+
subject: VALID_SUBJECT_URL,
73+
publicKey: {},
74+
privateKey: VALID_PRIVATE_KEY
75+
},
76+
{
77+
subject: VALID_SUBJECT_URL,
78+
publicKey: true,
79+
privateKey: VALID_PRIVATE_KEY
80+
},
81+
{
82+
subject: VALID_SUBJECT_URL,
83+
publicKey: VALID_PUBLIC_KEY,
84+
privateKey: urlBase64.encode(new Buffer(60))
85+
},
86+
{
87+
subject: VALID_SUBJECT_URL,
88+
publicKey: VALID_PUBLIC_KEY,
89+
privateKey: 'This is invalid'
90+
},
91+
{
92+
subject: VALID_SUBJECT_URL,
93+
publicKey: VALID_PUBLIC_KEY,
94+
privateKey: ''
95+
},
96+
{
97+
subject: VALID_SUBJECT_URL,
98+
publicKey: VALID_PUBLIC_KEY,
99+
privateKey: {}
100+
},
101+
{
102+
subject: VALID_SUBJECT_URL,
103+
publicKey: VALID_PUBLIC_KEY,
104+
privateKey: true
105+
}
106+
];
107+
108+
test('Invalid input should throw an error', function() {
109+
invalidInputs.forEach(function(invalidInput, index) {
110+
assert.throws(function() {
111+
webPush.setVapidDetails(
112+
invalidInput.subject,
113+
invalidInput.publicKey,
114+
invalidInput.privateKey
115+
);
116+
}, 'Error not thrown on input index: ' + index);
117+
});
118+
});
119+
});

0 commit comments

Comments
 (0)