|
| 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').post('/update/traefik').reply(200, {updated: true}); |
| 15 | + // spy on console |
| 16 | + const consoleSpy = sinon.spy(console, 'log'); |
| 17 | + // execute login |
| 18 | + update({target: 'traefik'}).then(() => { |
| 19 | + // make sure log in was successful |
| 20 | + // check that server was called |
| 21 | + t.ok(updateServer.isDone()); |
| 22 | + // first check console output |
| 23 | + t.deepEqual( |
| 24 | + consoleSpy.args, |
| 25 | + [['Updating traefik on:', 'http://localhost:8080'], ['Successfully updated traefik!']], |
| 26 | + 'Correct log output' |
| 27 | + ); |
| 28 | + // restore console |
| 29 | + console.log.restore(); |
| 30 | + updateServer.done(); |
| 31 | + t.end(); |
| 32 | + }); |
| 33 | + }); |
| 34 | + |
| 35 | + // test update error |
| 36 | + tap.test('Should display update error', t => { |
| 37 | + // handle correct request |
| 38 | + const response = {updated: false, error: 'Test error', log: 'log'}; |
| 39 | + const updateServer = nock('http://localhost:8080').post('/update/traefik').reply(500, response); |
| 40 | + // spy on console |
| 41 | + const consoleSpy = sinon.spy(console, 'log'); |
| 42 | + // execute login |
| 43 | + update({target: 'traefik'}).then(() => { |
| 44 | + // make sure log in was successful |
| 45 | + // check that server was called |
| 46 | + t.ok(updateServer.isDone()); |
| 47 | + // first check console output |
| 48 | + t.deepEqual( |
| 49 | + consoleSpy.args, |
| 50 | + [ |
| 51 | + ['Updating traefik on:', 'http://localhost:8080'], |
| 52 | + ['Error updating traefik:', 'Test error'], |
| 53 | + ['Update log:\n'], |
| 54 | + ['log'], |
| 55 | + ], |
| 56 | + 'Correct log output' |
| 57 | + ); |
| 58 | + // restore console |
| 59 | + console.log.restore(); |
| 60 | + updateServer.done(); |
| 61 | + t.end(); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + // test deauth |
| 66 | + tap.test('Should deauth on 401', t => { |
| 67 | + // copy original config for restoration |
| 68 | + const originalConfig = Object.assign({}, userConfig); |
| 69 | + // handle correct request |
| 70 | + const updateServer = nock('http://localhost:8080').post(`/update/traefik`).reply(401); |
| 71 | + // spy on console |
| 72 | + const consoleSpy = sinon.spy(console, 'log'); |
| 73 | + // execute login |
| 74 | + update({target: 'traefik'}).then(() => { |
| 75 | + // make sure log in was successful |
| 76 | + // check that server was called |
| 77 | + t.ok(updateServer.isDone()); |
| 78 | + // first check console output |
| 79 | + t.deepEqual( |
| 80 | + consoleSpy.args, |
| 81 | + [ |
| 82 | + ['Updating traefik on:', 'http://localhost:8080'], |
| 83 | + ['Error: authorization expired!', 'Please, relogin and try again.'], |
| 84 | + ], |
| 85 | + 'Correct log output' |
| 86 | + ); |
| 87 | + // check config |
| 88 | + t.notOk(userConfig.user, 'Should not have user'); |
| 89 | + t.notOk(userConfig.token, 'Should not have token'); |
| 90 | + // restore console |
| 91 | + console.log.restore(); |
| 92 | + // tear down nock |
| 93 | + updateServer.done(); |
| 94 | + // restore original config |
| 95 | + updateConfig(originalConfig); |
| 96 | + t.end(); |
| 97 | + }); |
| 98 | + }); |
| 99 | +}; |
0 commit comments