Skip to content

Commit

Permalink
Merge pull request #6 from uphold/bugfix/get-token
Browse files Browse the repository at this point in the history
Fix `getToken()` method
  • Loading branch information
ruipenso authored Aug 2, 2017
2 parents 412fa94 + f20ba48 commit 705e7cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/core/sdk.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ export default class SDK {
getToken() {
return this.storage.getItem(this.options.accessTokenKey)
.then(access_token => {
if (!access_token) {
this.tokenRequestPromise = null;

return Promise.reject();
}

return this.storage.getItem(this.options.refreshTokenKey)
.then(refresh_token => ({
access_token,
Expand Down
26 changes: 22 additions & 4 deletions test/core/sdk.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,20 @@ describe('SDK', () => {
});

it('should throw an error if access token is empty', done => {
sdk.storage.getItem.mockImplementation(key => {
if (key === 'uphold.access_token') {
return Promise.resolve('');
}
});

return sdk.getToken()
.catch(e => {
expect(e).toBeInstanceOf(AuthorizationRequiredError);
done();
});
});

it('should throw an error if access token is null', done => {
sdk.storage.getItem.mockReturnValue(Promise.reject());

return sdk.getToken()
Expand Down Expand Up @@ -294,16 +308,20 @@ describe('SDK', () => {
});

it('should queue authorized requests', () => {
sdk.storage = {
getItem: jest.fn(() => Promise.resolve('token'))
};

return Promise.all([
sdk.authorize('code'),
sdk.api('/foo'),
sdk.api('/bar'),
sdk.api('/biz', { authenticate: false })
])
.then(() => {
expect(sdk.client.request.mock.calls.length).toBe(4);
expect(sdk.client.request.mock.calls[0][0]).toBe('https://api.uphold.com/oauth2/token');
expect(sdk.client.request.mock.calls[1][0]).toBe('https://api.uphold.com/v0/biz');
expect(sdk.client.request.mock.calls.length).toBe(3);
expect(sdk.client.request.mock.calls[0][0]).toBe('https://api.uphold.com/v0/biz');
expect(sdk.client.request.mock.calls[1][0]).toBe('https://api.uphold.com/v0/foo');
expect(sdk.client.request.mock.calls[2][0]).toBe('https://api.uphold.com/v0/bar');
});
});
});
Expand Down

0 comments on commit 705e7cc

Please sign in to comment.