Skip to content
This repository has been archived by the owner on Jun 7, 2019. It is now read-only.

Make api utils tests atomic - Closes #279 #312

Merged
merged 4 commits into from
Aug 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/api/liskApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
*/
import privateApi from './privateApi';
import config from '../../config.json';
import { extend } from './utils';
import cryptoModule from '../transactions/crypto';

const LiskJS = {
Expand All @@ -55,7 +54,7 @@ function LiskAPI(providedOptions = {}) {
return new LiskAPI(providedOptions);
}

const options = extend(config.options, providedOptions);
const options = Object.assign({}, config.options, providedOptions);
const getDefaultPort = () => {
if (options.testnet) return 7000;
if (options.ssl) return 443;
Expand Down
94 changes: 42 additions & 52 deletions src/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,66 +12,56 @@
* Removal or modification of this copyright notice is prohibited.
*
*/
module.exports = {

/**
* @method trimObj
* @param obj
*
* @return trimmed object
*/
trimObj: function trimObj(obj) {
const isArray = Array.isArray(obj);
if (!isArray && typeof obj !== 'object') {
return Number.isInteger(obj)
? obj.toString()
: obj;
}
function trimObj(obj) {
const isArray = Array.isArray(obj);
if (!isArray && typeof obj !== 'object') {
return Number.isInteger(obj)
? obj.toString()
: obj;
}

const trim = value => (
typeof value === 'string'
? value.trim()
: trimObj(value)
);
const trim = value => (
typeof value === 'string'
? value.trim()
: trimObj(value)
);

return isArray
? obj.map(trim)
: Object.entries(obj)
.reduce((accumulator, [key, value]) => {
const trimmedKey = trim(key);
const trimmedValue = trim(value);
return Object.assign({}, accumulator, {
[trimmedKey]: trimmedValue,
});
}, {});
},
/**
* @method toQueryString
* @param obj
*
* @return query string
*/
toQueryString(obj) {
const parts = Object.entries(obj)
.reduce((accumulator, [key, value]) => [
...accumulator,
`${encodeURIComponent(key)}=${encodeURI(value)}`,
], []);
return isArray
? obj.map(trim)
: Object.entries(obj)
.reduce((accumulator, [key, value]) => {
const trimmedKey = trim(key);
const trimmedValue = trim(value);
return Object.assign({}, accumulator, {
[trimmedKey]: trimmedValue,
});
}, {});
}

/**
* @method toQueryString
* @param obj
*
* @return query string
*/
function toQueryString(obj) {
const parts = Object.entries(obj)
.reduce((accumulator, [key, value]) => [
...accumulator,
`${encodeURIComponent(key)}=${encodeURIComponent(value)}`,
], []);

return parts.join('&');
},
return parts.join('&');
}

/**
* Extend a JavaScript object with the key/value pairs of another.
* @method extend
* @param obj
* @param src
*
* @return obj Object
*/
extend(obj, src) {
// clone settings
const cloneObj = JSON.parse(JSON.stringify(obj));
Object.keys(src).forEach((key) => { cloneObj[key] = src[key]; });
return cloneObj;
},
module.exports = {
trimObj,
toQueryString,
};
104 changes: 45 additions & 59 deletions test/api/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,68 @@
*/
import utils from '../../src/api/utils';

describe('api utils', () => {
const port = 7000;

describe('#trimObj', () => {
const untrimmedObj = {
' my_Obj ': ' myval ',
};

const trimmedObj = {
my_Obj: 'myval', // eslint-disable-line camelcase
};

it('should not be equal before trim', () => {
(untrimmedObj).should.not.be.equal(trimmedObj);
describe('api utils module', () => {
describe('exports', () => {
it('should be an object', () => {
(utils).should.be.type('object');
});

it('should be equal after trim an Object in keys and value', () => {
const trimIt = utils.trimObj(untrimmedObj);
it('should export trimObj function', () => {
(utils).should.have.property('trimObj').be.type('function');
});

(trimIt).should.be.eql(trimmedObj);
it('should export toQueryString function', () => {
(utils).should.have.property('toQueryString').be.type('function');
});
});

it('should accept numbers and strings as value', () => {
const obj = {
myObj: 2,
};
describe('#trimObj', () => {
const { trimObj } = utils;

const trimmedObjWithNumberValue = utils.trimObj(obj);
(trimmedObjWithNumberValue).should.be.ok();
(trimmedObjWithNumberValue).should.be.eql({ myObj: '2' });
it('should not trim strings', () => {
const str = ' string ';
const trimmedString = trimObj(str);
(trimmedString).should.be.equal(str);
});
});

describe('#extend', () => {
const defaultOptions = {
testnet: false,
ssl: false,
randomPeer: true,
node: null,
port: null,
nethash: null,
bannedPeers: [],
};
it('should convert integers to strings', () => {
const trimmedInteger = trimObj(123);
(trimmedInteger).should.be.eql('123');
});

const options = {
ssl: true,
port,
testnet: true,
};
it('should convert nested integers to strings', () => {
const trimmedObject = trimObj({ myObj: 2 });
(trimmedObject).should.be.eql({ myObj: '2' });
});

it('should extend obj1 by obj2 and not modify original obj1', () => {
const result = utils.extend(defaultOptions, options);
it('should remove whitespace from keys and values', () => {
const trimmedObject = trimObj({ ' my_Obj ': ' my val ' });
(trimmedObject).should.be.eql({ my_Obj: 'my val' }); // eslint-disable-line camelcase
});

(result).should.be.eql({
testnet: true,
ssl: true,
randomPeer: true,
node: null,
port,
nethash: null,
bannedPeers: [],
});
(result).should.be.not.eql(defaultOptions);
it('should trim each member of an array', () => {
const trimmedArray = trimObj([' string ', { ' key ': ' value ' }, [' array item ']]);
(trimmedArray).should.be.eql(['string', { key: 'value' }, ['array item']]);
});
});

describe('#toQueryString', () => {
it('should create a http string from an object. Like { obj: "myval", key: "myval" } -> obj=myval&key=myval', () => {
const myObj = {
obj: 'myval',
key: 'my2ndval',
};
const { toQueryString } = utils;

const serialised = utils.toQueryString(myObj);
it('should create a query string from an object', () => {
const queryString = toQueryString({
key1: 'value1',
key2: 'value2',
key3: 'value3',
});
(queryString).should.be.equal('key1=value1&key2=value2&key3=value3');
});

(serialised).should.be.equal('obj=myval&key=my2ndval');
it('should escape invalid special characters', () => {
const queryString = toQueryString({
'key:/;?': 'value:/;?',
});
(queryString).should.be.equal('key%3A%2F%3B%3F=value%3A%2F%3B%3F');
});
});
});