[Workplace Search] Fix redirect and state for personal oAuth plugin (#95238)

* Move source added route to top-level index component

* Use state passed back from oAuth app to determine context

The previous tests weren’t actually using this state so they have been updated with actual state data for proper testing
This commit is contained in:
Scotty Bollinger 2021-03-23 17:02:36 -05:00 committed by GitHub
parent 3cfb4f061e
commit 3ff76fd022
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 15 deletions

View file

@ -16,6 +16,7 @@ import { shallow } from 'enzyme';
import { Layout } from '../shared/layout';
import { WorkplaceSearchHeaderActions } from './components/layout';
import { SourceAdded } from './views/content_sources/components/source_added';
import { ErrorState } from './views/error_state';
import { Overview as OverviewMVP } from './views/overview_mvp';
import { SetupGuide } from './views/setup_guide';
@ -94,4 +95,10 @@ describe('WorkplaceSearchConfigured', () => {
expect(wrapper.find(Layout).first().prop('readOnlyMode')).toEqual(true);
});
it('renders SourceAdded', () => {
const wrapper = shallow(<WorkplaceSearchConfigured />);
expect(wrapper.find(SourceAdded)).toHaveLength(1);
});
});

View file

@ -24,12 +24,14 @@ import {
GROUPS_PATH,
SETUP_GUIDE_PATH,
SOURCES_PATH,
SOURCE_ADDED_PATH,
PERSONAL_SOURCES_PATH,
ORG_SETTINGS_PATH,
ROLE_MAPPINGS_PATH,
SECURITY_PATH,
} from './routes';
import { SourcesRouter } from './views/content_sources';
import { SourceAdded } from './views/content_sources/components/source_added';
import { SourceSubNav } from './views/content_sources/components/source_sub_nav';
import { PrivateSourcesLayout } from './views/content_sources/private_sources_layout';
import { ErrorState } from './views/error_state';
@ -82,6 +84,9 @@ export const WorkplaceSearchConfigured: React.FC<InitialAppData> = (props) => {
<Route path={SETUP_GUIDE_PATH}>
<SetupGuide />
</Route>
<Route path={SOURCE_ADDED_PATH}>
<SourceAdded />
</Route>
<Route exact path="/">
{errorConnecting ? <ErrorState /> : <OverviewMVP />}
</Route>

View file

@ -275,12 +275,12 @@ describe('AddSourceLogic', () => {
describe('saveSourceParams', () => {
const params = {
code: 'code123',
state: '"{"state": "foo"}"',
session_state: 'session123',
state:
'{"action":"create","context":"organization","service_type":"gmail","csrf_token":"token==","index_permissions":false}',
};
const queryString =
'code=code123&state=%22%7B%22state%22%3A%20%22foo%22%7D%22&session_state=session123';
'?state=%7B%22action%22:%22create%22,%22context%22:%22organization%22,%22service_type%22:%22gmail%22,%22csrf_token%22:%22token%3D%3D%22,%22index_permissions%22:false%7D&code=code123';
const response = { serviceName: 'name', indexPermissions: false, serviceType: 'zendesk' };
@ -303,9 +303,18 @@ describe('AddSourceLogic', () => {
await nextTick();
expect(setAddedSourceSpy).toHaveBeenCalledWith(serviceName, indexPermissions, serviceType);
expect(navigateToUrl).toHaveBeenCalledWith(
getSourcesPath(SOURCES_PATH, AppLogic.values.isOrganization)
);
expect(navigateToUrl).toHaveBeenCalledWith(getSourcesPath(SOURCES_PATH, true));
});
it('redirects to private dashboard when account context', async () => {
const accountQueryString =
'?state=%7B%22action%22:%22create%22,%22context%22:%22account%22,%22service_type%22:%22gmail%22,%22csrf_token%22:%22token%3D%3D%22,%22index_permissions%22:false%7D&code=code';
AddSourceLogic.actions.saveSourceParams(accountQueryString);
await nextTick();
expect(navigateToUrl).toHaveBeenCalledWith(getSourcesPath(SOURCES_PATH, false));
});
it('handles error', async () => {

View file

@ -470,12 +470,13 @@ export const AddSourceLogic = kea<MakeLogicType<AddSourceValues, AddSourceAction
},
saveSourceParams: async ({ search }) => {
const { http } = HttpLogic.values;
const { isOrganization } = AppLogic.values;
const { navigateToUrl } = KibanaLogic.values;
const { setAddedSource } = SourcesLogic.actions;
const params = (parseQueryParams(search) as unknown) as OauthParams;
const query = { ...params, kibana_host: kibanaHost };
const route = '/api/workplace_search/sources/create';
const state = JSON.parse(params.state);
const isOrganization = state.context !== 'account';
try {
const response = await http.get(route, { query });

View file

@ -11,6 +11,8 @@ import { useLocation } from 'react-router-dom';
import { Location } from 'history';
import { useActions } from 'kea';
import { EuiPage, EuiPageBody } from '@elastic/eui';
import { Loading } from '../../../../shared/loading';
import { AddSourceLogic } from './add_source/add_source_logic';
@ -28,5 +30,11 @@ export const SourceAdded: React.FC = () => {
saveSourceParams(search);
}, []);
return <Loading />;
return (
<EuiPage>
<EuiPageBody>
<Loading />
</EuiPageBody>
</EuiPage>
);
};

View file

@ -34,7 +34,7 @@ describe('SourcesRouter', () => {
});
it('renders sources routes', () => {
const TOTAL_ROUTES = 62;
const TOTAL_ROUTES = 61;
const wrapper = shallow(<SourcesRouter />);
expect(wrapper.find(Switch)).toHaveLength(1);

View file

@ -19,7 +19,6 @@ import { AppLogic } from '../../app_logic';
import { NAV } from '../../constants';
import {
ADD_SOURCE_PATH,
SOURCE_ADDED_PATH,
SOURCE_DETAILS_PATH,
PERSONAL_SOURCES_PATH,
SOURCES_PATH,
@ -27,7 +26,6 @@ import {
} from '../../routes';
import { AddSource, AddSourceList } from './components/add_source';
import { SourceAdded } from './components/source_added';
import { OrganizationSources } from './organization_sources';
import { PrivateSources } from './private_sources';
import { staticSourceData } from './source_data';
@ -115,10 +113,6 @@ export const SourcesRouter: React.FC = () => {
<SetPageChrome trail={[NAV.SOURCES, NAV.ADD_SOURCE]} />
<AddSourceList />
</Route>
<Route path={getSourcesPath(SOURCE_ADDED_PATH, isOrganization)} exact>
<SetPageChrome trail={[NAV.SOURCES, NAV.ADD_SOURCE]} />
<SourceAdded />
</Route>
<Route path={getSourcesPath(SOURCE_DETAILS_PATH, isOrganization)}>
<SourceRouter />
</Route>