Skip to content
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

feat: update Discover flow to support update resource mechanism #43774

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 103 additions & 1 deletion web/packages/teleport/src/Discover/Discover.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import { render, screen } from 'design/utils/testing';
import { Resource } from 'gen-proto-ts/teleport/userpreferences/v1/onboard_pb';

import TeleportContextProvider from 'teleport/TeleportContextProvider';
import { Discover } from 'teleport/Discover/Discover';
import { Discover, DiscoverComponent } from 'teleport/Discover/Discover';
import { ResourceViewConfig } from 'teleport/Discover/flow';
import { FeaturesContextProvider } from 'teleport/FeaturesContext';
import { createTeleportContext, getAcl } from 'teleport/mocks/contexts';
import { getOSSFeatures } from 'teleport/features';
Expand All @@ -47,6 +48,9 @@ import { makeTestUserContext } from 'teleport/User/testHelpers/makeTestUserConte
import { makeDefaultUserPreferences } from 'teleport/services/userPreferences/userPreferences';

import { ResourceKind } from './Shared';
import { useDiscover } from './useDiscover';

import type { ResourceSpec } from 'teleport/Discover/SelectResource/types';

beforeEach(() => {
jest.restoreAllMocks();
Expand Down Expand Up @@ -204,3 +208,101 @@ describe('location state', () => {
).not.toBeInTheDocument();
});
});

type updateProps = {
resourceName?: string;
resourceSpecForUpdate?: ResourceSpec;
};

const renderUpdate = (props: updateProps) => {
const defaultPref = makeDefaultUserPreferences();
defaultPref.onboard.preferredResources = [Resource.WEB_APPLICATIONS];

mockUserContextProviderWith(
makeTestUserContext({ preferences: defaultPref })
);

const userAcl = getAcl();
const ctx = createTeleportContext({ customAcl: userAcl });

const MockComponent1 = () => {
const { agentMeta } = useDiscover();
return (
<>
{agentMeta.resourceName === 'saml2' ? agentMeta.resourceName : 'saml1'}
</>
);
};

const testViews: ResourceViewConfig[] = [
{
kind: ResourceKind.SamlApplication,
views() {
return [
{
title: 'MockComponent1',
component: MockComponent1,
},
];
},
},
];

return render(
<MemoryRouter
initialEntries={[
{ pathname: cfg.routes.discover, state: { entity: '' } },
]}
>
<TeleportContextProvider ctx={ctx}>
<DiscoverComponent
eViewConfigs={testViews}
isUpdateFlow={true}
resourceSpecForUpdate={props.resourceSpecForUpdate}
agentMetaForUpdate={{ resourceName: props.resourceName }}
/>
</TeleportContextProvider>
</MemoryRouter>
);
};

test('update flow: renders single component based on resourceSpecForUpdatvalue', () => {
const resourceSpecForUpdate: ResourceSpec = {
name: 'Connect My Computer',
kind: ResourceKind.ConnectMyComputer,
event: null,
icon: 'Laptop',
keywords: '',
hasAccess: true,
};

renderUpdate({ resourceSpecForUpdate: resourceSpecForUpdate });

expect(screen.queryAllByTestId(ResourceKind.Server).length).toBeFalsy();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not entirely sure if this (queryAllByTestId().length).toBeFalsy()) is the correct way to check that resources are not rendered for the update flow. lmk if theres a better way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe:

expect(screen.queryByTestId((ResourceKind.Server)).not.toBeInTheDocument();


expect(screen.queryAllByTestId(ResourceKind.Database).length).toBeFalsy();

expect(screen.queryAllByTestId(ResourceKind.Application).length).toBeFalsy();

expect(screen.queryAllByTestId(ResourceKind.Kubernetes).length).toBeFalsy();

expect(screen.getByText('Sign In & Connect My Computer')).toBeInTheDocument();
});

test('update flow: agentMeta is prepopulated based on agentMetaForUpdate', () => {
const resourceSpecForUpdate: ResourceSpec = {
name: 'MockComponent1',
kind: ResourceKind.SamlApplication,
event: null,
icon: 'Application',
keywords: '',
hasAccess: true,
};

renderUpdate({
resourceName: 'saml2',
resourceSpecForUpdate: resourceSpecForUpdate,
});

expect(screen.getByText('saml2')).toBeInTheDocument();
});
24 changes: 20 additions & 4 deletions web/packages/teleport/src/Discover/Discover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@ import { findViewAtIndex } from 'teleport/components/Wizard/flow';

import { EViewConfigs } from './types';

import { DiscoverProvider, useDiscover } from './useDiscover';
import { DiscoverProvider, useDiscover, AgentMeta } from './useDiscover';
import { DiscoverIcon } from './SelectResource/icons';

import type { ResourceSpec } from './SelectResource';

function DiscoverContent() {
const {
currentStep,
Expand Down Expand Up @@ -94,10 +96,21 @@ function DiscoverContent() {
);
}

export function DiscoverComponent({ eViewConfigs = [] }: Props) {
export function DiscoverComponent({
eViewConfigs = [],
isUpdateFlow,
resourceSpecForUpdate,
agentMetaForUpdate,
}: DiscoverComponentProps) {
const location = useLocation();
return (
<DiscoverProvider eViewConfigs={eViewConfigs} key={location.key}>
<DiscoverProvider
eViewConfigs={eViewConfigs}
key={location.key}
isUpdateFlow={isUpdateFlow}
resourceSpecForUpdate={resourceSpecForUpdate}
agentMetaForUpdate={agentMetaForUpdate}
>
<DiscoverContent />
</DiscoverProvider>
);
Expand All @@ -107,6 +120,9 @@ export function Discover() {
return <DiscoverComponent />;
}

type Props = {
export type DiscoverComponentProps = {
eViewConfigs?: EViewConfigs;
isUpdateFlow?: boolean;
resourceSpecForUpdate?: ResourceSpec;
agentMetaForUpdate?: AgentMeta;
};
21 changes: 21 additions & 0 deletions web/packages/teleport/src/Discover/useDiscover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export interface DiscoverContextState<T = any> {
emitErrorEvent(errorStr: string): void;
emitEvent(status: DiscoverEventStepStatus, custom?: CustomEventInput): void;
eventState: EventState;
isUpdateFlow?: boolean;
}

type EventState = {
Expand All @@ -93,6 +94,14 @@ type DiscoverProviderProps = {
mockCtx?: DiscoverContextState;
// Extra view configs that are passed in. This is used to add view configs from Enterprise.
eViewConfigs?: EViewConfigs;
// isUpdateFlow indicates if the Discover flow is an update resource flow.
isUpdateFlow?: boolean;
// resourceSpecForUpdate specifies ResourceSpec which should be used to
// start a Discover flow.
resourceSpecForUpdate?: ResourceSpec;
// agentMetaForUpdate includes data that will be used to prepopulate input fields
// in the respective Discover compnents.
agentMetaForUpdate?: AgentMeta;
flyinghermit marked this conversation as resolved.
Show resolved Hide resolved
};

// DiscoverUrlLocationState define fields to preserve state between
Expand All @@ -117,6 +126,9 @@ export function DiscoverProvider({
mockCtx,
children,
eViewConfigs = [],
isUpdateFlow,
resourceSpecForUpdate,
agentMetaForUpdate,
}: React.PropsWithChildren<DiscoverProviderProps>) {
const history = useHistory();
const location = useLocation<DiscoverUrlLocationState>();
Expand Down Expand Up @@ -229,6 +241,14 @@ export function DiscoverProvider({
});
}

// trigger update Discover flow.
useEffect(() => {
if (isUpdateFlow) {
onSelectResource(resourceSpecForUpdate);
updateAgentMeta(agentMetaForUpdate);
}
}, [agentMetaForUpdate]);

// If a location.state.discover was provided, that means the user is
// coming back from another location to resume the flow.
// Users will resume at the step that is +1 from the step they left from.
Expand Down Expand Up @@ -453,6 +473,7 @@ export function DiscoverProvider({
emitErrorEvent,
emitEvent,
eventState,
isUpdateFlow,
};

return (
Expand Down
Loading