|
| 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: update} = require('../src/commands/update'); |
| 8 | +const {userConfig, updateConfig} = require('../src/config'); |
| 9 | + |
| 10 | +module.exports = () => { |
| 11 | + // test update |
| 12 | + tap.test('Should update traefik', t => { |
| 13 | + // handle correct request |
| 14 | + const updateServer = nock('http://localhost:8080') |
| 15 | + .post('/update/traefik') |
| 16 | + .reply(200, {updated: true}); |
| 17 | + // spy on console |
| 18 | + const consoleSpy = sinon.spy(console, 'log'); |
| 19 | + // execute login |
| 20 | + update({target: 'traefik'}).then(() => { |
| 21 | + // make sure log in was successful |
| 22 | + // check that server was called |
| 23 | + t.ok(updateServer.isDone()); |
| 24 | + // first check console output |
| 25 | + t.deepEqual( |
| 26 | + consoleSpy.args, |
| 27 | + [['Updating traefik on:', 'http://localhost:8080'], ['Successfully updated traefik!']], |
| 28 | + 'Correct log output' |
| 29 | + ); |
| 30 | + // restore console |
| 31 | + console.log.restore(); |
| 32 | + updateServer.done(); |
| 33 | + t.end(); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 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 | + |
| 63 | + // test update error |
| 64 | + tap.test('Should display update error', t => { |
| 65 | + // handle correct request |
| 66 | + const response = {updated: false, error: 'Test error', log: 'log'}; |
| 67 | + const updateServer = nock('http://localhost:8080') |
| 68 | + .post('/update/traefik') |
| 69 | + .reply(500, response); |
| 70 | + // spy on console |
| 71 | + const consoleSpy = sinon.spy(console, 'log'); |
| 72 | + // execute login |
| 73 | + update({target: 'traefik'}).then(() => { |
| 74 | + // make sure log in was successful |
| 75 | + // check that server was called |
| 76 | + t.ok(updateServer.isDone()); |
| 77 | + // first check console output |
| 78 | + t.deepEqual( |
| 79 | + consoleSpy.args, |
| 80 | + [ |
| 81 | + ['Updating traefik on:', 'http://localhost:8080'], |
| 82 | + ['Error updating traefik:', 'Test error'], |
| 83 | + ['Update log:\n'], |
| 84 | + ['log'], |
| 85 | + ], |
| 86 | + 'Correct log output' |
| 87 | + ); |
| 88 | + // restore console |
| 89 | + console.log.restore(); |
| 90 | + updateServer.done(); |
| 91 | + t.end(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + // test deauth |
| 96 | + tap.test('Should deauth on 401', t => { |
| 97 | + // copy original config for restoration |
| 98 | + const originalConfig = Object.assign({}, userConfig); |
| 99 | + // handle correct request |
| 100 | + const updateServer = nock('http://localhost:8080') |
| 101 | + .post(`/update/traefik`) |
| 102 | + .reply(401); |
| 103 | + // spy on console |
| 104 | + const consoleSpy = sinon.spy(console, 'log'); |
| 105 | + // execute login |
| 106 | + update({target: 'traefik'}).then(() => { |
| 107 | + // make sure log in was successful |
| 108 | + // check that server was called |
| 109 | + t.ok(updateServer.isDone()); |
| 110 | + // first check console output |
| 111 | + t.deepEqual( |
| 112 | + consoleSpy.args, |
| 113 | + [ |
| 114 | + ['Updating traefik on:', 'http://localhost:8080'], |
| 115 | + ['Error: authorization expired!', 'Please, relogin and try again.'], |
| 116 | + ], |
| 117 | + 'Correct log output' |
| 118 | + ); |
| 119 | + // check config |
| 120 | + t.notOk(userConfig.user, 'Should not have user'); |
| 121 | + t.notOk(userConfig.token, 'Should not have token'); |
| 122 | + // restore console |
| 123 | + console.log.restore(); |
| 124 | + // tear down nock |
| 125 | + updateServer.done(); |
| 126 | + // restore original config |
| 127 | + updateConfig(originalConfig); |
| 128 | + t.end(); |
| 129 | + }); |
| 130 | + }); |
| 131 | +}; |
0 commit comments