UNPKG

@thoughtspot/visual-embed-sdk

Version:
55 lines 2.6 kB
import { getAuthenticationToken, resetCachedAuthToken, validateAuthToken } from './authToken'; import * as authServiceInstance from './utils/authService/authService'; import { formatTemplate } from './utils'; import { logger } from './utils/logger'; import { ERROR_MESSAGE } from './errors'; describe('AuthToken Unit tests', () => { test('getAuthenticationToken: When verification is disabled', async () => { jest.spyOn(authServiceInstance, 'verifyTokenService'); const token = await getAuthenticationToken({ getAuthToken: async () => 'abc3', disableTokenVerification: true, }); expect(token).toBe('abc3'); expect(authServiceInstance.verifyTokenService).not.toHaveBeenCalled(); }); test('getAuthenticationToken: When verification is enabled', async () => { resetCachedAuthToken(); jest.clearAllMocks(); jest.spyOn(authServiceInstance, 'verifyTokenService').mockImplementation(() => Promise.resolve(true)); const token = await getAuthenticationToken({ thoughtSpotHost: 'test', getAuthToken: async () => 'abc2', disableTokenVerification: false, }); expect(token).toBe('abc2'); expect(authServiceInstance.verifyTokenService).toHaveBeenCalledWith('test', 'abc2'); }); test('validateAuthToken : When token is invalid by type number', async () => { jest.spyOn(logger, 'error').mockImplementation(() => { }); const loggerSpy = jest.spyOn(logger, 'error'); const authToken = 123; const errorMessage = formatTemplate(ERROR_MESSAGE.INVALID_TOKEN_TYPE_ERROR, { invalidType: typeof authToken, }); await expect(validateAuthToken({ thoughtSpotHost: 'test', }, authToken)).rejects.toThrow(errorMessage); expect(loggerSpy).toHaveBeenCalledWith(errorMessage); loggerSpy.mockRestore(); }); test('validateAuthToken : When token is invalid by type object', async () => { jest.spyOn(logger, 'error').mockImplementation(() => { }); const loggerSpy = jest.spyOn(logger, 'error'); const authToken = {}; const errorMessage = formatTemplate(ERROR_MESSAGE.INVALID_TOKEN_TYPE_ERROR, { invalidType: typeof authToken, }); await expect(validateAuthToken({ thoughtSpotHost: 'test', }, authToken)).rejects.toThrow(errorMessage); expect(loggerSpy).toHaveBeenCalledWith(errorMessage); loggerSpy.mockRestore(); }); }); //# sourceMappingURL=authToken.spec.js.map