Skip to content

Commit

Permalink
Added new post method for token API
Browse files Browse the repository at this point in the history
  • Loading branch information
gaurav-thoughtspot committed Aug 22, 2022
1 parent 11b2088 commit 7769821
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
39 changes: 39 additions & 0 deletions src/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,45 @@ describe('Unit test for auth', () => {
expect(isLoggedIn).toBe(false);
});

test('doTokenAuth: when user is not loggedIn & fetchAuthPostService failed than fetchAuthService should call', async () => {
jest.spyOn(window, 'alert').mockImplementation(() => undefined);
jest.spyOn(authService, 'fetchSessionInfoService').mockImplementation(
() => false,
);
jest.spyOn(
authService,
'fetchAuthTokenService',
).mockImplementation(() => ({ text: () => Promise.resolve('abc') }));
jest.spyOn(authService, 'fetchAuthPostService').mockImplementation(() =>
// eslint-disable-next-line prefer-promise-reject-errors
Promise.reject({
status: 500,
}),
);
jest.spyOn(authService, 'fetchAuthService').mockImplementation(() =>
Promise.resolve({
status: 200,
type: 'opaqueredirect',
}),
);
expect(
await authInstance.doTokenAuth(
embedConfig.doTokenAuthSuccess('authToken2'),
),
).toBe(true);
expect(authService.fetchSessionInfoService).toBeCalled();
expect(authService.fetchAuthPostService).toBeCalledWith(
thoughtSpotHost,
username,
'authToken2',
);
expect(authService.fetchAuthService).toBeCalledWith(
thoughtSpotHost,
username,
'authToken2',
);
});

describe('doBasicAuth', () => {
beforeEach(() => {
global.fetch = window.fetch;
Expand Down
16 changes: 11 additions & 5 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
fetchAuthService,
fetchBasicAuthService,
fetchLogoutService,
fetchAuthPostService,
} from './utils/authService';

// eslint-disable-next-line import/no-mutable-exports
Expand Down Expand Up @@ -157,11 +158,16 @@ export const doTokenAuth = async (
const response = await fetchAuthTokenService(authEndpoint);
authToken = await response.text();
}
const resp = await fetchAuthService(
thoughtSpotHost,
username,
authToken,
);
let resp;
try {
resp = await fetchAuthPostService(
thoughtSpotHost,
username,
authToken,
);
} catch (e) {
resp = await fetchAuthService(thoughtSpotHost, username, authToken);
}
// token login issues a 302 when successful
loggedInStatus = resp.ok || resp.type === 'opaqueredirect';
if (loggedInStatus && embedConfig.detectCookieAccessSlow) {
Expand Down
20 changes: 20 additions & 0 deletions src/utils/authService.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
fetchAuthTokenService,
fetchAuthService,
fetchBasicAuthService,
fetchAuthPostService,
} from './authService';
import { EndPoints } from '../auth';

Expand Down Expand Up @@ -56,6 +57,25 @@ describe('Unit test for authService', () => {
);
});

test('fetchAuthPostService', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({ success: true, ok: true }),
);
await fetchAuthPostService(thoughtSpotHost, username, authToken);
expect(fetch).toBeCalledWith(
`${thoughtSpotHost}${EndPoints.TOKEN_LOGIN}`,
{
method: 'POST',
credentials: 'include',
redirect: 'manual',
body: 'username=tsuser&auth_token=token',
headers: {
'x-requested-by': 'ThoughtSpot',
},
},
);
});

test('fetchBasicAuthService called with manual redirect', async () => {
global.fetch = jest.fn(() =>
Promise.resolve({ success: true, ok: true }),
Expand Down
19 changes: 19 additions & 0 deletions src/utils/authService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,25 @@ export async function fetchAuthService(
);
}

export async function fetchAuthPostService(
thoughtSpotHost: string,
username: string,
authToken: string,
): Promise<any> {
return failureLoggedFetch(`${thoughtSpotHost}${EndPoints.TOKEN_LOGIN}`, {
method: 'POST',
headers: {
'x-requested-by': 'ThoughtSpot',
},
body: `username=${encodeURIComponent(
username,
)}&auth_token=${encodeURIComponent(authToken)}`,
credentials: 'include',
// We do not want to follow the redirect, as it starts giving a CORS error
redirect: 'manual',
});
}

export async function fetchBasicAuthService(
thoughtSpotHost: string,
username: string,
Expand Down

0 comments on commit 7769821

Please sign in to comment.