From ab312823b8b7199d1946d152339fbb8cbce08114 Mon Sep 17 00:00:00 2001 From: Shenoy Pratik Date: Wed, 1 May 2024 11:57:20 -0700 Subject: [PATCH] adding test for clear cache on logout Signed-off-by: Shenoy Pratik --- common/constants/data_sources.ts | 1 + .../catalog_cache/cache_intercept.test.ts | 57 +++++++++++++++++++ .../catalog_cache/cache_intercept.ts | 3 +- 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 public/framework/catalog_cache/cache_intercept.test.ts diff --git a/common/constants/data_sources.ts b/common/constants/data_sources.ts index 70dcec9435..008b6fa06d 100644 --- a/common/constants/data_sources.ts +++ b/common/constants/data_sources.ts @@ -94,3 +94,4 @@ export const OBS_DEFAULT_CLUSTER = 'observability-default'; // prefix key for ge export const OBS_S3_DATA_SOURCE = 'observability-s3'; // prefix key for generating data source id for s3 data sources in data selector export const S3_DATA_SOURCE_GROUP_DISPLAY_NAME = 'Amazon S3'; // display group name for Amazon-managed-s3 data sources in data selector export const S3_DATA_SOURCE_GROUP_SPARK_DISPLAY_NAME = 'Spark'; // display group name for OpenSearch-spark-s3 data sources in data selector +export const SECURITY_DASHBOARDS_LOGOUT_URL = '/logout'; diff --git a/public/framework/catalog_cache/cache_intercept.test.ts b/public/framework/catalog_cache/cache_intercept.test.ts new file mode 100644 index 0000000000..00038d84be --- /dev/null +++ b/public/framework/catalog_cache/cache_intercept.test.ts @@ -0,0 +1,57 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + */ + +import { SECURITY_DASHBOARDS_LOGOUT_URL } from '../../../common/constants/data_sources'; +import { + ASYNC_QUERY_ACCELERATIONS_CACHE, + ASYNC_QUERY_DATASOURCE_CACHE, +} from '../../../common/constants/shared'; +import { catalogRequestIntercept } from './cache_intercept'; + +interface LooseObject { + [key: string]: any; +} + +// Mock sessionStorage +const sessionStorageMock = (() => { + let store = {} as LooseObject; + return { + getItem(key: string) { + return store[key] || null; + }, + setItem(key: string, value: string) { + store[key] = value.toString(); + }, + removeItem(key: string) { + delete store[key]; + }, + clear() { + store = {}; + }, + }; +})(); + +Object.defineProperty(window, 'sessionStorage', { value: sessionStorageMock }); + +describe('Intercept error handler', () => { + beforeEach(() => { + jest.spyOn(window.sessionStorage, 'removeItem'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + const logoutPath = { + path: SECURITY_DASHBOARDS_LOGOUT_URL, + }; + + it('Intercept error handler should clear the cache session', () => { + const logoutInterceptFn = catalogRequestIntercept(); + logoutInterceptFn(logoutPath, null); + expect(sessionStorage.removeItem).toBeCalledWith(ASYNC_QUERY_DATASOURCE_CACHE); + expect(sessionStorage.removeItem).toBeCalledWith(ASYNC_QUERY_ACCELERATIONS_CACHE); + }); +}); diff --git a/public/framework/catalog_cache/cache_intercept.ts b/public/framework/catalog_cache/cache_intercept.ts index f3ed0ca34e..7adf204399 100644 --- a/public/framework/catalog_cache/cache_intercept.ts +++ b/public/framework/catalog_cache/cache_intercept.ts @@ -4,6 +4,7 @@ */ import { HttpFetchOptionsWithPath, IHttpInterceptController } from '../../../../../src/core/public'; +import { SECURITY_DASHBOARDS_LOGOUT_URL } from '../../../common/constants/data_sources'; import { CatalogCacheManager } from './cache_manager'; export function catalogRequestIntercept(): any { @@ -11,7 +12,7 @@ export function catalogRequestIntercept(): any { fetchOptions: Readonly, _controller: IHttpInterceptController ) => { - if (fetchOptions.path.includes('/logout')) { + if (fetchOptions.path.includes(SECURITY_DASHBOARDS_LOGOUT_URL)) { // Clears all user catalog cache details CatalogCacheManager.clearDataSourceCache(); CatalogCacheManager.clearAccelerationsCache();