-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent activation of previous workspace when launching Connect via d…
…eep link to a different cluster (#50063) (#50732) * Refactor `setActiveWorkspace` to async/await * Keep all the logic that restores a single workspace state in `getWorkspaceDefaultState` * Separate restoring state from setting active workspace * Allow the dialog to reopen documents to be closed without any decision * Add a test that verifies if the correct workspace is activated * Docs improvements * Return early if there's no restoredWorkspace * Fix logger name * Improve test name * Make restored state immutable * Fix comment * Do not wait for functions to be called in tests * Add tests for discarding documents reopen dialog * Move setting `isInitialized` to a separate method * Remove restored workspace when logging out so that we won't try to restore it when logging in again * Do not try to restore previous documents if the user already opened new ones * Do not close dialog in test * Improve `isInitialized` comment * Call `setActiveWorkspace` again when we fail to change the workspace * Fix the logic around restoring previous documents * Try to reopen documents also after `cluster-connect` is canceled * canRestoreDocuments -> hasDocumentsToReopen * Disallow parallel `setActiveWorkspace` calls (#50384) * Allow passing abortSignal to `open*Modal` methods, allow closing everything at once * Close all dialogs when activating a deep link * Allow only one `setActiveWorkspace` execution at a time * Review comments * Always unregister event listeners when a dialog is closed * Lint * Add a diagram for launching a deep link * Remove checking if the signal is aborted in `openImportantDialog`, rename it (cherry picked from commit 8e9b004)
- Loading branch information
Showing
20 changed files
with
959 additions
and
303 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
248 changes: 248 additions & 0 deletions
248
web/packages/teleterm/src/ui/AppInitializer/AppInitializer.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,248 @@ | ||
/** | ||
* Teleport | ||
* Copyright (C) 2024 Gravitational, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
import 'jest-canvas-mock'; | ||
|
||
import { act, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import { mockIntersectionObserver } from 'jsdom-testing-mocks'; | ||
|
||
import { render } from 'design/utils/testing'; | ||
|
||
import Logger, { NullService } from 'teleterm/logger'; | ||
import { MockedUnaryCall } from 'teleterm/services/tshd/cloneableClient'; | ||
import { makeRootCluster } from 'teleterm/services/tshd/testHelpers'; | ||
import { ResourcesContextProvider } from 'teleterm/ui/DocumentCluster/resourcesContext'; | ||
import { MockAppContextProvider } from 'teleterm/ui/fixtures/MockAppContextProvider'; | ||
import { MockAppContext } from 'teleterm/ui/fixtures/mocks'; | ||
import { ConnectionsContextProvider } from 'teleterm/ui/TopBar/Connections/connectionsContext'; | ||
import { IAppContext } from 'teleterm/ui/types'; | ||
import { VnetContextProvider } from 'teleterm/ui/Vnet'; | ||
|
||
import { AppInitializer } from './AppInitializer'; | ||
|
||
mockIntersectionObserver(); | ||
beforeAll(() => { | ||
Logger.init(new NullService()); | ||
}); | ||
|
||
jest.mock('teleterm/ui/ClusterConnect', () => ({ | ||
ClusterConnect: props => ( | ||
<div | ||
data-testid="mocked-dialog" | ||
data-dialog-kind="cluster-connect" | ||
data-dialog-is-hidden={props.hidden} | ||
> | ||
Connect to {props.dialog.clusterUri} | ||
<button onClick={props.dialog.onSuccess}>Connect to cluster</button> | ||
</div> | ||
), | ||
})); | ||
|
||
test('activating a workspace via deep link overrides the previously active workspace', async () => { | ||
// Before closing the app, both clusters were present in the state, with previouslyActiveCluster being active. | ||
// However, the user clicked a deep link pointing to deepLinkCluster. | ||
// The app should prioritize the user's intent by activating the workspace for the deep link, | ||
// rather than reactivating the previously active cluster. | ||
const previouslyActiveCluster = makeRootCluster({ | ||
uri: '/clusters/teleport-previously-active', | ||
proxyHost: 'teleport-previously-active:3080', | ||
name: 'teleport-previously-active', | ||
connected: false, | ||
}); | ||
const deepLinkCluster = makeRootCluster({ | ||
uri: '/clusters/teleport-deep-link', | ||
proxyHost: 'teleport-deep-link:3080', | ||
name: 'teleport-deep-link', | ||
connected: false, | ||
}); | ||
const appContext = new MockAppContext(); | ||
jest | ||
.spyOn(appContext.statePersistenceService, 'getWorkspacesState') | ||
.mockReturnValue({ | ||
rootClusterUri: previouslyActiveCluster.uri, | ||
workspaces: { | ||
[previouslyActiveCluster.uri]: { | ||
localClusterUri: previouslyActiveCluster.uri, | ||
documents: [], | ||
location: undefined, | ||
}, | ||
[deepLinkCluster.uri]: { | ||
localClusterUri: deepLinkCluster.uri, | ||
documents: [], | ||
location: undefined, | ||
}, | ||
}, | ||
}); | ||
appContext.mainProcessClient.configService.set( | ||
'usageReporting.enabled', | ||
false | ||
); | ||
jest.spyOn(appContext.tshd, 'listRootClusters').mockReturnValue( | ||
new MockedUnaryCall({ | ||
clusters: [deepLinkCluster, previouslyActiveCluster], | ||
}) | ||
); | ||
|
||
render( | ||
<MockAppContextProvider appContext={appContext}> | ||
<ConnectionsContextProvider> | ||
<VnetContextProvider> | ||
<ResourcesContextProvider> | ||
<AppInitializer /> | ||
</ResourcesContextProvider> | ||
</VnetContextProvider> | ||
</ConnectionsContextProvider> | ||
</MockAppContextProvider> | ||
); | ||
|
||
expect( | ||
await screen.findByText(`Connect to ${previouslyActiveCluster.uri}`) | ||
).toBeInTheDocument(); | ||
|
||
// Launch a deep link and do not wait for the result. | ||
act(() => { | ||
void appContext.deepLinksService.launchDeepLink({ | ||
status: 'success', | ||
url: { | ||
host: deepLinkCluster.proxyHost, | ||
hostname: deepLinkCluster.name, | ||
port: '1234', | ||
pathname: '/authenticate_web_device', | ||
username: deepLinkCluster.loggedInUser.name, | ||
searchParams: { | ||
id: '123', | ||
redirect_uri: '', | ||
token: 'abc', | ||
}, | ||
}, | ||
}); | ||
}); | ||
|
||
// The previous dialog has been replaced without a user interaction. | ||
// In the real app, this happens fast enough that the user doesn't see the previous dialog. | ||
expect( | ||
await screen.findByText(`Connect to ${deepLinkCluster.uri}`) | ||
).toBeInTheDocument(); | ||
|
||
// We confirm the current cluster-connect dialog. | ||
const dialogSuccessButton = await screen.findByRole('button', { | ||
name: 'Connect to cluster', | ||
}); | ||
await userEvent.click(dialogSuccessButton); | ||
|
||
// Check if the first activated workspace is the one from the deep link. | ||
expect(await screen.findByTitle(/Current cluster:/)).toBeVisible(); | ||
expect( | ||
screen.queryByTitle(`Current cluster: ${deepLinkCluster.name}`) | ||
).toBeVisible(); | ||
}); | ||
|
||
test.each<{ | ||
name: string; | ||
action(appContext: IAppContext): Promise<void>; | ||
expectHasDocumentsToReopen: boolean; | ||
}>([ | ||
{ | ||
name: 'closing documents reopen dialog via close button discards previous documents', | ||
action: async () => { | ||
await userEvent.click(await screen.findByTitle('Close')); | ||
}, | ||
expectHasDocumentsToReopen: false, | ||
}, | ||
{ | ||
name: 'starting new session in document reopen dialog discards previous documents', | ||
action: async () => { | ||
await userEvent.click( | ||
await screen.findByRole('button', { name: 'Start New Session' }) | ||
); | ||
}, | ||
expectHasDocumentsToReopen: false, | ||
}, | ||
{ | ||
name: 'overwriting document reopen dialog with another regular dialog does not discard documents', | ||
action: async appContext => { | ||
act(() => { | ||
appContext.modalsService.openRegularDialog({ | ||
kind: 'change-access-request-kind', | ||
onConfirm() {}, | ||
onCancel() {}, | ||
}); | ||
}); | ||
}, | ||
expectHasDocumentsToReopen: true, | ||
}, | ||
])('$name', async testCase => { | ||
const rootCluster = makeRootCluster(); | ||
const appContext = new MockAppContext(); | ||
jest | ||
.spyOn(appContext.statePersistenceService, 'getWorkspacesState') | ||
.mockReturnValue({ | ||
rootClusterUri: rootCluster.uri, | ||
workspaces: { | ||
[rootCluster.uri]: { | ||
localClusterUri: rootCluster.uri, | ||
documents: [ | ||
{ | ||
kind: 'doc.access_requests', | ||
uri: '/docs/123', | ||
state: 'browsing', | ||
clusterUri: rootCluster.uri, | ||
requestId: '', | ||
title: 'Access Requests', | ||
}, | ||
], | ||
location: undefined, | ||
}, | ||
}, | ||
}); | ||
appContext.mainProcessClient.configService.set( | ||
'usageReporting.enabled', | ||
false | ||
); | ||
jest.spyOn(appContext.tshd, 'listRootClusters').mockReturnValue( | ||
new MockedUnaryCall({ | ||
clusters: [rootCluster], | ||
}) | ||
); | ||
|
||
render( | ||
<MockAppContextProvider appContext={appContext}> | ||
<ConnectionsContextProvider> | ||
<VnetContextProvider> | ||
<ResourcesContextProvider> | ||
<AppInitializer /> | ||
</ResourcesContextProvider> | ||
</VnetContextProvider> | ||
</ConnectionsContextProvider> | ||
</MockAppContextProvider> | ||
); | ||
|
||
expect( | ||
await screen.findByText( | ||
'Do you want to reopen tabs from the previous session?' | ||
) | ||
).toBeInTheDocument(); | ||
|
||
await testCase.action(appContext); | ||
|
||
expect( | ||
appContext.workspacesService.getWorkspace(rootCluster.uri) | ||
.hasDocumentsToReopen | ||
).toBe(testCase.expectHasDocumentsToReopen); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.