Skip to content

Commit

Permalink
normalize the responses to camelCase and add missing attrs on parseHa…
Browse files Browse the repository at this point in the history
…sh response
  • Loading branch information
glena committed Dec 7, 2016
1 parent e81734e commit 8eaf976
Show file tree
Hide file tree
Showing 12 changed files with 144 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/helper/error.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
function buildResponse(error, description) {
return {
error: error,
error_description: description
errorDescription: description
};
}

Expand Down
20 changes: 19 additions & 1 deletion src/helper/object.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,36 @@ function camelToSnake(str) {
return newKey;
}

function snakeToCamel(str) {
var parts = str.split('_');
return parts.reduce(function (p, c) {
return p + c.charAt(0).toUpperCase() + c.slice(1);
}, parts.shift());
}

function toSnakeCase(object, exceptions) {
exceptions = exceptions || [];

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

function toCamelCase(object, exceptions) {
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];
return p;
}, {});
}

module.exports = {
toSnakeCase: toSnakeCase,
toCamelCase: toCamelCase,
blacklist: blacklist,
merge: merge,
pick: pick,
Expand Down
7 changes: 4 additions & 3 deletions src/helper/response-handler.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var error = require('./error');
var objectHelper = require('./object');

function wrapCallback(cb) {
return function (err, data) {
Expand All @@ -14,11 +15,11 @@ function wrapCallback(cb) {
};

if (err.response && err.response.statusCode) {
errObj.status_code = err.response.statusCode;
errObj.statusCode = err.response.statusCode;
}

if (err.response && err.response.statusText) {
errObj.status_text = err.response.statusText;
errObj.statusText = err.response.statusText;
}

if (err.response && err.response.body) {
Expand All @@ -39,7 +40,7 @@ function wrapCallback(cb) {
return cb(errObj);
}

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

Expand Down
14 changes: 7 additions & 7 deletions test/authentication/authentication.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ describe('auth0.authentication', function () {
this.auth0.userInfo('abcd1234', function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
user_id: '...',
userId: '...',
provider: 'auth0',
connection: 'Username-Password-Authentication',
isSocial: false
Expand Down Expand Up @@ -261,9 +261,9 @@ describe('auth0.authentication', function () {
}, function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
'token_type': 'Bearer',
'expires_in': 36000,
'id_token': 'eyJ...'
'tokenType': 'Bearer',
'expiresIn': 36000,
'idToken': 'eyJ...'
});
done();
})
Expand Down Expand Up @@ -351,9 +351,9 @@ describe('auth0.authentication', function () {
}, function(err, data) {
expect(err).to.be(null);
expect(data).to.eql({
'token_type': 'Bearer',
'expires_in': 36000,
'id_token': 'eyJ...'
'tokenType': 'Bearer',
'expiresIn': 36000,
'idToken': 'eyJ...'
});
done();
})
Expand Down
12 changes: 6 additions & 6 deletions test/authentication/ro.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ describe('auth0.authentication', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
});
done();
});
Expand Down Expand Up @@ -147,9 +147,9 @@ describe('auth0.authentication', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
});
done();
});
Expand Down
2 changes: 1 addition & 1 deletion test/helper/iframe-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('helpers iframeHandler', function () {

expect(err).to.eql({
error:'some_error',
error_description: 'the+error+description'
errorDescription: 'the+error+description'
});

done();
Expand Down
76 changes: 73 additions & 3 deletions test/helper/object.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,33 @@ describe('helpers', function () {
var object = {
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrNAME3: 'attribute_3'
attrNAME3: 'attribute_3',
someObj: {
objAtt1: 'asd',
objAtt2: '123'
}
};

var newObject = objectHelper.toSnakeCase(object);

expect(object).to.eql({
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrNAME3: 'attribute_3'
attrNAME3: 'attribute_3',
someObj: {
objAtt1: 'asd',
objAtt2: '123'
}
});

expect(newObject).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr_name_3: 'attribute_3'
attr_name_3: 'attribute_3',
some_obj: {
obj_att_1: 'asd',
obj_att_2: '123'
}
});
});

Expand All @@ -316,4 +328,62 @@ 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',
some_obj: {
obj_att_1: 'asdf',
obj_att_2: '1234'
}
};

var newObject = objectHelper.toCamelCase(object);

expect(object).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3',
some_obj: {
obj_att_1: 'asdf',
obj_att_2: '1234'
}
});

expect(newObject).to.eql({
attrName1: 'attribute_1',
attrName22: 'attribute_2',
attrName3: 'attribute_3',
someObj: {
objAtt1: 'asdf',
objAtt2: '1234'
}
});
});

it('should change the casing to all the attributes that are not blacklisted', function () {
var object = {
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3'
};

var newObject = objectHelper.toCamelCase(object, ['attr_name_22']);

expect(object).to.eql({
attr_name_1: 'attribute_1',
attr_name_22: 'attribute_2',
attr__name_3: 'attribute_3'
});

expect(newObject).to.eql({
attrName1: 'attribute_1',
attr_name_22: 'attribute_2',
attrName3: 'attribute_3'
});
});
});
});
6 changes: 3 additions & 3 deletions test/helper/response-handler.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('helpers responseHandler', function () {
expect(data).to.be(undefined);
expect(err).to.eql({
error: 'generic_error',
error_description: 'Something went wrong'
errorDescription: 'Something went wrong'
});
done();
})(null, null);
Expand All @@ -31,8 +31,8 @@ describe('helpers responseHandler', function () {
expect(data).to.be(undefined);
expect(err).to.eql({
original: assert_err,
status_code: 400,
status_text: 'Bad request',
statusCode: 400,
statusText: 'Bad request',
code: 'the_error_code',
description: 'The error description.',
name: 'SomeName'
Expand Down
18 changes: 9 additions & 9 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({
'user_id': 'auth0|123',
'userId': 'auth0|123',
'email': '[email protected]'
});
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({
'user_id': 'auth0|123',
'userId': 'auth0|123',
'email': '[email protected]',
'user_metadata': { role: 'admin' }
'userMetadata': { role: 'admin' }
});
done();
})
Expand Down Expand Up @@ -281,20 +281,20 @@ describe('auth0.Management', function () {
expect(err).to.be(null);
expect(user).to.eql([{
'connection': 'twitter',
'user_id': '191919191919191',
'userId': '191919191919191',
'provider': 'twitter',
'profileData': {
'email': '',
'email_verified': false,
'emailVerified': false,
'name': '',
'username': 'johndoe',
'given_name': '',
'givenName': '',
'phoneNumber': '',
'phone_verified': false,
'family_name': ''
'phoneVerified': false,
'familyName': ''
},
'isSocial': false,
'access_token': ''
'accessToken': ''
}]);
done();
})
Expand Down
30 changes: 13 additions & 17 deletions test/web-auth/popup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ describe('auth0.WebAuth.popup', function () {
expect(relayUrl).to.be('https://me.auth0.com/relay.html');
expect(options).to.eql({});
cb(null, {
_id: '...',
email_verified: false,
email: '[email protected]'
});
Expand All @@ -102,8 +101,7 @@ describe('auth0.WebAuth.popup', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
_id: '...',
email_verified: false,
emailVerified: false,
email: '[email protected]'
});
done();
Expand Down Expand Up @@ -143,8 +141,7 @@ describe('auth0.WebAuth.popup', function () {
});

cb(null, {
_id: '...',
email_verified: false,
emailVerified: false,
email: '[email protected]'
});
});
Expand All @@ -159,8 +156,7 @@ describe('auth0.WebAuth.popup', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
_id: '...',
email_verified: false,
emailVerified: false,
email: '[email protected]'
});
done();
Expand Down Expand Up @@ -223,9 +219,9 @@ describe('auth0.WebAuth.popup', function () {
cb: function (cb) {
cb(null, {
body: {
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
}
});
}
Expand All @@ -242,9 +238,9 @@ describe('auth0.WebAuth.popup', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
});
done();
});
Expand Down Expand Up @@ -309,9 +305,9 @@ describe('auth0.WebAuth.popup', function () {
}, function (err, data) {
expect(err).to.be(null);
expect(data).to.eql({
id_token: 'id_token.id_token.id_token',
access_token: 'access_token',
token_type: 'bearer'
idToken: 'id_token.id_token.id_token',
accessToken: 'access_token',
tokenType: 'bearer'
});
done();
});
Expand Down Expand Up @@ -492,7 +488,7 @@ describe('auth0.WebAuth.popup', function () {
},
"code":"user_exists",
"description":"The user already exists.",
"status_code":400
"statusCode":400
});
done();
});
Expand Down
Loading

0 comments on commit 8eaf976

Please sign in to comment.