From 7e5d6aeb1db4aea5158b130d92b9b8f9f7214444 Mon Sep 17 00:00:00 2001 From: Alena Khineika Date: Mon, 25 Mar 2024 16:44:42 +0100 Subject: [PATCH] fix: can not sign out if not signed in yet COMPASS-7787 (#5617) --- .../src/store/atlas-signin-store.ts | 20 +++-------- .../tests/atlas-login.test.ts | 36 +++++++++++++++++++ .../src/stores/atlas-login.ts | 6 ++-- 3 files changed, 44 insertions(+), 18 deletions(-) diff --git a/packages/atlas-service/src/store/atlas-signin-store.ts b/packages/atlas-service/src/store/atlas-signin-store.ts index 4e88f230bac..3d7692caea4 100644 --- a/packages/atlas-service/src/store/atlas-signin-store.ts +++ b/packages/atlas-service/src/store/atlas-signin-store.ts @@ -23,28 +23,18 @@ export type AtlasAuthPluginServices = { export function activatePlugin( _: Record, 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()); diff --git a/packages/compass-e2e-tests/tests/atlas-login.test.ts b/packages/compass-e2e-tests/tests/atlas-login.test.ts index 1e037d85235..a743cb6a58c 100644 --- a/packages/compass-e2e-tests/tests/atlas-login.test.ts +++ b/packages/compass-e2e-tests/tests/atlas-login.test.ts @@ -41,6 +41,7 @@ describe('Atlas Login', function () { let oidcMockProvider: OIDCMockProvider; let getTokenPayload: OIDCMockProviderConfig['getTokenPayload']; let stopMockAtlasServer: () => Promise; + let numberOfOIDCAuthRequests = 0; before(async function () { skipForWeb(this, 'atlas-login not supported in compass-web'); @@ -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; @@ -104,6 +108,8 @@ describe('Atlas Login', function () { }); beforeEach(async function () { + numberOfOIDCAuthRequests = 0; + getTokenPayload = () => { return DEFAULT_TOKEN_PAYLOAD; }; @@ -152,6 +158,7 @@ describe('Atlas Login', function () { 'Logged in with Atlas account test@example.com' ); }); + expect(numberOfOIDCAuthRequests).to.eq(1); }); describe('telemetry', () => { @@ -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 test@example.com' + ); + }); + + 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 test@example.com' + ); + }); + 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')); diff --git a/packages/compass-settings/src/stores/atlas-login.ts b/packages/compass-settings/src/stores/atlas-login.ts index d637ac089af..24bbd96d0e9 100644 --- a/packages/compass-settings/src/stores/atlas-login.ts +++ b/packages/compass-settings/src/stores/atlas-login.ts @@ -209,9 +209,9 @@ export const getUserInfo = (): SettingsThunkAction> => { }; }; -export const signOut = (): SettingsThunkAction => { - return (dispatch, _getState, { atlasAuthService }) => { - void atlasAuthService.signOut(); +export const signOut = (): SettingsThunkAction> => { + return async (dispatch, _getState, { atlasAuthService }) => { + await atlasAuthService.signOut(); dispatch({ type: AtlasLoginSettingsActionTypes.SignOut }); }; };