Skip to content

Commit

Permalink
Merge pull request #307 from auth0/avoid-converting-casing
Browse files Browse the repository at this point in the history
Do not change casing of the user profile object
  • Loading branch information
glena authored Jan 16, 2017
2 parents 9d9df32 + 2570c0d commit ead2e64
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ Authentication.prototype.userInfo = function (accessToken, cb) {
return this.request
.get(url)
.set('Authorization', 'Bearer ' + accessToken)
.end(responseHandler(cb));
.end(responseHandler(cb, { ignoreCasing: true }));
};

/**
Expand Down
14 changes: 12 additions & 2 deletions src/helper/object.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-param-reassign */

var assert = require('./assert');
var objectAssign = require('./object-assign');

function pick(object, keys) {
Expand Down Expand Up @@ -75,21 +76,30 @@ function snakeToCamel(str) {
}

function toSnakeCase(object, exceptions) {
if (typeof(object) !== 'object' || assert.isArray(object) || !object === null) {
return object;
}

exceptions = exceptions || [];

return Object.keys(object).reduce(function (p, key) {
var newKey = exceptions.indexOf(key) === -1 ? camelToSnake(key) : key;
p[newKey] = typeof(object[key]) === 'object' ? toSnakeCase(object[key]) : object[key];
p[newKey] = toSnakeCase(object[key]);
return p;
}, {});
}

function toCamelCase(object, exceptions) {

if (typeof(object) !== 'object' || assert.isArray(object) || !object === null) {
return object;
}

exceptions = exceptions || [];

return Object.keys(object).reduce(function (p, key) {
var newKey = exceptions.indexOf(key) === -1 ? snakeToCamel(key) : key;
p[newKey] = typeof(object[key]) === 'object' ? toCamelCase(object[key]) : object[key];
p[newKey] = toCamelCase(object[key]);
return p;
}, {});
}
Expand Down
15 changes: 13 additions & 2 deletions src/helper/response-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
var error = require('./error');
var objectHelper = require('./object');

function wrapCallback(cb) {
function wrapCallback(cb, options) {
options = options || {};
options.ignoreCasing = options.ignoreCasing ? options.ignoreCasing : false;

return function (err, data) {
var errObj;

Expand Down Expand Up @@ -49,7 +52,15 @@ function wrapCallback(cb) {
return cb(errObj);
}

return cb(null, ((data.type && data.type === 'text/html') ? data.text : objectHelper.toCamelCase(data.body || data)));
if (data.type && (data.type === 'text/html' || data.type === 'text/plain')) {
return cb(null, data.text);
}

if (options.ignoreCasing) {
return cb(null, data.body || data);
}

return cb(null, objectHelper.toCamelCase(data.body || data));
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/management/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Management.prototype.getUser = function (userId, cb) {

return this.request
.get(url)
.end(responseHandler(cb));
.end(responseHandler(cb, { ignoreCasing: true }));
};

/**
Expand All @@ -70,7 +70,7 @@ Management.prototype.patchUserMetadata = function (userId, userMetadata, cb) {
return this.request
.patch(url)
.send({ user_metadata: userMetadata })
.end(responseHandler(cb));
.end(responseHandler(cb, { ignoreCasing: true }));
};

/**
Expand All @@ -95,7 +95,7 @@ Management.prototype.linkUser = function (userId, secondaryUserToken, cb) {
return this.request
.post(url)
.send({ link_with: secondaryUserToken })
.end(responseHandler(cb));
.end(responseHandler(cb, { ignoreCasing: true }));
};

module.exports = Management;
6 changes: 3 additions & 3 deletions test/authentication/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ describe('auth0.authentication', function () {
user_id: '...',
provider: 'auth0',
connection: 'Username-Password-Authentication',
isSocial: false
is_social: false
}
});
}
Expand All @@ -228,10 +228,10 @@ describe('auth0.authentication', function () {
this.auth0.userInfo('abcd1234', function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
userId: '...',
user_id: '...',
provider: 'auth0',
connection: 'Username-Password-Authentication',
isSocial: false
is_social: false
});
done();
})
Expand Down
43 changes: 37 additions & 6 deletions test/helper/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,11 @@ describe('helpers', function () {
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrNAME3: 'attribute_3',
arrayAtt: [ 'one', 'two' ],
someObj: {
objAtt1: 'asd',
objAtt2: '123'
objAtt2: '123',
innerArrayAtt: [ 'one', 'two' ]
}
};

Expand All @@ -326,19 +328,26 @@ describe('helpers', function () {
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrNAME3: 'attribute_3',
arrayAtt: [ 'one', 'two' ],
someObj: {
objAtt1: 'asd',
objAtt2: '123'
objAtt2: '123',
innerArrayAtt: [ 'one', 'two' ]
}
});

expect(newObject.array_att).to.be.an('array');
expect(newObject.some_obj.inner_array_att).to.be.an('array');

expect(newObject).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr_name_3: 'attribute_3',
array_att: [ 'one', 'two' ],
some_obj: {
obj_att_1: 'asd',
obj_att_2: '123'
obj_att_2: '123',
inner_array_att: [ 'one', 'two' ]
}
});
});
Expand Down Expand Up @@ -367,14 +376,17 @@ describe('helpers', function () {
});

describe('toCamelCase', function () {

it('should change the casing to all the attributes', function () {
var object = {
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3',
arr_att: [ "one", "two" ],
some_obj: {
obj_att_1: 'asdf',
obj_att_2: '1234'
obj_att_2: '1234',
inner_array_att: [ 'one', 'two' ]
}
};

Expand All @@ -384,23 +396,42 @@ describe('helpers', function () {
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3',
arr_att: [ "one", "two" ],
some_obj: {
obj_att_1: 'asdf',
obj_att_2: '1234'
obj_att_2: '1234',
inner_array_att: [ 'one', 'two' ]
}
});

expect(newObject.arrAtt).to.be.an('array');
expect(newObject.someObj.innerArrayAtt).to.be.an('array');

expect(newObject).to.eql({
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrName3: 'attribute_3',
arrAtt: [ "one", "two" ],
someObj: {
objAtt1: 'asdf',
objAtt2: '1234'
objAtt2: '1234',
innerArrayAtt: [ 'one', 'two' ]
}
});
});

it.only('should not breack the string', function () {
var object = "some random string";

var newObject = objectHelper.toCamelCase(object);

expect(object).to.eql("some random string");

expect(newObject).to.be.a('string');

expect(newObject).to.eql("some random string");
});

it('should change the casing to all the attributes that are not blacklisted', function () {
var object = {
attr_name_1: 'attribute_1',
Expand Down
30 changes: 15 additions & 15 deletions test/management/management.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('auth0.Management', function () {
this.auth0.getUser('auth0|123', function(err, user) {
expect(err).to.be(null);
expect(user).to.eql({
'userId': 'auth0|123',
'user_id': 'auth0|123',
'email': 'me@example.com'
});
done();
Expand Down Expand Up @@ -182,9 +182,9 @@ describe('auth0.Management', function () {
this.auth0.patchUserMetadata('auth0|123', {role:'admin'}, function(err, user) {
expect(err).to.be(null);
expect(user).to.eql({
'userId': 'auth0|123',
'user_id': 'auth0|123',
'email': 'me@example.com',
'userMetadata': { role: 'admin' }
'user_metadata': { role: 'admin' }
});
done();
})
Expand Down Expand Up @@ -258,17 +258,17 @@ describe('auth0.Management', function () {
'connection': 'twitter',
'user_id': '191919191919191',
'provider': 'twitter',
'profileData': {
'profile_data': {
'email': '',
'email_verified': false,
'name': '',
'username': 'johndoe',
'given_name': '',
'phoneNumber': '',
'phone_number': '',
'phone_verified': false,
'family_name': ''
},
'isSocial': false,
'is_social': false,
'access_token': ''
}
]
Expand All @@ -281,20 +281,20 @@ describe('auth0.Management', function () {
expect(err).to.be(null);
expect(user).to.eql([{
'connection': 'twitter',
'userId': '191919191919191',
'user_id': '191919191919191',
'provider': 'twitter',
'profileData': {
'profile_data': {
'email': '',
'emailVerified': false,
'email_verified': false,
'name': '',
'username': 'johndoe',
'givenName': '',
'phoneNumber': '',
'phoneVerified': false,
'familyName': ''
'given_name': '',
'phone_number': '',
'phone_verified': false,
'family_name': ''
},
'isSocial': false,
'accessToken': ''
'is_social': false,
'access_token': ''
}]);
done();
})
Expand Down

0 comments on commit ead2e64

Please sign in to comment.