|
| 1 | +// npm packages |
| 2 | +const tap = require('tap'); |
| 3 | +const fs = require('fs'); |
| 4 | +const path = require('path'); |
| 5 | +const yaml = require('js-yaml'); |
| 6 | +const sinon = require('sinon'); |
| 7 | +const inquirer = require('inquirer'); |
| 8 | + |
| 9 | +// our packages |
| 10 | +const {handler: updateEndpoint} = require('../src/commands/endpoint'); |
| 11 | + |
| 12 | +module.exports = () => { |
| 13 | + const configPath = path.join(__dirname, 'fixtures', 'cli.config.yml'); |
| 14 | + const mockEndpoint = 'http://test.endpoint'; |
| 15 | + |
| 16 | + // load original config |
| 17 | + const origCfg = yaml.safeLoad(fs.readFileSync(configPath, 'utf8')); |
| 18 | + |
| 19 | + // test config generation |
| 20 | + tap.test('Should add new endpoint', t => { |
| 21 | + // spy on console |
| 22 | + const consoleSpy = sinon.spy(console, 'log'); |
| 23 | + // execute login |
| 24 | + updateEndpoint({url: mockEndpoint}).then(() => { |
| 25 | + // first check console output |
| 26 | + t.deepEqual( |
| 27 | + consoleSpy.args, |
| 28 | + [['Updating endpoint URL to:', 'http://test.endpoint'], ['Endpoint URL updated!']], |
| 29 | + 'Correct log output' |
| 30 | + ); |
| 31 | + // then check config changes |
| 32 | + const cfg = yaml.safeLoad(fs.readFileSync(configPath, 'utf8')); |
| 33 | + t.equal(cfg.endpoint, mockEndpoint, 'Correct endpoint'); |
| 34 | + t.equal(cfg.user, null, 'Correct new user'); |
| 35 | + t.equal(cfg.token, null, 'Correct token'); |
| 36 | + t.equal(cfg.endpoints.length, 1, 'Correct new endpoints list'); |
| 37 | + t.equal(cfg.endpoints[0].endpoint, origCfg.endpoint, 'Correct endpoint in list'); |
| 38 | + t.equal(cfg.endpoints[0].user.username, origCfg.user.username, 'Correct user in list'); |
| 39 | + t.equal(cfg.endpoints[0].token, origCfg.token, 'Correct token in list'); |
| 40 | + // restore console |
| 41 | + console.log.restore(); |
| 42 | + t.end(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + |
| 46 | + // test config generation |
| 47 | + tap.test('Should select old endpoint', t => { |
| 48 | + // spy on console |
| 49 | + const consoleSpy = sinon.spy(console, 'log'); |
| 50 | + // stup inquirer answers |
| 51 | + sinon.stub(inquirer, 'prompt').callsFake(() => Promise.resolve({newEndpoint: origCfg.endpoint})); |
| 52 | + // execute login |
| 53 | + updateEndpoint({}).then(() => { |
| 54 | + // first check console output |
| 55 | + t.deepEqual( |
| 56 | + consoleSpy.args, |
| 57 | + [['Updating endpoint URL to:', 'http://localhost:8080'], ['Endpoint URL updated!']], |
| 58 | + 'Correct log output' |
| 59 | + ); |
| 60 | + // then check config changes |
| 61 | + const cfg = yaml.safeLoad(fs.readFileSync(configPath, 'utf8')); |
| 62 | + t.equal(cfg.endpoint, origCfg.endpoint, 'Correct endpoint'); |
| 63 | + t.equal(cfg.user.username, origCfg.user.username, 'Correct new user'); |
| 64 | + t.equal(cfg.token, origCfg.token, 'Correct token'); |
| 65 | + t.equal(cfg.endpoints.length, 1, 'Correct new endpoints list'); |
| 66 | + t.equal(cfg.endpoints[0].endpoint, mockEndpoint, 'Correct endpoint in list'); |
| 67 | + t.notOk(cfg.endpoints[0].user, 'Correct user in list'); |
| 68 | + t.notOk(cfg.endpoints[0].token, 'Correct token in list'); |
| 69 | + // restore console |
| 70 | + console.log.restore(); |
| 71 | + // restore inquirer |
| 72 | + inquirer.prompt.restore(); |
| 73 | + // restore original config |
| 74 | + fs.writeFileSync(configPath, yaml.safeDump(origCfg), 'utf8'); |
| 75 | + t.end(); |
| 76 | + }); |
| 77 | + }); |
| 78 | +}; |
0 commit comments