Skip to content

Commit

Permalink
fix test and middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinlog committed Jun 17, 2020
1 parent af11a72 commit 1605736
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import * as reactTestingLibrary from '@testing-library/react';

import { HostList } from './index';
import { mockHostDetailsApiResult, mockHostResultList } from '../store/mock_host_result_list';
import { mockPolicyResultList } from '../../policy/store/policy_list/mock_policy_result_list';
import { AppContextTestRender, createAppRootMockRenderer } from '../../../../common/mock/endpoint';
import {
HostInfo,
Expand Down Expand Up @@ -229,6 +230,7 @@ describe('when on the hosts page', () => {
expect(flyout).not.toBeNull();
});
});

it('should display policy name value as a link', async () => {
const renderResult = render();
const policyDetailsLink = await renderResult.findByTestId('policyDetailsValue');
Expand All @@ -237,7 +239,11 @@ describe('when on the hosts page', () => {
`#/management/policy/${hostDetails.metadata.endpoint.policy.applied.id}`
);
});

it('should update the URL when policy name link is clicked', async () => {
const policyItem = mockPolicyResultList({ total: 1 }).items[0];
coreStart.http.get.mockReturnValue(Promise.resolve({ item: policyItem }));

const renderResult = render();
const policyDetailsLink = await renderResult.findByTestId('policyDetailsValue');
const userChangedUrlChecker = middlewareSpy.waitForAction('userChangedUrl');
Expand All @@ -249,6 +255,7 @@ describe('when on the hosts page', () => {
`/management/policy/${hostDetails.metadata.endpoint.policy.applied.id}`
);
});

it('should display policy status value as a link', async () => {
const renderResult = render();
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
Expand All @@ -257,6 +264,7 @@ describe('when on the hosts page', () => {
'#/management/endpoints?page_index=0&page_size=10&selected_host=1&show=policy_response'
);
});

it('should update the URL when policy status link is clicked', async () => {
const renderResult = render();
const policyStatusLink = await renderResult.findByTestId('policyStatusValue');
Expand All @@ -269,6 +277,7 @@ describe('when on the hosts page', () => {
'?page_index=0&page_size=10&selected_host=1&show=policy_response'
);
});

it('should display Success overall policy status', async () => {
const renderResult = render();
reactTestingLibrary.act(() => {
Expand All @@ -282,6 +291,7 @@ describe('when on the hosts page', () => {
policyStatusHealth.querySelector('[data-euiicon-type][color="success"]')
).not.toBeNull();
});

it('should display Warning overall policy status', async () => {
const renderResult = render();
reactTestingLibrary.act(() => {
Expand All @@ -295,6 +305,7 @@ describe('when on the hosts page', () => {
policyStatusHealth.querySelector('[data-euiicon-type][color="warning"]')
).not.toBeNull();
});

it('should display Failed overall policy status', async () => {
const renderResult = render();
reactTestingLibrary.act(() => {
Expand All @@ -308,6 +319,7 @@ describe('when on the hosts page', () => {
policyStatusHealth.querySelector('[data-euiicon-type][color="danger"]')
).not.toBeNull();
});

it('should display Unknown overall policy status', async () => {
const renderResult = render();
reactTestingLibrary.act(() => {
Expand All @@ -321,6 +333,7 @@ describe('when on the hosts page', () => {
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 All @@ -330,6 +343,7 @@ describe('when on the hosts page', () => {
"/app/logs/stream?logFilter=(expression:'host.id:1',kind:kuery)"
);
});

describe('when link to logs is clicked', () => {
beforeEach(async () => {
const renderResult = render();
Expand All @@ -343,6 +357,7 @@ describe('when on the hosts page', () => {
expect(coreStart.application.navigateToApp.mock.calls).toHaveLength(1);
});
});

describe('when showing host Policy Response panel', () => {
let renderResult: ReturnType<typeof render>;
beforeEach(async () => {
Expand All @@ -357,10 +372,12 @@ describe('when on the hosts page', () => {
dispatchServerReturnedHostPolicyResponse();
});
});

it('should hide the host details panel', async () => {
const hostDetailsFlyout = await renderResult.queryByTestId('hostDetailsFlyoutBody');
expect(hostDetailsFlyout).toBeNull();
});

it('should display policy response sub-panel', async () => {
expect(
await renderResult.findByTestId('hostDetailsPolicyResponseFlyoutHeader')
Expand All @@ -369,17 +386,20 @@ describe('when on the hosts page', () => {
await renderResult.findByTestId('hostDetailsPolicyResponseFlyoutBody')
).not.toBeNull();
});

it('should include the sub-panel title', async () => {
expect(
(await renderResult.findByTestId('hostDetailsPolicyResponseFlyoutTitle')).textContent
).toBe('Policy Response');
});

it('should show a configuration section for each protection', async () => {
const configAccordions = await renderResult.findAllByTestId(
'hostDetailsPolicyResponseConfigAccordion'
);
expect(configAccordions).not.toBeNull();
});

it('should show an actions section for each configuration', async () => {
const actionAccordions = await renderResult.findAllByTestId(
'hostDetailsPolicyResponseActionsAccordion'
Expand All @@ -392,6 +412,7 @@ describe('when on the hosts page', () => {
expect(statusHealth).not.toBeNull();
expect(message).not.toBeNull();
});

it('should not show any numbered badges if all actions are successful', () => {
const policyResponse = docGenerator.generatePolicyResponse(
new Date().getTime(),
Expand All @@ -411,6 +432,7 @@ describe('when on the hosts page', () => {
expect(e).not.toBeNull();
});
});

it('should show a numbered badge if at least one action failed', () => {
reactTestingLibrary.act(() => {
dispatchServerReturnedHostPolicyResponse(HostPolicyResponseActionStatus.failure);
Expand All @@ -420,6 +442,7 @@ describe('when on the hosts page', () => {
);
expect(attentionBadge).not.toBeNull();
});

it('should show a numbered badge if at least one action has a warning', () => {
reactTestingLibrary.act(() => {
dispatchServerReturnedHostPolicyResponse(HostPolicyResponseActionStatus.warning);
Expand All @@ -429,13 +452,15 @@ describe('when on the hosts page', () => {
);
expect(attentionBadge).not.toBeNull();
});

it('should include the back to details link', async () => {
const subHeaderBackLink = await renderResult.findByTestId('flyoutSubHeaderBackButton');
expect(subHeaderBackLink.textContent).toBe('Endpoint Details');
expect(subHeaderBackLink.getAttribute('href')).toBe(
'#/management/endpoints?page_index=0&page_size=10&selected_host=1'
);
});

it('should update URL when back to details link is clicked', async () => {
const subHeaderBackLink = await renderResult.findByTestId('flyoutSubHeaderBackButton');
const userChangedUrlChecker = middlewareSpy.waitForAction('userChangedUrl');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ export const policyDetailsMiddlewareFactory: ImmutableMiddlewareFactory<PolicyDe
let policyItem: PolicyData;

try {
policyItem = (await sendGetDatasource(http, id)).item;
const response = await sendGetDatasource(http, id);
policyItem = response.item;
} catch (error) {
dispatch({
type: 'serverFailedToReturnPolicyDetailsData',
Expand All @@ -45,7 +46,7 @@ export const policyDetailsMiddlewareFactory: ImmutableMiddlewareFactory<PolicyDe

// Until we get the Default configuration into the Endpoint package so that the datasource has
// the expected data structure, we will add it here manually.
if (policyItem && !policyItem.inputs.length) {
if (!policyItem.inputs.length) {
policyItem.inputs = [
{
type: 'endpoint',
Expand All @@ -69,7 +70,7 @@ export const policyDetailsMiddlewareFactory: ImmutableMiddlewareFactory<PolicyDe

// Agent summary is secondary data, so its ok for it to come after the details
// page is populated with the main content
if (policyItem && policyItem.config_id) {
if (policyItem.config_id) {
const { results } = await sendGetFleetAgentStatusForConfig(http, policyItem.config_id);
dispatch({
type: 'serverReturnedPolicyDetailsAgentSummaryData',
Expand Down

0 comments on commit 1605736

Please sign in to comment.