Skip to content

Commit

Permalink
SCAL-245513 : Reset session info cache on init (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
sastaachar authored Mar 4, 2025
1 parent 8151bfd commit dc18c5d
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@thoughtspot/visual-embed-sdk",
"version": "1.36.1",
"version": "1.36.2",
"description": "ThoughtSpot Embed SDK",
"module": "lib/src/index.js",
"main": "dist/tsembed.js",
Expand Down
5 changes: 3 additions & 2 deletions src/auth.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import EventEmitter from 'eventemitter3';
import { getAuthenticationToken, resetCachedAuthToken } from './authToken';
import { getAuthenticationToken } from './authToken';
import { getEmbedConfig } from './embed/embedConfig';
import { initMixpanel } from './mixpanel-service';
import {
Expand All @@ -17,6 +17,7 @@ import { isActiveService } from './utils/authService/tokenizedAuthService';
import { logger } from './utils/logger';
import { getSessionInfo, getPreauthInfo } from './utils/sessionInfoService';
import { ERROR_MESSAGE } from './errors';
import { resetAllCachedServices } from './utils/resetServices';

// eslint-disable-next-line import/no-mutable-exports
export let loggedInStatus = false;
Expand Down Expand Up @@ -470,7 +471,7 @@ export const doOIDCAuth = async (embedConfig: EmbedConfig) => {
export const logout = async (embedConfig: EmbedConfig): Promise<boolean> => {
const { thoughtSpotHost } = embedConfig;
await fetchLogoutService(thoughtSpotHost);
resetCachedAuthToken();
resetAllCachedServices();
const thoughtspotIframes = document.querySelectorAll("[data-ts-iframe='true']");
if (thoughtspotIframes?.length) {
thoughtspotIframes.forEach((el) => {
Expand Down
26 changes: 26 additions & 0 deletions src/embed/base.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import EventEmitter from 'eventemitter3';
import { EmbedConfig } from '../index';
import * as auth from '../auth';
import * as authService from '../utils/authService/authService';
import * as tokenAuthServices from '../utils/authService/tokenizedAuthService';
import * as authTokenService from '../authToken';
import * as index from '../index';
import * as base from './base';
import * as embedConfigInstance from './embedConfig';
import * as resetService from '../utils/resetServices';

import {
executeAfterWait,
Expand Down Expand Up @@ -413,6 +415,19 @@ describe('Base TS Embed', () => {
expect(embedConfigInstance.getEmbedConfig().autoLogin).toBe(false);
});

test('Logout method should reset caches', async () => {
jest.spyOn(tokenAuthServices, 'fetchLogoutService').mockResolvedValueOnce({});
jest.spyOn(resetService, 'resetAllCachedServices');
index.init({
thoughtSpotHost,
authType: index.AuthType.None,
autoLogin: true,
});
expect(resetService.resetAllCachedServices).toHaveBeenCalledTimes(1);
await index.logout();
expect(resetService.resetAllCachedServices).toHaveBeenCalledTimes(2);
});

test('config sanity, no ts host', () => {
expect(() => {
index.init({
Expand Down Expand Up @@ -481,3 +496,14 @@ describe('Base without init', () => {
expect(logger.error).toHaveBeenCalledTimes(4);
});
});

describe('Init tests', () => {
test('clear caches on init', () => {
jest.spyOn(resetService, 'resetAllCachedServices');
base.init({
thoughtSpotHost,
authType: index.AuthType.None,
});
expect(resetService.resetAllCachedServices).toBeCalled();
});
});
4 changes: 2 additions & 2 deletions src/embed/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
import EventEmitter from 'eventemitter3';
import { registerReportingObserver } from '../utils/reporting';
import { resetCachedAuthToken } from '../authToken';
import { logger, setGlobalLogLevelOverride } from '../utils/logger';
import { tokenizedFetch } from '../tokenizedFetch';
import { EndPoints } from '../utils/authService/authService';
Expand All @@ -34,6 +33,7 @@ import {
import { uploadMixpanelEvent, MIXPANEL_EVENT } from '../mixpanel-service';
import { getEmbedConfig, setEmbedConfig } from './embedConfig';
import { getQueryParamString } from '../utils';
import { resetAllCachedServices } from '../utils/resetServices';

const CONFIG_DEFAULTS: Partial<EmbedConfig> = {
loginFailedMessage: 'Not logged in',
Expand Down Expand Up @@ -193,7 +193,7 @@ function backwardCompat(embedConfig: EmbedConfig): EmbedConfig {
*/
export const init = (embedConfig: EmbedConfig): AuthEventEmitter => {
sanity(embedConfig);
resetCachedAuthToken();
resetAllCachedServices();
embedConfig = setEmbedConfig(
backwardCompat({
...CONFIG_DEFAULTS,
Expand Down
15 changes: 15 additions & 0 deletions src/utils/resetServices.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import * as authToken from '../authToken';
import { resetAllCachedServices } from './resetServices';
import * as sessionInfoService from './sessionInfoService';

describe('resetAllServices', () => {
it('should reset all services', () => {
const resetCachedAuthTokenSpy = jest.spyOn(authToken, 'resetCachedAuthToken');
const resetCachedSessionInfoSpy = jest.spyOn(sessionInfoService, 'resetCachedSessionInfo');
const resetCachedPreauthInfoSpy = jest.spyOn(sessionInfoService, 'resetCachedPreauthInfo');
resetAllCachedServices();
expect(resetCachedAuthTokenSpy).toHaveBeenCalled();
expect(resetCachedSessionInfoSpy).toHaveBeenCalled();
expect(resetCachedPreauthInfoSpy).toHaveBeenCalled();
});
});
14 changes: 14 additions & 0 deletions src/utils/resetServices.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { resetCachedAuthToken } from '../authToken';
import { resetCachedPreauthInfo, resetCachedSessionInfo } from './sessionInfoService';

/**
* This function resets all the services that are cached in the SDK.
* This is to be called when the user logs out of the application and also
* when init is called again.
* @version SDK: 1.30.2 | ThoughtSpot: *
*/
export function resetAllCachedServices(): void {
resetCachedAuthToken();
resetCachedSessionInfo();
resetCachedPreauthInfo();
}
19 changes: 17 additions & 2 deletions src/utils/sessionInfoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ export async function getPreauthInfo(allowCache = true): Promise<PreauthInfo> {
}

/**
* Returns the session info object and caches it for future use.
* Returns the cached session info object and caches it for future use.
* Once fetched the session info object is cached and returned from the cache on
* subsequent calls.
* This cache is cleared when inti is called OR resetCachedSessionInfo is called.
* @example ```js
* const sessionInfo = await getSessionInfo();
* console.log(sessionInfo);
Expand Down Expand Up @@ -152,9 +153,23 @@ export const getSessionDetails = (sessionInfoResp: any): SessionInfo => {
* const sessionInfo = await getSessionInfo();
* console.log(sessionInfo);
* ```
* @version SDK: 1.28.3 | ThoughtSpot ts7.april.cl, 7.2.1
* @version SDK: 1.28.3 | ThoughtSpot: *
* @returns {void}
*/
export function resetCachedSessionInfo(): void {
sessionInfo = null;
}

/**
* Resets the cached preauth info object and forces a new fetch on the next call.
* @example ```js
* resetCachedPreauthInfo();
* const preauthInfo = await getPreauthInfo();
* console.log(preauthInfo);
* ```
* @version SDK: 1.28.3 | ThoughtSpot: *
* @returns {void}
*/
export function resetCachedPreauthInfo(): void {
preauthInfo = null;
}

0 comments on commit dc18c5d

Please sign in to comment.