Skip to content

Commit 67dc162

Browse files
committed
Add tests for expiration
1 parent d354663 commit 67dc162

File tree

1 file changed

+92
-24
lines changed

1 file changed

+92
-24
lines changed

packages/core/test/auth-token-manager.test.ts

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -256,42 +256,106 @@ describe('authTokenManagers', () => {
256256
})
257257

258258
describe('.getToken()', () => {
259-
let bearer: AuthTokenManager
260-
let tokenProvider: jest.Mock<Promise<AuthTokenAndExpiration>>
261-
let authToken: AuthToken
259+
describe('when token has no expiration', () => {
260+
let bearer: AuthTokenManager
261+
let tokenProvider: jest.Mock<Promise<AuthTokenAndExpiration>>
262+
let authToken: AuthToken
263+
264+
beforeEach(async () => {
265+
authToken = auth.bearer('bearer my_bear')
266+
tokenProvider = jest.fn(async () => ({ token: authToken }))
267+
bearer = authTokenManagers.bearer({ tokenProvider })
268+
})
262269

263-
beforeEach(async () => {
264-
authToken = auth.bearer('bearer my_bear')
265-
tokenProvider = jest.fn(async () => ({ token: authToken }))
266-
bearer = authTokenManagers.bearer({ tokenProvider })
267-
})
270+
it('should call tokenProvider once and return the provided token many times', async () => {
271+
await expect(bearer.getToken()).resolves.toBe(authToken)
268272

269-
it('should call tokenProvider once and return the provided token many times', async () => {
270-
await expect(bearer.getToken()).resolves.toBe(authToken)
273+
expect(tokenProvider).toHaveBeenCalledTimes(1)
271274

272-
expect(tokenProvider).toHaveBeenCalledTimes(1)
275+
await expect(bearer.getToken()).resolves.toBe(authToken)
276+
await expect(bearer.getToken()).resolves.toBe(authToken)
273277

274-
await expect(bearer.getToken()).resolves.toBe(authToken)
275-
await expect(bearer.getToken()).resolves.toBe(authToken)
278+
expect(tokenProvider).toHaveBeenCalledTimes(1)
279+
})
276280

277-
expect(tokenProvider).toHaveBeenCalledTimes(1)
281+
it.each(BEARER_HANDLED_ERROR_CODES)('should reflect the authToken refreshed by handleSecurityException(authToken, "%s")', async (code: SecurityErrorCode) => {
282+
const newAuthToken = auth.bearer('bearer another_bear')
283+
await expect(bearer.getToken()).resolves.toBe(authToken)
284+
285+
expect(tokenProvider).toHaveBeenCalledTimes(1)
286+
287+
tokenProvider.mockReturnValueOnce(Promise.resolve({ token: newAuthToken }))
288+
289+
bearer.handleSecurityException(authToken, code)
290+
expect(tokenProvider).toHaveBeenCalledTimes(2)
291+
292+
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
293+
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
294+
295+
expect(tokenProvider).toHaveBeenCalledTimes(2)
296+
})
278297
})
279298

280-
it.each(BEARER_HANDLED_ERROR_CODES)('should reflect the authToken refreshed by handleSecurityException(authToken, "%s")', async (code: SecurityErrorCode) => {
281-
const newAuthToken = auth.bearer('bearer another_bear')
282-
await expect(bearer.getToken()).resolves.toBe(authToken)
299+
describe('when token has expiration', () => {
300+
let bearer: AuthTokenManager
301+
let tokenProvider: jest.Mock<Promise<AuthTokenAndExpiration>>
302+
let authToken: AuthToken
303+
304+
beforeEach(() => {
305+
jest.useFakeTimers()
306+
authToken = auth.bearer('bearer my_bearer')
307+
tokenProvider = jest.fn(async () => ({
308+
token: authToken,
309+
expiration: expirationIn({ milliseconds: 2000 })
310+
}))
311+
bearer = authTokenManagers.bearer({ tokenProvider })
312+
})
283313

284-
expect(tokenProvider).toHaveBeenCalledTimes(1)
314+
afterEach(() => {
315+
jest.useRealTimers()
316+
})
285317

286-
tokenProvider.mockReturnValueOnce(Promise.resolve({ token: newAuthToken }))
318+
it('should call tokenProvider if the existing token is expired', async () => {
319+
await expect(bearer.getToken()).resolves.toBe(authToken)
320+
expect(tokenProvider).toHaveBeenCalledTimes(1)
287321

288-
bearer.handleSecurityException(authToken, code)
289-
expect(tokenProvider).toHaveBeenCalledTimes(2)
322+
await expect(bearer.getToken()).resolves.toBe(authToken)
323+
expect(tokenProvider).toHaveBeenCalledTimes(1)
290324

291-
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
292-
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
325+
// the time passed, the token changed
326+
jest.advanceTimersByTime(2001)
327+
const newAuthToken = auth.bearer('bearer the_other_bear')
328+
tokenProvider.mockReturnValueOnce(Promise.resolve({
329+
token: newAuthToken,
330+
expiration: expirationIn({ milliseconds: 2000 })
331+
}))
293332

294-
expect(tokenProvider).toHaveBeenCalledTimes(2)
333+
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
334+
expect(tokenProvider).toHaveBeenCalledTimes(2)
335+
336+
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
337+
expect(tokenProvider).toHaveBeenCalledTimes(2)
338+
})
339+
340+
it.each(BEARER_HANDLED_ERROR_CODES)('should reflect the authToken refreshed by handleSecurityException(authToken, "%s")', async (code: SecurityErrorCode) => {
341+
const newAuthToken = auth.bearer('bearer another_bear')
342+
await expect(bearer.getToken()).resolves.toBe(authToken)
343+
344+
expect(tokenProvider).toHaveBeenCalledTimes(1)
345+
346+
tokenProvider.mockReturnValueOnce(Promise.resolve({
347+
token: newAuthToken,
348+
expiration: expirationIn({ milliseconds: 2000 })
349+
}))
350+
351+
bearer.handleSecurityException(authToken, code)
352+
expect(tokenProvider).toHaveBeenCalledTimes(2)
353+
354+
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
355+
await expect(bearer.getToken()).resolves.toBe(newAuthToken)
356+
357+
expect(tokenProvider).toHaveBeenCalledTimes(2)
358+
})
295359
})
296360
})
297361
})
@@ -300,3 +364,7 @@ describe('authTokenManagers', () => {
300364
async function triggerEventLoop (): Promise<unknown> {
301365
return await new Promise(resolve => setTimeout(resolve, 0))
302366
}
367+
368+
function expirationIn ({ milliseconds }: { milliseconds: number}): Date {
369+
return new Date(Date.now() + milliseconds)
370+
}

0 commit comments

Comments
 (0)