Skip to content

[Backport 2.x] Adding test for clear cache on logout #1797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

Check warning on line 14 in public/framework/catalog_cache/cache_intercept.test.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
}

// 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 {

Check warning on line 10 in public/framework/catalog_cache/cache_intercept.ts

View workflow job for this annotation

GitHub Actions / Lint

Unexpected any. Specify a different type
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
Loading