Skip to content

Commit

Permalink
Added tests for host details overall policy status
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Apr 22, 2020
1 parent 874c848 commit a0d2389
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 24 deletions.
26 changes: 13 additions & 13 deletions x-pack/plugins/endpoint/common/generate_data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
OSFields,
PolicyData,
HostPolicyResponse,
PolicyResponseActionStatus,
HostPolicyResponseActionStatus,
} from './types';
import { factory as policyFactory } from './models/policy_config';

Expand Down Expand Up @@ -540,23 +540,23 @@ export class EndpointDocGenerator {
applied: {
version: '1.0.0',
id: '17d4b81d-9940-4b64-9de5-3e03ef1fb5cf',
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
response: {
configurations: {
malware: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
concerned_actions: ['download_model', 'workflow', 'a_custom_future_action'],
},
events: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
concerned_actions: ['ingest_events_config', 'workflow'],
},
logging: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
concerned_actions: ['configure_elasticsearch_connection'],
},
streaming: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
concerned_actions: [
'detect_file_open_events',
'download_global_artifacts',
Expand All @@ -566,31 +566,31 @@ export class EndpointDocGenerator {
},
actions: {
download_model: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'model downloaded',
},
ingest_events_config: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'no action taken',
},
workflow: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'the flow worked well',
},
a_custom_future_action: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'future message',
},
configure_elasticsearch_connection: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'some message',
},
detect_file_open_events: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'some message',
},
download_global_artifacts: {
status: PolicyResponseActionStatus.success,
status: HostPolicyResponseActionStatus.success,
message: 'some message',
},
},
Expand Down
8 changes: 4 additions & 4 deletions x-pack/plugins/endpoint/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,7 @@ export type NewPolicyData = NewDatasource & {
/**
* the possible status for actions, configurations and overall Policy Response
*/
export enum PolicyResponseActionStatus {
export enum HostPolicyResponseActionStatus {
success = 'success',
failure = 'failure',
warning = 'warning',
Expand All @@ -587,7 +587,7 @@ export enum PolicyResponseActionStatus {
* The details of a given action
*/
interface HostPolicyResponseActionDetails {
status: PolicyResponseActionStatus;
status: HostPolicyResponseActionStatus;
message: string;
}

Expand Down Expand Up @@ -621,7 +621,7 @@ interface HostPolicyResponseActions {
}

interface HostPolicyResponseConfigurationStatus {
status: PolicyResponseActionStatus;
status: HostPolicyResponseActionStatus;
concerned_actions: Array<keyof HostPolicyResponseActions>;
}

Expand Down Expand Up @@ -652,7 +652,7 @@ export interface HostPolicyResponse {
applied: {
version: string;
id: string;
status: PolicyResponseActionStatus;
status: HostPolicyResponseActionStatus;
response: {
configurations: {
malware: HostPolicyResponseConfigurationStatus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ export const HostDetails = memo(({ details }: { details: HostMetadata }) => {
defaultMessage: 'Policy Status',
}),
description: (
<EuiHealth color={POLICY_STATUS_TO_HEALTH_COLOR[policyStatus] || 'subdued'}>
<EuiHealth
color={POLICY_STATUS_TO_HEALTH_COLOR[policyStatus] || 'subdued'}
data-test-subj="policyStatusHealth"
>
{/* eslint-disable-next-line @elastic/eui/href-or-on-click */}
<EuiLink
data-test-subj="policyStatusValue"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import {
mockHostResultList,
} from '../../store/hosts/mock_host_result_list';
import { AppContextTestRender, createAppRootMockRenderer } from '../../mocks';
import { HostInfo } from '../../../../../common/types';
import { HostInfo, HostPolicyResponseActionStatus } from '../../../../../common/types';
import { EndpointDocGenerator } from '../../../../../common/generate_data';

describe('when on the hosts page', () => {
const docGenerator = new EndpointDocGenerator();
let render: () => ReturnType<AppContextTestRender['render']>;
let history: AppContextTestRender['history'];
let store: AppContextTestRender['store'];
Expand Down Expand Up @@ -91,6 +93,19 @@ describe('when on the hosts page', () => {

describe('when there is a selected host in the url', () => {
let hostDetails: HostInfo;
const dispatchServerReturnedHostPolicyResponse = (
overallStatus: HostPolicyResponseActionStatus = HostPolicyResponseActionStatus.success
) => {
const policyResponse = docGenerator.generatePolicyResponse();
policyResponse.endpoint.policy.applied.status = overallStatus;
store.dispatch({
type: 'serverReturnedHostPolicyResponse',
payload: {
policy_response: policyResponse,
},
});
};

beforeEach(() => {
const {
host_status,
Expand Down Expand Up @@ -137,7 +152,6 @@ describe('when on the hosts page', () => {
const renderResult = render();
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
expect(policyStatusLink).not.toBeNull();
expect(policyStatusLink.textContent).toEqual('Success');
expect(policyStatusLink.getAttribute('href')).toEqual(
'?selected_host=1&show=policy_response'
);
Expand All @@ -152,10 +166,58 @@ describe('when on the hosts page', () => {
const changedUrlAction = await userChangedUrlChecker;
expect(changedUrlAction.payload.search).toEqual('?selected_host=1&show=policy_response');
});
it.todo('should display Success overall policy status');
it.todo('should display Warning overall policy status');
it.todo('should display Failed overall policy status');
it.todo('should display Unknown overall policy status');
it('should display Success overall policy status', async () => {
const renderResult = render();
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
expect(policyStatusLink.textContent).toEqual('Success');

const policyStatusHealth = await renderResult.findByTestId('policyStatusHealth');
expect(
policyStatusHealth.querySelector('[data-euiicon-type][color="success"]')
).not.toBeNull();
});
it('should display Warning overall policy status', async () => {
const renderResult = render();
await middlewareSpy.waitForAction('serverReturnedHostPolicyResponse');
reactTestingLibrary.act(() => {
dispatchServerReturnedHostPolicyResponse(HostPolicyResponseActionStatus.warning);
});
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
expect(policyStatusLink.textContent).toEqual('Warning');

const policyStatusHealth = await renderResult.findByTestId('policyStatusHealth');
expect(
policyStatusHealth.querySelector('[data-euiicon-type][color="warning"]')
).not.toBeNull();
});
it('should display Failed overall policy status', async () => {
const renderResult = render();
await middlewareSpy.waitForAction('serverReturnedHostPolicyResponse');
reactTestingLibrary.act(() => {
dispatchServerReturnedHostPolicyResponse(HostPolicyResponseActionStatus.failure);
});
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
expect(policyStatusLink.textContent).toEqual('Failed');

const policyStatusHealth = await renderResult.findByTestId('policyStatusHealth');
expect(
policyStatusHealth.querySelector('[data-euiicon-type][color="danger"]')
).not.toBeNull();
});
it('should display Unknown overall policy status', async () => {
const renderResult = render();
await middlewareSpy.waitForAction('serverReturnedHostPolicyResponse');
reactTestingLibrary.act(() => {
dispatchServerReturnedHostPolicyResponse('' as HostPolicyResponseActionStatus);
});
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
expect(policyStatusLink.textContent).toEqual('Unknown');

const policyStatusHealth = await renderResult.findByTestId('policyStatusHealth');
expect(
policyStatusHealth.querySelector('[data-euiicon-type][color="subdued"]')
).not.toBeNull();
});
it('should include the link to logs', async () => {
const renderResult = render();
const linkToLogs = await renderResult.findByTestId('hostDetailsLinkToLogs');
Expand Down

0 comments on commit a0d2389

Please sign in to comment.