Skip to content

Commit d42619f

Browse files
author
Matt Gaunt
committed
Wrap test for 0.10
1 parent bdbaad4 commit d42619f

File tree

1 file changed

+159
-157
lines changed

1 file changed

+159
-157
lines changed

test/test-cli.js

Lines changed: 159 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,195 @@
11
'use strict';
22

3-
const invalidNodeVersions = /0.(10|12).(\d+)/;
4-
if (process.versions.node.match(invalidNodeVersions)) {
5-
console.log('Skipping CLI tests as they can\'t run on node: ' + process.versions.node);
6-
return;
7-
}
8-
9-
const assert = require('assert');
10-
const urlBase64 = require('urlsafe-base64');
11-
const spawn = require('child_process').spawn;
12-
13-
const cliPath = 'src/cli.js';
14-
15-
suite('Test Encryption Helpers', function() {
16-
test('no args run', function() {
17-
return new Promise(resolve => {
18-
const webpushCLISpawn = spawn('node', [
19-
cliPath
20-
]);
21-
let errorData = '';
22-
let consoleOutput = '';
23-
webpushCLISpawn.stdout.on('data', data => {
24-
consoleOutput += data;
25-
});
3+
(function() {
4+
const invalidNodeVersions = /0.(10|12).(\d+)/;
5+
if (process.versions.node.match(invalidNodeVersions)) {
6+
console.log('Skipping CLI tests as they can\'t run on node: ' + process.versions.node);
7+
return;
8+
}
9+
10+
const assert = require('assert');
11+
const urlBase64 = require('urlsafe-base64');
12+
const spawn = require('child_process').spawn;
13+
14+
const cliPath = 'src/cli.js';
15+
16+
suite('Test Encryption Helpers', function() {
17+
test('no args run', function() {
18+
return new Promise(resolve => {
19+
const webpushCLISpawn = spawn('node', [
20+
cliPath
21+
]);
22+
let errorData = '';
23+
let consoleOutput = '';
24+
webpushCLISpawn.stdout.on('data', data => {
25+
consoleOutput += data;
26+
});
2627

27-
webpushCLISpawn.stderr.on('data', data => {
28-
errorData += data;
29-
});
28+
webpushCLISpawn.stderr.on('data', data => {
29+
errorData += data;
30+
});
3031

31-
webpushCLISpawn.on('close', code => {
32-
// No args should have code 1
33-
assert(code, 1);
32+
webpushCLISpawn.on('close', code => {
33+
// No args should have code 1
34+
assert(code, 1);
3435

35-
assert.equal(errorData, '');
36-
assert.notEqual(consoleOutput.indexOf('web-push send-notification'), -1);
37-
assert.notEqual(consoleOutput.indexOf('web-push generate-vapid-keys'), -1);
38-
resolve();
36+
assert.equal(errorData, '');
37+
assert.notEqual(consoleOutput.indexOf('web-push send-notification'), -1);
38+
assert.notEqual(consoleOutput.indexOf('web-push generate-vapid-keys'), -1);
39+
resolve();
40+
});
3941
});
4042
});
41-
});
4243

43-
test('test send-notification no args', function() {
44-
return new Promise(resolve => {
45-
const webpushCLISpawn = spawn('node', [
46-
cliPath,
47-
'send-notification'
48-
]);
49-
50-
webpushCLISpawn.on('close', code => {
51-
// No args should have code 1
52-
assert.equal(code, 1);
53-
resolve();
44+
test('test send-notification no args', function() {
45+
return new Promise(resolve => {
46+
const webpushCLISpawn = spawn('node', [
47+
cliPath,
48+
'send-notification'
49+
]);
50+
51+
webpushCLISpawn.on('close', code => {
52+
// No args should have code 1
53+
assert.equal(code, 1);
54+
resolve();
55+
});
5456
});
5557
});
56-
});
5758

58-
test('test send-notification only endpoint', function() {
59-
return new Promise(resolve => {
60-
const webpushCLISpawn = spawn('node', [
61-
cliPath,
62-
'send-notification',
63-
'--endpoint=https://example.push-service.com/'
64-
]);
65-
66-
let errorData = '';
67-
let consoleOutput = '';
68-
webpushCLISpawn.stdout.on('data', data => {
69-
consoleOutput += data;
70-
});
59+
test('test send-notification only endpoint', function() {
60+
return new Promise(resolve => {
61+
const webpushCLISpawn = spawn('node', [
62+
cliPath,
63+
'send-notification',
64+
'--endpoint=https://example.push-service.com/'
65+
]);
66+
67+
let errorData = '';
68+
let consoleOutput = '';
69+
webpushCLISpawn.stdout.on('data', data => {
70+
consoleOutput += data;
71+
});
7172

72-
webpushCLISpawn.stderr.on('data', data => {
73-
errorData += data;
74-
});
73+
webpushCLISpawn.stderr.on('data', data => {
74+
errorData += data;
75+
});
7576

76-
webpushCLISpawn.on('close', code => {
77-
assert.equal(code, 0);
78-
assert.equal(errorData, '');
79-
assert.equal(consoleOutput.indexOf('Error sending push message: '), 0);
80-
resolve();
77+
webpushCLISpawn.on('close', code => {
78+
assert.equal(code, 0);
79+
assert.equal(errorData, '');
80+
assert.equal(consoleOutput.indexOf('Error sending push message: '), 0);
81+
resolve();
82+
});
8183
});
8284
});
83-
});
8485

85-
test('test send-notification all options', function() {
86-
return new Promise(resolve => {
87-
const webpushCLISpawn = spawn('node', [
88-
cliPath,
89-
'send-notification',
90-
'--endpoint=https://example.push-service.com/',
91-
'--key=browser-key',
92-
'--auth=auth',
93-
'--payload=hello',
94-
'--ttl=1234',
95-
'--vapid-subject=http://example.push-serice.com/contact',
96-
'--vapid-pubkey=vapid-publicKey',
97-
'--vapid-pvtkey=vapid-privateKey',
98-
'--gcm-api-key=qwerty'
99-
]);
100-
101-
let errorData = '';
102-
let consoleOutput = '';
103-
webpushCLISpawn.stdout.on('data', data => {
104-
consoleOutput += data;
105-
});
86+
test('test send-notification all options', function() {
87+
return new Promise(resolve => {
88+
const webpushCLISpawn = spawn('node', [
89+
cliPath,
90+
'send-notification',
91+
'--endpoint=https://example.push-service.com/',
92+
'--key=browser-key',
93+
'--auth=auth',
94+
'--payload=hello',
95+
'--ttl=1234',
96+
'--vapid-subject=http://example.push-serice.com/contact',
97+
'--vapid-pubkey=vapid-publicKey',
98+
'--vapid-pvtkey=vapid-privateKey',
99+
'--gcm-api-key=qwerty'
100+
]);
101+
102+
let errorData = '';
103+
let consoleOutput = '';
104+
webpushCLISpawn.stdout.on('data', data => {
105+
consoleOutput += data;
106+
});
106107

107-
webpushCLISpawn.stderr.on('data', data => {
108-
errorData += data;
109-
});
108+
webpushCLISpawn.stderr.on('data', data => {
109+
errorData += data;
110+
});
110111

111-
webpushCLISpawn.on('close', code => {
112-
assert.equal(code, 0);
113-
assert.equal(errorData, '');
114-
assert.equal(consoleOutput.indexOf('Error sending push message: '), 0);
115-
resolve();
112+
webpushCLISpawn.on('close', code => {
113+
assert.equal(code, 0);
114+
assert.equal(errorData, '');
115+
assert.equal(consoleOutput.indexOf('Error sending push message: '), 0);
116+
resolve();
117+
});
116118
});
117119
});
118-
});
119-
120-
test('test generate vapid keys', function() {
121-
return new Promise(resolve => {
122-
const webpushCLISpawn = spawn('node', [
123-
cliPath,
124-
'generate-vapid-keys'
125-
]);
126-
127-
let errorData = '';
128-
let consoleOutput = '';
129-
webpushCLISpawn.stdout.on('data', data => {
130-
consoleOutput += data;
131-
});
132-
133-
webpushCLISpawn.stderr.on('data', data => {
134-
errorData += data;
135-
});
136120

137-
webpushCLISpawn.on('close', code => {
138-
assert.equal(code, 0);
139-
assert.equal(errorData, '');
140-
assert.notEqual(consoleOutput.indexOf('Public Key:'), -1);
141-
assert.notEqual(consoleOutput.indexOf('Private Key:'), -1);
121+
test('test generate vapid keys', function() {
122+
return new Promise(resolve => {
123+
const webpushCLISpawn = spawn('node', [
124+
cliPath,
125+
'generate-vapid-keys'
126+
]);
127+
128+
let errorData = '';
129+
let consoleOutput = '';
130+
webpushCLISpawn.stdout.on('data', data => {
131+
consoleOutput += data;
132+
});
142133

143-
const lines = consoleOutput.split('\n');
144-
const publicKeyTitleIndex = lines.findIndex(line => {
145-
return line.indexOf('Public Key:') !== -1;
134+
webpushCLISpawn.stderr.on('data', data => {
135+
errorData += data;
146136
});
147-
const publicKey = lines[publicKeyTitleIndex + 1].trim();
148-
assert.equal(urlBase64.decode(publicKey).length, 65);
149137

150-
const privateKeyTitleIndex = lines.findIndex(line => {
151-
return line.indexOf('Private Key:') !== -1;
138+
webpushCLISpawn.on('close', code => {
139+
assert.equal(code, 0);
140+
assert.equal(errorData, '');
141+
assert.notEqual(consoleOutput.indexOf('Public Key:'), -1);
142+
assert.notEqual(consoleOutput.indexOf('Private Key:'), -1);
143+
144+
const lines = consoleOutput.split('\n');
145+
const publicKeyTitleIndex = lines.findIndex(line => {
146+
return line.indexOf('Public Key:') !== -1;
147+
});
148+
const publicKey = lines[publicKeyTitleIndex + 1].trim();
149+
assert.equal(urlBase64.decode(publicKey).length, 65);
150+
151+
const privateKeyTitleIndex = lines.findIndex(line => {
152+
return line.indexOf('Private Key:') !== -1;
153+
});
154+
const privateKey = lines[privateKeyTitleIndex + 1].trim();
155+
assert.equal(urlBase64.decode(privateKey).length, 32);
156+
resolve();
152157
});
153-
const privateKey = lines[privateKeyTitleIndex + 1].trim();
154-
assert.equal(urlBase64.decode(privateKey).length, 32);
155-
resolve();
156158
});
157159
});
158-
});
159160

160-
test('test generate JSON vapid keys', function() {
161-
return new Promise(resolve => {
162-
const webpushCLISpawn = spawn('node', [
163-
cliPath,
164-
'generate-vapid-keys',
165-
'--json'
166-
]);
167-
168-
let errorData = '';
169-
let consoleOutput = '';
170-
webpushCLISpawn.stdout.on('data', data => {
171-
consoleOutput += data;
172-
});
161+
test('test generate JSON vapid keys', function() {
162+
return new Promise(resolve => {
163+
const webpushCLISpawn = spawn('node', [
164+
cliPath,
165+
'generate-vapid-keys',
166+
'--json'
167+
]);
168+
169+
let errorData = '';
170+
let consoleOutput = '';
171+
webpushCLISpawn.stdout.on('data', data => {
172+
consoleOutput += data;
173+
});
173174

174-
webpushCLISpawn.stderr.on('data', data => {
175-
errorData += data;
176-
});
175+
webpushCLISpawn.stderr.on('data', data => {
176+
errorData += data;
177+
});
177178

178-
webpushCLISpawn.on('close', code => {
179-
assert.equal(code, 0);
180-
assert.equal(errorData, '');
179+
webpushCLISpawn.on('close', code => {
180+
assert.equal(code, 0);
181+
assert.equal(errorData, '');
181182

182-
const vapidKeys = JSON.parse(consoleOutput);
183-
assert(vapidKeys.publicKey);
184-
assert(vapidKeys.privateKey);
183+
const vapidKeys = JSON.parse(consoleOutput);
184+
assert(vapidKeys.publicKey);
185+
assert(vapidKeys.privateKey);
185186

186-
assert.equal(urlBase64.decode(vapidKeys.privateKey).length, 32);
187-
assert.equal(urlBase64.decode(vapidKeys.publicKey).length, 65);
187+
assert.equal(urlBase64.decode(vapidKeys.privateKey).length, 32);
188+
assert.equal(urlBase64.decode(vapidKeys.publicKey).length, 65);
188189

189-
resolve();
190+
resolve();
191+
});
190192
});
191193
});
192194
});
193-
});
195+
})();

0 commit comments

Comments
 (0)