@@ -4,16 +4,22 @@ import { MemoryStorage, StorageKeys } from "../sessionManager";
4
4
import { setActiveStorage } from "./token" ;
5
5
import createFetchMock from "vitest-fetch-mock" ;
6
6
import { frameworkSettings } from "./exchangeAuthCode" ;
7
+ import * as refreshTokenTimer from "./refreshTimer" ;
8
+ import * as main from "../main" ;
7
9
8
10
const fetchMock = createFetchMock ( vi ) ;
9
11
10
12
describe ( "exchangeAuthCode" , ( ) => {
11
13
beforeEach ( ( ) => {
12
14
fetchMock . enableMocks ( ) ;
15
+ vi . spyOn ( refreshTokenTimer , "setRefreshTimer" ) ;
16
+ vi . spyOn ( main , "refreshToken" ) ;
17
+ vi . useFakeTimers ( ) ;
13
18
} ) ;
14
19
15
20
afterEach ( ( ) => {
16
21
fetchMock . resetMocks ( ) ;
22
+ vi . useRealTimers ( ) ;
17
23
} ) ;
18
24
19
25
it ( "missing state param" , async ( ) => {
@@ -142,10 +148,14 @@ describe("exchangeAuthCode", () => {
142
148
expect ( url ) . toBe ( "http://test.kinde.com/oauth2/token" ) ;
143
149
expect ( options ) . toMatchObject ( {
144
150
method : "POST" ,
145
- headers : {
146
- "Content-type" : "application/x-www-form-urlencoded; charset=UTF-8" ,
147
- } ,
148
151
} ) ;
152
+ expect ( ( options ?. headers as Headers ) . get ( "Content-type" ) ) . toEqual (
153
+ "application/x-www-form-urlencoded; charset=UTF-8" ,
154
+ ) ;
155
+ expect ( ( options ?. headers as Headers ) . get ( "Cache-Control" ) ) . toEqual (
156
+ "no-store" ,
157
+ ) ;
158
+ expect ( ( options ?. headers as Headers ) . get ( "Pragma" ) ) . toEqual ( "no-cache" ) ;
149
159
} ) ;
150
160
151
161
it ( "set the framework and version on header" , async ( ) => {
@@ -173,6 +183,7 @@ describe("exchangeAuthCode", () => {
173
183
access_token : "access_token" ,
174
184
refresh_token : "refresh_token" ,
175
185
id_token : "id_token" ,
186
+ expires_in : 3600 ,
176
187
} ) ,
177
188
) ;
178
189
@@ -188,11 +199,10 @@ describe("exchangeAuthCode", () => {
188
199
expect ( url ) . toBe ( "http://test.kinde.com/oauth2/token" ) ;
189
200
expect ( options ) . toMatchObject ( {
190
201
method : "POST" ,
191
- headers : {
192
- "Content-type" : "application/x-www-form-urlencoded; charset=UTF-8" ,
193
- "Kinde-SDK" : "Framework/Version" ,
194
- } ,
195
202
} ) ;
203
+ expect ( ( options ?. headers as Headers ) . get ( "Kinde-SDK" ) ) . toEqual (
204
+ "Framework/Version" ,
205
+ ) ;
196
206
} ) ;
197
207
198
208
it ( "should handle token exchange failure" , async ( ) => {
@@ -226,4 +236,50 @@ describe("exchangeAuthCode", () => {
226
236
error : "Token exchange failed: 500 - error" ,
227
237
} ) ;
228
238
} ) ;
239
+
240
+ it ( "should set the refresh timer" , async ( ) => {
241
+ const store = new MemoryStorage ( ) ;
242
+ setActiveStorage ( store ) ;
243
+
244
+ const state = "state" ;
245
+
246
+ await store . setItems ( {
247
+ [ StorageKeys . state ] : state ,
248
+ } ) ;
249
+
250
+ frameworkSettings . framework = "Framework" ;
251
+ frameworkSettings . frameworkVersion = "Version" ;
252
+
253
+ const input = "hello" ;
254
+
255
+ const urlParams = new URLSearchParams ( ) ;
256
+ urlParams . append ( "code" , input ) ;
257
+ urlParams . append ( "state" , state ) ;
258
+ urlParams . append ( "client_id" , "test" ) ;
259
+
260
+ fetchMock . mockResponseOnce (
261
+ JSON . stringify ( {
262
+ access_token : "access_token" ,
263
+ refresh_token : "refresh_token" ,
264
+ id_token : "id_token" ,
265
+ expires_in : 3600 ,
266
+ } ) ,
267
+ ) ;
268
+
269
+ await exchangeAuthCode ( {
270
+ urlParams,
271
+ domain : "http://test.kinde.com" ,
272
+ clientId : "test" ,
273
+ redirectURL : "http://test.kinde.com" ,
274
+ autoReferesh : true ,
275
+ } ) ;
276
+
277
+ expect ( refreshTokenTimer . setRefreshTimer ) . toHaveBeenCalledOnce ( ) ;
278
+ expect ( refreshTokenTimer . setRefreshTimer ) . toHaveBeenCalledWith (
279
+ 3600 ,
280
+ expect . any ( Function ) ,
281
+ ) ;
282
+ vi . advanceTimersByTime ( 3600 * 1000 ) ;
283
+ expect ( main . refreshToken ) . toHaveBeenCalledTimes ( 1 ) ;
284
+ } ) ;
229
285
} ) ;
0 commit comments