Skip to content

Commit 705e7cc

Browse files
authored
Merge pull request #6 from uphold/bugfix/get-token
Fix `getToken()` method
2 parents 412fa94 + f20ba48 commit 705e7cc

2 files changed

Lines changed: 28 additions & 4 deletions

File tree

src/core/sdk.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ export default class SDK {
7070
getToken() {
7171
return this.storage.getItem(this.options.accessTokenKey)
7272
.then(access_token => {
73+
if (!access_token) {
74+
this.tokenRequestPromise = null;
75+
76+
return Promise.reject();
77+
}
78+
7379
return this.storage.getItem(this.options.refreshTokenKey)
7480
.then(refresh_token => ({
7581
access_token,

test/core/sdk.spec.js

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ describe('SDK', () => {
9898
});
9999

100100
it('should throw an error if access token is empty', done => {
101+
sdk.storage.getItem.mockImplementation(key => {
102+
if (key === 'uphold.access_token') {
103+
return Promise.resolve('');
104+
}
105+
});
106+
107+
return sdk.getToken()
108+
.catch(e => {
109+
expect(e).toBeInstanceOf(AuthorizationRequiredError);
110+
done();
111+
});
112+
});
113+
114+
it('should throw an error if access token is null', done => {
101115
sdk.storage.getItem.mockReturnValue(Promise.reject());
102116

103117
return sdk.getToken()
@@ -294,16 +308,20 @@ describe('SDK', () => {
294308
});
295309

296310
it('should queue authorized requests', () => {
311+
sdk.storage = {
312+
getItem: jest.fn(() => Promise.resolve('token'))
313+
};
314+
297315
return Promise.all([
298-
sdk.authorize('code'),
299316
sdk.api('/foo'),
300317
sdk.api('/bar'),
301318
sdk.api('/biz', { authenticate: false })
302319
])
303320
.then(() => {
304-
expect(sdk.client.request.mock.calls.length).toBe(4);
305-
expect(sdk.client.request.mock.calls[0][0]).toBe('https://api.uphold.com/oauth2/token');
306-
expect(sdk.client.request.mock.calls[1][0]).toBe('https://api.uphold.com/v0/biz');
321+
expect(sdk.client.request.mock.calls.length).toBe(3);
322+
expect(sdk.client.request.mock.calls[0][0]).toBe('https://api.uphold.com/v0/biz');
323+
expect(sdk.client.request.mock.calls[1][0]).toBe('https://api.uphold.com/v0/foo');
324+
expect(sdk.client.request.mock.calls[2][0]).toBe('https://api.uphold.com/v0/bar');
307325
});
308326
});
309327
});

0 commit comments

Comments
 (0)