test(config) test/config -> server/config.spec

This commit is contained in:
coderaiser 2018-08-19 18:08:57 +03:00
parent eb11192ab7
commit 46c6ca51cf
2 changed files with 24 additions and 28 deletions

View file

@ -0,0 +1,3 @@
{
"password": "9b71d224bd62f3785d96d46ad3ea3d73319bfbc2890caadae2dff72519673ca72323c3d99ba5c11d7c7acc6e14b8c5da0c4663475c2e5c3adef46f73bcdec043"
}

122
server/config.spec.js Normal file
View file

@ -0,0 +1,122 @@
'use strict';
const os = require('os');
const path = require('path');
const test = require('tape');
const readjson = require('readjson');
const diff = require('sinon-called-with-diff');
const sinon = diff(require('sinon'));
const root = '../';
const dir = './';
const configPath = './config';
const config = require(configPath);
const {_cryptoPass} = config;
const {apiURL} = require(root + 'common/cloudfunc');
const pathHomeConfig = path.join(os.homedir(), '.cloudcmd.json');
const pathConfig = path.join(__dirname, '..', 'json', 'config.json');
const fixture = require('./config.fixture');
const {connect} = require('../test/before');
test('config: manage', (t) => {
t.equal(undefined, config(), 'should return "undefined"');
t.end();
});
test('config: manage: get', async (t) => {
const editor = 'deepword';
const {done} = await connect({
config: {editor}
});
done();
t.equal(config('editor'), editor, 'should get config');
t.end();
});
test('config: manage: get', async (t) => {
const editor = 'deepword';
const conf = {
editor
};
const {done} = await connect({config: conf});
config('editor', 'dword');
done();
t.equal('dword', config('editor'), 'should set config');
t.end();
});
test('config: manage: get: *', (t) => {
const data = config('*');
const keys = Object.keys(data);
t.ok(keys.length > 1, 'should return config data');
t.end();
});
test('config: listen: no socket', (t) => {
t.throws(config.listen, 'should throw when no socket');
t.end();
});
test('config: listen: authCheck: not function', (t) => {
const socket = {};
const fn = () => config.listen(socket, 'hello');
t.throws(fn, 'should throw when authCheck not function');
t.end();
});
test('config: cryptoPass: no password', (t) => {
const json = {
hello: 'world',
};
const result = _cryptoPass(json);
t.equal(result, json, 'should not change json');
t.end();
});
test('config: cryptoPass', (t) => {
const json = {
password: 'hello',
};
const {password} = fixture;
const expected = {
password,
};
const result = _cryptoPass(json);
t.deepEqual(result, expected, 'should crypt password');
t.end();
});
test('config: middle: no', (t) => {
const {middle} = config;
const next = sinon.stub();
const res = null;
const url = `${apiURL}/config`;
const method = 'POST';
const req = {
url,
method
};
middle(req, res, next);
t.ok(next.calledWith(), 'should call next');
t.end();
});