Skip to content

Commit 7643f4e

Browse files
committed
Add way to generate deployment token, addresses #33
1 parent 7d9f4fe commit 7643f4e

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

src/commands/token.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// npm packages
2+
const got = require('got');
3+
const chalk = require('chalk');
4+
5+
// our packages
6+
const {userConfig, isLoggedIn, logout} = require('../config');
7+
8+
exports.command = ['token'];
9+
exports.describe = 'generate new deployment token';
10+
exports.builder = {};
11+
exports.handler = async () => {
12+
if (!isLoggedIn()) {
13+
return;
14+
}
15+
16+
console.log(chalk.bold('Generating new deployment token for:'), userConfig.endpoint);
17+
18+
// services request url
19+
const remoteUrl = `${userConfig.endpoint}/deployToken`;
20+
// construct shared request params
21+
const options = {
22+
headers: {
23+
Authorization: `Bearer ${userConfig.token}`,
24+
},
25+
json: true,
26+
};
27+
// try sending request
28+
try {
29+
const {body} = await got(remoteUrl, options);
30+
const {token} = body;
31+
console.log(chalk.bold('New token generated:'));
32+
console.log('');
33+
console.log(token);
34+
console.log('');
35+
console.log(chalk.yellow('WARNING!'), 'Make sure to write it down, you will not be able to get it again!');
36+
} catch (e) {
37+
// if authorization is expired/broken/etc
38+
if (e.statusCode === 401) {
39+
logout(userConfig);
40+
console.log(chalk.red('Error: authorization expired!'), 'Please, relogin and try again.');
41+
return;
42+
}
43+
44+
console.log(chalk.red('Error generating deployment token:'), e.toString());
45+
}
46+
};

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const logs = require('./commands/logs');
99
const remove = require('./commands/remove');
1010
const endpoint = require('./commands/endpoint');
1111
const config = require('./commands/config');
12+
const token = require('./commands/token');
1213

1314
// init program
1415
yargs
@@ -22,4 +23,5 @@ yargs
2223
.command(list)
2324
.command(logs)
2425
.command(remove)
26+
.command(token)
2527
.command(config).argv;

test/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ const deploy = require('./deploy');
55
const logs = require('./logs');
66
const list = require('./list');
77
const config = require('./config');
8+
const token = require('./token');
89

910
// run tests
1011
login();
@@ -13,3 +14,4 @@ deploy();
1314
logs();
1415
list();
1516
config();
17+
token();

test/token.js

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// npm packages
2+
const tap = require('tap');
3+
const nock = require('nock');
4+
const sinon = require('sinon');
5+
6+
// our packages
7+
const {handler: token} = require('../src/commands/token');
8+
const {userConfig, updateConfig} = require('../src/config');
9+
10+
module.exports = () => {
11+
// test removal
12+
tap.test('Should generate token', t => {
13+
// handle correct request
14+
const tokenServer = nock('http://localhost:8080').get('/deployToken').reply(200, {token: 'test'});
15+
// spy on console
16+
const consoleSpy = sinon.spy(console, 'log');
17+
// execute login
18+
token().then(() => {
19+
// make sure log in was successful
20+
// check that server was called
21+
t.ok(tokenServer.isDone());
22+
// first check console output
23+
t.deepEqual(
24+
consoleSpy.args,
25+
[
26+
['Generating new deployment token for:', 'http://localhost:8080'],
27+
['New token generated:'],
28+
[''],
29+
['test'],
30+
[''],
31+
['WARNING!', 'Make sure to write it down, you will not be able to get it again!'],
32+
],
33+
'Correct log output'
34+
);
35+
// restore console
36+
console.log.restore();
37+
tokenServer.done();
38+
t.end();
39+
});
40+
});
41+
42+
// test
43+
tap.test('Should deauth on 401', t => {
44+
// copy original config for restoration
45+
const originalConfig = Object.assign({}, userConfig);
46+
// handle correct request
47+
const tokenServer = nock('http://localhost:8080').get('/deployToken').reply(401);
48+
// spy on console
49+
const consoleSpy = sinon.spy(console, 'log');
50+
// execute login
51+
token().then(() => {
52+
// make sure log in was successful
53+
// check that server was called
54+
t.ok(tokenServer.isDone());
55+
// first check console output
56+
t.deepEqual(
57+
consoleSpy.args,
58+
[
59+
['Generating new deployment token for:', 'http://localhost:8080'],
60+
['Error: authorization expired!', 'Please, relogin and try again.'],
61+
],
62+
'Correct log output'
63+
);
64+
// restore console
65+
console.log.restore();
66+
// tear down nock
67+
tokenServer.done();
68+
// restore original config
69+
updateConfig(originalConfig);
70+
t.end();
71+
});
72+
});
73+
};

0 commit comments

Comments
 (0)