Skip to content

Commit

Permalink
fix: can not sign out if not signed in yet COMPASS-7787 (#5617)
Browse files Browse the repository at this point in the history
  • Loading branch information
alenakhineika authored Mar 25, 2024
1 parent 84fd461 commit 7e5d6ae
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 18 deletions.
20 changes: 5 additions & 15 deletions packages/atlas-service/src/store/atlas-signin-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,18 @@ export type AtlasAuthPluginServices = {
export function activatePlugin(
_: Record<string, never>,
services: AtlasAuthPluginServices,
{ on, addCleanup, cleanup }: ActivateHelpers
{ on, cleanup }: ActivateHelpers
) {
store = configureStore(services);

const onSignedOut = () => store.dispatch(signedOut);
const onTokenRefreshFailed = () => store.dispatch(tokenRefreshFailed);
const onSignedOut = () => store.dispatch(signedOut());
const onTokenRefreshFailed = () => store.dispatch(tokenRefreshFailed());

if (ipcRenderer) {
on(ipcRenderer, 'atlas-service-token-refresh-failed', onSignedOut);
on(ipcRenderer, 'atlas-service-signed-out', onTokenRefreshFailed);
on(ipcRenderer, 'atlas-service-token-refresh-failed', onTokenRefreshFailed);
on(ipcRenderer, 'atlas-service-signed-out', onSignedOut);
}

addCleanup(() => {
if (ipcRenderer) {
ipcRenderer.off(
'atlas-service-token-refresh-failed',
onTokenRefreshFailed
);
ipcRenderer.off('atlas-service-signed-out', onSignedOut);
}
});

// Restore the sign-in state when plugin is activated
void store.dispatch(restoreSignInState());

Expand Down
36 changes: 36 additions & 0 deletions packages/compass-e2e-tests/tests/atlas-login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('Atlas Login', function () {
let oidcMockProvider: OIDCMockProvider;
let getTokenPayload: OIDCMockProviderConfig['getTokenPayload'];
let stopMockAtlasServer: () => Promise<void>;
let numberOfOIDCAuthRequests = 0;

before(async function () {
skipForWeb(this, 'atlas-login not supported in compass-web');
Expand Down Expand Up @@ -71,6 +72,9 @@ describe('Atlas Login', function () {
res.setHeader('Location', url.searchParams.get('fromURI') ?? '');
res.end();
break;
case '/authorize':
numberOfOIDCAuthRequests += 1;
break;
case '/v1/userinfo':
if (isAuthorised(req)) {
res.statusCode = 200;
Expand Down Expand Up @@ -104,6 +108,8 @@ describe('Atlas Login', function () {
});

beforeEach(async function () {
numberOfOIDCAuthRequests = 0;

getTokenPayload = () => {
return DEFAULT_TOKEN_PAYLOAD;
};
Expand Down Expand Up @@ -152,6 +158,7 @@ describe('Atlas Login', function () {
'Logged in with Atlas account [email protected]'
);
});
expect(numberOfOIDCAuthRequests).to.eq(1);
});

describe('telemetry', () => {
Expand Down Expand Up @@ -218,6 +225,35 @@ describe('Atlas Login', function () {
});
});

it('should sign in user when disconnected and clicking again on "Log in with Atlas" button', async function () {
await browser.openSettingsModal('Feature Preview');

await browser.openSettingsModal('Feature Preview');
await browser.clickVisible(Selectors.LogInWithAtlasButton);

let loginStatus = browser.$(Selectors.AtlasLoginStatus);

await browser.waitUntil(async () => {
return (
(await loginStatus.getText()).trim() ===
'Logged in with Atlas account [email protected]'
);
});

await browser.clickVisible(Selectors.DisconnectAtlasAccountButton);

await browser.clickVisible(Selectors.LogInWithAtlasButton);

loginStatus = browser.$(Selectors.AtlasLoginStatus);
await browser.waitUntil(async () => {
return (
(await loginStatus.getText()).trim() ===
'Logged in with Atlas account [email protected]'
);
});
expect(numberOfOIDCAuthRequests).to.eq(2);
});

it('should show toast with error if sign in failed', async function () {
getTokenPayload = () => {
return Promise.reject(new Error('Auth failed'));
Expand Down
6 changes: 3 additions & 3 deletions packages/compass-settings/src/stores/atlas-login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,9 @@ export const getUserInfo = (): SettingsThunkAction<Promise<void>> => {
};
};

export const signOut = (): SettingsThunkAction<void> => {
return (dispatch, _getState, { atlasAuthService }) => {
void atlasAuthService.signOut();
export const signOut = (): SettingsThunkAction<Promise<void>> => {
return async (dispatch, _getState, { atlasAuthService }) => {
await atlasAuthService.signOut();
dispatch({ type: AtlasLoginSettingsActionTypes.SignOut });
};
};
Expand Down

0 comments on commit 7e5d6ae

Please sign in to comment.