@thoughtspot/visual-embed-sdk 1.29.0-alpha.3 → 1.29.0-alpha.authRefactor

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (75) hide show
  1. package/cjs/package.json +1 -2
  2. package/cjs/src/embed/liveboard.js +1 -1
  3. package/cjs/src/embed/liveboard.js.map +1 -1
  4. package/cjs/src/embed/sage.d.ts +1 -1
  5. package/cjs/src/embed/ts-embed.d.ts.map +1 -1
  6. package/cjs/src/embed/ts-embed.js +0 -9
  7. package/cjs/src/embed/ts-embed.js.map +1 -1
  8. package/cjs/src/embed/ts-embed.spec.js +0 -18
  9. package/cjs/src/embed/ts-embed.spec.js.map +1 -1
  10. package/cjs/src/types.d.ts +1 -22
  11. package/cjs/src/types.d.ts.map +1 -1
  12. package/cjs/src/types.js +0 -3
  13. package/cjs/src/types.js.map +1 -1
  14. package/cjs/src/utils/sessionInfoService.d.ts +66 -0
  15. package/cjs/src/utils/sessionInfoService.d.ts.map +1 -0
  16. package/cjs/src/utils/sessionInfoService.js +92 -0
  17. package/cjs/src/utils/sessionInfoService.js.map +1 -0
  18. package/dist/src/embed/sage.d.ts +1 -1
  19. package/dist/src/embed/ts-embed.d.ts.map +1 -1
  20. package/dist/src/types.d.ts +1 -22
  21. package/dist/src/types.d.ts.map +1 -1
  22. package/dist/tsembed-react.es.js +2 -14
  23. package/dist/tsembed-react.js +2 -14
  24. package/dist/tsembed.es.js +2 -14
  25. package/dist/tsembed.js +2 -14
  26. package/dist/visual-embed-sdk-react-full.d.ts +2 -23
  27. package/dist/visual-embed-sdk-react.d.ts +2 -23
  28. package/dist/visual-embed-sdk.d.ts +2 -23
  29. package/lib/package.json +1 -2
  30. package/lib/src/embed/liveboard.js +1 -1
  31. package/lib/src/embed/liveboard.js.map +1 -1
  32. package/lib/src/embed/sage.d.ts +1 -1
  33. package/lib/src/embed/ts-embed.d.ts.map +1 -1
  34. package/lib/src/embed/ts-embed.js +0 -9
  35. package/lib/src/embed/ts-embed.js.map +1 -1
  36. package/lib/src/embed/ts-embed.spec.js +0 -18
  37. package/lib/src/embed/ts-embed.spec.js.map +1 -1
  38. package/lib/src/types.d.ts +1 -22
  39. package/lib/src/types.d.ts.map +1 -1
  40. package/lib/src/types.js +0 -3
  41. package/lib/src/types.js.map +1 -1
  42. package/lib/src/utils/sessionInfoService.d.ts +66 -0
  43. package/lib/src/utils/sessionInfoService.d.ts.map +1 -0
  44. package/lib/src/utils/sessionInfoService.js +85 -0
  45. package/lib/src/utils/sessionInfoService.js.map +1 -0
  46. package/lib/src/visual-embed-sdk.d.ts +2 -23
  47. package/package.json +1 -3
  48. package/src/auth.spec.ts +66 -72
  49. package/src/auth.ts +43 -59
  50. package/src/authToken.ts +10 -4
  51. package/src/embed/app.spec.ts +4 -2
  52. package/src/embed/base.spec.ts +1 -0
  53. package/src/embed/base.ts +3 -0
  54. package/src/embed/embed.spec.ts +2 -0
  55. package/src/embed/events.spec.ts +2 -0
  56. package/src/embed/liveboard.spec.ts +2 -0
  57. package/src/embed/pinboard.spec.ts +2 -0
  58. package/src/embed/sage.spec.ts +3 -0
  59. package/src/embed/sage.ts +1 -1
  60. package/src/embed/search.spec.ts +1 -0
  61. package/src/embed/ts-embed-trigger.spec.ts +3 -0
  62. package/src/embed/ts-embed.spec.ts +8 -22
  63. package/src/embed/ts-embed.ts +1 -9
  64. package/src/index.ts +2 -1
  65. package/src/mixpanel-service.spec.ts +4 -3
  66. package/src/mixpanel-service.ts +3 -1
  67. package/src/react/index.spec.tsx +7 -0
  68. package/src/react/index.tsx +1 -0
  69. package/src/types.ts +0 -21
  70. package/src/utils/authService/authService.spec.ts +9 -4
  71. package/src/utils/authService/authService.ts +1 -0
  72. package/src/utils/authService/tokenizedAuthService.ts +38 -8
  73. package/src/utils/processData.spec.ts +3 -2
  74. package/src/utils/processData.ts +1 -3
  75. package/src/utils/sessionInfoService.ts +101 -0
@@ -0,0 +1,66 @@
1
+ export type SessionInfo = {
2
+ releaseVersion: string;
3
+ userGUID: string;
4
+ currentOrgId: number;
5
+ privileges: string[];
6
+ mixpanelToken: string;
7
+ isPublicUser: boolean;
8
+ clusterId: string;
9
+ clusterName: string;
10
+ [key: string]: any;
11
+ };
12
+ /**
13
+ * Returns the session info object and caches it for future use.
14
+ * Once fetched the session info object is cached and returned from the cache on
15
+ * subsequent calls.
16
+ *
17
+ * @example ```js
18
+ * const sessionInfo = await getSessionInfo();
19
+ * console.log(sessionInfo);
20
+ * ```
21
+ * @version SDK: 1.28.3 | ThoughtSpot: *
22
+ * @returns {Promise<SessionInfo>} The session info object.
23
+ */
24
+ export declare function getSessionInfo(): Promise<SessionInfo>;
25
+ /**
26
+ * Returns the cached session info object. If the client is not authenticated the
27
+ * function will return null.
28
+ *
29
+ * @example ```js
30
+ * const sessionInfo = getSessionInfoSync();
31
+ * if (sessionInfo) {
32
+ * console.log(sessionInfo);
33
+ * } else {
34
+ * console.log('Not authenticated');
35
+ * }
36
+ * ```
37
+ * @returns {SessionInfo | null} The session info object.
38
+ * @version SDK: 1.28.3 | ThoughtSpot: *
39
+ */
40
+ export declare function getSessionInfoSync(): SessionInfo | null;
41
+ /**
42
+ * Processes the session info response and returns the session info object.
43
+ *
44
+ * @param sessionInfoResp {any} Response from the session info API.
45
+ * @returns {SessionInfo} The session info object.
46
+ * @example ```js
47
+ * const sessionInfoResp = await fetch(sessionInfoPath);
48
+ * const sessionInfo = getSessionDetails(sessionInfoResp);
49
+ * console.log(sessionInfo);
50
+ * ```
51
+ * @version SDK: 1.28.3 | ThoughtSpot: *
52
+ */
53
+ export declare const getSessionDetails: (sessionInfoResp: any) => SessionInfo;
54
+ /**
55
+ * Resets the cached session info object and forces a new fetch on the next call.
56
+ *
57
+ * @example ```js
58
+ * resetCachedSessionInfo();
59
+ * const sessionInfo = await getSessionInfo();
60
+ * console.log(sessionInfo);
61
+ * ```
62
+ * @version SDK: 1.28.3 | ThoughtSpot ts7.april.cl, 7.2.1
63
+ * @returns {void}
64
+ */
65
+ export declare function resetCachedSessionInfo(): void;
66
+ //# sourceMappingURL=sessionInfoService.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionInfoService.d.ts","sourceRoot":"","sources":["../../../src/utils/sessionInfoService.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,WAAW,GAAG;IACtB,cAAc,EAAE,MAAM,CAAC;IACvB,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,OAAO,CAAC;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,WAAW,EAAE,MAAM,CAAC;IACpB,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAC;CACtB,CAAC;AAIF;;;;;;;;;;;GAWG;AACH,wBAAsB,cAAc,IAAI,OAAO,CAAC,WAAW,CAAC,CAQ3D;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,kBAAkB,IAAI,WAAW,GAAG,IAAI,CAEvD;AAED;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,iBAAiB,oBAAqB,GAAG,KAAG,WAexD,CAAC;AAEF;;;;;;;;;;GAUG;AACH,wBAAgB,sBAAsB,IAAI,IAAI,CAE7C"}
@@ -0,0 +1,85 @@
1
+ import { getEmbedConfig } from '../embed/embedConfig';
2
+ import { fetchSessionInfoService } from './authService';
3
+ let sessionInfo = null;
4
+ /**
5
+ * Returns the session info object and caches it for future use.
6
+ * Once fetched the session info object is cached and returned from the cache on
7
+ * subsequent calls.
8
+ *
9
+ * @example ```js
10
+ * const sessionInfo = await getSessionInfo();
11
+ * console.log(sessionInfo);
12
+ * ```
13
+ * @version SDK: 1.28.3 | ThoughtSpot: *
14
+ * @returns {Promise<SessionInfo>} The session info object.
15
+ */
16
+ export async function getSessionInfo() {
17
+ if (!sessionInfo) {
18
+ const host = getEmbedConfig().thoughtSpotHost;
19
+ const sessionResponse = await fetchSessionInfoService(host);
20
+ const processedSessionInfo = getSessionDetails(sessionResponse);
21
+ sessionInfo = processedSessionInfo;
22
+ }
23
+ return sessionInfo;
24
+ }
25
+ /**
26
+ * Returns the cached session info object. If the client is not authenticated the
27
+ * function will return null.
28
+ *
29
+ * @example ```js
30
+ * const sessionInfo = getSessionInfoSync();
31
+ * if (sessionInfo) {
32
+ * console.log(sessionInfo);
33
+ * } else {
34
+ * console.log('Not authenticated');
35
+ * }
36
+ * ```
37
+ * @returns {SessionInfo | null} The session info object.
38
+ * @version SDK: 1.28.3 | ThoughtSpot: *
39
+ */
40
+ export function getSessionInfoSync() {
41
+ return sessionInfo;
42
+ }
43
+ /**
44
+ * Processes the session info response and returns the session info object.
45
+ *
46
+ * @param sessionInfoResp {any} Response from the session info API.
47
+ * @returns {SessionInfo} The session info object.
48
+ * @example ```js
49
+ * const sessionInfoResp = await fetch(sessionInfoPath);
50
+ * const sessionInfo = getSessionDetails(sessionInfoResp);
51
+ * console.log(sessionInfo);
52
+ * ```
53
+ * @version SDK: 1.28.3 | ThoughtSpot: *
54
+ */
55
+ export const getSessionDetails = (sessionInfoResp) => {
56
+ const devMixpanelToken = sessionInfoResp.configInfo.mixpanelConfig.devSdkKey;
57
+ const prodMixpanelToken = sessionInfoResp.configInfo.mixpanelConfig.prodSdkKey;
58
+ const mixpanelToken = sessionInfoResp.configInfo.mixpanelConfig.production
59
+ ? prodMixpanelToken
60
+ : devMixpanelToken;
61
+ return {
62
+ userGUID: sessionInfoResp.userGUID,
63
+ mixpanelToken,
64
+ isPublicUser: sessionInfoResp.configInfo.isPublicUser,
65
+ releaseVersion: sessionInfoResp.releaseVersion,
66
+ clusterId: sessionInfoResp.configInfo.selfClusterId,
67
+ clusterName: sessionInfoResp.configInfo.selfClusterName,
68
+ ...sessionInfoResp,
69
+ };
70
+ };
71
+ /**
72
+ * Resets the cached session info object and forces a new fetch on the next call.
73
+ *
74
+ * @example ```js
75
+ * resetCachedSessionInfo();
76
+ * const sessionInfo = await getSessionInfo();
77
+ * console.log(sessionInfo);
78
+ * ```
79
+ * @version SDK: 1.28.3 | ThoughtSpot ts7.april.cl, 7.2.1
80
+ * @returns {void}
81
+ */
82
+ export function resetCachedSessionInfo() {
83
+ sessionInfo = null;
84
+ }
85
+ //# sourceMappingURL=sessionInfoService.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"sessionInfoService.js","sourceRoot":"","sources":["../../../src/utils/sessionInfoService.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AACtD,OAAO,EAAE,uBAAuB,EAAE,MAAM,eAAe,CAAC;AAcxD,IAAI,WAAW,GAAuB,IAAI,CAAC;AAE3C;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc;IAChC,IAAI,CAAC,WAAW,EAAE;QACd,MAAM,IAAI,GAAG,cAAc,EAAE,CAAC,eAAe,CAAC;QAC9C,MAAM,eAAe,GAAG,MAAM,uBAAuB,CAAC,IAAI,CAAC,CAAC;QAC5D,MAAM,oBAAoB,GAAG,iBAAiB,CAAC,eAAe,CAAC,CAAC;QAChE,WAAW,GAAG,oBAAoB,CAAC;KACtC;IACD,OAAO,WAAW,CAAC;AACvB,CAAC;AAED;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,kBAAkB;IAC9B,OAAO,WAAW,CAAC;AACvB,CAAC;AAED;;;;;;;;;;;GAWG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,eAAoB,EAAe,EAAE;IACnE,MAAM,gBAAgB,GAAG,eAAe,CAAC,UAAU,CAAC,cAAc,CAAC,SAAS,CAAC;IAC7E,MAAM,iBAAiB,GAAG,eAAe,CAAC,UAAU,CAAC,cAAc,CAAC,UAAU,CAAC;IAC/E,MAAM,aAAa,GAAG,eAAe,CAAC,UAAU,CAAC,cAAc,CAAC,UAAU;QACtE,CAAC,CAAC,iBAAiB;QACnB,CAAC,CAAC,gBAAgB,CAAC;IACvB,OAAO;QACH,QAAQ,EAAE,eAAe,CAAC,QAAQ;QAClC,aAAa;QACb,YAAY,EAAE,eAAe,CAAC,UAAU,CAAC,YAAY;QACrD,cAAc,EAAE,eAAe,CAAC,cAAc;QAC9C,SAAS,EAAE,eAAe,CAAC,UAAU,CAAC,aAAa;QACnD,WAAW,EAAE,eAAe,CAAC,UAAU,CAAC,eAAe;QACvD,GAAG,eAAe;KACrB,CAAC;AACN,CAAC,CAAC;AAEF;;;;;;;;;;GAUG;AACH,MAAM,UAAU,sBAAsB;IAClC,WAAW,GAAG,IAAI,CAAC;AACvB,CAAC"}
@@ -1984,24 +1984,6 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
1984
1984
  * @version SDK: 1.27.9
1985
1985
  */
1986
1986
  disableSDKTracking?: boolean;
1987
- /**
1988
- * Overrides default/user preffered locale for date formatting
1989
- *
1990
- * @version SDK: 1.28.4 | Thoughtspot: 10.0.0.cl, 9.5.0.sw
1991
- */
1992
- dateFormatLocale?: string;
1993
- /**
1994
- * Overrides default/user preffered locale for number formatting
1995
- *
1996
- * @version SDK: 1.28.4 | Thoughtspot: 10.0.0.cl, 9.5.0.sw
1997
- */
1998
- numberFormatLocale?: string;
1999
- /**
2000
- * Format to be used for currency when currency format is set to infer from browser
2001
- *
2002
- * @version SDK: 1.28.4 | Thoughtspot: 10.0.0.cl, 9.5.0.sw
2003
- */
2004
- currencyFormat?: string;
2005
1987
  }
2006
1988
  export interface LayoutConfig {
2007
1989
  }
@@ -4465,10 +4447,7 @@ declare module '@thoughtspot/visual-embed-sdk/types' {
4465
4447
  ClientLogLevel = "clientLogLevel",
4466
4448
  OverrideNativeConsole = "overrideConsoleLogs",
4467
4449
  enableAskSage = "enableAskSage",
4468
- CollapseSearchBarInitially = "collapseSearchBarInitially",
4469
- DateFormatLocale = "dateFormatLocale",
4470
- NumberFormatLocale = "numberFormatLocale",
4471
- CurrencyFormat = "currencyFormat"
4450
+ CollapseSearchBarInitially = "collapseSearchBarInitially"
4472
4451
  }
4473
4452
  /**
4474
4453
  * ThoughtSpot application pages include actions and menu commands
@@ -5994,7 +5973,7 @@ declare module '@thoughtspot/visual-embed-sdk/embed/sage' {
5994
5973
  showObjectResults?: boolean;
5995
5974
  /**
5996
5975
  * flag used by the TS product tour page to show the blue search bar
5997
- * even after the search is completed. This is different from Thoughtspot Embedded Sage Embed
5976
+ * even after the search is completed. This is different from TSE Sage Embed
5998
5977
  * experience where it mimics closer to the non-embed case.
5999
5978
  * The Sample questions container is collapsed when this value is set after
6000
5979
  * does a search.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@thoughtspot/visual-embed-sdk",
3
- "version": "1.29.0-alpha.3",
3
+ "version": "1.29.0-alpha.authRefactor",
4
4
  "description": "ThoughtSpot Embed SDK",
5
5
  "module": "lib/src/index.js",
6
6
  "main": "dist/tsembed.js",
@@ -64,7 +64,6 @@
64
64
  "test": "npm run test-sdk && npm run test-docs",
65
65
  "posttest": "cat ./coverage/sdk/lcov.info | coveralls",
66
66
  "is-publish-allowed": "node scripts/is-publish-allowed.js",
67
- "prepublishOnly": "npm run is-publish-allowed && npm run test && npm run tsc && npm run bundle-dts-file && npm run bundle-dts && npm run bundle-dts-react && npm run bundle-dts-react-full && npm run build",
68
67
  "check-size": "npm run build && size-limit",
69
68
  "publish-dev": "npm publish --tag dev",
70
69
  "publish-prod": "npm publish --tag latest"
@@ -175,7 +174,6 @@
175
174
  "keywords": [
176
175
  "thoughtspot",
177
176
  "everywhere",
178
- "embedded",
179
177
  "embed",
180
178
  "sdk",
181
179
  "analytics"
package/src/auth.spec.ts CHANGED
@@ -1,12 +1,13 @@
1
1
  import * as authInstance from './auth';
2
- import * as authService from './utils/authService/authService';
3
- import * as tokenAuthService from './utils/authService/tokenizedAuthService';
4
- import * as checkReleaseVersionInBetaInstance from './utils';
5
- import * as mixPanelService from './mixpanel-service';
2
+ import * as authTokenService from './authToken';
6
3
  import * as EmbedConfig from './embed/embedConfig';
7
- import { AuthType, EmbedEvent } from './types';
4
+ import * as mixPanelService from './mixpanel-service';
8
5
  import { executeAfterWait } from './test/test-utils';
9
- import { resetCachedAuthToken } from './authToken';
6
+ import { AuthType, EmbedEvent } from './types';
7
+ import * as checkReleaseVersionInBetaInstance from './utils';
8
+ import * as authService from './utils/authService/authService';
9
+ import * as tokenAuthService from './utils/authService/tokenizedAuthService';
10
+ import * as SessionService from './utils/sessionInfoService';
10
11
 
11
12
  const thoughtSpotHost = 'http://localhost:3000';
12
13
  const username = 'tsuser';
@@ -18,7 +19,7 @@ export const embedConfig: any = {
18
19
  thoughtSpotHost,
19
20
  username,
20
21
  authEndpoint: 'auth',
21
- authType: AuthType.AuthServer,
22
+ authType: AuthType.TrustedAuthToken,
22
23
  getAuthToken: jest.fn(() => Promise.resolve(token)),
23
24
  }),
24
25
  doTokenAuthWithCookieDetect: {
@@ -112,31 +113,47 @@ export const mockSessionInfo = {
112
113
  },
113
114
  };
114
115
 
116
+ export const mockSessionInfoApiResponse = {
117
+ userGUID: '1234',
118
+ releaseVersion: 'test',
119
+ configInfo: {
120
+ isPublicUser: false,
121
+ mixpanelConfig: {
122
+ production: true,
123
+ devSdkKey: 'devKey',
124
+ prodSdkKey: 'prodKey',
125
+ },
126
+ },
127
+ };
128
+
115
129
  describe('Unit test for auth', () => {
116
130
  beforeEach(() => {
131
+ jest.resetAllMocks();
117
132
  global.fetch = window.fetch;
118
133
  });
119
134
  afterEach(() => {
120
- resetCachedAuthToken();
135
+ authTokenService.resetCachedAuthToken();
136
+ SessionService.resetCachedSessionInfo();
121
137
  });
122
138
  test('endpoints, SAML_LOGIN_TEMPLATE', () => {
123
139
  const ssoTemplateUrl = authService.EndPoints.SAML_LOGIN_TEMPLATE(thoughtSpotHost);
124
140
  expect(ssoTemplateUrl).toBe(`/callosum/v1/saml/login?targetURLPath=${thoughtSpotHost}`);
125
141
  });
126
142
 
127
- test('when session info giving response', async () => {
128
- jest.spyOn(mixPanelService, 'initMixpanel').mockImplementation(() => Promise.resolve());
129
- authInstance.initSession(mockSessionInfo);
130
- const sessionInfo = await authInstance.getSessionInfo();
131
- expect(sessionInfo).toStrictEqual(mockSessionInfo);
143
+ test('when session info giving response, it is cached', async () => {
144
+ jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockResolvedValueOnce(mockSessionInfoApiResponse);
145
+ const sessionInfo = await SessionService.getSessionInfo();
146
+ expect(sessionInfo.mixpanelToken).toEqual('prodKey');
147
+ expect(sessionInfo.isPublicUser).toEqual(false);
148
+ await SessionService.getSessionInfo();
149
+ expect(tokenAuthService.fetchSessionInfoService).toHaveBeenCalledTimes(1);
132
150
  });
133
151
 
134
152
  test('Disable mixpanel when disableSDKTracking flag is set', () => {
135
- jest.clearAllMocks();
136
- jest.resetAllMocks();
137
153
  jest.spyOn(mixPanelService, 'initMixpanel');
154
+ jest.spyOn(SessionService, 'getSessionInfo').mockReturnValue(mockSessionInfo);
138
155
  jest.spyOn(EmbedConfig, 'getEmbedConfig').mockReturnValue({ disableSDKTracking: true });
139
- authInstance.initSession(mockSessionInfo);
156
+ authInstance.postLoginService();
140
157
  expect(mixPanelService.initMixpanel).not.toBeCalled();
141
158
  });
142
159
 
@@ -163,28 +180,22 @@ describe('Unit test for auth', () => {
163
180
  });
164
181
 
165
182
  test('doTokenAuth: when user is loggedIn', async () => {
166
- jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(async () => ({
167
- json: () => mockSessionInfo,
168
- status: 200,
169
- }));
170
- jest.spyOn(authInstance, 'getSessionDetails').mockReturnValue(mockSessionInfo);
171
- jest.spyOn(authInstance, 'initSession').mockReturnValue(null);
183
+ const getAuthenticationTokenMock = jest.spyOn(authTokenService, 'getAuthenticationToken');
184
+ jest.spyOn(tokenAuthService, 'isActiveService').mockImplementation(async () => true);
172
185
  await authInstance.doTokenAuth(embedConfig.doTokenAuthSuccess('authToken'));
173
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
186
+ expect(authTokenService.getAuthenticationToken).not.toBeCalled();
174
187
  expect(authInstance.loggedInStatus).toBe(true);
188
+ getAuthenticationTokenMock.mockRestore();
175
189
  });
176
190
 
177
191
  test('doTokenAuth: when user is not loggedIn & getAuthToken have response', async () => {
178
- jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(() => false);
179
- jest.spyOn(authService, 'fetchAuthTokenService').mockImplementation(() => ({
180
- text: () => Promise.resolve('abc'),
181
- }));
192
+ jest.spyOn(tokenAuthService, 'isActiveService').mockImplementation(async () => false);
182
193
  jest.spyOn(authService, 'fetchAuthService').mockImplementation(() => Promise.resolve({
183
194
  status: 200,
195
+ ok: true,
184
196
  }));
185
197
  jest.spyOn(authService, 'verifyTokenService').mockResolvedValueOnce(true);
186
198
  await authInstance.doTokenAuth(embedConfig.doTokenAuthSuccess('authToken2'));
187
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
188
199
  expect(authService.fetchAuthService).toBeCalledWith(
189
200
  thoughtSpotHost,
190
201
  username,
@@ -193,26 +204,25 @@ describe('Unit test for auth', () => {
193
204
  });
194
205
 
195
206
  test('doTokenAuth: when user is not loggedIn & getAuthToken not present, isLoggedIn should called', async () => {
196
- jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(() => false);
197
- jest.spyOn(authService, 'fetchAuthTokenService').mockImplementation(() => Promise.resolve({ text: () => Promise.resolve('abc') }));
207
+ jest.spyOn(tokenAuthService, 'isActiveService').mockImplementation(async () => false);
208
+ jest.spyOn(authService, 'fetchAuthTokenService').mockImplementation(() => ({
209
+ text: () => Promise.resolve('abc'),
210
+ }));
198
211
  jest.spyOn(authService, 'fetchAuthService').mockImplementation(() => Promise.resolve({
199
212
  status: 200,
200
213
  ok: true,
201
214
  }));
202
215
  jest.spyOn(authService, 'verifyTokenService').mockResolvedValueOnce(true);
203
216
  await authInstance.doTokenAuth(embedConfig.doTokenAuthFailureWithoutGetAuthToken);
217
+ expect(authService.fetchAuthTokenService).toBeCalledWith('auth');
204
218
  await executeAfterWait(() => {
205
219
  expect(authInstance.loggedInStatus).toBe(true);
206
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
207
- expect(authService.fetchAuthService).toBeCalledWith(
208
- thoughtSpotHost,
209
- username,
210
- 'authToken2',
211
- );
220
+ expect(authService.fetchAuthService).toBeCalledWith(thoughtSpotHost, username, 'abc');
212
221
  });
213
222
  });
214
223
 
215
224
  test('doTokenAuth: Should raise error when duplicate token is used', async () => {
225
+ jest.spyOn(tokenAuthService, 'isActiveService').mockImplementation(async () => false);
216
226
  jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockResolvedValue({
217
227
  status: 401,
218
228
  });
@@ -254,8 +264,10 @@ describe('Unit test for auth', () => {
254
264
  ok: true,
255
265
  }));
256
266
  jest.spyOn(authService, 'verifyTokenService').mockResolvedValueOnce(true);
267
+ jest.spyOn(tokenAuthService, 'isActiveService').mockResolvedValueOnce(false);
268
+ jest.spyOn(tokenAuthService, 'isActiveService').mockResolvedValueOnce(false);
257
269
  const isLoggedIn = await authInstance.doTokenAuth(embedConfig.doTokenAuthWithCookieDetect);
258
- expect(tokenAuthService.fetchSessionInfoService).toHaveBeenCalledTimes(2);
270
+ expect(tokenAuthService.isActiveService).toHaveBeenCalledTimes(2);
259
271
  expect(isLoggedIn).toBe(false);
260
272
  });
261
273
 
@@ -278,7 +290,6 @@ describe('Unit test for auth', () => {
278
290
  expect(await authInstance.doTokenAuth(embedConfig.doTokenAuthSuccess('authToken2'))).toBe(
279
291
  true,
280
292
  );
281
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
282
293
  expect(authService.fetchAuthPostService).toBeCalledWith(
283
294
  thoughtSpotHost,
284
295
  username,
@@ -297,20 +308,9 @@ describe('Unit test for auth', () => {
297
308
  });
298
309
 
299
310
  it('when user is loggedIn', async () => {
300
- spyOn(checkReleaseVersionInBetaInstance, 'checkReleaseVersionInBeta');
301
- jest.spyOn(authInstance, 'getSessionDetails').mockReturnValue(mockSessionInfo);
302
- jest.spyOn(authInstance, 'initSession').mockReturnValue(null);
303
- jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(
304
- async () => ({
305
- json: () => mockSessionInfo,
306
- status: 200,
307
- }),
308
- );
311
+ jest.spyOn(tokenAuthService, 'isActiveService').mockResolvedValueOnce(true);
309
312
  await authInstance.doBasicAuth(embedConfig.doBasicAuth);
310
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
311
313
  expect(authInstance.loggedInStatus).toBe(true);
312
- expect(authInstance.getSessionDetails).toBeCalled();
313
- expect(authInstance.initSession).toBeCalled();
314
314
  });
315
315
 
316
316
  it('when user is not loggedIn', async () => {
@@ -321,7 +321,7 @@ describe('Unit test for auth', () => {
321
321
  }));
322
322
 
323
323
  await authInstance.doBasicAuth(embedConfig.doBasicAuth);
324
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
324
+ // expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
325
325
  expect(authService.fetchBasicAuthService).toBeCalled();
326
326
  expect(authInstance.loggedInStatus).toBe(true);
327
327
  });
@@ -349,10 +349,8 @@ describe('Unit test for auth', () => {
349
349
  status: 200,
350
350
  }),
351
351
  );
352
- jest.spyOn(authInstance, 'getSessionDetails').mockReturnValue(mockSessionInfo);
353
- jest.spyOn(authInstance, 'initSession').mockReturnValue(null);
352
+ jest.spyOn(tokenAuthService, 'isActiveService').mockReturnValue(true);
354
353
  await authInstance.doSamlAuth(embedConfig.doSamlAuth);
355
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
356
354
  expect(window.location.hash).toBe('');
357
355
  expect(authInstance.loggedInStatus).toBe(true);
358
356
  });
@@ -360,7 +358,6 @@ describe('Unit test for auth', () => {
360
358
  it('when user is not loggedIn & isAtSSORedirectUrl is true', async () => {
361
359
  jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(() => Promise.reject());
362
360
  await authInstance.doSamlAuth(embedConfig.doSamlAuth);
363
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
364
361
  expect(window.location.hash).toBe('');
365
362
  expect(authInstance.loggedInStatus).toBe(false);
366
363
  });
@@ -374,7 +371,6 @@ describe('Unit test for auth', () => {
374
371
  });
375
372
  jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(() => Promise.reject());
376
373
  await authInstance.doSamlAuth(embedConfig.doSamlAuth);
377
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
378
374
  expect(global.window.location.href).toBe(samalLoginUrl);
379
375
  });
380
376
 
@@ -387,14 +383,9 @@ describe('Unit test for auth', () => {
387
383
  });
388
384
  spyOn(authInstance, 'samlCompletionPromise');
389
385
  global.window.open = jest.fn();
390
- jest.spyOn(tokenAuthService, 'fetchSessionInfoService')
391
- .mockImplementationOnce(() => Promise.reject())
392
- .mockImplementationOnce(async () => ({
393
- json: () => mockSessionInfo,
394
- status: 200,
395
- }));
396
- jest.spyOn(authInstance, 'getSessionDetails').mockReturnValue(mockSessionInfo);
397
- jest.spyOn(authInstance, 'initSession').mockReturnValue(null);
386
+ jest.spyOn(tokenAuthService, 'isActiveService')
387
+ .mockReturnValueOnce(false)
388
+ .mockReturnValueOnce(true);
398
389
  expect(await authInstance.samlCompletionPromise).not.toBe(null);
399
390
  expect(
400
391
  await authInstance.doSamlAuth({
@@ -404,15 +395,13 @@ describe('Unit test for auth', () => {
404
395
  document.getElementById('ts-auth-btn').click();
405
396
  window.postMessage({ type: EmbedEvent.SAMLComplete }, '*');
406
397
  await authInstance.samlCompletionPromise;
407
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
408
- expect(authInstance.getSessionDetails).toBeCalled();
409
- expect(authInstance.initSession).toBeCalled();
398
+ expect(authInstance.loggedInStatus).toBe(true);
410
399
  });
411
400
  });
412
401
 
413
402
  describe('doOIDCAuth', () => {
414
403
  afterEach(() => {
415
- resetCachedAuthToken();
404
+ authTokenService.resetCachedAuthToken();
416
405
  delete global.window;
417
406
  global.window = Object.create(originalWindow);
418
407
  global.window.open = jest.fn();
@@ -422,7 +411,6 @@ describe('Unit test for auth', () => {
422
411
  it('when user is not loggedIn & isAtSSORedirectUrl is true', async () => {
423
412
  jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockImplementation(() => Promise.reject());
424
413
  await authInstance.doOIDCAuth(embedConfig.doOidcAuth);
425
- expect(tokenAuthService.fetchSessionInfoService).toBeCalled();
426
414
  expect(window.location.hash).toBe('');
427
415
  expect(authInstance.loggedInStatus).toBe(false);
428
416
  });
@@ -465,6 +453,7 @@ describe('Unit test for auth', () => {
465
453
 
466
454
  it('authenticate: when authType is Basic', async () => {
467
455
  jest.spyOn(authInstance, 'doBasicAuth');
456
+ jest.spyOn(authService, 'fetchBasicAuthService').mockImplementation(() => Promise.resolve({ status: 200, ok: true }));
468
457
  await authInstance.authenticate(embedConfig.basicAuthSuccess);
469
458
  expect(authInstance.doBasicAuth).toBeCalled();
470
459
  expect(authInstance.loggedInStatus).toBe(true);
@@ -481,6 +470,7 @@ describe('Unit test for auth', () => {
481
470
  });
482
471
 
483
472
  it('doCookielessTokenAuth should resolve to true if valid token is passed', async () => {
473
+ jest.clearAllMocks();
484
474
  jest.spyOn(authService, 'verifyTokenService').mockResolvedValueOnce(true);
485
475
  const isLoggedIn = await authInstance.doCookielessTokenAuth(
486
476
  embedConfig.doCookielessAuth('testToken'),
@@ -500,11 +490,11 @@ describe('Unit test for auth', () => {
500
490
  authInstance.setAuthEE(testObject as any);
501
491
  expect(authInstance.getAuthEE()).toBe(testObject);
502
492
  });
503
- it('getSessionDetails returns the correct details given sessionInfo', () => {
493
+ it('getSessionDetails returns the correct details given sessionInfo', async () => {
504
494
  jest.clearAllMocks();
505
495
  jest.restoreAllMocks();
506
496
 
507
- const details = authInstance.getSessionDetails({
497
+ jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockReturnValue({
508
498
  userGUID: '1234',
509
499
  releaseVersion: '1',
510
500
  configInfo: {
@@ -515,13 +505,14 @@ describe('Unit test for auth', () => {
515
505
  },
516
506
  },
517
507
  });
508
+ const details = await SessionService.getSessionInfo();
518
509
  expect(details).toEqual(
519
510
  expect.objectContaining({
520
511
  mixpanelToken: 'devKey',
521
512
  }),
522
513
  );
523
514
 
524
- const details2 = authInstance.getSessionDetails({
515
+ jest.spyOn(tokenAuthService, 'fetchSessionInfoService').mockReturnValue({
525
516
  configInfo: {
526
517
  mixpanelConfig: {
527
518
  devSdkKey: 'devKey',
@@ -530,6 +521,9 @@ describe('Unit test for auth', () => {
530
521
  },
531
522
  },
532
523
  });
524
+
525
+ SessionService.resetCachedSessionInfo();
526
+ const details2 = await SessionService.getSessionInfo();
533
527
  expect(details2).toEqual(
534
528
  expect.objectContaining({
535
529
  mixpanelToken: 'prodKey',