-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Prevent activation of previous workspace when launching Connect via deep link to a different cluster #50063
Open
gzdunek
wants to merge
10
commits into
master
Choose a base branch
from
gzdunek/device-trust-correct-workspace
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Prevent activation of previous workspace when launching Connect via deep link to a different cluster #50063
Changes from 1 commit
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
51c37e9
Refactor `setActiveWorkspace` to async/await
gzdunek f43b0ae
Keep all the logic that restores a single workspace state in `getWork…
gzdunek dc70bdf
Separate restoring state from setting active workspace
gzdunek a6bb953
Allow the dialog to reopen documents to be closed without any decision
gzdunek e941385
Add a test that verifies if the correct workspace is activated
gzdunek 9fc2003
Docs improvements
gzdunek cc2644a
Return early if there's no restoredWorkspace
gzdunek 3dde323
Fix logger name
gzdunek 8c4dbec
Improve test name
gzdunek 8776788
Make restored state immutable
gzdunek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
183 changes: 183 additions & 0 deletions
183
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,183 @@ | ||
/** | ||
* 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 { render } from 'design/utils/testing'; | ||
import { screen, act, waitFor } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
|
||
import { MockAppContext } from 'teleterm/ui/fixtures/mocks'; | ||
import { makeRootCluster } from 'teleterm/services/tshd/testHelpers'; | ||
import { MockAppContextProvider } from 'teleterm/ui/fixtures/MockAppContextProvider'; | ||
import { ConnectionsContextProvider } from 'teleterm/ui/TopBar/Connections/connectionsContext'; | ||
import { VnetContextProvider } from 'teleterm/ui/Vnet'; | ||
import Logger, { NullService } from 'teleterm/logger'; | ||
import { MockedUnaryCall } from 'teleterm/services/tshd/cloneableClient'; | ||
import { ResourcesContextProvider } from 'teleterm/ui/DocumentCluster/resourcesContext'; | ||
|
||
import { AppInitializer } from './AppInitializer'; | ||
|
||
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} | ||
> | ||
<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], | ||
}) | ||
); | ||
jest.spyOn(appContext.modalsService, 'openRegularDialog'); | ||
const userInterfaceReady = withPromiseResolver(); | ||
jest | ||
.spyOn(appContext.mainProcessClient, 'signalUserInterfaceReadiness') | ||
.mockImplementation(() => userInterfaceReady.resolve()); | ||
|
||
render( | ||
<MockAppContextProvider appContext={appContext}> | ||
<ConnectionsContextProvider> | ||
<VnetContextProvider> | ||
<ResourcesContextProvider> | ||
<AppInitializer /> | ||
</ResourcesContextProvider> | ||
</VnetContextProvider> | ||
</ConnectionsContextProvider> | ||
</MockAppContextProvider> | ||
); | ||
|
||
// Wait for the app to finish initialization. | ||
await act(() => userInterfaceReady.promise); | ||
// 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 cluster-connect dialog should be opened two times. | ||
// The first one comes from restoring the previous session, but it is | ||
// immediately canceled and replaced with a dialog to the cluster from | ||
// the deep link. | ||
await waitFor( | ||
() => { | ||
expect(appContext.modalsService.openRegularDialog).toHaveBeenCalledTimes( | ||
2 | ||
); | ||
}, | ||
// A small timeout to prevent potential race conditions. | ||
{ timeout: 10 } | ||
); | ||
expect(appContext.modalsService.openRegularDialog).toHaveBeenNthCalledWith( | ||
1, | ||
expect.objectContaining({ | ||
kind: 'cluster-connect', | ||
clusterUri: previouslyActiveCluster.uri, | ||
}) | ||
); | ||
expect(appContext.modalsService.openRegularDialog).toHaveBeenNthCalledWith( | ||
2, | ||
expect.objectContaining({ | ||
kind: 'cluster-connect', | ||
clusterUri: deepLinkCluster.uri, | ||
}) | ||
); | ||
|
||
// We blindly 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(); | ||
}); | ||
|
||
//TODO(gzdunek): Replace with Promise.withResolvers after upgrading to Node.js 22. | ||
function withPromiseResolver() { | ||
let resolver: () => void; | ||
const promise = new Promise<void>(resolve => (resolver = resolve)); | ||
return { | ||
resolve: resolver, | ||
promise, | ||
}; | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't we wait here for the connect-cluster dialog to be opened with the correct content rather than doing
waitFor
for a mocked function? As inexpect(screen.findByText(<whatever is shown in the modal>))
. If possible, we shouldn't usetoHaveBeenCalled
as something that moves tests forward, but rather the actual results that the action has on the interface that the user sees.