Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trim username, email and phoneNumber params in every request #895

Merged
merged 7 commits into from
Jan 23, 2019
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
19 changes: 18 additions & 1 deletion src/helper/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ function getOriginFromUrl(url) {
return origin;
}

function trim(options, key) {
var trimmed = extend(options);
if (options[key]) {
trimmed[key] = options[key].trim();
}
return trimmed;
}

function trimMultiple(options, keys) {
return keys.reduce(trim, options);
}

function trimUserDetails(options) {
return trimMultiple(options, ['username', 'email', 'phoneNumber']);
}

export default {
toSnakeCase: toSnakeCase,
toCamelCase: toCamelCase,
Expand All @@ -156,5 +172,6 @@ export default {
getKeysNotIn: getKeysNotIn,
extend: extend,
getOriginFromUrl: getOriginFromUrl,
getLocationFromUrl: getLocationFromUrl
getLocationFromUrl: getLocationFromUrl,
trimUserDetails: trimUserDetails
};
3 changes: 2 additions & 1 deletion src/helper/request-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import request from 'superagent';
import base64Url from './base64_url';
import version from '../version';
import objectHelper from './object';

// ------------------------------------------------ RequestWrapper

Expand Down Expand Up @@ -45,7 +46,7 @@ RequestObj.prototype.set = function(key, value) {
};

RequestObj.prototype.send = function(body) {
this.request = this.request.send(body);
this.request = this.request.send(objectHelper.trimUserDetails(body));
return this;
};

Expand Down
80 changes: 78 additions & 2 deletions test/helper/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ describe('helpers', function() {
});

describe('extend', function() {
it('shold merge objects attributes', function() {
it('should merge objects attributes', function() {
var object1 = {
attr1: 'attribute_1',
attr2: 'attribute_2'
Expand Down Expand Up @@ -305,7 +305,7 @@ describe('helpers', function() {
});
});

it('shold merge picking attributes of both objects', function() {
it('should merge picking attributes of both objects', function() {
var object1 = {
attr1: 'attribute_1',
attr2: 'attribute_2',
Expand Down Expand Up @@ -580,4 +580,80 @@ describe('helpers', function() {
});
}
});
describe('trimUserDetails', function() {
var options;
function getTrimmed() {
return objectHelper.trimUserDetails(options);
}
beforeEach(function() {
options = {
email: ' me@example.com ',
phoneNumber: ' +16505555555 ',
username: ' johndoe '
};
});
it('should trim the username, email, and phoneNumber in an options object', function() {
expect(getTrimmed()).to.eql({
email: 'me@example.com',
phoneNumber: '+16505555555',
username: 'johndoe'
});
});
it('should not mutate the original options object', function() {
expect(options)
.to.not.equal(getTrimmed())
.and.eql({
email: ' me@example.com ',
phoneNumber: ' +16505555555 ',
username: ' johndoe '
});
});
it('should only trim username & email--not other properties', function() {
options.otherAttribute = ' stay untrimmed my friend ';
expect(getTrimmed()).to.eql({
email: 'me@example.com',
otherAttribute: ' stay untrimmed my friend ',
phoneNumber: '+16505555555',
username: 'johndoe'
});
});
it('should not fail when username, email, and/or phoneNumber are absent', function() {
options = {
attr1: 'attribute_1',
attr2: 'attribute_2',
attr3: 'attribute_3'
};
expect(getTrimmed()).to.eql(options);
options = {
attr1: 'attribute_1',
attr2: 'attribute_2',
username: ' johndoe '
};
expect(getTrimmed()).to.eql({
attr1: 'attribute_1',
attr2: 'attribute_2',
username: 'johndoe'
});
options = {
attr1: 'attribute_1',
attr2: 'attribute_2',
email: ' email@example.com '
};
expect(getTrimmed()).to.eql({
attr1: 'attribute_1',
attr2: 'attribute_2',
email: 'email@example.com'
});
options = {
attr1: 'attribute_1',
attr2: 'attribute_2',
phoneNumber: ' +16505555555 '
};
expect(getTrimmed()).to.eql({
attr1: 'attribute_1',
attr2: 'attribute_2',
phoneNumber: '+16505555555'
});
});
});
});
11 changes: 11 additions & 0 deletions test/helper/request-builder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import RequestMock from '../mock/request-mock';
import RequestBuilder from '../../src/helper/request-builder';
import base64url from '../../src/helper/base64_url';
import version from '../../src/version';
import objectHelper from '../../src/helper/object';

var telemetryInfo = new RequestBuilder({}).getTelemetryData();

Expand Down Expand Up @@ -140,6 +141,9 @@ describe('helpers requestBuilder', function() {

it('should post stuff', function() {
var req = new RequestBuilder({});
var trimUserDetailsStub = stub(objectHelper, 'trimUserDetails', function(obj) {
return obj;
});
var handler = req
.post('https://test.com')
.send({
Expand All @@ -148,6 +152,8 @@ describe('helpers requestBuilder', function() {
})
.end(function(err, data) {});

expect(trimUserDetailsStub).to.be.called;
trimUserDetailsStub.restore();
expect(handler.getMethod()).to.eql('POST');
expect(handler.getUrl()).to.eql('https://test.com');
expect(handler.getBody()).to.eql({
Expand Down Expand Up @@ -286,6 +292,9 @@ describe('helpers requestBuilder', function() {

it('should post stuff', function() {
var req = new RequestBuilder({});
var trimUserDetailsStub = stub(objectHelper, 'trimUserDetails', function(obj) {
return obj;
});
var handler = req
.post('https://test.com', { noHeaders: true })
.send({
Expand All @@ -294,6 +303,8 @@ describe('helpers requestBuilder', function() {
})
.end(function(err, data) {});

expect(trimUserDetailsStub).to.be.called;
trimUserDetailsStub.restore();
expect(handler.getMethod()).to.eql('POST');
expect(handler.getUrl()).to.eql('https://test.com');
expect(handler.getBody()).to.eql({
Expand Down