|
| 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