Skip to content

Commit

Permalink
Adding test for clear cache on logout (opensearch-project#1794)
Browse files Browse the repository at this point in the history
* adding test for clear cache on logout

Signed-off-by: Shenoy Pratik <[email protected]>

* update name to logout

Signed-off-by: Shenoy Pratik <[email protected]>

---------

Signed-off-by: Shenoy Pratik <[email protected]>
  • Loading branch information
ps48 authored May 1, 2024
1 parent f1b920c commit 3ec4e78
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions common/constants/data_sources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
57 changes: 57 additions & 0 deletions public/framework/catalog_cache/cache_intercept.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
3 changes: 2 additions & 1 deletion public/framework/catalog_cache/cache_intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
*/

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 {
return (
fetchOptions: Readonly<HttpFetchOptionsWithPath>,
_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();
Expand Down

0 comments on commit 3ec4e78

Please sign in to comment.