• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

thoughtspot / visual-embed-sdk / #1435

14 Jan 2025 09:42PM UTC coverage: 41.895% (-52.0%) from 93.86%
#1435

Pull #65

Prashant.patil
SCAL-233454-exp checking-mixpanel-error
Pull Request #65: test-exported memb

273 of 1106 branches covered (24.68%)

Branch coverage included in aggregate %.

84 of 356 new or added lines in 14 files covered. (23.6%)

1011 existing lines in 23 files now uncovered.

1301 of 2651 relevant lines covered (49.08%)

9.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

51.92
/src/authToken.ts
1
import { ERROR_MESSAGE } from './errors';
24✔
2
import { EmbedConfig } from './types';
3
import { fetchAuthTokenService, verifyTokenService } from './utils/authService/authService';
24✔
4
import { logger } from './utils/logger';
24✔
5

6
let cachedAuthToken: string | null = null;
24✔
7

8
// This method can be used to get the authToken using the embedConfig
9
/**
10
 *
11
 * @param embedConfig
12
 */
13
export async function getAuthenticationToken(embedConfig: EmbedConfig): Promise<string> {
24✔
14
    // Since we don't have token validation enabled , we cannot tell if the
15
    // cached token is valid or not. So we will always fetch a new token.
16
    if (cachedAuthToken && !embedConfig.disableTokenVerification) {
2!
17
        let isCachedTokenStillValid;
UNCOV
18
        try {
×
UNCOV
19
            isCachedTokenStillValid = await validateAuthToken(embedConfig, cachedAuthToken, true);
×
20
        } catch {
UNCOV
21
            isCachedTokenStillValid = false;
×
22
        }
23

UNCOV
24
        if (isCachedTokenStillValid) return cachedAuthToken;
×
25
    }
26

27
    const { authEndpoint, getAuthToken } = embedConfig;
2✔
28

29
    let authToken = null;
2✔
30
    if (getAuthToken) {
2!
31
        authToken = await getAuthToken();
2✔
32
    } else {
UNCOV
33
        const response = await fetchAuthTokenService(authEndpoint);
×
UNCOV
34
        authToken = await response.text();
×
35
    }
36

37
    try {
2✔
38
        // this will throw error if the token is not valid
39
        await validateAuthToken(embedConfig, authToken);
2✔
40
    } catch (e) {
UNCOV
41
        logger.error(`${ERROR_MESSAGE.INVALID_TOKEN_ERROR} Error : ${e.message}`);
×
UNCOV
42
        throw e;
×
43
    }
44

45
    cachedAuthToken = authToken;
2✔
46
    return authToken;
2✔
47
}
48

49
const validateAuthToken = async (
24✔
50
    embedConfig: EmbedConfig,
51
    authToken: string,
52
    suppressAlert?: boolean,
53
): Promise<boolean> => {
54
    if (embedConfig.disableTokenVerification) {
2✔
55
        logger.info('Token verification is disabled. Assuming token is valid.');
1✔
56
        return true;
1✔
57
    }
58
    try {
1✔
59
        const isTokenValid = await verifyTokenService(embedConfig.thoughtSpotHost, authToken);
1✔
60
        if (isTokenValid) return true;
1✔
61
    } catch {
62
        return false;
×
63
    }
64

UNCOV
65
    if (cachedAuthToken && cachedAuthToken === authToken) {
×
UNCOV
66
        if (!embedConfig.suppressErrorAlerts && !suppressAlert) {
×
67
            // eslint-disable-next-line no-alert
UNCOV
68
            alert(ERROR_MESSAGE.DUPLICATE_TOKEN_ERR);
×
69
        }
UNCOV
70
        throw new Error(ERROR_MESSAGE.DUPLICATE_TOKEN_ERR);
×
71
    } else {
UNCOV
72
        throw new Error(ERROR_MESSAGE.INVALID_TOKEN_ERROR);
×
73
    }
74
};
75

76
/**
77
 * Resets the auth token and a new token will be fetched on the next request.
78
 * @example
79
 * ```js
80
 * resetCachedAuthToken();
81
 * ```
82
 * @version SDK: 1.28.0 | ThoughtSpot: *
83
 * @group Authentication / Init
84
 */
85
export const resetCachedAuthToken = (): void => {
24✔
86
    cachedAuthToken = null;
1✔
87
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc