Skip to content

Commit

Permalink
added error handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Gonzalo Lopez committed Feb 7, 2025
1 parent ed627fa commit ba1b2b5
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 24 deletions.
10 changes: 7 additions & 3 deletions src/utils/getAccessToken.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@ import {getAuthData} from './oauth';
*/
export const getAccessToken = async () => {
try {
const data = await getAuthData();
const {oauthTokens} = data || {};
const {oauthTokens = {}, error} = await getAuthData();

if (!oauthTokens || !Object.keys(oauthTokens).length)
if (error) throw new Error(error);

if (oauthTokens?.constructor !== Object)
throw new Error('Invalid authentication tokens types');

if (!Object.keys(oauthTokens).length)
throw new Error('Expired authentication tokens');

const {accessToken = ''} = oauthTokens;
Expand Down
7 changes: 5 additions & 2 deletions src/utils/getUserInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import {getAuthData} from './oauth';
*/
export const getUserInfo = async () => {
try {
const {oauthTokens} = await getAuthData();
const {oauthTokens = {}, error} = await getAuthData();

if (!oauthTokens || oauthTokens.constructor !== Object) return null;
if (error) throw new Error(error);

if (oauthTokens?.constructor !== Object)
throw new Error('Invalid authentication tokens types');

if (!Object.keys(oauthTokens).length)
throw new Error('Expired authentication tokens');
Expand Down
5 changes: 3 additions & 2 deletions src/utils/oauth.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ export const userAuthorize = async (config = {}) => {
* @param {object} tokens - oauth tokens
* @returns {object} - object with user login data
*/
export const getLoginObj = (tokens) => ({
export const getLoginObj = (tokens, error = null) => ({
isLogged: !!tokens,
oauthTokens: tokens || null,
error,
});

/**
Expand All @@ -159,7 +160,7 @@ export const getAuthData = async (config = {}) => {

return getLoginObj(oauthTokens);
} catch (error) {
return getLoginObj();
return getLoginObj(null, error.message);
}
};

Expand Down
40 changes: 36 additions & 4 deletions test/utils/getAccessToken.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,40 @@ import * as oauth from '../../src/utils/oauth';

describe('getAccessToken', () => {
describe('reject with error', () => {
it.each([null, undefined, 'test', {}, false, true, {oauthTokens: {}}])(
it('when get auth data fails', async () => {
oauth.getAuthData = jest.fn().mockReturnValueOnce({
oauthTokens: {},
error: 'refresh error message',
});

await expect(getAccessToken()).rejects.toThrowError(
'refresh error message',
);
});
it.each([null, 'test', false, true])(
'error - invalid oauthTokens object',
async (invalidAuthData) => {
oauth.getAuthData = jest.fn().mockReturnValueOnce(invalidAuthData);
oauth.getAuthData = jest
.fn()
.mockReturnValueOnce({oauthTokens: invalidAuthData});

await expect(getAccessToken()).rejects.toThrowError(
'Expired authentication tokens',
'Invalid authentication tokens types',
);
},
);

it.each([null, undefined, {}, false, true, {oauthTokens: {}}])(
it('error - expired authentication tokens', async () => {
oauth.getAuthData = jest.fn().mockReturnValueOnce({
oauthTokens: undefined,
error: null,
});

await expect(getAccessToken()).rejects.toThrowError(
'Expired authentication tokens',
);
});
it.each([null, false, true])(
'error - invalid accessToken',
async (invalidAccessToken) => {
oauth.getAuthData = jest.fn().mockReturnValueOnce({
Expand All @@ -26,6 +48,16 @@ describe('getAccessToken', () => {
);
},
);

it('error - expired authentication access token when oauthTokens not contains accessToken data', async () => {
oauth.getAuthData = jest.fn().mockReturnValueOnce({
oauthTokens: {mockedToken: 'mockedToken'},
});

await expect(getAccessToken()).rejects.toThrowError(
'Expired authentication access token',
);
});
});

it('returns the accessToken', async () => {
Expand Down
32 changes: 19 additions & 13 deletions test/utils/getUserInfo.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as jwtDecode from 'jwt-decode';
import jwtDecodeUserMock from '../../mocks/jwt-decode';
import {getUserInfo} from '../../src/utils/getUserInfo';
import keys from '../../src/keys';
import * as oauth from '../../src/utils/oauth';

jest.mock('jwt-decode', () => {
const jwtDecodeMock = () => ({
Expand Down Expand Up @@ -44,30 +45,35 @@ describe('getUserInfo', () => {
expect(error.message).toBe('Expired authentication id token');
}
});
});

describe('return null when', () => {
it('obtains null oauthData or this is not an object', async () => {
jest.spyOn(AsyncStorage, 'getItem').mockReturnValueOnce(null);

const response = await getUserInfo();
try {
await getUserInfo();
} catch (error) {
expect(error.message).toBe('Invalid authentication tokens types');
}
});

it('get auth data throws an error', async () => {
oauth.getAuthData = jest.fn().mockReturnValueOnce({
error: 'refresh error message',
});

expect(response).toBeNull();
await expect(getUserInfo()).rejects.toThrowError('refresh error message');
});
});

describe('returns with', () => {
it('a correct response', async () => {
const dataMock = {idToken: 'example'};
await jest.spyOn(AsyncStorage, 'getItem').mockReturnValueOnce(dataMock);
await jest.spyOn(jwtDecode, 'default');
oauth.getAuthData = jest.fn().mockReturnValueOnce({
oauthTokens: dataMock,
});
jest.spyOn(jwtDecode, 'default');

try {
const response = await getUserInfo();
expect(response).toEqual(jwtDecodeUserMock);
} catch (error) {
console.warn(error);
}
const response = await getUserInfo();
expect(response).toEqual(jwtDecodeUserMock);
});
});
});
5 changes: 5 additions & 0 deletions test/utils/oauth.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ describe('OAuth Utils', () => {
expect(loginData).toEqual({
isLogged: true,
oauthTokens: tokensMock,
error: null,
});
});

Expand All @@ -201,6 +202,7 @@ describe('OAuth Utils', () => {
expect(loginData).toEqual({
isLogged: false,
oauthTokens: null,
error: null,
});
});
});
Expand All @@ -211,6 +213,7 @@ describe('OAuth Utils', () => {
expect(res).toEqual({
isLogged: false,
oauthTokens: null,
error: null,
});
});

Expand Down Expand Up @@ -239,6 +242,7 @@ describe('OAuth Utils', () => {
accessToken: 'access-token-1',
idToken: 'id-token-1',
},
error: null,
});
} catch (error) {
console.error(error);
Expand All @@ -264,6 +268,7 @@ describe('OAuth Utils', () => {
expect(res).toEqual({
isLogged: false,
oauthTokens: null,
error: null,
});
} catch (e) {
console.error('e', e);
Expand Down

0 comments on commit ba1b2b5

Please sign in to comment.