Permalink
Browse files

fix(authentication): consistent throw if token not found

  • Loading branch information...
doktordirk committed May 27, 2016
1 parent 877fa50 commit 41454e3903a0be7e5a3dbdd76e3387ba95f22295
Showing with 15 additions and 2 deletions.
  1. +6 −2 src/authentication.js
  2. +9 −0 test/authentication.spec.js
@@ -186,8 +186,12 @@ export class Authentication {
}

if (typeof responseTokenProp === 'object') {
const tokenRootData = tokenRoot && tokenRoot.split('.').reduce(function(o, x) { return o[x]; }, responseTokenProp);
return tokenRootData ? tokenRootData[tokenName] : responseTokenProp[tokenName];
const tokenRootData = tokenRoot && tokenRoot.split('.').reduce((o, x) => o[x], responseTokenProp);
const token = tokenRootData ? tokenRootData[tokenName] : responseTokenProp[tokenName];

if (!token) throw new Error('Token not found in response');

return token;
}

const token = response[tokenName] === undefined ? null : response[tokenName];
@@ -306,6 +306,15 @@ describe('Authentication', () => {
)).toBe('some');
});

it('Should throw if token not found in nested', () => {
const fail = () => authentication.getTokenFromResponse(
{tokenProp: {wrongTokenName: 'some'}},
'tokenProp',
'tokenName'
);
expect(fail).toThrow();
});

it('Should return token if response has a string in tokenName in tokenRoot of tokenProp', () => {
expect(authentication.getTokenFromResponse(
{tokenProp: {tokenRoot1: {tokenRoot2: {tokenName: 'some'}}}},

0 comments on commit 41454e3

Please sign in to comment.