Skip to content

Commit 45e0027

Browse files
committed
Add update server target, closes #51
1 parent 7ae0f6a commit 45e0027

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

src/commands/update.js

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

99
// valid targets list
10-
const validTargets = ['traefik'];
10+
const validTargets = ['traefik', 'server'];
1111

1212
exports.command = ['update [target]'];
1313
exports.describe = 'update given target';
@@ -63,10 +63,10 @@ exports.handler = async ({target} = {target: 'self'}) => {
6363
return;
6464
}
6565

66-
const reason = e.response.body ? e.response.body.error : e.toString();
66+
const reason = e.response.body && e.response.body.error ? e.response.body.error : e.toString();
6767
console.log(chalk.red(`Error updating ${target}:`), reason);
6868
console.log('Update log:\n');
69-
e.response.body.log
69+
(e.response.body.log || 'No log available')
7070
.split('\n')
7171
.map(l => {
7272
try {

test/fixtures/cli.config.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
endpoint: 'http://localhost:8080'
2+
endpoints:
3+
- endpoint: 'http://test.endpoint'
4+
user: null
5+
token: null
26
token: test-token
37
user:
48
username: admin

test/update.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ module.exports = () => {
1111
// test update
1212
tap.test('Should update traefik', t => {
1313
// handle correct request
14-
const updateServer = nock('http://localhost:8080').post('/update/traefik').reply(200, {updated: true});
14+
const updateServer = nock('http://localhost:8080')
15+
.post('/update/traefik')
16+
.reply(200, {updated: true});
1517
// spy on console
1618
const consoleSpy = sinon.spy(console, 'log');
1719
// execute login
@@ -32,11 +34,39 @@ module.exports = () => {
3234
});
3335
});
3436

37+
// test update
38+
tap.test('Should update server', t => {
39+
// handle correct request
40+
const updateServer = nock('http://localhost:8080')
41+
.post('/update/server')
42+
.reply(200, {updated: true});
43+
// spy on console
44+
const consoleSpy = sinon.spy(console, 'log');
45+
// execute login
46+
update({target: 'server'}).then(() => {
47+
// make sure log in was successful
48+
// check that server was called
49+
t.ok(updateServer.isDone());
50+
// first check console output
51+
t.deepEqual(
52+
consoleSpy.args,
53+
[['Updating server on:', 'http://localhost:8080'], ['Successfully updated server!']],
54+
'Correct log output'
55+
);
56+
// restore console
57+
console.log.restore();
58+
updateServer.done();
59+
t.end();
60+
});
61+
});
62+
3563
// test update error
3664
tap.test('Should display update error', t => {
3765
// handle correct request
3866
const response = {updated: false, error: 'Test error', log: 'log'};
39-
const updateServer = nock('http://localhost:8080').post('/update/traefik').reply(500, response);
67+
const updateServer = nock('http://localhost:8080')
68+
.post('/update/traefik')
69+
.reply(500, response);
4070
// spy on console
4171
const consoleSpy = sinon.spy(console, 'log');
4272
// execute login
@@ -67,7 +97,9 @@ module.exports = () => {
6797
// copy original config for restoration
6898
const originalConfig = Object.assign({}, userConfig);
6999
// handle correct request
70-
const updateServer = nock('http://localhost:8080').post(`/update/traefik`).reply(401);
100+
const updateServer = nock('http://localhost:8080')
101+
.post(`/update/traefik`)
102+
.reply(401);
71103
// spy on console
72104
const consoleSpy = sinon.spy(console, 'log');
73105
// execute login

0 commit comments

Comments
 (0)