|
| 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