UNPKG

@thoughtspot/visual-embed-sdk

Version:
68 lines 3.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); require("jest-fetch-mock"); const embedConfigModule = tslib_1.__importStar(require("./embed/embedConfig")); const authTokenModule = tslib_1.__importStar(require("./authToken")); const types_1 = require("./types"); const tokenizedFetch_1 = require("./tokenizedFetch"); jest.mock('./embed/embedConfig'); jest.mock('./authToken'); const mockGetEmbedConfig = embedConfigModule.getEmbedConfig; const mockGetAuthenticationToken = authTokenModule.getAuthenticationToken; const mockGetCacheAuthToken = authTokenModule.getCacheAuthToken; describe('tokenizedFetch', () => { beforeEach(() => { jest.clearAllMocks(); fetchMock.resetMocks(); }); describe('non-cookieless auth', () => { beforeEach(() => { mockGetEmbedConfig.mockReturnValue({ authType: types_1.AuthType.TrustedAuthToken }); }); it('should add Authorization Bearer header when cachedAuthToken exists', async () => { mockGetCacheAuthToken.mockReturnValue('my-cached-token'); fetchMock.mockResponseOnce(JSON.stringify({})); await (0, tokenizedFetch_1.tokenizedFetch)('https://example.com/api', { method: 'GET' }); expect(fetchMock).toHaveBeenCalledTimes(1); const request = fetchMock.mock.calls[0][0]; expect(request).toBeInstanceOf(Request); expect(request.headers.get('Authorization')).toBe('Bearer my-cached-token'); }); it('should fetch with credentials include when no cachedAuthToken', async () => { mockGetCacheAuthToken.mockReturnValue(null); fetchMock.mockResponseOnce(JSON.stringify({})); await (0, tokenizedFetch_1.tokenizedFetch)('https://example.com/api', { method: 'GET' }); expect(fetchMock).toHaveBeenCalledWith('https://example.com/api', { credentials: 'include', method: 'GET', }); }); }); describe('cookieless auth (TrustedAuthTokenCookieless)', () => { beforeEach(() => { mockGetEmbedConfig.mockReturnValue({ authType: types_1.AuthType.TrustedAuthTokenCookieless, thoughtSpotHost: 'https://example.com', }); }); it('should add Authorization Bearer header from getAuthenticationToken', async () => { mockGetAuthenticationToken.mockResolvedValue('cookieless-token'); fetchMock.mockResponseOnce(JSON.stringify({})); await (0, tokenizedFetch_1.tokenizedFetch)('https://example.com/api', { method: 'POST' }); expect(mockGetAuthenticationToken).toHaveBeenCalled(); const request = fetchMock.mock.calls[0][0]; expect(request).toBeInstanceOf(Request); expect(request.headers.get('Authorization')).toBe('Bearer cookieless-token'); }); it('should not add Authorization header when getAuthenticationToken returns null', async () => { mockGetAuthenticationToken.mockResolvedValue(null); fetchMock.mockResponseOnce(JSON.stringify({})); await (0, tokenizedFetch_1.tokenizedFetch)('https://example.com/api', { method: 'POST' }); const request = fetchMock.mock.calls[0][0]; expect(request).toBeInstanceOf(Request); expect(request.headers.get('Authorization')).toBeNull(); }); }); }); //# sourceMappingURL=tokenizedFetch.spec.js.map