From 8f8b0c52e908be4005d9707727a5b9a92d4a1023 Mon Sep 17 00:00:00 2001 From: Jainam Tushar Sheth Date: Thu, 4 May 2023 16:41:18 -0400 Subject: [PATCH] Rename old isTokenValid to isTokenExpired --- .../app/commerce-api/auth.js | 4 ++-- .../app/commerce-api/index.js | 2 +- .../app/commerce-api/index.test.js | 2 +- .../app/commerce-api/utils.js | 8 ++++---- .../app/commerce-api/utils.test.js | 18 +++++++++--------- .../app/hooks/use-auth-modal.test.js | 2 +- .../app/pages/checkout/index.test.js | 2 +- .../app/pages/login/index.test.js | 2 +- .../app/pages/registration/index.test.jsx | 2 +- .../template-retail-react-app/jest-setup.js | 4 ++-- 10 files changed, 23 insertions(+), 23 deletions(-) diff --git a/packages/template-retail-react-app/app/commerce-api/auth.js b/packages/template-retail-react-app/app/commerce-api/auth.js index c94ce5ade7..576260a0c4 100644 --- a/packages/template-retail-react-app/app/commerce-api/auth.js +++ b/packages/template-retail-react-app/app/commerce-api/auth.js @@ -9,7 +9,7 @@ import {getAppOrigin} from 'pwa-kit-react-sdk/utils/url' import {HTTPError} from 'pwa-kit-react-sdk/ssr/universal/errors' import {createCodeVerifier, generateCodeChallenge} from './pkce' -import {isTokenValid, createGetTokenBody, hasSFRAAuthStateChanged} from './utils' +import {isTokenExpired, createGetTokenBody, hasSFRAAuthStateChanged} from './utils' import { usidStorageKey, cidStorageKey, @@ -142,7 +142,7 @@ class Auth { get isTokenValid() { return ( - isTokenValid(this.authToken) && + !isTokenExpired(this.authToken) && !hasSFRAAuthStateChanged(this._storage, this._storageCopy) ) } diff --git a/packages/template-retail-react-app/app/commerce-api/index.js b/packages/template-retail-react-app/app/commerce-api/index.js index 4c16e5f03b..9f7d9bfe88 100644 --- a/packages/template-retail-react-app/app/commerce-api/index.js +++ b/packages/template-retail-react-app/app/commerce-api/index.js @@ -10,7 +10,7 @@ import * as sdk from 'commerce-sdk-isomorphic' import {getAppOrigin} from 'pwa-kit-react-sdk/utils/url' import ShopperBaskets from './shopper-baskets' import OcapiShopperOrders from './ocapi-shopper-orders' -import {getTenantId, isError, isTokenValid} from './utils' +import {getTenantId, isError} from './utils' import Auth from './auth' import EinsteinAPI from './einstein' diff --git a/packages/template-retail-react-app/app/commerce-api/index.test.js b/packages/template-retail-react-app/app/commerce-api/index.test.js index a37bec8b81..7fc834c5ae 100644 --- a/packages/template-retail-react-app/app/commerce-api/index.test.js +++ b/packages/template-retail-react-app/app/commerce-api/index.test.js @@ -232,7 +232,7 @@ describe('CommerceAPI', () => { }) test('Use same customer if token is valid', async () => { const Utils = require('./utils') - jest.spyOn(Utils, 'isTokenValid').mockReturnValue(true) + jest.spyOn(Utils, 'isTokenExpired').mockReturnValue(false) const _CommerceAPI = require('./index').default const api = new _CommerceAPI(apiConfig) diff --git a/packages/template-retail-react-app/app/commerce-api/utils.js b/packages/template-retail-react-app/app/commerce-api/utils.js index f3eb279c2b..c7655ddaa8 100644 --- a/packages/template-retail-react-app/app/commerce-api/utils.js +++ b/packages/template-retail-react-app/app/commerce-api/utils.js @@ -17,18 +17,18 @@ import fetch from 'cross-fetch' * @param {string} token - The JWT bearer token to be inspected * @returns {boolean} */ -export function isTokenValid(token) { +export function isTokenExpired(token) { if (!token) { - return false + return true } const {exp, iat} = jwtDecode(token.replace('Bearer ', '')) const validTimeSeconds = exp - iat - 60 const tokenAgeSeconds = Date.now() / 1000 - iat if (validTimeSeconds > tokenAgeSeconds) { - return true + return false } - return false + return true } // Returns fomrulated body for SopperLogin getToken endpoint diff --git a/packages/template-retail-react-app/app/commerce-api/utils.test.js b/packages/template-retail-react-app/app/commerce-api/utils.test.js index b47d87b016..bad374557f 100644 --- a/packages/template-retail-react-app/app/commerce-api/utils.test.js +++ b/packages/template-retail-react-app/app/commerce-api/utils.test.js @@ -7,7 +7,7 @@ import jwt from 'njwt' import { camelCaseKeysToUnderscore, - isTokenValid, + isTokenExpired, keysToCamel, convertSnakeCaseToSentenceCase, handleAsyncError, @@ -27,20 +27,20 @@ jest.mock('./utils', () => { } }) -describe('isTokenValid', () => { - test('returns false when no token given', () => { - expect(isTokenValid()).toBe(false) +describe('isTokenExpired', () => { + test('returns true when no token given', () => { + expect(isTokenExpired()).toBe(true) }) - test('returns true for valid token', () => { + test('returns false for valid token', () => { const token = createJwt(600) const bearerToken = `Bearer ${token}` - expect(isTokenValid(token)).toBe(true) - expect(isTokenValid(bearerToken)).toBe(true) + expect(isTokenExpired(token)).toBe(false) + expect(isTokenExpired(bearerToken)).toBe(false) }) - test('returns false if token expires within 60 econds', () => { - expect(isTokenValid(createJwt(59))).toBe(false) + test('returns true if token expires within 60 econds', () => { + expect(isTokenExpired(createJwt(59))).toBe(true) }) }) diff --git a/packages/template-retail-react-app/app/hooks/use-auth-modal.test.js b/packages/template-retail-react-app/app/hooks/use-auth-modal.test.js index 8639143ce9..6d3d86a4aa 100644 --- a/packages/template-retail-react-app/app/hooks/use-auth-modal.test.js +++ b/packages/template-retail-react-app/app/hooks/use-auth-modal.test.js @@ -47,7 +47,7 @@ jest.mock('../commerce-api/utils', () => { const originalModule = jest.requireActual('../commerce-api/utils') return { ...originalModule, - isTokenValid: jest.fn().mockReturnValue(true), + isTokenExpired: jest.fn().mockReturnValue(false), hasSFRAAuthStateChanged: jest.fn().mockReturnValue(false), createGetTokenBody: jest.fn().mockReturnValue({ grantType: 'test', diff --git a/packages/template-retail-react-app/app/pages/checkout/index.test.js b/packages/template-retail-react-app/app/pages/checkout/index.test.js index 1a83342bae..4740639340 100644 --- a/packages/template-retail-react-app/app/pages/checkout/index.test.js +++ b/packages/template-retail-react-app/app/pages/checkout/index.test.js @@ -38,7 +38,7 @@ jest.mock('../../commerce-api/utils', () => { const originalModule = jest.requireActual('../../commerce-api/utils') return { ...originalModule, - isTokenValid: jest.fn().mockReturnValue(true), + isTokenExpired: jest.fn().mockReturnValue(false), hasSFRAAuthStateChanged: jest.fn().mockReturnValue(false), createGetTokenBody: jest.fn().mockReturnValue({ grantType: 'test', diff --git a/packages/template-retail-react-app/app/pages/login/index.test.js b/packages/template-retail-react-app/app/pages/login/index.test.js index 971cf1aa75..1c1e552b49 100644 --- a/packages/template-retail-react-app/app/pages/login/index.test.js +++ b/packages/template-retail-react-app/app/pages/login/index.test.js @@ -43,7 +43,7 @@ jest.mock('../../commerce-api/utils', () => { const originalModule = jest.requireActual('../../commerce-api/utils') return { ...originalModule, - isTokenValid: jest.fn().mockReturnValue(true), + isTokenExpired: jest.fn().mockReturnValue(false), hasSFRAAuthStateChanged: jest.fn().mockReturnValue(false), createGetTokenBody: jest.fn().mockReturnValue({ grantType: 'test', diff --git a/packages/template-retail-react-app/app/pages/registration/index.test.jsx b/packages/template-retail-react-app/app/pages/registration/index.test.jsx index f80ca14c7a..46367574bb 100644 --- a/packages/template-retail-react-app/app/pages/registration/index.test.jsx +++ b/packages/template-retail-react-app/app/pages/registration/index.test.jsx @@ -47,7 +47,7 @@ jest.mock('../../commerce-api/utils', () => { const originalModule = jest.requireActual('../../commerce-api/utils') return { ...originalModule, - isTokenValid: jest.fn().mockReturnValue(true), + isTokenExpired: jest.fn().mockReturnValue(false), hasSFRAAuthStateChanged: jest.fn().mockReturnValue(false), createGetTokenBody: jest.fn().mockReturnValue({ grantType: 'test', diff --git a/packages/template-retail-react-app/jest-setup.js b/packages/template-retail-react-app/jest-setup.js index 686886cb62..e57453f4e7 100644 --- a/packages/template-retail-react-app/jest-setup.js +++ b/packages/template-retail-react-app/jest-setup.js @@ -84,12 +84,12 @@ jest.mock('pwa-kit-runtime/utils/ssr-config', () => { } }) -// Mock isTokenValid globally +// Mock isTokenExpired globally jest.mock('./app/commerce-api/utils', () => { const originalModule = jest.requireActual('./app/commerce-api/utils') return { ...originalModule, - isTokenValid: jest.fn().mockReturnValue(true) + isTokenExpired: jest.fn().mockReturnValue(false) } })