Skip to content

Commit

Permalink
Mock config to fix issues in concurrent tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yamalight committed Feb 22, 2018
1 parent db2dd53 commit a85744f
Show file tree
Hide file tree
Showing 16 changed files with 222 additions and 160 deletions.
39 changes: 22 additions & 17 deletions .eslintrc
@@ -1,8 +1,5 @@
{
"extends": [
"airbnb",
"prettier"
],
"extends": ["airbnb", "prettier"],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module",
Expand All @@ -20,20 +17,28 @@
"object-curly-spacing": ["warn", "never"],
"max-len": ["error", 120, 4],
"no-confusing-arrow": "warn",
"comma-dangle": ["error", {
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "ignore"
}],
"no-underscore-dangle": "off",
"comma-dangle": [
"error",
{
"arrays": "always-multiline",
"objects": "always-multiline",
"imports": "always-multiline",
"exports": "always-multiline",
"functions": "ignore"
}
],
"react/jsx-filename-extension": "off",
"prettier/prettier": ["error", {
"trailingComma": "es5",
"singleQuote": true,
"bracketSpacing": false,
"printWidth": 120
}],
"prettier/prettier": [
"error",
{
"trailingComma": "es5",
"singleQuote": true,
"bracketSpacing": false,
"printWidth": 120
}
],
"global-require": "off",
"no-console": "off",
"no-unused-expressions": "off",
"no-param-reassign": "off",
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -5,4 +5,4 @@ coverage/
exoframe-linux
exoframe-macos
exoframe-win.exe
.idea/
.idea/
2 changes: 1 addition & 1 deletion src/commands/login.js
Expand Up @@ -32,7 +32,7 @@ exports.builder = {
},
};
exports.handler = async ({key, passphrase, url}) => {
if(url && url.length) {
if (url && url.length) {
await endpointHandler({url});
}

Expand Down
5 changes: 1 addition & 4 deletions src/config/index.js
Expand Up @@ -6,10 +6,7 @@ const yaml = require('js-yaml');
const chalk = require('chalk');

// construct paths
const baseFolder =
process.env.NODE_ENV === 'testing'
? path.join(__dirname, '..', '..', 'test', 'fixtures')
: path.join(os.homedir(), '.exoframe');
const baseFolder = path.join(os.homedir(), '.exoframe');
const configPath = path.join(baseFolder, 'cli.config.yml');

const defaultConfig = {
Expand Down
49 changes: 49 additions & 0 deletions test/__mocks__/config.js
@@ -0,0 +1,49 @@
/* eslint-env jest */
// mock config module
const cfg = jest.genMockFromModule('../../src/config/index.js');

// test config
const testConfig = {
endpoint: 'http://localhost:8080',
endpoints: [
{
endpoint: 'http://test.endpoint',
user: null,
token: null,
},
],
token: 'test-token',
user: {
username: 'admin',
},
};

// saved configs for re-use
const savedConfigs = {};

// mock config
let mockConfig = Object.assign({}, testConfig);

cfg.__save = key => {
savedConfigs[key] = Object.assign({}, mockConfig);
};
cfg.__restore = key => {
mockConfig = Object.assign({}, savedConfigs[key]);
};
cfg.updateConfig = newCfg => {
mockConfig = Object.assign(mockConfig, newCfg);
};
cfg.isLoggedIn = () => {
if (!mockConfig.user || !mockConfig.user.username) {
return false;
}
return true;
};
cfg.logout = newCfg => {
delete newCfg.user;
delete newCfg.token;
mockConfig = Object.assign({}, newCfg);
};
cfg.userConfig = mockConfig;

module.exports = cfg;
19 changes: 19 additions & 0 deletions test/__snapshots__/login.test.js.snap
Expand Up @@ -26,6 +26,25 @@ Array [
]
`;

exports[`Should login and update endpoint when endpoint was provided 1`] = `
Array [
Array [
"Updating endpoint URL to:",
"http://my-awesome-endpoint",
],
Array [
"Endpoint URL updated!",
],
Array [
"Logging in to:",
"http://my-awesome-endpoint",
],
Array [
"Successfully logged in!",
],
]
`;

exports[`Should login using key with passphrase 1`] = `
Array [
Array [
Expand Down
3 changes: 3 additions & 0 deletions test/config.test.js
@@ -1,4 +1,7 @@
/* eslint-env jest */
// mock config for testing
jest.mock('../src/config', () => require('./__mocks__/config'));

// npm packages
const fs = require('fs');
const path = require('path');
Expand Down
25 changes: 12 additions & 13 deletions test/deploy.test.js
@@ -1,4 +1,7 @@
/* eslint-env jest */
// mock config for testing
jest.mock('../src/config', () => require('./__mocks__/config'));

// npm packages
const fs = require('fs');
const path = require('path');
Expand All @@ -8,7 +11,7 @@ const _ = require('highland');
const {Readable} = require('stream');

// our packages
const {userConfig, updateConfig} = require('../src/config');
const cfg = require('../src/config');

// require deploy with stub for opn
jest.mock('opn', () => jest.fn());
Expand Down Expand Up @@ -108,15 +111,15 @@ test('Should deploy without auth but with token', done => {
// spy on console
const consoleSpy = sinon.spy(console, 'log');
// copy original config for restoration
const originalConfig = Object.assign({}, userConfig);
const originalConfig = Object.assign({}, cfg.userConfig);

// handle correct request
const deployServer = nock('http://localhost:8080')
.post('/deploy')
.reply(() => replyWithStream([{message: 'Deployment success!', deployments, level: 'info'}]));

// remove auth from config
updateConfig({endpoint: 'http://localhost:8080'});
cfg.updateConfig({endpoint: 'http://localhost:8080'});

// execute login
deploy({token: 'test-token'}).then(() => {
Expand All @@ -130,7 +133,7 @@ test('Should deploy without auth but with token', done => {
// tear down nock
deployServer.done();
// restore original config
updateConfig(originalConfig);
cfg.updateConfig(originalConfig);
done();
});
});
Expand Down Expand Up @@ -314,9 +317,9 @@ test('Should not deploy with config without project name', done => {
const consoleSpy = sinon.spy(console, 'log');

// corrupt config with string
const cfg = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'exoframe.json')));
cfg.name = '';
fs.writeFileSync(path.join(__dirname, '..', 'exoframe.json'), JSON.stringify(cfg));
const exoConfig = JSON.parse(fs.readFileSync(path.join(__dirname, '..', 'exoframe.json')));
exoConfig.name = '';
fs.writeFileSync(path.join(__dirname, '..', 'exoframe.json'), JSON.stringify(exoConfig));

// execute deploy
deploy().then(() => {
Expand Down Expand Up @@ -371,8 +374,6 @@ test('Should not deploy with non-existent path', done => {

// test
test('Should deauth on 401', done => {
// copy original config for restoration
const originalConfig = Object.assign({}, userConfig);
// handle correct request
const deployServer = nock('http://localhost:8080')
.post('/deploy')
Expand All @@ -387,14 +388,12 @@ test('Should deauth on 401', done => {
// first check console output
expect(consoleSpy.args).toMatchSnapshot();
// check config
expect(userConfig.user).toBeUndefined();
expect(userConfig.token).toBeUndefined();
expect(cfg.userConfig.user).toBeUndefined();
expect(cfg.userConfig.token).toBeUndefined();
// restore console
console.log.restore();
// tear down nock
deployServer.done();
// restore original config
updateConfig(originalConfig);
done();
});
});

0 comments on commit a85744f

Please sign in to comment.