Skip to content

Commit 3828514

Browse files
authored
Merge pull request #246 from SeaLife/master
#244 allow exoframe rm with authentication token
2 parents 2791771 + 4ae18b9 commit 3828514

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ exoframe-win.exe
88
.idea/
99
.DS_Store
1010

11+
package-lock.json
12+
exoframe.json

src/commands/remove.js

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,35 @@ const {userConfig, isLoggedIn, logout} = require('../config');
77

88
exports.command = ['remove <id>', 'rm <id>'];
99
exports.describe = 'remove active deployment';
10-
exports.builder = {};
11-
exports.handler = async ({id}) => {
12-
if (!isLoggedIn()) {
10+
exports.builder = {
11+
token: {
12+
alias: 't',
13+
description: 'Deployment token to be used for authentication',
14+
},
15+
};
16+
exports.handler = async (args = {}) => {
17+
const deployToken = args.token;
18+
const id = args.id;
19+
20+
if (!deployToken && !isLoggedIn()) {
21+
console.log(chalk.red('Error!'), '\nYou need to sign in first or supply a authentication token.');
1322
return;
1423
}
1524

1625
console.log(chalk.bold('Removing deployment:'), id);
1726

1827
// services request url
1928
const remoteUrl = `${userConfig.endpoint}/remove/${encodeURIComponent(id)}`;
29+
let authToken = userConfig.token;
30+
31+
if (deployToken) {
32+
authToken = deployToken;
33+
console.log('\nRemoving using given token..');
34+
}
2035
// construct shared request params
2136
const options = {
2237
headers: {
23-
Authorization: `Bearer ${userConfig.token}`,
38+
Authorization: `Bearer ${authToken}`,
2439
},
2540
body: {},
2641
json: true,

test/__snapshots__/remove.test.js.snap

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,34 @@ Array [
2525
]
2626
`;
2727

28+
exports[`Should remove by token instead of default auth 1`] = `
29+
Array [
30+
Array [
31+
"Removing deployment:",
32+
"test-id",
33+
],
34+
Array [
35+
"
36+
Removing using given token..",
37+
],
38+
Array [
39+
"Deployment removed!",
40+
],
41+
]
42+
`;
43+
44+
exports[`Should remove by url 1`] = `
45+
Array [
46+
Array [
47+
"Removing deployment:",
48+
"test.example.com",
49+
],
50+
Array [
51+
"Deployment removed!",
52+
],
53+
]
54+
`;
55+
2856
exports[`Should show not found error 1`] = `
2957
Array [
3058
Array [

test/remove.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const {handler: remove} = require('../src/commands/remove');
1111
const cfg = require('../src/config');
1212

1313
const id = 'test-id';
14+
const url = 'test.example.com';
1415

1516
// test removal
1617
test('Should remove', done => {
@@ -34,6 +35,46 @@ test('Should remove', done => {
3435
});
3536
});
3637

38+
test('Should remove by url', done => {
39+
const rmServer = nock('http://localhost:8080')
40+
.post(`/remove/${url}`)
41+
.reply(204);
42+
43+
const consoleSpy = sinon.spy(console, 'log');
44+
45+
remove({id: url}).then(() => {
46+
// make sure log in was successful
47+
// check that server was called
48+
expect(rmServer.isDone()).toBeTruthy();
49+
// first check console output
50+
expect(consoleSpy.args).toMatchSnapshot();
51+
// restore console
52+
console.log.restore();
53+
rmServer.done();
54+
done();
55+
});
56+
});
57+
58+
test('Should remove by token instead of default auth', done => {
59+
const rmServer = nock('http://localhost:8080')
60+
.post(`/remove/${id}`)
61+
.reply(204);
62+
63+
const consoleSpy = sinon.spy(console, 'log');
64+
65+
remove({id: id, token: 'test-token'}).then(() => {
66+
// make sure log in was successful
67+
// check that server was called
68+
expect(rmServer.isDone()).toBeTruthy();
69+
// first check console output
70+
expect(consoleSpy.args).toMatchSnapshot();
71+
// restore console
72+
console.log.restore();
73+
rmServer.done();
74+
done();
75+
});
76+
});
77+
3778
// test removal error
3879
test('Should show remove error', done => {
3980
// handle correct request

0 commit comments

Comments
 (0)