-
Notifications
You must be signed in to change notification settings - Fork 57
/
testAuthCmd.js
202 lines (174 loc) · 5.62 KB
/
testAuthCmd.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* Copyright 2019 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/
/* eslint-disable no-console, no-underscore-dangle */
/* eslint-env mocha */
const assert = require('assert');
const crypto = require('crypto');
const path = require('path');
const stream = require('stream');
const fs = require('fs-extra');
const nock = require('nock');
const fetchAPI = require('@adobe/helix-fetch');
const AuthCommand = require('../src/auth.cmd');
const {
assertFile,
createTestRoot,
} = require('./utils.js');
process.env.HELIX_FETCH_FORCE_HTTP1 = 'true';
class TestStream extends stream.Writable {
constructor() {
super();
this.data = '';
}
_write(chunk, enc, next) {
console.log(chunk.toString());
this.data += chunk.toString();
next();
}
}
describe('Integration test for auth', () => {
let testRoot;
beforeEach(async () => {
testRoot = await createTestRoot();
// reset nock. potential conflict with replay
nock.restore();
nock.cleanAll();
nock.activate();
});
afterEach(() => {
nock.restore();
nock.cleanAll();
});
it('auth fails if console is no tty', async () => {
try {
const cmd = new AuthCommand()
.withDirectory(testRoot);
cmd._stdout = new TestStream();
await cmd.run();
assert.fail('auth should fail on no tty');
} catch (e) {
assert.equal(e.message, 'Interactive authentication tool not supported on non interactive consoles.');
}
});
it('auth create an .env file with the token', async () => {
const githubApi = nock('https://api.github.com')
.get('/user')
.reply(200, JSON.stringify({
name: 'Mr Black',
login: 'test-agent',
}));
const out = new TestStream();
out.isTTY = true;
const token = crypto.randomBytes(16).toString('hex');
const cmd = new AuthCommand()
.withDirectory(testRoot)
.withOpenBrowser(false)
.on('server-start', async (port) => {
const res = await fetchAPI.fetch(`http://127.0.0.1:${port}/`, {
method: 'POST',
body: {
token,
},
});
if (!res.ok) {
throw new Error(`Cannot request the server: ${await res.text()}`);
}
await res.buffer();
// required so that the server can stop properly
fetchAPI.disconnectAll();
});
cmd._stdin = new stream.PassThrough();
cmd._stdout = out;
cmd._askAdd = 'yes';
cmd._askFile = '.foo';
await cmd.run();
githubApi.done();
const file = await fs.readFile(path.resolve(testRoot, '.foo'), 'utf-8');
const tokenLine = `HLX_GITHUB_TOKEN=${token}\n`;
assert.ok(out.data.indexOf('Mr Black') > 0);
assert.ok(out.data.indexOf('test-agent') > 0);
assert.ok(out.data.indexOf(tokenLine) < 0);
assert.equal(file, tokenLine);
});
it('auth reports the token', async () => {
const githubApi = nock('https://api.github.com')
.get('/user')
.reply(200, JSON.stringify({
name: 'Mr Black',
login: 'test-agent',
}));
const out = new TestStream();
out.isTTY = true;
const token = crypto.randomBytes(16).toString('hex');
const cmd = new AuthCommand()
.withDirectory(testRoot)
.withOpenBrowser(false)
.on('server-start', async (port) => {
const res = await fetchAPI.fetch(`http://127.0.0.1:${port}/`, {
method: 'POST',
body: {
token,
},
});
if (!res.ok) {
throw new Error(`Cannot request the server: ${await res.text()}`);
}
await res.buffer();
// required so that the server can stop properly
fetchAPI.disconnectAll();
});
cmd._stdin = new stream.PassThrough();
cmd._stdout = out;
cmd._askAdd = 'no';
cmd._askFile = '.foo';
await cmd.run();
githubApi.done();
assertFile(path.resolve(testRoot, '.foo'), true);
const tokenLine = `HLX_GITHUB_TOKEN=${token}`;
assert.ok(out.data.indexOf('Mr Black') > 0);
assert.ok(out.data.indexOf('test-agent') > 0);
assert.ok(out.data.indexOf(tokenLine) > 0);
});
it('auth fires an error if GitHub user API has an issue', async () => {
const githubApi = nock('https://api.github.com')
.get('/user')
.reply(500, 'GitHub API has an issue');
const out = new TestStream();
out.isTTY = true;
const token = crypto.randomBytes(16).toString('hex');
const cmd = new AuthCommand()
.withDirectory(testRoot)
.withOpenBrowser(false)
.on('server-start', async (port) => {
const res = await fetchAPI.fetch(`http://127.0.0.1:${port}/`, {
method: 'POST',
body: {
token,
},
});
if (!res.ok) {
throw new Error(`Cannot request the server: ${await res.text()}`);
}
await res.buffer();
// required so that the server can stop properly
fetchAPI.disconnectAll();
});
cmd._stdout = out;
try {
await cmd.run();
assert.fail('auth should fail');
} catch (e) {
assert.equal(e.message, '500 - "GitHub API has an issue"');
}
githubApi.done();
});
});