Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions webpack/InsightsVulnerabilityHostIndexExtensions/CVECountCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,31 @@ import PropTypes from 'prop-types';
import { UnknownIcon } from '@patternfly/react-icons';
import { Link } from 'react-router-dom';
import { useAPI } from 'foremanReact/common/hooks/API/APIHooks';

import { insightsCloudUrl } from '../InsightsCloudSync/InsightsCloudSyncHelpers';
import { useAdvisorEngineConfig } from '../common/Hooks/ConfigHooks';

const vulnerabilityApiPath = path =>
insightsCloudUrl(`api/vulnerability/v1/${path}`);

export const CVECountCell = ({ hostDetails }) => {
const isIopEnabled = useAdvisorEngineConfig();

// eslint-disable-next-line camelcase
const uuid = hostDetails?.subscription_facet_attributes?.uuid;

const key = `HOST_CVE_COUNT_${uuid}`;
const response = useAPI(
uuid ? 'get' : null,
isIopEnabled && uuid ? 'get' : null,
vulnerabilityApiPath(`systems?uuid=${uuid}`),
{
key,
}
);

if (!isIopEnabled) {
return <UnknownIcon />;
}

if (uuid === undefined) {
return <UnknownIcon />;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import { API } from 'foremanReact/redux/API';
import { CVECountCell } from '../CVECountCell';
import * as ConfigHooks from '../../common/Hooks/ConfigHooks';

jest.mock('foremanReact/redux/API');
jest.mock('../../common/Hooks/ConfigHooks');

API.get.mockImplementation(async () => ({
data: [
{
Expand All @@ -14,15 +17,58 @@ API.get.mockImplementation(async () => ({
],
}));

const hostDetailsMock = {
name: 'test-host.example.com',
subscription_facet_attributes: {
uuid: 'test-uuid-123',
},
};

describe('CVECountCell', () => {
it('renders an empty cves count column', () => {
const hostDetailsMock = {
beforeEach(() => {
ConfigHooks.useAdvisorEngineConfig.mockReturnValue(true);
});

afterEach(() => {
jest.clearAllMocks();
});

it('renders an empty cves count column when no subscription UUID', () => {
const hostDetailsMockIoP = {
name: 'test-host.example.com',
subscription_facet_attributes: {
uuid: null, // no subscription
},
};
render(<CVECountCell hostDetails={hostDetailsMockIoP} />);
expect(screen.getByRole('img', { hidden: true })).toBeTruthy();
});

it('renders UnknownIcon when IoP is not enabled', () => {
ConfigHooks.useAdvisorEngineConfig.mockReturnValue(false);
render(<CVECountCell hostDetails={hostDetailsMock} />);
expect(screen.getByRole('img', { hidden: true })).toBeTruthy();
expect(ConfigHooks.useAdvisorEngineConfig).toHaveBeenCalled();
});

it('renders UnknownIcon when IoP is enabled but CVE API call fails', () => {
// Mock successful IoP config
ConfigHooks.useAdvisorEngineConfig.mockReturnValue(true);
// Mock CVE API failure - override the global mock for this test
API.get.mockImplementationOnce(async () => {
throw new Error('CVE API call failed');
});

render(<CVECountCell hostDetails={hostDetailsMock} />);
// Should render UnknownIcon when CVE API fails
expect(screen.getByRole('img', { hidden: true })).toBeTruthy();
expect(ConfigHooks.useAdvisorEngineConfig).toHaveBeenCalled();
});

it('renders UnknownIcon when IoP is undefined (API call pending)', () => {
ConfigHooks.useAdvisorEngineConfig.mockReturnValue(undefined);
render(<CVECountCell hostDetails={hostDetailsMock} />);
expect(screen.getByRole('img', { hidden: true })).toBeTruthy();
expect(ConfigHooks.useAdvisorEngineConfig).toHaveBeenCalled();
});
});