Skip to content

Commit 5522306

Browse files
author
Crhistian
committed
🐛 access token not saved after refresh #74
1 parent afc56c2 commit 5522306

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

codegen/templates/api/Tokens.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ class Tokens {
153153
if (clientID) {
154154
try {
155155
const refreshRequest = await Auth.RefreshToken(refreshToken, clientID)
156-
return refreshRequest.access_token
156+
const accessToken = refreshRequest.access_token
157+
this.SetAccessToken(accessToken)
158+
return accessToken
157159
} catch (e) {
158160
return ''
159161
}

src/api/Tokens.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,9 @@ class Tokens {
153153
if (clientID) {
154154
try {
155155
const refreshRequest = await Auth.RefreshToken(refreshToken, clientID)
156-
return refreshRequest.access_token
156+
const accessToken = refreshRequest.access_token
157+
this.SetAccessToken(accessToken)
158+
return accessToken
157159
} catch (e) {
158160
return ''
159161
}

tests/invalid-token.test.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ beforeEach(() => {
3131
baseApiUrl: 'https://api.ordercloud.io',
3232
apiVersion: 'v1',
3333
timeoutInMilliseconds: 10 * 1000,
34-
clientID: null,
34+
clientID: undefined,
3535
})
3636
})
3737

@@ -72,7 +72,7 @@ describe('has expired access token', () => {
7272
access_token: testdata.accessTokenFromRefresh,
7373
expires_in: 32000000000,
7474
token_type: 'bearer',
75-
refresh_token: undefined,
75+
refresh_token: 'mockAccessToken',
7676
}
7777
return Promise.resolve(response)
7878
})
@@ -110,7 +110,7 @@ describe('has expired access token', () => {
110110
access_token: testdata.accessTokenFromRefresh,
111111
expires_in: 32000000000,
112112
token_type: 'bearer',
113-
refresh_token: undefined,
113+
refresh_token: 'mockAccessToken',
114114
}
115115
return Promise.resolve(response)
116116
})
@@ -165,14 +165,15 @@ describe('has no access token', () => {
165165
Configuration.Set({ clientID: testdata.clientID })
166166

167167
const GetRefreshTokenSpy = jest.spyOn(Tokens, 'GetRefreshToken')
168+
const SetAccessTokenSpy = jest.spyOn(Tokens, 'SetAccessToken')
168169
const RefreshTokenSpy = jest
169170
.spyOn(Auth, 'RefreshToken')
170171
.mockImplementationOnce(() => {
171172
const response: RequiredDeep<AccessToken> = {
172173
access_token: testdata.accessTokenFromRefresh,
173174
expires_in: 32000000000,
174175
token_type: 'bearer',
175-
refresh_token: undefined,
176+
refresh_token: 'mockAccessToken',
176177
}
177178
return Promise.resolve(response)
178179
})
@@ -186,6 +187,10 @@ describe('has no access token', () => {
186187
testdata.refreshToken,
187188
testdata.clientID
188189
)
190+
expect(SetAccessTokenSpy).toHaveBeenCalledTimes(1)
191+
expect(SetAccessTokenSpy).toHaveBeenCalledWith(
192+
testdata.accessTokenFromRefresh
193+
)
189194
expect(mockAxios.delete).toHaveBeenCalledWith(
190195
`${apiUrl}/products/${testdata.productID}`,
191196
{

0 commit comments

Comments
 (0)