From 3ec4e78b4a725eed0ec848c80dbdf47069219a80 Mon Sep 17 00:00:00 2001 From: Shenoy Pratik Date: Wed, 1 May 2024 12:55:33 -0700 Subject: [PATCH] Adding test for clear cache on logout (#1794) * adding test for clear cache on logout Signed-off-by: Shenoy Pratik * update name to logout Signed-off-by: Shenoy Pratik --------- 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 70dcec943..008b6fa06 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 000000000..708be81ce --- /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 logout handler', () => { + beforeEach(() => { + jest.spyOn(window.sessionStorage, 'removeItem'); + }); + + afterEach(() => { + jest.restoreAllMocks(); + }); + + const logoutPath = { + path: SECURITY_DASHBOARDS_LOGOUT_URL, + }; + + it('Intercept logout 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 f3ed0ca34..7adf20439 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();