Compare commits

...

10 Commits

Author SHA1 Message Date
Alex Corey
e985b98d61 Adds constructed inventory groups and related groups. 2023-02-23 14:03:58 -05:00
Alex Corey
0fae313338 Merge pull request #13590 from ansible/constructedInventoryHosts
Constructed inventory hosts
2023-02-22 11:07:25 -05:00
Marliana Lara
f1cab55051 Merge pull request #13598 from marshmalien/constructed-inventory-sync-button
Add constructed inventory detail sync button
2023-02-22 10:20:45 -05:00
Alex Corey
0d88cee6bf Creates constructed inventory host lists by reusing, and renaming smart inventory host list components. 2023-02-22 10:01:31 -05:00
Marliana Lara
295ec4f22a Update inventory details after inventory source sync 2023-02-21 17:47:56 -05:00
Hao Liu
103b4567fe Merge pull request #13600 from gamuniz/fix_validation_bug_constructed
[constructed-inventory]Fix validation issue constructed
2023-02-20 10:33:21 -05:00
Gabe Muniz
8ba2b1b50c [constructed-inventory]Fix validation issue constructed 2023-02-20 10:20:23 -05:00
Marliana Lara
83ccf1dd36 Add constructed inventory detail's sync button 2023-02-18 14:51:01 -05:00
Marliana Lara
dc049af0eb Merge pull request #13578 from marshmalien/constructed-inv-search-option
[constructed-inventory] Add "constructed" option to search columns
2023-02-16 13:05:22 -05:00
Marliana Lara
8ea8558605 Add constructed inv option to search columns 2023-02-15 17:21:23 -05:00
57 changed files with 1414 additions and 520 deletions

View File

@@ -2272,7 +2272,7 @@ class InventorySourceSerializer(UnifiedJobTemplateSerializer, InventorySourceOpt
if get_field_from_model_or_attrs('source') == 'scm': if get_field_from_model_or_attrs('source') == 'scm':
if ('source' in attrs or 'source_project' in attrs) and get_field_from_model_or_attrs('source_project') is None: if ('source' in attrs or 'source_project' in attrs) and get_field_from_model_or_attrs('source_project') is None:
raise serializers.ValidationError({"source_project": _("Project required for scm type sources.")}) raise serializers.ValidationError({"source_project": _("Project required for scm type sources.")})
elif get_field_from_model_or_attrs('source') == 'constructed': elif (get_field_from_model_or_attrs('source') == 'constructed') and (self.instance and self.instance.source != 'constructed'):
raise serializers.ValidationError({"Error": _('constructed not a valid source for inventory')}) raise serializers.ValidationError({"Error": _('constructed not a valid source for inventory')})
else: else:
redundant_scm_fields = list(filter(lambda x: attrs.get(x, None), ['source_project', 'source_path'])) redundant_scm_fields = list(filter(lambda x: attrs.get(x, None), ['source_project', 'source_path']))

View File

@@ -7,5 +7,4 @@ class ConstructedInventories extends InstanceGroupsMixin(Base) {
this.baseUrl = 'api/v2/constructed_inventories/'; this.baseUrl = 'api/v2/constructed_inventories/';
} }
} }
export default ConstructedInventories; export default ConstructedInventories;

View File

@@ -13,7 +13,7 @@ class Inventories extends InstanceGroupsMixin(Base) {
this.readGroups = this.readGroups.bind(this); this.readGroups = this.readGroups.bind(this);
this.readGroupsOptions = this.readGroupsOptions.bind(this); this.readGroupsOptions = this.readGroupsOptions.bind(this);
this.promoteGroup = this.promoteGroup.bind(this); this.promoteGroup = this.promoteGroup.bind(this);
this.readSourceInventories = this.readSourceInventories.bind(this); this.readInputInventories = this.readInputInventories.bind(this);
} }
readAccessList(id, params) { readAccessList(id, params) {
@@ -73,7 +73,7 @@ class Inventories extends InstanceGroupsMixin(Base) {
}); });
} }
readSourceInventories(inventoryId, params) { readInputInventories(inventoryId, params) {
return this.http.get(`${this.baseUrl}${inventoryId}/input_inventories/`, { return this.http.get(`${this.baseUrl}${inventoryId}/input_inventories/`, {
params, params,
}); });

View File

@@ -8,11 +8,11 @@ import ContentLoading from 'components/ContentLoading';
import RoutedTabs from 'components/RoutedTabs'; import RoutedTabs from 'components/RoutedTabs';
import useRequest from 'hooks/useRequest'; import useRequest from 'hooks/useRequest';
import { InventoriesAPI } from 'api'; import { InventoriesAPI } from 'api';
import SmartInventoryHostDetail from '../SmartInventoryHostDetail'; import AdvancedInventoryHostDetail from '../AdvancedInventoryHostDetail';
function SmartInventoryHost({ inventory, setBreadcrumb }) { function AdvancedInventoryHost({ inventory, setBreadcrumb }) {
const { params, path, url } = useRouteMatch( const { params, path, url } = useRouteMatch(
'/inventories/smart_inventory/:id/hosts/:hostId' '/inventories/:inventoryType/:id/hosts/:hostId'
); );
const { const {
@@ -28,7 +28,7 @@ function SmartInventoryHost({ inventory, setBreadcrumb }) {
); );
return response; return response;
}, [inventory.id, params.hostId]), }, [inventory.id, params.hostId]),
null { isLoading: true }
); );
useEffect(() => { useEffect(() => {
@@ -44,7 +44,6 @@ function SmartInventoryHost({ inventory, setBreadcrumb }) {
if (error) { if (error) {
return <ContentError error={error} />; return <ContentError error={error} />;
} }
const tabsArray = [ const tabsArray = [
{ {
name: ( name: (
@@ -53,7 +52,7 @@ function SmartInventoryHost({ inventory, setBreadcrumb }) {
{t`Back to Hosts`} {t`Back to Hosts`}
</> </>
), ),
link: `/inventories/smart_inventory/${inventory.id}/hosts`, link: `/inventories/${params.inventoryType}/${inventory.id}/hosts`,
id: 0, id: 0,
}, },
{ {
@@ -72,17 +71,19 @@ function SmartInventoryHost({ inventory, setBreadcrumb }) {
{!isLoading && host && ( {!isLoading && host && (
<Switch> <Switch>
<Redirect <Redirect
from="/inventories/smart_inventory/:id/hosts/:hostId" from="/inventories/:inventoryType/:id/hosts/:hostId"
to={`${path}/details`} to={`${path}/details`}
exact exact
/> />
<Route key="details" path={`${path}/details`}> <Route key="details" path={`${path}/details`}>
<SmartInventoryHostDetail host={host} /> <AdvancedInventoryHostDetail host={host} />
</Route> </Route>
<Route key="not-found" path="*"> <Route key="not-found" path="*">
<ContentError isNotFound> <ContentError isNotFound>
<Link to={`${url}/details`}> <Link to={`${url}/details`}>
{t`View smart inventory host details`} {params.inventoryType === 'smart_inventory'
? t`View smart inventory host details`
: t`View constructed inventory host details`}
</Link> </Link>
</ContentError> </ContentError>
</Route> </Route>
@@ -92,4 +93,4 @@ function SmartInventoryHost({ inventory, setBreadcrumb }) {
); );
} }
export default SmartInventoryHost; export default AdvancedInventoryHost;

View File

@@ -7,14 +7,14 @@ import {
waitForElement, waitForElement,
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import mockHost from '../shared/data.host.json'; import mockHost from '../shared/data.host.json';
import SmartInventoryHost from './SmartInventoryHost'; import AdvancedInventoryHost from './AdvancedInventoryHost';
jest.mock('../../../api'); jest.mock('../../../api');
jest.mock('react-router-dom', () => ({ jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'), ...jest.requireActual('react-router-dom'),
useRouteMatch: () => ({ useRouteMatch: () => ({
params: { id: 1234, hostId: 2 }, params: { id: 1234, hostId: 2 },
path: '/inventories/smart_inventory/:id/hosts/:hostId', path: '/inventories/:inventoryType/:id/hosts/:hostId',
url: '/inventories/smart_inventory/1234/hosts/2', url: '/inventories/smart_inventory/1234/hosts/2',
}), }),
})); }));
@@ -24,7 +24,7 @@ const mockSmartInventory = {
name: 'Mock Smart Inventory', name: 'Mock Smart Inventory',
}; };
describe('<SmartInventoryHost />', () => { describe('<AdvancedInventoryHost />', () => {
let wrapper; let wrapper;
let history; let history;
@@ -36,7 +36,7 @@ describe('<SmartInventoryHost />', () => {
InventoriesAPI.readHostDetail.mockResolvedValue(mockHost); InventoriesAPI.readHostDetail.mockResolvedValue(mockHost);
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHost <AdvancedInventoryHost
inventory={mockSmartInventory} inventory={mockSmartInventory}
setBreadcrumb={() => {}} setBreadcrumb={() => {}}
/> />
@@ -55,7 +55,7 @@ describe('<SmartInventoryHost />', () => {
InventoriesAPI.readHostDetail.mockRejectedValueOnce(new Error()); InventoriesAPI.readHostDetail.mockRejectedValueOnce(new Error());
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHost <AdvancedInventoryHost
inventory={mockSmartInventory} inventory={mockSmartInventory}
setBreadcrumb={() => {}} setBreadcrumb={() => {}}
/> />
@@ -76,7 +76,7 @@ describe('<SmartInventoryHost />', () => {
}); });
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHost <AdvancedInventoryHost
inventory={mockSmartInventory} inventory={mockSmartInventory}
setBreadcrumb={() => {}} setBreadcrumb={() => {}}
/>, />,

View File

@@ -0,0 +1 @@
export { default } from './AdvancedInventoryHost';

View File

@@ -1,5 +1,5 @@
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import { Link, useParams } from 'react-router-dom';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Host } from 'types'; import { Host } from 'types';
@@ -8,7 +8,8 @@ import { Detail, DetailList, UserDateDetail } from 'components/DetailList';
import Sparkline from 'components/Sparkline'; import Sparkline from 'components/Sparkline';
import { VariablesDetail } from 'components/CodeEditor'; import { VariablesDetail } from 'components/CodeEditor';
function SmartInventoryHostDetail({ host }) { function AdvancedInventoryHostDetail({ host }) {
const { inventoryType } = useParams();
const { const {
created, created,
description, description,
@@ -24,6 +25,7 @@ function SmartInventoryHostDetail({ host }) {
type: 'job', type: 'job',
})); }));
const inventoryKind = inventory.kind === '' ? 'inventory' : inventoryType;
return ( return (
<CardBody> <CardBody>
<DetailList gutter="sm"> <DetailList gutter="sm">
@@ -37,7 +39,7 @@ function SmartInventoryHostDetail({ host }) {
<Detail <Detail
label={t`Inventory`} label={t`Inventory`}
value={ value={
<Link to={`/inventories/inventory/${inventory?.id}/details`}> <Link to={`/inventories/${inventoryKind}/${inventory?.id}/details`}>
{inventory?.name} {inventory?.name}
</Link> </Link>
} }
@@ -61,8 +63,8 @@ function SmartInventoryHostDetail({ host }) {
); );
} }
SmartInventoryHostDetail.propTypes = { AdvancedInventoryHostDetail.propTypes = {
host: Host.isRequired, host: Host.isRequired,
}; };
export default SmartInventoryHostDetail; export default AdvancedInventoryHostDetail;

View File

@@ -1,15 +1,17 @@
import React from 'react'; import React from 'react';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import SmartInventoryHostDetail from './SmartInventoryHostDetail'; import AdvancedInventoryHostDetail from './AdvancedInventoryHostDetail';
import mockHost from '../shared/data.host.json'; import mockHost from '../shared/data.host.json';
jest.mock('../../../api'); jest.mock('../../../api');
describe('<SmartInventoryHostDetail />', () => { describe('<AdvancedInventoryHostDetail />', () => {
let wrapper; let wrapper;
beforeAll(() => { beforeAll(() => {
wrapper = mountWithContexts(<SmartInventoryHostDetail host={mockHost} />); wrapper = mountWithContexts(
<AdvancedInventoryHostDetail host={mockHost} />
);
}); });
test('should render Details', () => { test('should render Details', () => {
@@ -30,11 +32,12 @@ describe('<SmartInventoryHostDetail />', () => {
test('should not load Activity', () => { test('should not load Activity', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHostDetail <AdvancedInventoryHostDetail
host={{ host={{
...mockHost, ...mockHost,
summary_fields: { summary_fields: {
recent_jobs: [], recent_jobs: [],
inventory: { kind: 'constructed', id: 2 },
}, },
}} }}
/> />

View File

@@ -0,0 +1 @@
export { default } from './AdvancedInventoryHostDetail';

View File

@@ -13,7 +13,7 @@ import { getQSConfig, parseQueryString } from 'util/qs';
import { InventoriesAPI } from 'api'; import { InventoriesAPI } from 'api';
import { Inventory } from 'types'; import { Inventory } from 'types';
import AdHocCommands from 'components/AdHocCommands/AdHocCommands'; import AdHocCommands from 'components/AdHocCommands/AdHocCommands';
import SmartInventoryHostListItem from './SmartInventoryHostListItem'; import AdvancedInventoryHostListItem from './AdvancedInventoryHostListItem';
const QS_CONFIG = getQSConfig('host', { const QS_CONFIG = getQSConfig('host', {
page: 1, page: 1,
@@ -21,7 +21,7 @@ const QS_CONFIG = getQSConfig('host', {
order_by: 'name', order_by: 'name',
}); });
function SmartInventoryHostList({ inventory }) { function AdvancedInventoryHostList({ inventory }) {
const location = useLocation(); const location = useLocation();
const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false); const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false);
const { const {
@@ -61,7 +61,10 @@ function SmartInventoryHostList({ inventory }) {
useEffect(() => { useEffect(() => {
fetchHosts(); fetchHosts();
}, [fetchHosts]); }, [fetchHosts]);
const inventoryType =
inventory.kind === 'constructed'
? 'constructed_inventory'
: 'smart_inventory';
return ( return (
<PaginatedTable <PaginatedTable
contentError={contentError} contentError={contentError}
@@ -114,10 +117,11 @@ function SmartInventoryHostList({ inventory }) {
</HeaderRow> </HeaderRow>
} }
renderRow={(host, index) => ( renderRow={(host, index) => (
<SmartInventoryHostListItem <AdvancedInventoryHostListItem
key={host.id} key={host.id}
host={host} host={host}
detailUrl={`/inventories/smart_inventory/${inventory.id}/hosts/${host.id}/details`} inventoryType={inventoryType}
detailUrl={`/inventories/${inventoryType}/${inventory.id}/hosts/${host.id}/details`}
isSelected={selected.some((row) => row.id === host.id)} isSelected={selected.some((row) => row.id === host.id)}
onSelect={() => handleSelect(host)} onSelect={() => handleSelect(host)}
rowIndex={index} rowIndex={index}
@@ -127,8 +131,8 @@ function SmartInventoryHostList({ inventory }) {
); );
} }
SmartInventoryHostList.propTypes = { AdvancedInventoryHostList.propTypes = {
inventory: Inventory.isRequired, inventory: Inventory.isRequired,
}; };
export default SmartInventoryHostList; export default AdvancedInventoryHostList;

View File

@@ -5,13 +5,13 @@ import {
mountWithContexts, mountWithContexts,
waitForElement, waitForElement,
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import SmartInventoryHostList from './SmartInventoryHostList'; import AdvancedInventoryHostList from './AdvancedInventoryHostList';
import mockInventory from '../shared/data.inventory.json'; import mockInventory from '../shared/data.inventory.json';
import mockHosts from '../shared/data.hosts.json'; import mockHosts from '../shared/data.hosts.json';
jest.mock('../../../api'); jest.mock('../../../api');
describe('<SmartInventoryHostList />', () => { describe('<AdvancedInventoryHostList />', () => {
let wrapper; let wrapper;
const clonedInventory = { const clonedInventory = {
...mockInventory, ...mockInventory,
@@ -44,7 +44,7 @@ describe('<SmartInventoryHostList />', () => {
}); });
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHostList inventory={clonedInventory} /> <AdvancedInventoryHostList inventory={clonedInventory} />
); );
}); });
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0); await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
@@ -55,12 +55,12 @@ describe('<SmartInventoryHostList />', () => {
}); });
test('initially renders successfully', () => { test('initially renders successfully', () => {
expect(wrapper.find('SmartInventoryHostList').length).toBe(1); expect(wrapper.find('AdvancedInventoryHostList').length).toBe(1);
}); });
test('should fetch hosts from api and render them in the list', () => { test('should fetch hosts from api and render them in the list', () => {
expect(InventoriesAPI.readHosts).toHaveBeenCalled(); expect(InventoriesAPI.readHosts).toHaveBeenCalled();
expect(wrapper.find('SmartInventoryHostListItem').length).toBe(3); expect(wrapper.find('AdvancedInventoryHostListItem').length).toBe(3);
}); });
test('should select and deselect all items', async () => { test('should select and deselect all items', async () => {
@@ -87,7 +87,7 @@ describe('<SmartInventoryHostList />', () => {
); );
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHostList inventory={mockInventory} /> <AdvancedInventoryHostList inventory={mockInventory} />
); );
}); });
await waitForElement(wrapper, 'ContentError', (el) => el.length === 1); await waitForElement(wrapper, 'ContentError', (el) => el.length === 1);

View File

@@ -9,20 +9,26 @@ import { Tr, Td } from '@patternfly/react-table';
import Sparkline from 'components/Sparkline'; import Sparkline from 'components/Sparkline';
import { Host } from 'types'; import { Host } from 'types';
function SmartInventoryHostListItem({ function AdvancedInventoryHostListItem({
detailUrl, detailUrl,
host, host: {
name,
id,
summary_fields: { recent_jobs, inventory },
},
isSelected, isSelected,
onSelect, onSelect,
rowIndex, rowIndex,
inventoryType,
}) { }) {
const recentPlaybookJobs = host.summary_fields.recent_jobs.map((job) => ({ const recentPlaybookJobs = recent_jobs.map((job) => ({
...job, ...job,
type: 'job', type: 'job',
})); }));
const inventoryKind = inventory.kind === '' ? 'inventory' : inventoryType;
const inventoryLink = `/inventories/${inventoryKind}/${inventory.id}/details`;
return ( return (
<Tr id={`host-row-${host.id}`} ouiaId={`host-row-${host.id}`}> <Tr id={`host-row-${id}`} ouiaId={`host-row-${id}`}>
<Td <Td
select={{ select={{
rowIndex, rowIndex,
@@ -32,28 +38,24 @@ function SmartInventoryHostListItem({
/> />
<Td dataLabel={t`Name`}> <Td dataLabel={t`Name`}>
<Link to={`${detailUrl}`}> <Link to={`${detailUrl}`}>
<b>{host.name}</b> <b>{name}</b>
</Link> </Link>
</Td> </Td>
<Td dataLabel={t`Recent jobs`}> <Td dataLabel={t`Recent jobs`}>
<Sparkline jobs={recentPlaybookJobs} /> <Sparkline jobs={recentPlaybookJobs} />
</Td> </Td>
<Td dataLabel={t`Inventory`}> <Td dataLabel={t`Inventory`}>
<Link <Link to={inventoryLink}>{inventory.name}</Link>
to={`/inventories/inventory/${host.summary_fields.inventory.id}/details`}
>
{host.summary_fields.inventory.name}
</Link>
</Td> </Td>
</Tr> </Tr>
); );
} }
SmartInventoryHostListItem.propTypes = { AdvancedInventoryHostListItem.propTypes = {
detailUrl: string.isRequired, detailUrl: string.isRequired,
host: Host.isRequired, host: Host.isRequired,
isSelected: bool.isRequired, isSelected: bool.isRequired,
onSelect: func.isRequired, onSelect: func.isRequired,
}; };
export default SmartInventoryHostListItem; export default AdvancedInventoryHostListItem;

View File

@@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import SmartInventoryHostListItem from './SmartInventoryHostListItem'; import AdvancedInventoryHostListItem from './AdvancedInventoryHostListItem';
const mockHost = { const mockHost = {
id: 2, id: 2,
@@ -19,14 +19,14 @@ const mockHost = {
}, },
}; };
describe('<SmartInventoryHostListItem />', () => { describe('<AdvancedInventoryHostListItem />', () => {
let wrapper; let wrapper;
beforeEach(() => { beforeEach(() => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<table> <table>
<tbody> <tbody>
<SmartInventoryHostListItem <AdvancedInventoryHostListItem
detailUrl="/inventories/smart_inventory/1/hosts/2" detailUrl="/inventories/smart_inventory/1/hosts/2"
host={mockHost} host={mockHost}
isSelected={false} isSelected={false}

View File

@@ -0,0 +1,27 @@
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import { Inventory } from 'types';
import AdvancedInventoryHostList from './AdvancedInventoryHostList';
import AdvancedInventoryHost from '../AdvancedInventoryHost';
function AdvancedInventoryHosts({ inventory, setBreadcrumb }) {
return (
<Switch>
<Route key="host" path="/inventories/:inventoryType/:id/hosts/:hostId">
<AdvancedInventoryHost
setBreadcrumb={setBreadcrumb}
inventory={inventory}
/>
</Route>
<Route key="host-list" path="/inventories/:inventoryType/:id/hosts">
<AdvancedInventoryHostList inventory={inventory} />
</Route>
</Switch>
);
}
AdvancedInventoryHosts.propTypes = {
inventory: Inventory.isRequired,
};
export default AdvancedInventoryHosts;

View File

@@ -5,37 +5,39 @@ import {
mountWithContexts, mountWithContexts,
waitForElement, waitForElement,
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import SmartInventoryHosts from './SmartInventoryHosts'; import AdvancedInventoryHosts from './AdvancedInventoryHosts';
jest.mock('../../../api'); jest.mock('../../../api');
jest.mock('./SmartInventoryHostList', () => { jest.mock('./AdvancedInventoryHostList', () => {
const SmartInventoryHostList = () => <div />; const AdvancedInventoryHostList = () => <div />;
return { return {
__esModule: true, __esModule: true,
default: SmartInventoryHostList, default: AdvancedInventoryHostList,
}; };
}); });
describe('<SmartInventoryHosts />', () => { describe('<AdvancedInventoryHosts />', () => {
test('should render smart inventory host list', () => { test('should render smart inventory host list', () => {
const history = createMemoryHistory({ const history = createMemoryHistory({
initialEntries: ['/inventories/smart_inventory/1/hosts'], initialEntries: ['/inventories/smart_inventory/1/hosts'],
}); });
const match = { const match = {
path: '/inventories/smart_inventory/:id/hosts', path: '/inventories/:inventoryType/:id/hosts',
url: '/inventories/smart_inventory/1/hosts', url: '/inventories/smart_inventory/1/hosts',
isExact: true, isExact: true,
}; };
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<SmartInventoryHosts inventory={{ id: 1 }} />, <AdvancedInventoryHosts inventory={{ id: 1 }} />,
{ {
context: { router: { history, route: { match } } }, context: { router: { history, route: { match } } },
} }
); );
expect(wrapper.find('SmartInventoryHostList').length).toBe(1); expect(wrapper.find('AdvancedInventoryHostList').length).toBe(1);
expect(wrapper.find('SmartInventoryHostList').prop('inventory')).toEqual({ expect(wrapper.find('AdvancedInventoryHostList').prop('inventory')).toEqual(
id: 1, {
}); id: 1,
}
);
jest.clearAllMocks(); jest.clearAllMocks();
}); });
@@ -45,20 +47,23 @@ describe('<SmartInventoryHosts />', () => {
initialEntries: ['/inventories/smart_inventory/1/hosts/2'], initialEntries: ['/inventories/smart_inventory/1/hosts/2'],
}); });
const match = { const match = {
path: '/inventories/smart_inventory/:id/hosts/:hostId', path: '/inventories/:inventoryType/:id/hosts/:hostId',
url: '/inventories/smart_inventory/1/hosts/2', url: '/inventories/smart_inventory/1/hosts/2',
isExact: true, isExact: true,
}; };
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<SmartInventoryHosts inventory={{ id: 1 }} setBreadcrumb={() => {}} />, <AdvancedInventoryHosts
inventory={{ id: 1 }}
setBreadcrumb={() => {}}
/>,
{ {
context: { router: { history, route: { match } } }, context: { router: { history, route: { match } } },
} }
); );
}); });
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0); await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
expect(wrapper.find('SmartInventoryHost').length).toBe(1); expect(wrapper.find('AdvancedInventoryHost').length).toBe(1);
jest.clearAllMocks(); jest.clearAllMocks();
}); });
}); });

View File

@@ -0,0 +1 @@
export { default } from './AdvancedInventoryHosts';

View File

@@ -22,8 +22,8 @@ import { ResourceAccessList } from 'components/ResourceAccessList';
import RoutedTabs from 'components/RoutedTabs'; import RoutedTabs from 'components/RoutedTabs';
import ConstructedInventoryDetail from './ConstructedInventoryDetail'; import ConstructedInventoryDetail from './ConstructedInventoryDetail';
import ConstructedInventoryEdit from './ConstructedInventoryEdit'; import ConstructedInventoryEdit from './ConstructedInventoryEdit';
import ConstructedInventoryGroups from './ConstructedInventoryGroups'; import InventoryGroups from './InventoryGroups';
import ConstructedInventoryHosts from './ConstructedInventoryHosts'; import AdvancedInventoryHosts from './AdvancedInventoryHosts';
import { getInventoryPath } from './shared/utils'; import { getInventoryPath } from './shared/utils';
function ConstructedInventory({ setBreadcrumb }) { function ConstructedInventory({ setBreadcrumb }) {
@@ -42,8 +42,7 @@ function ConstructedInventory({ setBreadcrumb }) {
); );
return data; return data;
}, [match.params.id]), }, [match.params.id]),
{ isLoading: true }
null
); );
useEffect(() => { useEffect(() => {
@@ -111,7 +110,11 @@ function ConstructedInventory({ setBreadcrumb }) {
} }
let showCardHeader = true; let showCardHeader = true;
if (['edit'].some((name) => location.pathname.includes(name))) { if (
['edit', 'add', 'groups/', 'hosts/'].some((name) =>
location.pathname.includes(name)
)
) {
showCardHeader = false; showCardHeader = false;
} }
@@ -154,13 +157,19 @@ function ConstructedInventory({ setBreadcrumb }) {
path="/inventories/constructed_inventory/:id/hosts" path="/inventories/constructed_inventory/:id/hosts"
key="hosts" key="hosts"
> >
<ConstructedInventoryHosts /> <AdvancedInventoryHosts
inventory={inventory}
setBreadcrumb={setBreadcrumb}
/>
</Route>, </Route>,
<Route <Route
path="/inventories/constructed_inventory/:id/groups" path="/inventories/constructed_inventory/:id/groups"
key="groups" key="constructed_inventory_groups"
> >
<ConstructedInventoryGroups /> <InventoryGroups
inventory={inventory}
setBreadcrumb={setBreadcrumb}
/>
</Route>, </Route>,
<Route <Route
key="jobs" key="jobs"

View File

@@ -2,7 +2,10 @@ import React from 'react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import { createMemoryHistory } from 'history'; import { createMemoryHistory } from 'history';
import { ConstructedInventoriesAPI } from 'api'; import { ConstructedInventoriesAPI } from 'api';
import { mountWithContexts } from '../../../testUtils/enzymeHelpers'; import {
mountWithContexts,
waitForElement,
} from '../../../testUtils/enzymeHelpers';
import mockInventory from './shared/data.inventory.json'; import mockInventory from './shared/data.inventory.json';
import ConstructedInventory from './ConstructedInventory'; import ConstructedInventory from './ConstructedInventory';
@@ -18,13 +21,10 @@ jest.mock('react-router-dom', () => ({
describe('<ConstructedInventory />', () => { describe('<ConstructedInventory />', () => {
let wrapper; let wrapper;
beforeEach(async () => { test('should render expected tabs', async () => {
ConstructedInventoriesAPI.readDetail.mockResolvedValue({ ConstructedInventoriesAPI.readDetail.mockResolvedValue({
data: mockInventory, data: mockInventory,
}); });
});
test('should render expected tabs', async () => {
const expectedTabs = [ const expectedTabs = [
'Back to Inventories', 'Back to Inventories',
'Details', 'Details',
@@ -45,6 +45,9 @@ describe('<ConstructedInventory />', () => {
}); });
test('should show content error when user attempts to navigate to erroneous route', async () => { test('should show content error when user attempts to navigate to erroneous route', async () => {
ConstructedInventoriesAPI.readDetail.mockResolvedValue({
data: { ...mockInventory, kind: 'constructed' },
});
const history = createMemoryHistory({ const history = createMemoryHistory({
initialEntries: ['/inventories/constructed_inventory/1/foobar'], initialEntries: ['/inventories/constructed_inventory/1/foobar'],
}); });
@@ -60,7 +63,7 @@ describe('<ConstructedInventory />', () => {
match: { match: {
params: { id: 1 }, params: { id: 1 },
url: '/inventories/constructed_inventory/1/foobar', url: '/inventories/constructed_inventory/1/foobar',
path: '/inventories/constructed_inventory/1/foobar', path: '/inventories/:inventoryType/:id/foobar',
}, },
}, },
}, },
@@ -68,6 +71,7 @@ describe('<ConstructedInventory />', () => {
} }
); );
}); });
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
expect(wrapper.find('ContentError').length).toBe(1); expect(wrapper.find('ContentError').length).toBe(1);
}); });
}); });

View File

@@ -5,54 +5,103 @@ import { t } from '@lingui/macro';
import { import {
Button, Button,
Chip, Chip,
Label,
LabelGroup,
TextList, TextList,
TextListItem, TextListItem,
TextListItemVariants, TextListItemVariants,
TextListVariants, TextListVariants,
Tooltip,
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import { InventoriesAPI, ConstructedInventoriesAPI } from 'api';
import { Inventory } from 'types';
import { formatDateString } from 'util/dates';
import { relatedResourceDeleteRequests } from 'util/getRelatedResourceDeleteDetails';
import useRequest, { useDismissableError } from 'hooks/useRequest';
import AlertModal from 'components/AlertModal'; import AlertModal from 'components/AlertModal';
import { CardBody, CardActionsRow } from 'components/Card'; import { CardBody, CardActionsRow } from 'components/Card';
import { DetailList, Detail, UserDateDetail } from 'components/DetailList'; import ChipGroup from 'components/ChipGroup';
import { VariablesDetail } from 'components/CodeEditor'; import { VariablesDetail } from 'components/CodeEditor';
import DeleteButton from 'components/DeleteButton';
import ErrorDetail from 'components/ErrorDetail';
import ContentError from 'components/ContentError'; import ContentError from 'components/ContentError';
import ContentLoading from 'components/ContentLoading'; import ContentLoading from 'components/ContentLoading';
import ChipGroup from 'components/ChipGroup'; import { DetailList, Detail, UserDateDetail } from 'components/DetailList';
import Popover from 'components/Popover'; import DeleteButton from 'components/DeleteButton';
import { InventoriesAPI, ConstructedInventoriesAPI } from 'api'; import ErrorDetail from 'components/ErrorDetail';
import useRequest, { useDismissableError } from 'hooks/useRequest';
import { Inventory } from 'types';
import { relatedResourceDeleteRequests } from 'util/getRelatedResourceDeleteDetails';
import InstanceGroupLabels from 'components/InstanceGroupLabels'; import InstanceGroupLabels from 'components/InstanceGroupLabels';
import JobCancelButton from 'components/JobCancelButton';
import Popover from 'components/Popover';
import StatusLabel from 'components/StatusLabel';
import ConstructedInventorySyncButton from './ConstructedInventorySyncButton';
import useWsInventorySourcesDetails from '../shared/useWsInventorySourcesDetails';
import getHelpText from '../shared/Inventory.helptext'; import getHelpText from '../shared/Inventory.helptext';
function JobStatusLabel({ job }) {
if (!job) {
return null;
}
return (
<Tooltip
position="top"
content={
<>
<div>{t`MOST RECENT SYNC`}</div>
<div>
{t`JOB ID:`} {job.id}
</div>
<div>
{t`STATUS:`} {job.status.toUpperCase()}
</div>
{job.finished && (
<div>
{t`FINISHED:`} {formatDateString(job.finished)}
</div>
)}
</>
}
key={job.id}
>
<Link to={`/jobs/inventory/${job.id}`}>
<StatusLabel status={job.status} />
</Link>
</Tooltip>
);
}
function ConstructedInventoryDetail({ inventory }) { function ConstructedInventoryDetail({ inventory }) {
const history = useHistory(); const history = useHistory();
const helpText = getHelpText(); const helpText = getHelpText();
const { const {
result: { instanceGroups, sourceInventories, actions }, result: { instanceGroups, inputInventories, inventorySource, actions },
request: fetchRelatedDetails, request: fetchRelatedDetails,
error: contentError, error: contentError,
isLoading, isLoading,
} = useRequest( } = useRequest(
useCallback(async () => { useCallback(async () => {
const [response, sourceInvResponse, options] = await Promise.all([ const [
instanceGroupsResponse,
inputInventoriesResponse,
inventorySourceResponse,
optionsResponse,
] = await Promise.all([
InventoriesAPI.readInstanceGroups(inventory.id), InventoriesAPI.readInstanceGroups(inventory.id),
InventoriesAPI.readSourceInventories(inventory.id), InventoriesAPI.readInputInventories(inventory.id),
ConstructedInventoriesAPI.readOptions(inventory.id), InventoriesAPI.readSources(inventory.id),
ConstructedInventoriesAPI.readOptions(),
]); ]);
return { return {
instanceGroups: response.data.results, instanceGroups: instanceGroupsResponse.data.results,
sourceInventories: sourceInvResponse.data.results, inputInventories: inputInventoriesResponse.data.results,
actions: options.data.actions.GET, inventorySource: inventorySourceResponse.data.results[0],
actions: optionsResponse.data.actions.GET,
}; };
}, [inventory.id]), }, [inventory.id]),
{ {
instanceGroups: [], instanceGroups: [],
sourceInventories: [], inputInventories: [],
inventorySource: {},
actions: {}, actions: {},
isLoading: true, isLoading: true,
} }
@@ -62,6 +111,16 @@ function ConstructedInventoryDetail({ inventory }) {
fetchRelatedDetails(); fetchRelatedDetails();
}, [fetchRelatedDetails]); }, [fetchRelatedDetails]);
const wsInventorySource = useWsInventorySourcesDetails(inventorySource);
const inventorySourceSyncJob =
wsInventorySource.summary_fields?.current_job ||
wsInventorySource.summary_fields?.last_job ||
null;
const wsInventory = {
...inventory,
...wsInventorySource?.summary_fields?.inventory,
};
const { request: deleteInventory, error: deleteError } = useRequest( const { request: deleteInventory, error: deleteError } = useRequest(
useCallback(async () => { useCallback(async () => {
await InventoriesAPI.destroy(inventory.id); await InventoriesAPI.destroy(inventory.id);
@@ -71,9 +130,6 @@ function ConstructedInventoryDetail({ inventory }) {
const { error, dismissError } = useDismissableError(deleteError); const { error, dismissError } = useDismissableError(deleteError);
const { organization, user_capabilities: userCapabilities } =
inventory.summary_fields;
const deleteDetailsRequests = const deleteDetailsRequests =
relatedResourceDeleteRequests.inventory(inventory); relatedResourceDeleteRequests.inventory(inventory);
@@ -93,6 +149,14 @@ function ConstructedInventoryDetail({ inventory }) {
value={inventory.name} value={inventory.name}
dataCy="constructed-inventory-name" dataCy="constructed-inventory-name"
/> />
<Detail
label={t`Last Job Status`}
value={
inventorySourceSyncJob && (
<JobStatusLabel job={inventorySourceSyncJob} />
)
}
/>
<Detail <Detail
label={t`Description`} label={t`Description`}
value={inventory.description} value={inventory.description}
@@ -113,26 +177,28 @@ function ConstructedInventoryDetail({ inventory }) {
label={t`Organization`} label={t`Organization`}
dataCy="constructed-inventory-organization" dataCy="constructed-inventory-organization"
value={ value={
<Link to={`/organizations/${organization.id}/details`}> <Link
{organization.name} to={`/organizations/${inventory.summary_fields?.organization.id}/details`}
>
{inventory.summary_fields?.organization.name}
</Link> </Link>
} }
/> />
<Detail <Detail
label={actions.total_groups.label} label={actions.total_groups.label}
value={inventory.total_groups} value={wsInventory.total_groups}
helpText={actions.total_groups.help_text} helpText={actions.total_groups.help_text}
dataCy="constructed-inventory-total-groups" dataCy="constructed-inventory-total-groups"
/> />
<Detail <Detail
label={actions.total_hosts.label} label={actions.total_hosts.label}
value={inventory.total_hosts} value={wsInventory.total_hosts}
helpText={actions.total_hosts.help_text} helpText={actions.total_hosts.help_text}
dataCy="constructed-inventory-total-hosts" dataCy="constructed-inventory-total-hosts"
/> />
<Detail <Detail
label={actions.total_inventory_sources.label} label={actions.total_inventory_sources.label}
value={inventory.total_inventory_sources} value={wsInventory.total_inventory_sources}
helpText={actions.total_inventory_sources.help_text} helpText={actions.total_inventory_sources.help_text}
dataCy="constructed-inventory-sources" dataCy="constructed-inventory-sources"
/> />
@@ -144,7 +210,7 @@ function ConstructedInventoryDetail({ inventory }) {
/> />
<Detail <Detail
label={actions.inventory_sources_with_failures.label} label={actions.inventory_sources_with_failures.label}
value={inventory.inventory_sources_with_failures} value={wsInventory.inventory_sources_with_failures}
helpText={actions.inventory_sources_with_failures.help_text} helpText={actions.inventory_sources_with_failures.help_text}
dataCy="constructed-inventory-sources-with-failures" dataCy="constructed-inventory-sources-with-failures"
/> />
@@ -204,26 +270,29 @@ function ConstructedInventoryDetail({ inventory }) {
/> />
<Detail <Detail
fullWidth fullWidth
label={t`Source Inventories`} label={t`Input Inventories`}
value={ value={
<ChipGroup <LabelGroup numLabels={5}>
numChips={5} {inputInventories?.map((inputInventory) => (
totalChips={sourceInventories?.length} <Label
ouiaId="source-inventory-chips" color="blue"
> key={inputInventory.id}
{sourceInventories?.map((sourceInventory) => ( render={({ className, content, componentRef }) => (
<Link <Link
key={sourceInventory.id} className={className}
to={`/inventories/inventory/${sourceInventory.id}/details`} innerRef={componentRef}
to={`/inventories/inventory/${inputInventory.id}/details`}
>
{content}
</Link>
)}
> >
<Chip key={sourceInventory.id} isReadOnly> {inputInventory.name}
{sourceInventory.name} </Label>
</Chip>
</Link>
))} ))}
</ChipGroup> </LabelGroup>
} }
isEmpty={sourceInventories?.length === 0} isEmpty={inputInventories?.length === 0}
/> />
<VariablesDetail <VariablesDetail
label={actions.source_vars.label} label={actions.source_vars.label}
@@ -245,7 +314,7 @@ function ConstructedInventoryDetail({ inventory }) {
/> />
</DetailList> </DetailList>
<CardActionsRow> <CardActionsRow>
{userCapabilities.edit && ( {inventory?.summary_fields?.user_capabilities?.edit && (
<Button <Button
ouiaId="inventory-detail-edit-button" ouiaId="inventory-detail-edit-button"
component={Link} component={Link}
@@ -254,7 +323,21 @@ function ConstructedInventoryDetail({ inventory }) {
{t`Edit`} {t`Edit`}
</Button> </Button>
)} )}
{userCapabilities.delete && ( {inventorySource?.summary_fields?.user_capabilities?.start &&
(['new', 'running', 'pending', 'waiting'].includes(
inventorySourceSyncJob?.status
) ? (
<JobCancelButton
job={{ id: inventorySourceSyncJob.id, type: 'inventory_update' }}
errorTitle={t`Constructed Inventory Source Sync Error`}
title={t`Cancel Constructed Inventory Source Sync`}
errorMessage={t`Failed to cancel Constructed Inventory Source Sync`}
buttonText={t`Cancel Sync`}
/>
) : (
<ConstructedInventorySyncButton inventoryId={inventory.id} />
))}
{inventory?.summary_fields?.user_capabilities?.delete && (
<DeleteButton <DeleteButton
name={inventory.name} name={inventory.name}
modalTitle={t`Delete Inventory`} modalTitle={t`Delete Inventory`}

View File

@@ -1,8 +1,17 @@
import React from 'react'; import React from 'react';
import { act } from 'react-dom/test-utils'; import { Router } from 'react-router-dom';
import { InventoriesAPI, CredentialTypesAPI } from 'api'; import { InventoriesAPI, ConstructedInventoriesAPI } from 'api';
import {
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; render,
screen,
waitForElementToBeRemoved,
} from '@testing-library/react';
import '@testing-library/jest-dom';
import { createMemoryHistory } from 'history';
import { I18nProvider } from '@lingui/react';
import { i18n } from '@lingui/core';
import { en } from 'make-plural/plurals';
import english from '../../../locales/en/messages';
import ConstructedInventoryDetail from './ConstructedInventoryDetail'; import ConstructedInventoryDetail from './ConstructedInventoryDetail';
jest.mock('../../../api'); jest.mock('../../../api');
@@ -30,6 +39,15 @@ const mockInventory = {
copy: true, copy: true,
adhoc: true, adhoc: true,
}, },
labels: {
count: 1,
results: [
{
id: 17,
name: 'seventeen',
},
],
},
}, },
created: '2019-10-04T16:56:48.025455Z', created: '2019-10-04T16:56:48.025455Z',
modified: '2019-10-04T16:56:48.025468Z', modified: '2019-10-04T16:56:48.025468Z',
@@ -46,21 +64,187 @@ const mockInventory = {
total_inventory_sources: 0, total_inventory_sources: 0,
inventory_sources_with_failures: 0, inventory_sources_with_failures: 0,
pending_deletion: false, pending_deletion: false,
prevent_instance_group_fallback: false, prevent_instance_group_fallback: true,
update_cache_timeout: 0, update_cache_timeout: 0,
limit: '', limit: '',
verbosity: 1, verbosity: 1,
source_vars:
'{\n "plugin": "constructed",\n "strict": true,\n "groups": {\n "shutdown": "resolved_state == \\"shutdown\\"",\n "shutdown_in_product_dev": "resolved_state == \\"shutdown\\" and account_alias == \\"product_dev\\""\n },\n "compose": {\n "resolved_state": "state | default(\\"running\\")"\n }\n}',
}; };
describe('<ConstructedInventoryDetail />', () => { describe('<ConstructedInventoryDetail />', () => {
test('initially renders successfully', async () => { const history = createMemoryHistory({
let wrapper; initialEntries: ['/inventories/constructed_inventory/1/details'],
await act(async () => { });
wrapper = mountWithContexts(
<ConstructedInventoryDetail inventory={mockInventory} /> const Component = (props) => (
); <I18nProvider i18n={i18n}>
<Router history={history}>
<ConstructedInventoryDetail inventory={mockInventory} {...props} />
</Router>
</I18nProvider>
);
beforeEach(() => {
i18n.loadLocaleData({ en: { plurals: en } });
i18n.load({ en: english });
i18n.activate('en');
InventoriesAPI.readInstanceGroups.mockResolvedValue({
data: { results: [] },
}); });
expect(wrapper.length).toBe(1); InventoriesAPI.readInputInventories.mockResolvedValue({
expect(wrapper.find('ConstructedInventoryDetail').length).toBe(1); data: {
results: [
{
id: 123,
name: 'input_inventory_123',
},
{
id: 456,
name: 'input_inventory_456',
},
],
},
});
InventoriesAPI.readSources.mockResolvedValue({
data: {
results: [
{
id: 999,
type: 'inventory_source',
summary_fields: {
last_job: {
id: 101,
name: 'Auto-created source for: Constructed Inv',
status: 'successful',
finished: '2023-02-02T22:22:22.222220Z',
},
user_capabilities: {
start: true,
},
},
},
],
},
});
ConstructedInventoriesAPI.readOptions.mockResolvedValue({
data: {
related: {},
actions: {
GET: {
limit: {
label: 'Limit',
help_text: '',
},
total_groups: {
label: 'Total Groups',
help_text: '',
},
total_hosts: {
label: 'Total Hosts',
help_text: '',
},
total_inventory_sources: {
label: 'Total inventory sources',
help_text: '',
},
update_cache_timeout: {
label: 'Update cache timeout',
help_text: '',
},
inventory_sources_with_failures: {
label: 'Inventory sources with failures',
help_text: '',
},
source_vars: {
label: 'Source vars',
help_text: '',
},
verbosity: {
label: 'Verbosity',
help_text: '',
},
created: {
label: 'Created by',
help_text: '',
},
modified: {
label: 'Modified by',
help_text: '',
},
},
},
},
});
});
afterEach(() => {
jest.clearAllMocks();
});
test('should render details', async () => {
render(<Component />);
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'));
expect(screen.getByText('Name')).toBeInTheDocument();
expect(screen.getByText('Constructed Inv')).toBeInTheDocument();
expect(screen.getByText('Last Job Status')).toBeInTheDocument();
expect(screen.getByText('Successful')).toBeInTheDocument();
expect(screen.getByText('Type')).toBeInTheDocument();
expect(screen.getByText('Constructed Inventory')).toBeInTheDocument();
});
test('should render action buttons', async () => {
render(<Component />);
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'));
expect(screen.getByRole('link', { name: 'Edit' })).toHaveAttribute(
'href',
'/inventories/constructed_inventory/1/edit'
);
expect(
screen.getByRole('button', { name: 'Start inventory source sync' })
).toBeInTheDocument();
expect(screen.getByRole('button', { name: 'Delete' })).toBeInTheDocument();
});
test('should show cancel sync button during an inventory source sync running job', async () => {
InventoriesAPI.readSources.mockResolvedValue({
data: {
results: [
{
id: 999,
type: 'inventory_source',
summary_fields: {
current_job: {
id: 111,
name: 'Auto-created source for: Constructed Inv',
status: 'running',
},
user_capabilities: {
start: true,
},
},
},
],
},
});
render(<Component />);
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'));
expect(
screen.getByRole('button', {
name: 'Cancel Constructed Inventory Source Sync',
})
).toBeInTheDocument();
});
test('should show error when the api throws while fetching details', async () => {
InventoriesAPI.readInputInventories.mockRejectedValueOnce(new Error());
render(<Component />);
await waitForElementToBeRemoved(() => screen.getByRole('progressbar'));
expect(
screen.getByText(
'There was an error loading this content. Please reload the page.'
)
).toBeInTheDocument();
}); });
}); });

View File

@@ -0,0 +1,59 @@
import React, { useCallback } from 'react';
import { t } from '@lingui/macro';
import PropTypes from 'prop-types';
import { Button, Tooltip } from '@patternfly/react-core';
import useRequest, { useDismissableError } from 'hooks/useRequest';
import AlertModal from 'components/AlertModal/AlertModal';
import ErrorDetail from 'components/ErrorDetail/ErrorDetail';
import { InventoriesAPI } from 'api';
function ConstructedInventorySyncButton({ inventoryId }) {
const testId = `constructed-inventory-${inventoryId}-sync`;
const {
isLoading: startSyncLoading,
error: startSyncError,
request: startSyncProcess,
} = useRequest(
useCallback(
async () => InventoriesAPI.syncAllSources(inventoryId),
[inventoryId]
),
{}
);
const { error: startError, dismissError: dismissStartError } =
useDismissableError(startSyncError);
return (
<>
<Tooltip content={t`Start sync process`} position="top">
<Button
ouiaId={testId}
isDisabled={startSyncLoading}
aria-label={t`Start inventory source sync`}
variant="secondary"
onClick={startSyncProcess}
>
{t`Sync`}
</Button>
</Tooltip>
{startError && (
<AlertModal
isOpen={startError}
variant="error"
title={t`Error!`}
onClose={dismissStartError}
>
{t`Failed to sync constructed inventory source`}
<ErrorDetail error={startError} />
</AlertModal>
)}
</>
);
}
ConstructedInventorySyncButton.propTypes = {
inventoryId: PropTypes.number.isRequired,
};
export default ConstructedInventorySyncButton;

View File

@@ -0,0 +1,41 @@
import React from 'react';
import { InventoriesAPI } from 'api';
import ConstructedInventorySyncButton from './ConstructedInventorySyncButton';
import { render, fireEvent, screen, waitFor } from '@testing-library/react';
import '@testing-library/jest-dom';
jest.mock('../../../api');
const inventory = { id: 100, name: 'Constructed Inventory' };
describe('<ConstructedInventorySyncButton />', () => {
const Component = () => (
<ConstructedInventorySyncButton inventoryId={inventory.id} />
);
test('should render start sync button', () => {
render(<Component />);
expect(
screen.getByRole('button', { name: 'Start inventory source sync' })
).toBeInTheDocument();
});
test('should make expected api request on sync', async () => {
render(<Component />);
const syncButton = screen.queryByText('Sync');
fireEvent.click(syncButton);
await waitFor(() =>
expect(InventoriesAPI.syncAllSources).toHaveBeenCalledWith(100)
);
});
test('should show alert modal on throw', async () => {
InventoriesAPI.syncAllSources.mockRejectedValueOnce(new Error());
render(<Component />);
await waitFor(() => {
const syncButton = screen.queryByText('Sync');
fireEvent.click(syncButton);
});
expect(screen.getByRole('dialog', { name: 'Alert modal Error!' }));
});
});

View File

@@ -1,13 +0,0 @@
/* eslint i18next/no-literal-string: "off" */
import React from 'react';
import { CardBody } from 'components/Card';
function ConstructedInventoryGroups() {
return (
<CardBody>
<div>Coming Soon!</div>
</CardBody>
);
}
export default ConstructedInventoryGroups;

View File

@@ -1,15 +0,0 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import ConstructedInventoryGroups from './ConstructedInventoryGroups';
describe('<ConstructedInventoryGroups />', () => {
test('initially renders successfully', async () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(<ConstructedInventoryGroups />);
});
expect(wrapper.length).toBe(1);
expect(wrapper.find('ConstructedInventoryGroups').length).toBe(1);
});
});

View File

@@ -1 +0,0 @@
export { default } from './ConstructedInventoryGroups';

View File

@@ -1,13 +0,0 @@
/* eslint i18next/no-literal-string: "off" */
import React from 'react';
import { CardBody } from 'components/Card';
function ConstructedInventoryHosts() {
return (
<CardBody>
<div>Coming Soon!</div>
</CardBody>
);
}
export default ConstructedInventoryHosts;

View File

@@ -1,15 +0,0 @@
import React from 'react';
import { act } from 'react-dom/test-utils';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import ConstructedInventoryHosts from './ConstructedInventoryHosts';
describe('<ConstructedInventoryHosts />', () => {
test('initially renders successfully', async () => {
let wrapper;
await act(async () => {
wrapper = mountWithContexts(<ConstructedInventoryHosts />);
});
expect(wrapper.length).toBe(1);
expect(wrapper.find('ConstructedInventoryHosts').length).toBe(1);
});
});

View File

@@ -1 +0,0 @@
export { default } from './ConstructedInventoryHosts';

View File

@@ -23,7 +23,7 @@ function InventoryGroup({ setBreadcrumb, inventory }) {
const [inventoryGroup, setInventoryGroup] = useState(null); const [inventoryGroup, setInventoryGroup] = useState(null);
const [contentLoading, setContentLoading] = useState(true); const [contentLoading, setContentLoading] = useState(true);
const [contentError, setContentError] = useState(null); const [contentError, setContentError] = useState(null);
const { id: inventoryId, groupId } = useParams(); const { id: inventoryId, groupId, inventoryType } = useParams();
const location = useLocation(); const location = useLocation();
useEffect(() => { useEffect(() => {
@@ -50,22 +50,22 @@ function InventoryGroup({ setBreadcrumb, inventory }) {
{t`Back to Groups`} {t`Back to Groups`}
</> </>
), ),
link: `/inventories/inventory/${inventory.id}/groups`, link: `/inventories/${inventoryType}/${inventoryId}/groups`,
id: 99, id: 99,
}, },
{ {
name: t`Details`, name: t`Details`,
link: `/inventories/inventory/${inventory.id}/groups/${inventoryGroup?.id}/details`, link: `/inventories/${inventoryType}/${inventoryId}/groups/${inventoryGroup?.id}/details`,
id: 0, id: 0,
}, },
{ {
name: t`Related Groups`, name: t`Related Groups`,
link: `/inventories/inventory/${inventory.id}/groups/${inventoryGroup?.id}/nested_groups`, link: `/inventories/${inventoryType}/${inventoryId}/groups/${inventoryGroup?.id}/nested_groups`,
id: 1, id: 1,
}, },
{ {
name: t`Hosts`, name: t`Hosts`,
link: `/inventories/inventory/${inventory.id}/groups/${inventoryGroup?.id}/nested_hosts`, link: `/inventories/${inventoryType}/${inventoryId}/groups/${inventoryGroup?.id}/nested_hosts`,
id: 2, id: 2,
}, },
]; ];
@@ -105,32 +105,32 @@ function InventoryGroup({ setBreadcrumb, inventory }) {
{showCardHeader && <RoutedTabs tabsArray={tabsArray} />} {showCardHeader && <RoutedTabs tabsArray={tabsArray} />}
<Switch> <Switch>
<Redirect <Redirect
from="/inventories/inventory/:id/groups/:groupId" from="/inventories/:inventoryType/:id/groups/:groupId"
to="/inventories/inventory/:id/groups/:groupId/details" to="/inventories/:inventoryType/:id/groups/:groupId/details"
exact exact
/> />
{inventoryGroup && [ {inventoryGroup && [
<Route <Route
key="edit" key="edit"
path="/inventories/inventory/:id/groups/:groupId/edit" path="/inventories/:inventoryType/:id/groups/:groupId/edit"
> >
<InventoryGroupEdit inventoryGroup={inventoryGroup} /> <InventoryGroupEdit inventoryGroup={inventoryGroup} />
</Route>, </Route>,
<Route <Route
key="details" key="details"
path="/inventories/inventory/:id/groups/:groupId/details" path="/inventories/:inventoryType/:id/groups/:groupId/details"
> >
<InventoryGroupDetail inventoryGroup={inventoryGroup} /> <InventoryGroupDetail inventoryGroup={inventoryGroup} />
</Route>, </Route>,
<Route <Route
key="hosts" key="hosts"
path="/inventories/inventory/:id/groups/:groupId/nested_hosts" path="/inventories/:inventoryType/:id/groups/:groupId/nested_hosts"
> >
<InventoryGroupHosts inventoryGroup={inventoryGroup} /> <InventoryGroupHosts inventoryGroup={inventoryGroup} />
</Route>, </Route>,
<Route <Route
key="relatedGroups" key="relatedGroups"
path="/inventories/inventory/:id/groups/:groupId/nested_groups" path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups"
> >
<InventoryRelatedGroups /> <InventoryRelatedGroups />
</Route>, </Route>,
@@ -138,7 +138,7 @@ function InventoryGroup({ setBreadcrumb, inventory }) {
<Route key="not-found" path="*"> <Route key="not-found" path="*">
<ContentError> <ContentError>
{inventory && ( {inventory && (
<Link to={`/inventories/inventory/${inventory.id}/details`}> <Link to={`/inventories/:inventoryType/${inventory.id}/details`}>
{t`View Inventory Details`} {t`View Inventory Details`}
</Link> </Link>
)} )}

View File

@@ -11,15 +11,16 @@ import {
import InventoryGroup from './InventoryGroup'; import InventoryGroup from './InventoryGroup';
jest.mock('../../../api'); jest.mock('../../../api');
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
}),
}));
describe('<InventoryGroup />', () => { describe('<InventoryGroup />', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 1,
inventoryType: 'inventory',
}),
}));
let wrapper; let wrapper;
let history; let history;
const inventory = { id: 1, name: 'Foo' }; const inventory = { id: 1, name: 'Foo' };
@@ -41,11 +42,11 @@ describe('<InventoryGroup />', () => {
}, },
}); });
history = createMemoryHistory({ history = createMemoryHistory({
initialEntries: ['/inventories/inventory/1/groups/1/details'], initialEntries: [`/inventories/inventory/1/groups/1/details`],
}); });
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<Route path="/inventories/inventory/:id/groups"> <Route path="/inventories/:inventoryType/:id/groups">
<InventoryGroup setBreadcrumb={() => {}} inventory={inventory} /> <InventoryGroup setBreadcrumb={() => {}} inventory={inventory} />
</Route>, </Route>,
{ context: { router: { history } } } { context: { router: { history } } }
@@ -63,7 +64,7 @@ describe('<InventoryGroup />', () => {
expect(routedTabs).toHaveLength(1); expect(routedTabs).toHaveLength(1);
const tabs = routedTabs.prop('tabsArray'); const tabs = routedTabs.prop('tabsArray');
expect(tabs[0].link).toEqual('/inventories/inventory/1/groups'); expect(tabs[0].link).toEqual(`/inventories/inventory/1/groups`);
expect(tabs[1].name).toEqual('Details'); expect(tabs[1].name).toEqual('Details');
expect(tabs[2].name).toEqual('Related Groups'); expect(tabs[2].name).toEqual('Related Groups');
expect(tabs[3].name).toEqual('Hosts'); expect(tabs[3].name).toEqual('Hosts');
@@ -71,7 +72,7 @@ describe('<InventoryGroup />', () => {
test('should show content error when user attempts to navigate to erroneous route', async () => { test('should show content error when user attempts to navigate to erroneous route', async () => {
history = createMemoryHistory({ history = createMemoryHistory({
initialEntries: ['/inventories/inventory/1/groups/1/foobar'], initialEntries: [`/inventories/inventory/1/groups/1/foobar`],
}); });
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
@@ -92,3 +93,60 @@ describe('<InventoryGroup />', () => {
await waitForElement(wrapper, 'ContentError', (el) => el.length === 1); await waitForElement(wrapper, 'ContentError', (el) => el.length === 1);
}); });
}); });
describe('constructed inventory', () => {
let wrapper;
let history;
const inventory = { id: 1, name: 'Foo' };
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'constructed_inventory',
}),
}));
beforeEach(async () => {
GroupsAPI.readDetail.mockResolvedValue({
data: {
id: 1,
name: 'Foo',
description: 'Bar',
variables: 'bizz: buzz',
summary_fields: {
inventory: { id: 1 },
created_by: { id: 1, username: 'Athena' },
modified_by: { id: 1, username: 'Apollo' },
},
created: '2020-04-25T01:23:45.678901Z',
modified: '2020-04-25T01:23:45.678901Z',
},
});
history = createMemoryHistory({
initialEntries: [`/inventories/constructed_inventory/1/groups/1/details`],
});
await act(async () => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups">
<InventoryGroup setBreadcrumb={() => {}} inventory={inventory} />
</Route>,
{ context: { router: { history } } }
);
});
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
});
test('Constructed Inventory expect all tabs to exist, including Back to Groups', () => {
const routedTabs = wrapper.find('RoutedTabs');
expect(routedTabs).toHaveLength(1);
const tabs = routedTabs.prop('tabsArray');
expect(tabs[0].link).toEqual(`/inventories/constructed_inventory/1/groups`);
expect(tabs[1].name).toEqual('Details');
expect(tabs[2].name).toEqual('Related Groups');
expect(tabs[3].name).toEqual('Hosts');
});
});

View File

@@ -1,9 +1,8 @@
import React, { useState } from 'react'; import React, { useState } from 'react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { useHistory, useParams } from 'react-router-dom';
import { Button } from '@patternfly/react-core'; import { Button } from '@patternfly/react-core';
import { useHistory, useParams } from 'react-router-dom';
import { VariablesDetail } from 'components/CodeEditor'; import { VariablesDetail } from 'components/CodeEditor';
import { CardBody, CardActionsRow } from 'components/Card'; import { CardBody, CardActionsRow } from 'components/Card';
import ErrorDetail from 'components/ErrorDetail'; import ErrorDetail from 'components/ErrorDetail';
@@ -12,6 +11,7 @@ import { DetailList, Detail, UserDateDetail } from 'components/DetailList';
import InventoryGroupsDeleteModal from '../shared/InventoryGroupsDeleteModal'; import InventoryGroupsDeleteModal from '../shared/InventoryGroupsDeleteModal';
function InventoryGroupDetail({ inventoryGroup }) { function InventoryGroupDetail({ inventoryGroup }) {
const { inventoryType, id, groupId } = useParams();
const { const {
summary_fields: { created_by, modified_by, user_capabilities }, summary_fields: { created_by, modified_by, user_capabilities },
created, created,
@@ -22,7 +22,6 @@ function InventoryGroupDetail({ inventoryGroup }) {
} = inventoryGroup; } = inventoryGroup;
const [error, setError] = useState(false); const [error, setError] = useState(false);
const history = useHistory(); const history = useHistory();
const params = useParams();
return ( return (
<CardBody> <CardBody>
@@ -47,31 +46,33 @@ function InventoryGroupDetail({ inventoryGroup }) {
user={modified_by} user={modified_by}
/> />
</DetailList> </DetailList>
<CardActionsRow> {inventoryType !== 'constructed_inventory' && (
{user_capabilities?.edit && ( <CardActionsRow>
<Button {user_capabilities?.edit && (
ouiaId="inventory-group-detail-edit-button" <Button
variant="primary" ouiaId="inventory-group-detail-edit-button"
aria-label={t`Edit`} variant="primary"
onClick={() => aria-label={t`Edit`}
history.push( onClick={() =>
`/inventories/inventory/${params.id}/groups/${params.groupId}/edit` history.push(
) `/inventories/inventory/${id}/groups/${groupId}/edit`
} )
> }
{t`Edit`} >
</Button> {t`Edit`}
)} </Button>
{user_capabilities?.delete && ( )}
<InventoryGroupsDeleteModal {user_capabilities?.delete && (
groups={[inventoryGroup]} <InventoryGroupsDeleteModal
isDisabled={false} groups={[inventoryGroup]}
onAfterDelete={() => isDisabled={false}
history.push(`/inventories/inventory/${params.id}/groups`) onAfterDelete={() =>
} history.push(`/inventories/inventory/${id}/groups`)
/> }
)} />
</CardActionsRow> )}
</CardActionsRow>
)}
{error && ( {error && (
<AlertModal <AlertModal
variant="error" variant="error"

View File

@@ -39,6 +39,14 @@ describe('<InventoryGroupDetail />', () => {
let history; let history;
describe('User has full permissions', () => { describe('User has full permissions', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 3,
inventoryType: 'inventory',
}),
}));
beforeEach(async () => { beforeEach(async () => {
await act(async () => { await act(async () => {
history = createMemoryHistory({ history = createMemoryHistory({
@@ -116,6 +124,14 @@ describe('<InventoryGroupDetail />', () => {
}); });
describe('User has read-only permissions', () => { describe('User has read-only permissions', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 3,
inventoryType: 'inventory',
}),
}));
test('should hide edit/delete buttons', async () => { test('should hide edit/delete buttons', async () => {
const readOnlyGroup = { const readOnlyGroup = {
...inventoryGroup, ...inventoryGroup,
@@ -159,4 +175,48 @@ describe('<InventoryGroupDetail />', () => {
expect(wrapper.find('button[aria-label="Delete"]').length).toBe(0); expect(wrapper.find('button[aria-label="Delete"]').length).toBe(0);
}); });
}); });
describe('Cannot edit or delete constructed inventory group', () => {
beforeEach(async () => {
await act(async () => {
history = createMemoryHistory({
initialEntries: ['/inventories/inventory/1/groups/1/details'],
});
wrapper = mountWithContexts(
<Route path="/inventories/inventory/:id/groups/:groupId">
<InventoryGroupDetail inventoryGroup={inventoryGroup} />
</Route>,
{
context: {
router: {
history,
route: {
location: history.location,
match: {
params: {
id: 1,
group: 2,
inventoryType: 'constructed_inventory',
},
},
},
},
},
}
);
await waitForElement(
wrapper,
'ContentLoading',
(el) => el.length === 0
);
});
});
afterEach(() => {
jest.clearAllMocks();
});
test('should not show edit button', () => {
const editButton = wrapper.find('Button[aria-label="edit"]');
expect(editButton.length).toBe(0);
expect(wrapper.find('Button[aria-label="delete"]').length).toBe(0);
});
});
}); });

View File

@@ -34,7 +34,7 @@ const QS_CONFIG = getQSConfig('host', {
function InventoryGroupHostList() { function InventoryGroupHostList() {
const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false); const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false);
const [isModalOpen, setIsModalOpen] = useState(false); const [isModalOpen, setIsModalOpen] = useState(false);
const { id: inventoryId, groupId } = useParams(); const { id: inventoryId, groupId, inventoryType } = useParams();
const location = useLocation(); const location = useLocation();
const { const {
@@ -145,9 +145,11 @@ function InventoryGroupHostList() {
useDismissableError(associateErr); useDismissableError(associateErr);
const { error: disassociateError, dismissError: dismissDisassociateError } = const { error: disassociateError, dismissError: dismissDisassociateError } =
useDismissableError(disassociateErr); useDismissableError(disassociateErr);
const isNotConstructedInventory = inventoryType !== 'constructed_inventory';
const canAdd = const canAdd =
actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); actions &&
Object.prototype.hasOwnProperty.call(actions, 'POST') &&
isNotConstructedInventory;
const addFormUrl = `/inventories/inventory/${inventoryId}/groups/${groupId}/nested_hosts/add`; const addFormUrl = `/inventories/inventory/${inventoryId}/groups/${groupId}/nested_hosts/add`;
const addExistingHost = t`Add existing host`; const addExistingHost = t`Add existing host`;
const addNewHost = t`Add new host`; const addNewHost = t`Add new host`;
@@ -240,17 +242,21 @@ function InventoryGroupHostList() {
/>, />,
] ]
: []), : []),
<DisassociateButton ...(isNotConstructedInventory
key="disassociate" ? [
onDisassociate={handleDisassociate} <DisassociateButton
itemsToDisassociate={selected} key="disassociate"
modalTitle={t`Disassociate host from group?`} onDisassociate={handleDisassociate}
modalNote={t` itemsToDisassociate={selected}
modalTitle={t`Disassociate host from group?`}
modalNote={t`
Note that only hosts directly in this group can Note that only hosts directly in this group can
be disassociated. Hosts in sub-groups must be disassociated be disassociated. Hosts in sub-groups must be disassociated
directly from the sub-group level that they belong. directly from the sub-group level that they belong.
`} `}
/>, />,
]
: []),
]} ]}
/> />
)} )}
@@ -259,8 +265,8 @@ function InventoryGroupHostList() {
key={host.id} key={host.id}
rowIndex={index} rowIndex={index}
host={host} host={host}
detailUrl={`/inventories/inventory/${inventoryId}/hosts/${host.id}/details`} detailUrl={`/inventories/${inventoryType}/${inventoryId}/hosts/${host.id}/details`}
editUrl={`/inventories/inventory/${inventoryId}/hosts/${host.id}/edit`} editUrl={`/inventories/${inventoryType}/${inventoryId}/hosts/${host.id}/edit`}
isSelected={selected.some((row) => row.id === host.id)} isSelected={selected.some((row) => row.id === host.id)}
onSelect={() => handleSelect(host)} onSelect={() => handleSelect(host)}
/> />

View File

@@ -8,19 +8,20 @@ import {
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import InventoryGroupHostList from './InventoryGroupHostList'; import InventoryGroupHostList from './InventoryGroupHostList';
import mockHosts from '../shared/data.hosts.json'; import mockHosts from '../shared/data.hosts.json';
import { Route } from 'react-router-dom';
jest.mock('../../../api/models/Groups'); jest.mock('../../../api/models/Groups');
jest.mock('../../../api/models/Inventories'); jest.mock('../../../api/models/Inventories');
jest.mock('../../../api/models/CredentialTypes'); jest.mock('../../../api/models/CredentialTypes');
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
}),
}));
describe('<InventoryGroupHostList />', () => { describe('<InventoryGroupHostList />', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
}),
}));
let wrapper; let wrapper;
beforeEach(async () => { beforeEach(async () => {
@@ -303,3 +304,64 @@ describe('<InventoryGroupHostList />', () => {
expect(wrapper.find('AdHocCommands')).toHaveLength(0); expect(wrapper.find('AdHocCommands')).toHaveLength(0);
}); });
}); });
describe('<InventoryGroupHostList> for constructed inventories', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'constructed_inventory',
}),
}));
let wrapper;
beforeEach(async () => {
GroupsAPI.readAllHosts.mockResolvedValue({
data: { ...mockHosts },
});
InventoriesAPI.readHostsOptions.mockResolvedValue({
data: {
actions: {
GET: {},
POST: {},
},
},
});
InventoriesAPI.readAdHocOptions.mockResolvedValue({
data: {
actions: {
GET: {
module_name: {
choices: [
['command', 'command'],
['shell', 'shell'],
],
},
},
POST: {},
},
},
});
const history = createMemoryHistory({
initialEntries: ['/inventories/constructed_inventory/1/groups/2/hosts'],
});
await act(async () => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups/:groupId/hosts">
<InventoryGroupHostList />
</Route>,
{ context: { router: { history } } }
);
});
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
});
afterEach(() => {
jest.clearAllMocks();
});
test('Should not show associate, or disassociate button', async () => {
expect(wrapper.find('AddDropDownButton').length).toBe(0);
expect(wrapper.find('DisassociateButton').length).toBe(0);
});
});

View File

@@ -1,6 +1,6 @@
import 'styled-components/macro'; import 'styled-components/macro';
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import { Link, useParams } from 'react-router-dom';
import { string, bool, func, number } from 'prop-types'; import { string, bool, func, number } from 'prop-types';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Button, Tooltip } from '@patternfly/react-core'; import { Button, Tooltip } from '@patternfly/react-core';
@@ -24,7 +24,7 @@ function InventoryGroupHostListItem({
...job, ...job,
type: 'job', type: 'job',
})); }));
const { inventoryType } = useParams();
const labelId = `check-action-${host.id}`; const labelId = `check-action-${host.id}`;
return ( return (
@@ -57,22 +57,24 @@ function InventoryGroupHostListItem({
> >
<HostToggle host={host} /> <HostToggle host={host} />
</ActionItem> </ActionItem>
<ActionItem {inventoryType !== 'constructed_inventory' && (
tooltip={t`Edit Host`} <ActionItem
visible={host.summary_fields.user_capabilities?.edit} tooltip={t`Edit Host`}
> visible={host.summary_fields.user_capabilities?.edit}
<Tooltip content={t`Edit Host`} position="top"> >
<Button <Tooltip content={t`Edit Host`} position="top">
ouiaId={`${host.id}-edit-button`} <Button
aria-label={t`Edit Host`} ouiaId={`${host.id}-edit-button`}
variant="plain" aria-label={t`Edit Host`}
component={Link} variant="plain"
to={`${editUrl}`} component={Link}
> to={`${editUrl}`}
<PencilAltIcon /> >
</Button> <PencilAltIcon />
</Tooltip> </Button>
</ActionItem> </Tooltip>
</ActionItem>
)}
</ActionsTd> </ActionsTd>
</Tr> </Tr>
); );

View File

@@ -1,28 +1,35 @@
import React from 'react'; import React from 'react';
import { createMemoryHistory } from 'history';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import InventoryGroupHostListItem from './InventoryGroupHostListItem'; import InventoryGroupHostListItem from './InventoryGroupHostListItem';
import mockHosts from '../shared/data.hosts.json'; import mockHosts from '../shared/data.hosts.json';
import { Route } from 'react-router-dom';
jest.mock('../../../api'); jest.mock('../../../api');
describe('<InventoryGroupHostListItem />', () => { describe('<InventoryGroupHostListItem />', () => {
let wrapper; let wrapper;
const mockHost = mockHosts.results[0]; const mockHost = mockHosts.results[0];
const history = createMemoryHistory({
initialEntries: ['/inventories/inventory/1/groups/2/hosts'],
});
beforeEach(() => { beforeEach(() => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<table> <Route path="/inventories/:inventoryType/:id/groups/:groupId/hosts">
<tbody> <table>
<InventoryGroupHostListItem <tbody>
detailUrl="/host/1" <InventoryGroupHostListItem
editUrl="/host/1" detailUrl="/host/1"
host={mockHost} editUrl="/host/1"
isSelected={false} host={mockHost}
onSelect={() => {}} isSelected={false}
rowIndex={0} onSelect={() => {}}
/> rowIndex={0}
</tbody> />
</table> </tbody>
</table>
</Route>,
{ context: { router: { history } } }
); );
}); });
@@ -52,19 +59,60 @@ describe('<InventoryGroupHostListItem />', () => {
const copyMockHost = { ...mockHost }; const copyMockHost = { ...mockHost };
copyMockHost.summary_fields.user_capabilities.edit = false; copyMockHost.summary_fields.user_capabilities.edit = false;
wrapper = mountWithContexts( wrapper = mountWithContexts(
<table> <Route path="/inventories/:inventoryType/:id/groups/:groupId/hosts">
<tbody> <table>
<InventoryGroupHostListItem <tbody>
detailUrl="/host/1" <InventoryGroupHostListItem
editUrl="/host/1" detailUrl="/host/1"
host={mockHost} editUrl="/host/1"
isSelected={false} host={mockHost}
onSelect={() => {}} isSelected={false}
rowIndex={0} onSelect={() => {}}
/> rowIndex={0}
</tbody> />
</table> </tbody>
</table>
</Route>,
{ context: { router: { history } } }
); );
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy(); expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
}); });
}); });
describe('<InventoryGroupHostListItem> inside constructed inventories', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'constructed_inventory',
}),
}));
let wrapper;
const mockHost = mockHosts.results[0];
const history = createMemoryHistory({
initialEntries: ['/inventories/constructed_inventory/1/groups/2/hosts'],
});
beforeEach(() => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups/:groupId/hosts">
<table>
<tbody>
<InventoryGroupHostListItem
detailUrl="/host/1"
editUrl="/host/1"
host={mockHost}
isSelected={false}
onSelect={() => {}}
rowIndex={0}
/>
</tbody>
</table>
</Route>,
{ context: { router: { history } } }
);
});
test('Edit button hidden for constructed inventory', () => {
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
});
});

View File

@@ -9,7 +9,7 @@ function InventoryGroupHosts({ inventoryGroup }) {
<Route path="/inventories/inventory/:id/groups/:groupId/nested_hosts/add"> <Route path="/inventories/inventory/:id/groups/:groupId/nested_hosts/add">
<InventoryGroupHostAdd inventoryGroup={inventoryGroup} /> <InventoryGroupHostAdd inventoryGroup={inventoryGroup} />
</Route> </Route>
<Route path="/inventories/inventory/:id/groups/:groupId/nested_hosts"> <Route path="/inventories/:inventoryType/:id/groups/:groupId/nested_hosts">
<InventoryGroupHostList /> <InventoryGroupHostList />
</Route> </Route>
</Switch> </Switch>

View File

@@ -1,25 +1,20 @@
import React from 'react'; import React from 'react';
import { bool, func, number, oneOfType, string } from 'prop-types'; import { bool, func } from 'prop-types';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Button } from '@patternfly/react-core'; import { Button } from '@patternfly/react-core';
import { Tr, Td } from '@patternfly/react-table'; import { Tr, Td } from '@patternfly/react-table';
import { Link } from 'react-router-dom'; import { Link, useParams } from 'react-router-dom';
import { PencilAltIcon } from '@patternfly/react-icons'; import { PencilAltIcon } from '@patternfly/react-icons';
import { ActionsTd, ActionItem } from 'components/PaginatedTable'; import { ActionsTd, ActionItem } from 'components/PaginatedTable';
import { Group } from 'types'; import { Group } from 'types';
function InventoryGroupItem({ function InventoryGroupItem({ group, isSelected, onSelect, rowIndex }) {
group, const { id: inventoryId, inventoryType } = useParams();
inventoryId,
isSelected,
onSelect,
rowIndex,
}) {
const labelId = `check-action-${group.id}`; const labelId = `check-action-${group.id}`;
const detailUrl = `/inventories/inventory/${inventoryId}/groups/${group.id}/details`; const detailUrl = `/inventories/${inventoryType}/${inventoryId}/groups/${group.id}/details`;
const editUrl = `/inventories/inventory/${inventoryId}/groups/${group.id}/edit`; const editUrl = `/inventories/${inventoryType}/${inventoryId}/groups/${group.id}/edit`;
return ( return (
<Tr id={`group-row-${group.id}`} ouiaId={`group-row-${group.id}`}> <Tr id={`group-row-${group.id}`} ouiaId={`group-row-${group.id}`}>
@@ -36,29 +31,30 @@ function InventoryGroupItem({
<b>{group.name}</b> <b>{group.name}</b>
</Link> </Link>
</Td> </Td>
<ActionsTd dataLabel={t`Actions`} gridColumns="auto 40px"> {inventoryType !== 'constructed_inventory' && (
<ActionItem <ActionsTd dataLabel={t`Actions`} gridColumns="auto 40px">
visible={group.summary_fields.user_capabilities.edit} <ActionItem
tooltip={t`Edit group`} visible={group.summary_fields.user_capabilities.edit}
> tooltip={t`Edit group`}
<Button
ouiaId={`${group.id}-edit-button`}
aria-label={t`Edit Group`}
variant="plain"
component={Link}
to={editUrl}
> >
<PencilAltIcon /> <Button
</Button> ouiaId={`${group.id}-edit-button`}
</ActionItem> aria-label={t`Edit Group`}
</ActionsTd> variant="plain"
component={Link}
to={editUrl}
>
<PencilAltIcon />
</Button>
</ActionItem>
</ActionsTd>
)}
</Tr> </Tr>
); );
} }
InventoryGroupItem.propTypes = { InventoryGroupItem.propTypes = {
group: Group.isRequired, group: Group.isRequired,
inventoryId: oneOfType([number, string]).isRequired,
isSelected: bool.isRequired, isSelected: bool.isRequired,
onSelect: func.isRequired, onSelect: func.isRequired,
}; };

View File

@@ -1,4 +1,6 @@
import React from 'react'; import React from 'react';
import { Route } from 'react-router-dom';
import { act } from 'react-dom/test-utils';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import InventoryGroupItem from './InventoryGroupItem'; import InventoryGroupItem from './InventoryGroupItem';
@@ -57,4 +59,39 @@ describe('<InventoryGroupItem />', () => {
); );
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy(); expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
}); });
test('edit button should be hidden from constructed inventory group', async () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({ id: 42, inventoryType: 'constructed_inventory' }),
}));
const mockGroup = {
id: 2,
type: 'group',
name: 'foo',
inventory: 1,
summary_fields: {
user_capabilities: {
edit: true,
},
},
};
await act(async () => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups">
<table>
<tbody>
<InventoryGroupItem
group={mockGroup}
inventoryId={1}
isSelected={false}
onSelect={() => {}}
/>
</tbody>
</table>
</Route>
);
});
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
});
}); });

View File

@@ -16,11 +16,14 @@ function InventoryGroups({ setBreadcrumb, inventory }) {
inventory={inventory} inventory={inventory}
/> />
</Route> </Route>
<Route key="details" path="/inventories/inventory/:id/groups/:groupId/"> <Route
key="details"
path="/inventories/:inventoryType/:id/groups/:groupId/"
>
<InventoryGroup inventory={inventory} setBreadcrumb={setBreadcrumb} /> <InventoryGroup inventory={inventory} setBreadcrumb={setBreadcrumb} />
</Route> </Route>
<Route key="list" path="/inventories/inventory/:id/groups"> <Route key="list" path="/inventories/:inventoryType/:id/groups">
<InventoryGroupsList /> <InventoryGroupsList inventory={inventory} />
</Route> </Route>
</Switch> </Switch>
); );

View File

@@ -29,7 +29,7 @@ function cannotDelete(item) {
function InventoryGroupsList() { function InventoryGroupsList() {
const location = useLocation(); const location = useLocation();
const { id: inventoryId } = useParams(); const { id: inventoryId, inventoryType } = useParams();
const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false); const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false);
const { const {
@@ -102,9 +102,11 @@ function InventoryGroupsList() {
} }
return t`Select a row to delete`; return t`Select a row to delete`;
}; };
const isNotConstructedInventory = inventoryType !== 'constructed_inventory';
const canAdd = const canAdd =
actions && Object.prototype.hasOwnProperty.call(actions, 'POST'); actions &&
Object.prototype.hasOwnProperty.call(actions, 'POST') &&
isNotConstructedInventory;
return ( return (
<PaginatedTable <PaginatedTable
@@ -139,14 +141,13 @@ function InventoryGroupsList() {
headerRow={ headerRow={
<HeaderRow qsConfig={QS_CONFIG}> <HeaderRow qsConfig={QS_CONFIG}>
<HeaderCell sortKey="name">{t`Name`}</HeaderCell> <HeaderCell sortKey="name">{t`Name`}</HeaderCell>
<HeaderCell>{t`Actions`}</HeaderCell> {isNotConstructedInventory && <HeaderCell>{t`Actions`}</HeaderCell>}
</HeaderRow> </HeaderRow>
} }
renderRow={(item, index) => ( renderRow={(item, index) => (
<InventoryGroupItem <InventoryGroupItem
key={item.id} key={item.id}
group={item} group={item}
inventoryId={inventoryId}
isSelected={selected.some((row) => row.id === item.id)} isSelected={selected.some((row) => row.id === item.id)}
onSelect={() => handleSelect(item)} onSelect={() => handleSelect(item)}
rowIndex={index} rowIndex={index}
@@ -177,20 +178,28 @@ function InventoryGroupsList() {
/>, />,
] ]
: []), : []),
<Tooltip content={renderTooltip()} position="top" key="delete"> ...(isNotConstructedInventory
<div> ? [
<InventoryGroupsDeleteModal <Tooltip
groups={selected} content={renderTooltip()}
isDisabled={ position="top"
selected.length === 0 || selected.some(cannotDelete) key="delete"
} >
onAfterDelete={() => { <div>
fetchData(); <InventoryGroupsDeleteModal
clearSelected(); groups={selected}
}} isDisabled={
/> selected.length === 0 || selected.some(cannotDelete)
</div> }
</Tooltip>, onAfterDelete={() => {
fetchData();
clearSelected();
}}
/>
</div>
</Tooltip>,
]
: []),
]} ]}
/> />
)} )}

View File

@@ -10,12 +10,6 @@ import {
import InventoryGroupsList from './InventoryGroupsList'; import InventoryGroupsList from './InventoryGroupsList';
jest.mock('../../../api'); jest.mock('../../../api');
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
}),
}));
const mockGroups = [ const mockGroups = [
{ {
id: 1, id: 1,
@@ -60,7 +54,14 @@ const mockGroups = [
describe('<InventoryGroupsList />', () => { describe('<InventoryGroupsList />', () => {
let wrapper; let wrapper;
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'inventory',
}),
}));
beforeEach(async () => { beforeEach(async () => {
InventoriesAPI.readGroups.mockResolvedValue({ InventoriesAPI.readGroups.mockResolvedValue({
data: { data: {
@@ -96,7 +97,7 @@ describe('<InventoryGroupsList />', () => {
}); });
await act(async () => { await act(async () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<Route path="/inventories/inventory/:id/groups"> <Route path="/inventories/:inventoryType/:id/groups">
<InventoryGroupsList /> <InventoryGroupsList />
</Route>, </Route>,
{ {
@@ -316,3 +317,77 @@ describe('<InventoryGroupsList/> error handling', () => {
}); });
}); });
}); });
describe('Constructed Inventory group', () => {
let wrapper;
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'constructed_inventory',
}),
}));
beforeEach(async () => {
InventoriesAPI.readGroups.mockResolvedValue({
data: {
count: mockGroups.length,
results: mockGroups,
},
});
InventoriesAPI.readGroupsOptions.mockResolvedValue({
data: {
actions: {
GET: {},
POST: {},
},
},
});
InventoriesAPI.readAdHocOptions.mockResolvedValue({
data: {
actions: {
GET: {
module_name: {
choices: [
['command', 'command'],
['shell', 'shell'],
],
},
},
POST: {},
},
},
});
const history = createMemoryHistory({
initialEntries: ['/inventories/constructed_inventory/3/groups'],
});
await act(async () => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups">
<InventoryGroupsList />
</Route>,
{
context: {
router: {
history,
route: {
location: history.location,
},
},
},
}
);
});
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
});
afterEach(() => {
jest.clearAllMocks();
});
test('should not show add button', () => {
expect(wrapper.find('ToolbarAddButton').length).toBe(0);
expect(wrapper.find('ToolbarDeleteButton').length).toBe(0);
expect(wrapper.find('AdHocCommands').length).toBe(1);
});
});

View File

@@ -195,6 +195,7 @@ function InventoryList() {
options: [ options: [
['', t`Inventory`], ['', t`Inventory`],
['smart', t`Smart Inventory`], ['smart', t`Smart Inventory`],
['constructed', t`Constructed Inventory`],
], ],
}, },
{ {

View File

@@ -33,7 +33,7 @@ function InventoryRelatedGroupList() {
const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false); const [isAdHocLaunchLoading, setIsAdHocLaunchLoading] = useState(false);
const [associateError, setAssociateError] = useState(null); const [associateError, setAssociateError] = useState(null);
const [disassociateError, setDisassociateError] = useState(null); const [disassociateError, setDisassociateError] = useState(null);
const { id: inventoryId, groupId } = useParams(); const { id: inventoryId, groupId, inventoryType } = useParams();
const location = useLocation(); const location = useLocation();
const { const {
@@ -69,9 +69,10 @@ function InventoryRelatedGroupList() {
searchableKeys: getSearchableKeys(actions.data.actions?.GET), searchableKeys: getSearchableKeys(actions.data.actions?.GET),
canAdd: canAdd:
actions.data.actions && actions.data.actions &&
Object.prototype.hasOwnProperty.call(actions.data.actions, 'POST'), Object.prototype.hasOwnProperty.call(actions.data.actions, 'POST') &&
inventoryType !== 'constructed_inventory',
}; };
}, [groupId, location.search, inventoryId]), }, [groupId, location.search, inventoryType, inventoryId]),
{ {
groups: [], groups: [],
itemCount: 0, itemCount: 0,
@@ -164,7 +165,7 @@ function InventoryRelatedGroupList() {
]} ]}
/> />
); );
const isNotConstructedInventory = inventoryType !== 'constructed_inventory';
return ( return (
<> <>
<PaginatedTable <PaginatedTable
@@ -218,19 +219,23 @@ function InventoryRelatedGroupList() {
/>, />,
] ]
: []), : []),
<DisassociateButton ...(isNotConstructedInventory
key="disassociate" ? [
onDisassociate={disassociateGroups} <DisassociateButton
itemsToDisassociate={selected} key="disassociate"
modalTitle={t`Disassociate related group(s)?`} onDisassociate={disassociateGroups}
/>, itemsToDisassociate={selected}
modalTitle={t`Disassociate related group(s)?`}
/>,
]
: []),
]} ]}
/> />
)} )}
headerRow={ headerRow={
<HeaderRow qsConfig={QS_CONFIG}> <HeaderRow qsConfig={QS_CONFIG}>
<HeaderCell sortKey="name">{t`Name`}</HeaderCell> <HeaderCell sortKey="name">{t`Name`}</HeaderCell>
<HeaderCell>{t`Actions`}</HeaderCell> {isNotConstructedInventory && <HeaderCell>{t`Actions`}</HeaderCell>}
</HeaderRow> </HeaderRow>
} }
renderRow={(group, index) => ( renderRow={(group, index) => (

View File

@@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import { createMemoryHistory } from 'history';
import { Route } from 'react-router-dom';
import { GroupsAPI, InventoriesAPI } from 'api'; import { GroupsAPI, InventoriesAPI } from 'api';
import { import {
mountWithContexts, mountWithContexts,
@@ -13,14 +14,6 @@ jest.mock('../../../api/models/Groups');
jest.mock('../../../api/models/Inventories'); jest.mock('../../../api/models/Inventories');
jest.mock('../../../api/models/CredentialTypes'); jest.mock('../../../api/models/CredentialTypes');
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
}),
}));
const mockGroups = [ const mockGroups = [
{ {
id: 1, id: 1,
@@ -65,6 +58,14 @@ const mockGroups = [
describe('<InventoryRelatedGroupList />', () => { describe('<InventoryRelatedGroupList />', () => {
let wrapper; let wrapper;
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 2,
groupId: 2,
inventoryType: 'inventory',
}),
}));
beforeEach(async () => { beforeEach(async () => {
GroupsAPI.readChildren.mockResolvedValue({ GroupsAPI.readChildren.mockResolvedValue({
@@ -210,11 +211,22 @@ describe('<InventoryRelatedGroupList />', () => {
GroupsAPI.readPotentialGroups.mockResolvedValue({ GroupsAPI.readPotentialGroups.mockResolvedValue({
data: { count: mockGroups.length, results: mockGroups }, data: { count: mockGroups.length, results: mockGroups },
}); });
await act(async () => { const history = createMemoryHistory({
wrapper = mountWithContexts(<InventoryRelatedGroupList />); initialEntries: ['/inventories/inventory/2/groups/2/nested_groups'],
}); });
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0); await act(async () => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups">
<InventoryRelatedGroupList />
</Route>,
{ context: { router: { history } } }
);
});
await waitForElement(
wrapper,
'InventoryRelatedGroupList',
(el) => el.length > 0
);
act(() => wrapper.find('Button[aria-label="Add"]').prop('onClick')()); act(() => wrapper.find('Button[aria-label="Add"]').prop('onClick')());
wrapper.update(); wrapper.update();
await act(async () => await act(async () =>
@@ -222,9 +234,9 @@ describe('<InventoryRelatedGroupList />', () => {
.find('DropdownItem[aria-label="Add existing group"]') .find('DropdownItem[aria-label="Add existing group"]')
.prop('onClick')() .prop('onClick')()
); );
expect(GroupsAPI.readPotentialGroups).toBeCalledWith(2, { expect(GroupsAPI.readPotentialGroups).toBeCalledWith('2', {
not__id: 2, not__id: '2',
not__parents: 2, not__parents: '2',
order_by: 'name', order_by: 'name',
page: 1, page: 1,
page_size: 5, page_size: 5,
@@ -261,3 +273,85 @@ describe('<InventoryRelatedGroupList />', () => {
expect(wrapper.find('AdHocCommands')).toHaveLength(0); expect(wrapper.find('AdHocCommands')).toHaveLength(0);
}); });
}); });
describe('<InventoryRelatedGroupList> for constructed inventories', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'constructed_inventory',
}),
}));
let wrapper;
beforeEach(async () => {
GroupsAPI.readChildren.mockResolvedValue({
data: { ...mockRelatedGroups },
});
InventoriesAPI.readGroupsOptions.mockResolvedValue({
data: {
actions: {
GET: {},
POST: {},
},
related_search_fields: [
'parents__search',
'inventory__search',
'inventory_sources__search',
'created_by__search',
'children__search',
'modified_by__search',
'hosts__search',
],
},
});
InventoriesAPI.readAdHocOptions.mockResolvedValue({
data: {
actions: {
GET: {
module_name: {
choices: [
['command', 'command'],
['shell', 'shell'],
],
},
},
POST: {},
},
},
});
const history = createMemoryHistory({
initialEntries: [
'/inventories/constructed_inventory/1/groups/2/nested_groupss',
],
});
await act(async () => {
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups">
<InventoryRelatedGroupList />
</Route>,
{ context: { router: { history } } }
);
});
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
});
afterEach(() => {
jest.clearAllMocks();
});
test('Should not show associate, or disassociate button', async () => {
InventoriesAPI.readHostsOptions.mockResolvedValueOnce({
data: {
actions: {
GET: {},
},
},
});
await waitForElement(wrapper, 'ContentLoading', (el) => el.length === 0);
expect(wrapper.find('AddDropDownButton').length).toBe(0);
expect(wrapper.find('DisassociateButton').length).toBe(0);
});
});

View File

@@ -1,6 +1,6 @@
import 'styled-components/macro'; import 'styled-components/macro';
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import { Link, useParams } from 'react-router-dom';
import { string, bool, func, number } from 'prop-types'; import { string, bool, func, number } from 'prop-types';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
@@ -21,7 +21,7 @@ function InventoryRelatedGroupListItem({
onSelect, onSelect,
}) { }) {
const labelId = `check-action-${group.id}`; const labelId = `check-action-${group.id}`;
const { inventoryType } = useParams();
return ( return (
<Tr <Tr
id={group.id} id={group.id}
@@ -41,22 +41,24 @@ function InventoryRelatedGroupListItem({
<b>{group.name}</b> <b>{group.name}</b>
</Link> </Link>
</Td> </Td>
<ActionsTd dataLabel={t`Actions`}> {inventoryType !== 'constructed_inventory' && (
<ActionItem <ActionsTd dataLabel={t`Actions`}>
tooltip={t`Edit Group`} <ActionItem
visible={group.summary_fields.user_capabilities?.edit} tooltip={t`Edit Group`}
> visible={group.summary_fields.user_capabilities?.edit}
<Button
ouiaId={`${group.id}-edit-button`}
aria-label={t`Edit Group`}
variant="plain"
component={Link}
to={`${editUrl}`}
> >
<PencilAltIcon /> <Button
</Button> ouiaId={`${group.id}-edit-button`}
</ActionItem> aria-label={t`Edit Group`}
</ActionsTd> variant="plain"
component={Link}
to={`${editUrl}`}
>
<PencilAltIcon />
</Button>
</ActionItem>
</ActionsTd>
)}
</Tr> </Tr>
); );
} }

View File

@@ -1,28 +1,43 @@
import React from 'react'; import React from 'react';
import { createMemoryHistory } from 'history';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import InventoryRelatedGroupListItem from './InventoryRelatedGroupListItem'; import InventoryRelatedGroupListItem from './InventoryRelatedGroupListItem';
import mockRelatedGroups from '../shared/data.relatedGroups.json'; import mockRelatedGroups from '../shared/data.relatedGroups.json';
import { Route } from 'react-router-dom';
jest.mock('../../../api'); jest.mock('../../../api');
const mockGroup = mockRelatedGroups.results[0];
describe('<InventoryRelatedGroupListItem />', () => { describe('<InventoryRelatedGroupListItem />', () => {
let wrapper; let wrapper;
const mockGroup = mockRelatedGroups.results[0]; const history = createMemoryHistory({
initialEntries: ['/inventories/inventory/1/groups/2/nested_groups'],
});
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'inventory',
}),
}));
beforeEach(() => { beforeEach(() => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<table> <Route path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups">
<tbody> <table>
<InventoryRelatedGroupListItem <tbody>
detailUrl="/group/1" <InventoryRelatedGroupListItem
editUrl="/group/1" detailUrl="/group/1"
group={mockGroup} editUrl="/group/1"
isSelected={false} group={mockGroup}
onSelect={() => {}} isSelected={false}
rowIndex={0} onSelect={() => {}}
/> rowIndex={0}
</tbody> />
</table> </tbody>
</table>
</Route>,
{ context: { router: { history } } }
); );
}); });
@@ -36,18 +51,60 @@ describe('<InventoryRelatedGroupListItem />', () => {
test('edit button hidden from users without edit capabilities', () => { test('edit button hidden from users without edit capabilities', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<table> <Route path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups">
<tbody> <table>
<InventoryRelatedGroupListItem <tbody>
detailUrl="/group/1" <InventoryRelatedGroupListItem
editUrl="/group/1" detailUrl="/group/1"
group={mockRelatedGroups.results[2]} editUrl="/group/1"
isSelected={false} group={mockRelatedGroups.results[2]}
onSelect={() => {}} isSelected={false}
rowIndex={0} onSelect={() => {}}
/> rowIndex={0}
</tbody> />
</table> </tbody>
</table>
</Route>,
{ context: { router: { history } } }
);
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
});
});
describe('<InventoryRelatedGroupList> for constructed inventories', () => {
jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useParams: () => ({
id: 1,
groupId: 2,
inventoryType: 'constructed_inventory',
}),
}));
let wrapper;
test('edit button hidden from users without edit capabilities', () => {
const history = createMemoryHistory({
initialEntries: [
'/inventories/constructed_inventory/1/groups/2/nested_groups',
],
});
wrapper = mountWithContexts(
<Route path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups">
<table>
<tbody>
<InventoryRelatedGroupListItem
detailUrl="/group/1"
editUrl="/group/1"
group={mockGroup}
isSelected={false}
onSelect={() => {}}
rowIndex={0}
/>
</tbody>
</table>
</Route>,
{ context: { router: { history } } }
); );
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy(); expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
}); });

View File

@@ -8,13 +8,13 @@ function InventoryRelatedGroups() {
<Switch> <Switch>
<Route <Route
key="addRelatedGroups" key="addRelatedGroups"
path="/inventories/inventory/:id/groups/:groupId/nested_groups/add" path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups/add"
> >
<InventoryRelatedGroupAdd /> <InventoryRelatedGroupAdd />
</Route> </Route>
<Route <Route
key="relatedGroups" key="relatedGroups"
path="/inventories/inventory/:id/groups/:groupId/nested_groups" path="/inventories/:inventoryType/:id/groups/:groupId/nested_groups"
> >
<InventoryRelatedGroupList /> <InventoryRelatedGroupList />
</Route> </Route>

View File

@@ -31,7 +31,7 @@ import { formatDateString } from 'util/dates';
import Popover from 'components/Popover'; import Popover from 'components/Popover';
import { VERBOSITY } from 'components/VerbositySelectField'; import { VERBOSITY } from 'components/VerbositySelectField';
import InventorySourceSyncButton from '../shared/InventorySourceSyncButton'; import InventorySourceSyncButton from '../shared/InventorySourceSyncButton';
import useWsInventorySourcesDetails from '../InventorySources/useWsInventorySourcesDetails'; import useWsInventorySourcesDetails from '../shared/useWsInventorySourcesDetails';
import getHelpText from '../shared/Inventory.helptext'; import getHelpText from '../shared/Inventory.helptext';
function InventorySourceDetail({ inventorySource }) { function InventorySourceDetail({ inventorySource }) {

View File

@@ -1,42 +0,0 @@
import { useState, useEffect } from 'react';
import useWebsocket from 'hooks/useWebsocket';
export default function useWsInventorySourcesDetails(initialSources) {
const [sources, setSources] = useState(initialSources);
const lastMessage = useWebsocket({
jobs: ['status_changed'],
control: ['limit_reached_1'],
});
useEffect(() => {
setSources(initialSources);
}, [initialSources]);
useEffect(
() => {
if (
!lastMessage?.unified_job_id ||
!lastMessage?.inventory_source_id ||
lastMessage.type !== 'inventory_update'
) {
return;
}
const updateSource = {
...sources,
summary_fields: {
...sources.summary_fields,
current_job: {
id: lastMessage.unified_job_id,
status: lastMessage.status,
finished: lastMessage.finished,
},
},
};
setSources(updateSource);
},
[lastMessage] // eslint-disable-line react-hooks/exhaustive-deps
);
return sources;
}

View File

@@ -22,7 +22,7 @@ import RoutedTabs from 'components/RoutedTabs';
import RelatedTemplateList from 'components/RelatedTemplateList'; import RelatedTemplateList from 'components/RelatedTemplateList';
import SmartInventoryDetail from './SmartInventoryDetail'; import SmartInventoryDetail from './SmartInventoryDetail';
import SmartInventoryEdit from './SmartInventoryEdit'; import SmartInventoryEdit from './SmartInventoryEdit';
import SmartInventoryHosts from './SmartInventoryHosts'; import AdvancedInventoryHosts from './AdvancedInventoryHosts';
import { getInventoryPath } from './shared/utils'; import { getInventoryPath } from './shared/utils';
function SmartInventory({ setBreadcrumb }) { function SmartInventory({ setBreadcrumb }) {
@@ -142,7 +142,7 @@ function SmartInventory({ setBreadcrumb }) {
/> />
</Route>, </Route>,
<Route key="hosts" path="/inventories/smart_inventory/:id/hosts"> <Route key="hosts" path="/inventories/smart_inventory/:id/hosts">
<SmartInventoryHosts <AdvancedInventoryHosts
inventory={inventory} inventory={inventory}
setBreadcrumb={setBreadcrumb} setBreadcrumb={setBreadcrumb}
/> />

View File

@@ -1 +0,0 @@
export { default } from './SmartInventoryHost';

View File

@@ -1 +0,0 @@
export { default } from './SmartInventoryHostDetail';

View File

@@ -1,27 +0,0 @@
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import { Inventory } from 'types';
import SmartInventoryHostList from './SmartInventoryHostList';
import SmartInventoryHost from '../SmartInventoryHost';
function SmartInventoryHosts({ inventory, setBreadcrumb }) {
return (
<Switch>
<Route key="host" path="/inventories/smart_inventory/:id/hosts/:hostId">
<SmartInventoryHost
setBreadcrumb={setBreadcrumb}
inventory={inventory}
/>
</Route>
<Route key="host-list" path="/inventories/smart_inventory/:id/hosts">
<SmartInventoryHostList inventory={inventory} />
</Route>
</Switch>
);
}
SmartInventoryHosts.propTypes = {
inventory: Inventory.isRequired,
};
export default SmartInventoryHosts;

View File

@@ -1 +0,0 @@
export { default } from './SmartInventoryHosts';

View File

@@ -0,0 +1,58 @@
import { useState, useEffect } from 'react';
import useWebsocket from 'hooks/useWebsocket';
import { InventorySourcesAPI } from 'api';
export default function useWsInventorySourcesDetails(initialSource) {
const [source, setSource] = useState(initialSource);
const lastMessage = useWebsocket({
jobs: ['status_changed'],
control: ['limit_reached_1'],
});
useEffect(() => {
setSource(initialSource);
}, [initialSource]);
useEffect(
() => {
if (
!lastMessage?.unified_job_id ||
!lastMessage?.inventory_source_id ||
lastMessage.type !== 'inventory_update'
) {
return;
}
if (
['successful', 'failed', 'error', 'cancelled'].includes(
lastMessage.status
)
) {
fetchSource();
}
setSource(updateSource(source, lastMessage));
},
[lastMessage] // eslint-disable-line react-hooks/exhaustive-deps
);
async function fetchSource() {
const { data } = await InventorySourcesAPI.readDetail(source.id);
setSource(data);
}
return source;
}
function updateSource(source, message) {
return {
...source,
summary_fields: {
...source.summary_fields,
current_job: {
id: message.unified_job_id,
status: message.status,
finished: message.finished,
},
},
};
}

View File

@@ -1,9 +1,12 @@
import React from 'react'; import React from 'react';
import { act } from 'react-dom/test-utils'; import { act } from 'react-dom/test-utils';
import WS from 'jest-websocket-mock'; import WS from 'jest-websocket-mock';
import { InventorySourcesAPI } from 'api';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
import useWsInventorySourceDetails from './useWsInventorySourcesDetails'; import useWsInventorySourceDetails from './useWsInventorySourcesDetails';
jest.mock('../../../api/models/InventorySources');
function TestInner() { function TestInner() {
return <div />; return <div />;
} }
@@ -111,6 +114,27 @@ describe('useWsProject', () => {
status: 'running', status: 'running',
finished: null, finished: null,
}); });
expect(InventorySourcesAPI.readDetail).toHaveBeenCalledTimes(0);
InventorySourcesAPI.readDetail.mockResolvedValue({
data: {},
});
await act(async () => {
mockServer.send(
JSON.stringify({
group_name: 'jobs',
inventory_id: 1,
status: 'successful',
type: 'inventory_update',
unified_job_id: 2,
unified_job_template_id: 1,
inventory_source_id: 1,
})
);
});
expect(InventorySourcesAPI.readDetail).toHaveBeenCalledTimes(1);
jest.clearAllMocks();
WS.clean(); WS.clean();
}); });
}); });