Skip to content

Commit

Permalink
Merge branch 'master' into uiAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
tausifmuzaffar committed Jul 31, 2017
2 parents 40dd8ae + 11bcb25 commit 3f583e8
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 3 deletions.
3 changes: 3 additions & 0 deletions api/v1/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ module.exports = {
NOT_ALLOWED: 405,
},
ALL_PERCENTS_RE: /%/g,
ALL_UNDERSCORES_RE: /_/g,
COMMA: ',',
COUNT_HEADER_NAME: 'X-Total-Count',
EMPTY_STRING: '',
ESCAPED_PERCENT: '\\%',
ESCAPED_UNDERSCORE: '\\_',
FILTER_DELIMITER: ',',
FILTER_NEGATION: '-',
MINUS: '-',
Expand All @@ -43,6 +45,7 @@ module.exports = {
SEQ_IN: '$in',
SEQ_OR: '$or',
SEQ_WILDCARD: '%',
SEQ_MATCH: '_',
SLASH: '/',
statuses: {
Critical: 'Critical',
Expand Down
22 changes: 20 additions & 2 deletions api/v1/helpers/verbs/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ function escapePercentLiterals(val) {
return val;
} // escapePercentLiterals

/**
* Escapes all underscore literals so they're not treated as single-character matches.
*
* @param {String} val - The value to transform
* @returns {String} the transformed value
*/
function escapeUnderscoreLiterals(val) {
if (typeof val === 'string' || val instanceof String) {
if (val.indexOf(constants.SEQ_MATCH) > -ONE) {
return val.replace(constants.ALL_UNDERSCORES_RE, constants.ESCAPED_UNDERSCORE);
}
}

return val;
} // escapeUnderscoreLiterals

/**
* Replaces all the asterisks from the query parameter value with the
* sequelize wildcard char.
Expand Down Expand Up @@ -88,8 +104,10 @@ function toWhereClause(val, props) {
}

const clause = {};
clause[constants.SEQ_LIKE] =
toSequelizeWildcards(escapePercentLiterals(val));
val = escapePercentLiterals(val);
val = escapeUnderscoreLiterals(val);
val = toSequelizeWildcards(val);
clause[constants.SEQ_LIKE] = val;
return clause;
} // toWhereClause

Expand Down
82 changes: 81 additions & 1 deletion tests/api/v1/helpers/findUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
* tests/api/v1/helpers/findUtils.js
*/
'use strict';
const filterArrFromArr = require('../../../../api/v1/helpers/verbs/findUtils.js').filterArrFromArr;
const fu = require('../../../../api/v1/helpers/verbs/findUtils.js');
const filterArrFromArr = fu.filterArrFromArr;
const options = fu.options;
const expect = require('chai').expect;
const ZERO = 0;
const ONE = 1;
Expand Down Expand Up @@ -53,3 +55,81 @@ describe('filter subject array with tags array', () => {
expect(result).to.deep.equal([sArr[ONE]]);
});
});

describe('build options object: ', () => {
let props;
let params;

beforeEach((done) => {
props = {};
params = {
fields: {},
sort: {},
limit: {},
offset: {},
name: {},
description: {}
};
done();
});

it('no values', () => {
const opts = {
where: {},
};

expect(options(params, props)).to.deep.equal(opts);
});

it('basic values', () => {
params.fields.value = ['name', 'description'];
params.sort.value = 'name';
params.limit.value = '10';
params.offset.value = '10';
params.name.value = 'name1';
params.description.value = 'desc1';

const opts = {
attributes: ['name', 'description', 'id'],
order: ['name'],
limit: 10,
offset: 10,
where: {
name: {
$iLike: 'name1',
},
description: {
$iLike: 'desc1',
},
},
};

expect(options(params, props)).to.deep.equal(opts);
});

it('replace/escape like clause', () => {
const opts = {
where: {
name: {
$iLike: '%name%',
},
},
};

params.name.value = '*name*';
opts.where.name.$iLike = '%name%';
expect(options(params, props)).to.deep.equal(opts);

params.name.value = 'na%me';
opts.where.name.$iLike = 'na\\%me';
expect(options(params, props)).to.deep.equal(opts);

params.name.value = 'na_me';
opts.where.name.$iLike = 'na\\_me';
expect(options(params, props)).to.deep.equal(opts);

params.name.value = '*n%am_e*';
opts.where.name.$iLike = '%n\\%am\\_e%';
expect(options(params, props)).to.deep.equal(opts);
});
});

0 comments on commit 3f583e8

Please sign in to comment.