Skip to content

Commit

Permalink
[Security Solution][Endpoint] Fix unhandled promise rejections in ski…
Browse files Browse the repository at this point in the history
…pped tests (elastic#115354) (elastic#115399)

* Fix errors and comment code in middleware (pending to fix this)

* Fix endpoint list middleware test

* Fix policy TA layout test

* Fix test returning missing promise

Co-authored-by: David Sánchez <[email protected]>
  • Loading branch information
kibanamachine and dasansol92 authored Oct 19, 2021
1 parent d925787 commit 78c2a33
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ jest.mock('../../../common/components/user_privileges/use_endpoint_privileges');
let onSearchMock: jest.Mock;
const mockUseEndpointPrivileges = useEndpointPrivileges as jest.Mock;

// unhandled promise rejection: https://github.com/elastic/kibana/issues/112699
describe('Search exceptions', () => {
let appTestContext: AppContextTestRender;
let renderResult: ReturnType<AppContextTestRender['render']>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,30 +122,27 @@ export const endpointActivityLogHttpMock =
const responseData = fleetActionGenerator.generateResponse({
agent_id: endpointMetadata.agent.id,
});

return {
body: {
page: 1,
pageSize: 50,
startDate: 'now-1d',
endDate: 'now',
data: [
{
type: 'response',
item: {
id: '',
data: responseData,
},
page: 1,
pageSize: 50,
startDate: 'now-1d',
endDate: 'now',
data: [
{
type: 'response',
item: {
id: '',
data: responseData,
},
{
type: 'action',
item: {
id: '',
data: actionData,
},
},
{
type: 'action',
item: {
id: '',
data: actionData,
},
],
},
},
],
};
},
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ jest.mock('../../../../common/lib/kibana');

type EndpointListStore = Store<Immutable<EndpointState>, Immutable<AppAction>>;

// unhandled promise rejection: https://github.com/elastic/kibana/issues/112699
describe.skip('endpoint list middleware', () => {
describe('endpoint list middleware', () => {
const getKibanaServicesMock = KibanaServices.get as jest.Mock;
let fakeCoreStart: jest.Mocked<CoreStart>;
let depsStart: DepsStartMock;
Expand Down Expand Up @@ -390,7 +389,6 @@ describe.skip('endpoint list middleware', () => {

it('should call get Activity Log API with correct paging options', async () => {
dispatchUserChangedUrl();

const updatePagingDispatched = waitForAction('endpointDetailsActivityLogUpdatePaging');
dispatchGetActivityLogPaging({ page: 3 });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,14 @@ import { createLoadedResourceState, isLoadedResourceState } from '../../../../..
import { getPolicyDetailsArtifactsListPath } from '../../../../../common/routing';
import { EndpointDocGenerator } from '../../../../../../../common/endpoint/generate_data';
import { policyListApiPathHandlers } from '../../../store/test_mock_utils';
import { licenseService } from '../../../../../../common/hooks/use_license';
import {
EndpointPrivileges,
useEndpointPrivileges,
} from '../../../../../../common/components/user_privileges/use_endpoint_privileges';

jest.mock('../../../../trusted_apps/service');
jest.mock('../../../../../../common/hooks/use_license', () => {
const licenseServiceInstance = {
isPlatinumPlus: jest.fn(),
};
return {
licenseService: licenseServiceInstance,
useLicense: () => {
return licenseServiceInstance;
},
};
});
jest.mock('../../../../../../common/components/user_privileges/use_endpoint_privileges');
const mockUseEndpointPrivileges = useEndpointPrivileges as jest.Mock;

let mockedContext: AppContextTestRender;
let waitForAction: MiddlewareActionSpyHelper['waitForAction'];
Expand All @@ -42,8 +36,17 @@ let coreStart: AppContextTestRender['coreStart'];
let http: typeof coreStart.http;
const generator = new EndpointDocGenerator();

// unhandled promise rejection: https://github.com/elastic/kibana/issues/112699
describe.skip('Policy trusted apps layout', () => {
describe('Policy trusted apps layout', () => {
const loadedUserEndpointPrivilegesState = (
endpointOverrides: Partial<EndpointPrivileges> = {}
): EndpointPrivileges => ({
loading: false,
canAccessFleet: true,
canAccessEndpointManagement: true,
isPlatinumPlus: true,
...endpointOverrides,
});

beforeEach(() => {
mockedContext = createAppRootMockRenderer();
http = mockedContext.coreStart.http;
Expand All @@ -59,6 +62,14 @@ describe.skip('Policy trusted apps layout', () => {
});
}

// GET Agent status for agent policy
if (path === '/api/fleet/agent-status') {
return Promise.resolve({
results: { events: 0, total: 5, online: 3, error: 1, offline: 1 },
success: true,
});
}

// Get package data
// Used in tests that route back to the list
if (policyListApiHandlers[path]) {
Expand All @@ -78,6 +89,10 @@ describe.skip('Policy trusted apps layout', () => {
render = () => mockedContext.render(<PolicyTrustedAppsLayout />);
});

afterAll(() => {
mockUseEndpointPrivileges.mockReset();
});

afterEach(() => reactTestingLibrary.cleanup());

it('should renders layout with no existing TA data', async () => {
Expand Down Expand Up @@ -121,7 +136,11 @@ describe.skip('Policy trusted apps layout', () => {
});

it('should hide assign button on empty state with unassigned policies when downgraded to a gold or below license', async () => {
(licenseService.isPlatinumPlus as jest.Mock).mockReturnValue(false);
mockUseEndpointPrivileges.mockReturnValue(
loadedUserEndpointPrivilegesState({
isPlatinumPlus: false,
})
);
const component = render();
mockedContext.history.push(getPolicyDetailsArtifactsListPath('1234'));

Expand All @@ -133,8 +152,13 @@ describe.skip('Policy trusted apps layout', () => {
});
expect(component.queryByTestId('assign-ta-button')).toBeNull();
});

it('should hide the `Assign trusted applications` button when there is data and the license is downgraded to gold or below', async () => {
(licenseService.isPlatinumPlus as jest.Mock).mockReturnValue(false);
mockUseEndpointPrivileges.mockReturnValue(
loadedUserEndpointPrivilegesState({
isPlatinumPlus: false,
})
);
TrustedAppsHttpServiceMock.mockImplementation(() => {
return {
getTrustedAppsList: () => getMockListResponse(),
Expand Down

0 comments on commit 78c2a33

Please sign in to comment.