From d10ddea86b086dd0cd14a088858424a725d9a792 Mon Sep 17 00:00:00 2001 From: Vaggelis Yfantis Date: Wed, 18 Oct 2023 13:36:18 +0300 Subject: [PATCH] test(clerk-js): Added a mockJwtToken function for testing the token cache --- packages/clerk-js/src/core/tokenCache.test.ts | 29 +++++++++++++++---- 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/packages/clerk-js/src/core/tokenCache.test.ts b/packages/clerk-js/src/core/tokenCache.test.ts index 0174493bd3..3ab86e3f8a 100644 --- a/packages/clerk-js/src/core/tokenCache.test.ts +++ b/packages/clerk-js/src/core/tokenCache.test.ts @@ -12,8 +12,25 @@ jest.mock('./resources/Base', () => { }; }); -const jwt = - 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2NzU4NzY3OTAsImRhdGEiOiJmb29iYXIiLCJpYXQiOjE2NzU4NzY3MzB9.Z1BC47lImYvaAtluJlY-kBo0qOoAk42Xb-gNrB2SxJg'; +const mockJwtToken = (now: number): string => { + const nowInSeconds = Math.floor(now / 1000); + const payload = { + iat: nowInSeconds, + exp: nowInSeconds + 60, + data: 'foobar', + }; + + const header = { + alg: 'HS256', + typ: 'JWT', + }; + + const base64Header = Buffer.from(JSON.stringify(header)).toString('base64'); + const base64Payload = Buffer.from(JSON.stringify(payload)).toString('base64'); + const signature = 'Z1BC47lImYvaAtluJlY-kBo0qOoAk42Xb-gNrB2SxJg'; + + return `${base64Header}.${base64Payload}.${signature}`; +}; describe('MemoryTokenCache', () => { beforeAll(() => { @@ -30,7 +47,7 @@ describe('MemoryTokenCache', () => { const token = new Token({ object: 'token', id: 'foo', - jwt, + jwt: mockJwtToken(Date.now()), }); const tokenResolver = new Promise(resolve => setTimeout(() => resolve(token), 100)); @@ -57,7 +74,7 @@ describe('MemoryTokenCache', () => { const token = new Token({ object: 'token', id: 'foo', - jwt, + jwt: mockJwtToken(Date.now()), }); let isResolved = false; @@ -113,7 +130,7 @@ describe('MemoryTokenCache', () => { const token = new Token({ object: 'token', id: 'foo', - jwt, + jwt: mockJwtToken(Date.now()), }); const tokenResolver = Promise.resolve(token); @@ -138,7 +155,7 @@ describe('MemoryTokenCache', () => { const token = new Token({ object: 'token', id: 'foo', - jwt, + jwt: mockJwtToken(Date.now()), }); const tokenResolver = Promise.resolve(token);