Show content error when the top-level inventory and host inventory do not match

This commit is contained in:
Marliana Lara 2020-03-10 12:19:36 -04:00
parent bbe5789e70
commit e816f73ecf
No known key found for this signature in database
GPG Key ID: 38C73B40DFA809EE
2 changed files with 71 additions and 62 deletions

View File

@ -23,6 +23,11 @@ import JobList from '@components/JobList';
import InventoryHostDetail from '../InventoryHostDetail';
import InventoryHostEdit from '../InventoryHostEdit';
const checkHostInventory = (host, inventory) =>
host &&
inventory &&
host.summary_fields?.inventory?.id === parseInt(inventory.id, 10);
function InventoryHost({ i18n, setBreadcrumb, inventory }) {
const location = useLocation();
const match = useRouteMatch('/inventories/inventory/:id/hosts/:hostId');
@ -40,7 +45,7 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
return {
host: data,
};
}, [match.params.hostId]), // eslint-disable-line react-hooks/exhaustive-deps
}, [match.params.hostId]),
{
host: null,
}
@ -48,7 +53,7 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
useEffect(() => {
fetchHost();
}, [fetchHost]);
}, [fetchHost, location.pathname]);
useEffect(() => {
if (inventory && host) {
@ -89,24 +94,7 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
},
];
let cardHeader = (
<TabbedCardHeader>
<RoutedTabs tabsArray={tabsArray} />
<CardActions>
<CardCloseButton linkTo={hostListUrl} />
</CardActions>
</TabbedCardHeader>
);
if (location.pathname.endsWith('edit')) {
cardHeader = null;
}
if (isLoading) {
return <ContentLoading />;
}
if (!isLoading && contentError) {
if (contentError) {
return (
<Card>
<ContentError error={contentError}>
@ -123,50 +111,62 @@ function InventoryHost({ i18n, setBreadcrumb, inventory }) {
);
}
const isInventoryValid = checkHostInventory(host, inventory);
if (!isLoading && !isInventoryValid) {
return (
<ContentError isNotFound>
<Link to={hostListUrl}>{i18n._(t`View all Inventory Hosts.`)}</Link>
</ContentError>
);
}
return (
<>
{cardHeader}
<Switch>
<Redirect
from="/inventories/inventory/:id/hosts/:hostId"
to="/inventories/inventory/:id/hosts/:hostId/details"
exact
/>
{host &&
inventory && [
<Route
key="details"
path="/inventories/inventory/:id/hosts/:hostId/details"
>
<InventoryHostDetail host={host} />
</Route>,
<Route
key="edit"
path="/inventories/inventory/:id/hosts/:hostId/edit"
>
<InventoryHostEdit host={host} inventory={inventory} />
</Route>,
<Route
key="completed-jobs"
path="/inventories/inventory/:id/hosts/:hostId/completed_jobs"
>
<JobList defaultParams={{ job__hosts: host.id }} />
</Route>,
]}
<Route
key="not-found"
path="*"
render={() =>
!isLoading && (
<ContentError isNotFound>
<Link to={`${match.url}/details`}>
{i18n._(`View Inventory Host Details`)}
</Link>
</ContentError>
)
}
/>
</Switch>
{['edit'].some(name => location.pathname.includes(name)) ? null : (
<TabbedCardHeader>
<RoutedTabs tabsArray={tabsArray} />
<CardActions>
<CardCloseButton linkTo={hostListUrl} />
</CardActions>
</TabbedCardHeader>
)}
{isLoading && <ContentLoading />}
{!isLoading && isInventoryValid && (
<Switch>
<Redirect
from="/inventories/inventory/:id/hosts/:hostId"
to="/inventories/inventory/:id/hosts/:hostId/details"
exact
/>
<Route
key="details"
path="/inventories/inventory/:id/hosts/:hostId/details"
>
<InventoryHostDetail host={host} />
</Route>
<Route
key="edit"
path="/inventories/inventory/:id/hosts/:hostId/edit"
>
<InventoryHostEdit host={host} inventory={inventory} />
</Route>
<Route
key="completed-jobs"
path="/inventories/inventory/:id/hosts/:hostId/completed_jobs"
>
<JobList defaultParams={{ job__hosts: host.id }} />
</Route>
<Route key="not-found" path="*">
<ContentError isNotFound>
<Link to={`${match.url}/details`}>
{i18n._(`View Inventory Host Details`)}
</Link>
</ContentError>
</Route>
</Switch>
)}
</>
);
}

View File

@ -76,4 +76,13 @@ describe('<InventoryHost />', () => {
});
await waitForElement(wrapper, 'ContentError', el => el.length === 1);
});
test('should show content error when inventory id does not match host inventory', async () => {
await act(async () => {
wrapper = mountWithContexts(
<InventoryHost inventory={{ id: 99 }} setBreadcrumb={() => {}} />
);
});
await waitForElement(wrapper, 'ContentError', el => el.length === 1);
});
});