Skip to content

Commit

Permalink
[Workplace Search] Fix redirect and state for personal oAuth plugin (#…
Browse files Browse the repository at this point in the history
…95238) (#95247)

* 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

Co-authored-by: Scotty Bollinger <scotty.bollinger@elastic.co>
  • Loading branch information
kibanamachine and scottybollinger authored Mar 24, 2021
1 parent 009c7b3 commit 32c0891
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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' };

Expand All @@ -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 () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -28,5 +30,11 @@ export const SourceAdded: React.FC = () => {
saveSourceParams(search);
}, []);

return <Loading />;
return (
<EuiPage>
<EuiPageBody>
<Loading />
</EuiPageBody>
</EuiPage>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@ import { AppLogic } from '../../app_logic';
import { NAV } from '../../constants';
import {
ADD_SOURCE_PATH,
SOURCE_ADDED_PATH,
SOURCE_DETAILS_PATH,
PERSONAL_SOURCES_PATH,
SOURCES_PATH,
getSourcesPath,
} 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';
Expand Down Expand Up @@ -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>
Expand Down

0 comments on commit 32c0891

Please sign in to comment.