-
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
base: master
Are you sure you want to change the base?
Prevent activation of previous workspace when launching Connect via deep link to a different cluster #50063
Changes from 5 commits
51c37e9
f43b0ae
dc70bdf
a6bb953
e941385
9fc2003
cc2644a
3dde323
8c4dbec
8776788
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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, | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,8 +176,9 @@ export default class AppContext implements IAppContext { | |
|
||
this.subscribeToDeepLinkLaunch(); | ||
this.notifyMainProcessAboutClusterListChanges(); | ||
this.clustersService.syncGatewaysAndCatchErrors(); | ||
void this.clustersService.syncGatewaysAndCatchErrors(); | ||
await this.clustersService.syncRootClustersAndCatchErrors(); | ||
this.workspacesService.restorePersistedState(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I close the app while I'm in workspace A with a valid cert and open it again through a deep link to workspace B, I can close the login modal and continue to use workspace A without making a decision wrt restored state. The user should be forced to make an action before interacting with a workspace. valid-cert-on-launch.movHow do we fix this? I almost feel like I'm just not sure at this point how this is going to interact with the rest of the app. |
||
} | ||
|
||
/** | ||
|
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.