Skip to content

Commit

Permalink
update fleet hie card to use summary api
Browse files Browse the repository at this point in the history
  • Loading branch information
ashokaditya committed Jan 27, 2022
1 parent 653ffba commit af83384
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,16 @@ export async function updateOneHostIsolationExceptionItem(
body: JSON.stringify(entry),
});
}
export async function getHostIsolationExceptionSummary(
http: HttpStart
): Promise<ExceptionListSummarySchema> {
export async function getHostIsolationExceptionSummary({
http,
filter,
}: {
http: HttpStart;
filter?: string;
}): Promise<ExceptionListSummarySchema> {
return http.get<ExceptionListSummarySchema>(`${EXCEPTION_LIST_URL}/summary`, {
query: {
filter,
list_id: ENDPOINT_HOST_ISOLATION_EXCEPTIONS_LIST_ID,
namespace_type: 'agnostic',
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function useCanSeeHostIsolationExceptionsMenu(): boolean {
useEffect(() => {
async function checkIfHasExceptions() {
try {
const summary = await getHostIsolationExceptionSummary(http);
const summary = await getHostIsolationExceptionSummary({ http });
if (summary?.total > 0) {
setCanSeeMenu(true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const FleetHostIsolationExceptionsCard = memo<PackageCustomExtensionCompo
isMounted.current = true;
const fetchStats = async () => {
try {
const summary = await getHostIsolationExceptionSummary(http);
const summary = await getHostIsolationExceptionSummary({ http });
if (isMounted.current) {
setStats(summary);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import React from 'react';
import uuid from 'uuid';
import { createAppRootMockRenderer } from '../../../../../../../common/mock/endpoint';
import { useUserPrivileges } from '../../../../../../../common/components/user_privileges';
import { getHostIsolationExceptionItems } from '../../../../../host_isolation_exceptions/service';
import { getHostIsolationExceptionSummary } from '../../../../../host_isolation_exceptions/service';
import { FleetIntegrationHostIsolationExceptionsCard } from './fleet_integration_host_isolation_exceptions_card';

jest.mock('../../../../../host_isolation_exceptions/service');
jest.mock('../../../../../../../common/components/user_privileges');

const getHostIsolationExceptionItemsMock = getHostIsolationExceptionItems as jest.Mock;
const getHostIsolationExceptionSummaryMock = getHostIsolationExceptionSummary as jest.Mock;

const useUserPrivilegesMock = useUserPrivileges as jest.Mock;

Expand All @@ -29,7 +29,7 @@ describe('Fleet host isolation exceptions card filters card', () => {
);
};
afterEach(() => {
getHostIsolationExceptionItemsMock.mockReset();
getHostIsolationExceptionSummaryMock.mockReset();
});
describe('With canIsolateHost privileges', () => {
beforeEach(() => {
Expand All @@ -41,17 +41,18 @@ describe('Fleet host isolation exceptions card filters card', () => {
});

it('should call the API and render the card correctly', async () => {
getHostIsolationExceptionItemsMock.mockResolvedValue({
getHostIsolationExceptionSummaryMock.mockResolvedValue({
linux: 5,
macos: 5,
total: 5,
windows: 5,
});
const renderResult = renderComponent();

await waitFor(() => {
expect(getHostIsolationExceptionItemsMock).toHaveBeenCalledWith({
expect(getHostIsolationExceptionSummaryMock).toHaveBeenCalledWith({
http: mockedContext.coreStart.http,
filter: `(exception-list-agnostic.attributes.tags:"policy:${policyId}" OR exception-list-agnostic.attributes.tags:"policy:all")`,
page: 1,
perPage: 1,
});
});

Expand All @@ -71,13 +72,16 @@ describe('Fleet host isolation exceptions card filters card', () => {
});

it('should not render the card if there are no exceptions associated', async () => {
getHostIsolationExceptionItemsMock.mockResolvedValue({
getHostIsolationExceptionSummaryMock.mockResolvedValue({
linux: 0,
macos: 0,
total: 0,
windows: 0,
});
const renderResult = renderComponent();

await waitFor(() => {
expect(getHostIsolationExceptionItemsMock).toHaveBeenCalled();
expect(getHostIsolationExceptionSummaryMock).toHaveBeenCalled();
});

expect(
Expand All @@ -86,13 +90,16 @@ describe('Fleet host isolation exceptions card filters card', () => {
});

it('should render the card if there are exceptions associated', async () => {
getHostIsolationExceptionItemsMock.mockResolvedValue({
getHostIsolationExceptionSummaryMock.mockResolvedValue({
linux: 1,
macos: 1,
total: 1,
windows: 1,
});
const renderResult = renderComponent();

await waitFor(() => {
expect(getHostIsolationExceptionItemsMock).toHaveBeenCalled();
expect(getHostIsolationExceptionSummaryMock).toHaveBeenCalled();
});

expect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
import { useAppUrl, useHttp, useToasts } from '../../../../../../../common/lib/kibana';
import { getPolicyHostIsolationExceptionsPath } from '../../../../../../common/routing';
import { parsePoliciesToKQL } from '../../../../../../common/utils';
import { getHostIsolationExceptionItems } from '../../../../../host_isolation_exceptions/service';
import { getHostIsolationExceptionSummary } from '../../../../../host_isolation_exceptions/service';
import { ExceptionItemsSummary } from './exception_items_summary';
import { LinkWithIcon } from './link_with_icon';
import { StyledEuiFlexItem } from './styled_components';
Expand Down Expand Up @@ -86,20 +86,12 @@ export const FleetIntegrationHostIsolationExceptionsCard = memo<{
isMounted.current = true;
const fetchStats = async () => {
try {
const summary = await getHostIsolationExceptionItems({
const summary = await getHostIsolationExceptionSummary({
http,
perPage: 1,
page: 1,
filter: parsePoliciesToKQL([policyId, 'all']),
});
if (isMounted.current) {
setStats({
total: summary.total,
// the following properties are not relevant for this specific card
windows: 0,
linux: 0,
macos: 0,
});
setStats(summary);
}
} catch (error) {
if (isMounted.current) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('Fleet trusted apps card', () => {
it('should render correctly with policyId', async () => {
TrustedAppsHttpServiceMock.mockImplementationOnce(() => {
return {
getTrustedAppsList: () => () => promise,
getTrustedAppsSummary: () => () => promise,
};
});
const component = await renderComponent({ policyId: 'policy-1' });
Expand Down

0 comments on commit af83384

Please sign in to comment.