diff --git a/server/cloudcmd.js b/server/cloudcmd.js index 7ae63b1c..4fea010b 100644 --- a/server/cloudcmd.js +++ b/server/cloudcmd.js @@ -28,7 +28,6 @@ const nomine = require('nomine'); const fileop = require('@cloudcmd/fileop'); const auth = currify(_auth); -const authenticate = currify(_authenticate); const setUrl = currify(_setUrl); const root = () => config('root'); @@ -86,14 +85,6 @@ function getPrefix(prefix) { return prefix || ''; } -module.exports._authCheck = authCheck; -function authCheck(socket, success) { - if (!config('auth')) - return success(); - - socket.on('auth', authenticate(socket, success)); -} - module.exports._auth = _auth; function _auth(accept, reject, username, password) { if (!config('auth')) @@ -108,18 +99,6 @@ function _auth(accept, reject, username, password) { reject(); } -module.exports._authenticate = _authenticate; -function _authenticate(socket, success, name, pass) { - const isName = name === config('username'); - const isPass = pass === config('password'); - - if (!isName || !isPass) - return socket.emit('reject'); - - success(); - socket.emit('accept'); -} - function listen(prefix, socket) { prefix = getPrefix(prefix); diff --git a/test/server/cloudcmd.js b/test/server/cloudcmd.js index dbeb57a1..93b2000a 100644 --- a/test/server/cloudcmd.js +++ b/test/server/cloudcmd.js @@ -7,15 +7,14 @@ const currify = require('currify'); const clean = require('clear-module'); const DIR = '../../server/'; -const cloudcmd = require(DIR + 'cloudcmd'); +const cloudcmdPath = DIR + 'cloudcmd'; + +const cloudcmd = require(cloudcmdPath); const config = require(DIR + 'config'); const { - _authenticate, _getPrefix, - _authCheck, _auth, _replacePrefix, - _replaceDist, } = cloudcmd; test('cloudcmd: args: no', (t) => { @@ -93,10 +92,19 @@ test('cloudcmd: replacePrefix', (t) => { }); test('cloudcmd: replaceDist', (t) => { + const {NODE_ENV} = process.env; + process.env.NODE_ENV = 'development'; + + clean(cloudcmdPath); + + const {_replaceDist} = require(cloudcmdPath); + const url = '/dist/hello'; const result = _replaceDist(url); const expected = '/dist-dev/hello'; + process.env.NODE_ENV = NODE_ENV; + t.equal(result, expected, 'should equal'); t.end(); }); @@ -174,95 +182,6 @@ test('cloudcmd: auth: accept: no auth', (t) => { t.end(); }); -test('cloudcmd: auth: reject', (t) => { - const auth = config('auth'); - const success = sinon.stub(); - const on = sinon.stub; - const socket = { - on, - }; - - config('auth', true); - _authCheck(socket, success); - config('auth', auth); - - t.notOk(success.called, 'should not call success'); - t.end(); -}); - -test('cloudcmd: authCheck: socket', (t) => { - const auth = config('auth'); - const success = sinon.stub(); - const on = sinon.stub(); - const socket = { - on, - }; - - config('auth', true); - _authCheck(socket, success); - config('auth', auth); - - t.ok(on.calledWith('auth'), 'should call socket.on'); - t.end(); -}); - -test('cloudcmd: authCheck: success', (t) => { - const success = sinon.stub(); - const auth = config('auth'); - const on = sinon.stub(); - const socket = { - on, - }; - - config('auth', true); - _authCheck(socket, success); - config('auth', auth); - - t.notOk(success.called, 'should not call success'); - t.end(); -}); - -test('cloudcmd: authenticate: reject', (t) => { - const success = sinon.stub(); - const emit = sinon.stub(); - const socket = { - emit, - }; - - _authenticate(socket, success, 'hello', 'world'); - t.ok(emit.calledWith('reject'), 'should reject'); - t.end(); -}); - -test('cloudcmd: authenticate: reject: success', (t) => { - const success = sinon.stub(); - const emit = sinon.stub(); - const socket = { - emit, - }; - - _authenticate(socket, success, 'hello', 'world'); - t.notOk(success.called, 'should not call success'); - t.end(); -}); - -test('cloudcmd: authenticate: accept: success', (t) => { - const success = sinon.stub(); - const emit = sinon.stub(); - const socket = { - emit, - }; - - const set = credentials(); - const reset = set('hello', 'world'); - - _authenticate(socket, success, 'hello', 'world'); - reset(); - - t.ok(success.called, 'should call success'); - t.end(); -}); - function credentials() { const username = config('username'); const password = config('password'); diff --git a/test/server/config.js b/test/server/config.js index feec9df3..12b0d577 100644 --- a/test/server/config.js +++ b/test/server/config.js @@ -10,18 +10,17 @@ const sinon = diff(require('sinon')); const root = '../../'; const dir = root + 'server/'; -const config = require(dir + 'config'); +const configPath = dir + 'config'; + +const config = require(configPath); const {_cryptoPass} = config; const {apiURL} = require(root + 'common/cloudfunc'); +const clean = require('clear-module'); const pathHomeConfig = path.join(os.homedir(), '.cloudcmd.json'); const pathConfig = path.join(__dirname, '..', '..', 'json', 'config.json'); const fixture = require('./config.fixture'); -const clean = (name) => { - delete require.cache[require.resolve(name)]; -}; - function readConfig() { return readjson.sync.try(pathHomeConfig) || require(pathConfig); } @@ -58,11 +57,11 @@ test('config: manage: get', (t) => { }); test('config: manage: get: *', (t) => { - clean(dir + 'config'); + clean(configPath); - const config = require(dir + 'config'); + const config = require(configPath); const data = config('*'); - const expected = Object.assign({}, require(pathConfig), readConfig()); + const expected = readConfig(); t.deepEqual(data, expected, 'should return config data'); t.end(); diff --git a/test/console.js b/test/server/console.js similarity index 75% rename from test/console.js rename to test/server/console.js index f7a35320..baef0184 100644 --- a/test/console.js +++ b/test/server/console.js @@ -1,13 +1,22 @@ 'use strict'; +const path = require('path'); + const test = require('tape'); const io = require('socket.io-client'); -const before = require('./before'); +const configPath = path.join(__dirname, '../..', 'server', 'config'); +const before = require('../before'); +const configFn = require(configPath); test('cloudcmd: console: enabled by default', (t) => { - before({}, (port, after) => { + const config = { + auth: false + }; + + before({config}, (port, after) => { const socket = io(`http://localhost:${port}/console`); + socket.emit('auth', configFn('username'), configFn('password')); socket.once('data', (data) => { socket.close(); @@ -23,6 +32,7 @@ test('cloudcmd: console: enabled', (t) => { before({config}, (port, after) => { const socket = io(`http://localhost:${port}/console`); + socket.emit('auth', configFn('username'), configFn('password')); socket.once('data', (data) => { socket.close(); diff --git a/test/server/root.js b/test/server/root.js index 38b64046..e3dcbeb5 100644 --- a/test/server/root.js +++ b/test/server/root.js @@ -6,18 +6,24 @@ const test = require('tape'); const diff = require('sinon-called-with-diff'); const sinon = diff(require('sinon')); -const dir = path.join('..', '..', 'server'); +const dir = path.join(__dirname, '..', '..', 'server'); const pathConfig = path.join(dir, 'config'); const pathRoot = `${dir}/root`; -const stub = require('mock-require'); const clean = require('clear-module'); +const {cache, resolve} = require; +const stub = (name, exports) => { + require(name); + + const resolved = resolve(name); + cache[resolved].exports = exports; +}; + test('cloudcmd: root: config', (t) => { clean(pathRoot); - const originalConfig = require(pathConfig); const config = sinon.stub().returns(false); stub(pathConfig, config); @@ -28,7 +34,9 @@ test('cloudcmd: root: config', (t) => { t.ok(config.calledWith('root'), 'should call config'); - stub(pathConfig, originalConfig); + clean(pathConfig); + clean(pathRoot); + t.end(); }); @@ -42,8 +50,8 @@ test('cloudcmd: root: mellow', (t) => { pathToWin }; - const originalMellow = stub('mellow', mellow); - const originalConfig = stub(pathConfig, config); + stub('mellow', mellow); + stub(pathConfig, config); const root = require(pathRoot); const dir = 'hello'; @@ -53,8 +61,9 @@ test('cloudcmd: root: mellow', (t) => { t.ok(pathToWin.calledWith(dir, dirRoot), 'should call mellow'); - stub('mellow', originalMellow); - stub(pathConfig, originalConfig); + clean('mellow'); + clean(pathConfig); + clean(pathRoot); t.end(); }); diff --git a/test/server/route.js b/test/server/route.js index 3efe4155..5bfe2c79 100644 --- a/test/server/route.js +++ b/test/server/route.js @@ -7,9 +7,14 @@ const {promisify} = require('es6-promisify'); const pullout = require('pullout'); const request = require('request'); -const routePath = '../../server/route'; +const rootDir = '../..'; + +const routePath = `${rootDir}/server/route`; +const beforePath = '../before'; +const configPath = `${rootDir}/server/config`; + const route = require(routePath); -const before = require('../before'); +const before = require(beforePath); const clean = require('clear-module'); @@ -180,23 +185,6 @@ test('cloudcmd: route: keys panel', (t) => { }); }); -test('cloudcmd: route: no index', (t) => { - const name = path.join(__dirname, '../../dist-dev/index.html'); - const nameAfter = path.join(__dirname, '../../dist-dev/index1.html'); - - fs.renameSync(name, nameAfter); - - before({}, (port, after) => { - getStr(`http://localhost:${port}/`) - .then((result) => { - fs.renameSync(nameAfter, name); - t.equal(result.indexOf('ENOENT'), 0, 'should not found index.html'); - t.end(); - after(); - }); - }); -}); - test('cloudcmd: route: getIndexPath: production', (t) => { const isDev = false; const name = path.join(__dirname, '..', '..', 'dist', 'index.html'); @@ -294,6 +282,7 @@ test('cloudcmd: route: realpath: error', (t) => { clean('../before'); clean(routePath); + clean(configPath); const before = require('../before'); const root = path.join(__dirname, '..', 'fixture'); diff --git a/test/server/terminal.js b/test/server/terminal.js index a8c71bcf..8efc143c 100644 --- a/test/server/terminal.js +++ b/test/server/terminal.js @@ -5,12 +5,20 @@ const mock = require('mock-require'); const diff = require('sinon-called-with-diff'); const sinon = diff(require('sinon')); -const stub = require('mock-require'); +//const stub = require('mock-require'); const clean = require('clear-module'); const configPath = '../../server/config'; const terminalPath = '../../server/terminal'; +const {cache, resolve} = require; +const stub = (name, exports) => { + require(name); + + const resolved = resolve(name); + cache[resolved].exports = exports; +}; + test('cloudcmd: terminal: disabled', (t) => { clean(terminalPath); stub(configPath, () => { diff --git a/test/server/validate.js b/test/server/validate.js index e68b2085..6d0fca12 100644 --- a/test/server/validate.js +++ b/test/server/validate.js @@ -14,9 +14,17 @@ const exitPath = `${dir}/server/exit`; const columnsPath = `${dir}/server/columns`; const validate = require(validatePath); -const stub = require('mock-require'); +//const stub = require('mock-require'); const clear = require('clear-module'); +const {cache, resolve} = require; +const stub = (name, exports) => { + require(name); + + const resolved = resolve(name); + cache[resolved].exports = exports; +}; + test('validate: root: bad', (t) => { const config = { root: Math.random()