Skip to content

Commit

Permalink
[Cases] Add space only tests (#99409)
Browse files Browse the repository at this point in the history
* Starting spaces tests

* Finishing space only tests

* Refactoring createCaseWithConnector

* Fixing spelling

* Addressing PR feedback and creating alert tests

* Fixing mocks
  • Loading branch information
jonathan-buttner authored May 10, 2021
1 parent 21d173d commit b910889
Show file tree
Hide file tree
Showing 46 changed files with 2,173 additions and 139 deletions.
1 change: 1 addition & 0 deletions x-pack/plugins/cases/server/client/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const createCasesSubClientMock = (): CasesSubClientMock => {
delete: jest.fn(),
getTags: jest.fn(),
getReporters: jest.fn(),
getCaseIDsByAlertID: jest.fn(),
};
};

Expand Down
1 change: 1 addition & 0 deletions x-pack/scripts/functional_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const onlyNotInCoverageTests = [
require.resolve('../test/alerting_api_integration/security_and_spaces/config.ts'),
require.resolve('../test/case_api_integration/security_and_spaces/config_basic.ts'),
require.resolve('../test/case_api_integration/security_and_spaces/config_trial.ts'),
require.resolve('../test/case_api_integration/spaces_only/config.ts'),
require.resolve('../test/apm_api_integration/basic/config.ts'),
require.resolve('../test/apm_api_integration/trial/config.ts'),
require.resolve('../test/apm_api_integration/rules/config.ts'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import { FtrProviderContext as CommonFtrProviderContext } from '../../../common/ftr_provider_context';
import { Role, User, UserInfo } from './types';
import { superUser, users } from './users';
import { users } from './users';
import { roles } from './roles';
import { spaces } from './spaces';

Expand Down Expand Up @@ -90,5 +90,3 @@ export const deleteSpacesAndUsers = async (getService: CommonFtrProviderContext[
await deleteSpaces(getService);
await deleteUsersAndRoles(getService);
};

export const superUserSpace1Auth = { user: superUser, space: 'space1' };
5 changes: 5 additions & 0 deletions x-pack/test/case_api_integration/common/lib/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ import {
} from '../../../../plugins/cases/common/api';

export const defaultUser = { email: null, full_name: null, username: 'elastic' };
/**
* A null filled user will occur when the security plugin is disabled
*/
export const nullUser = { email: null, full_name: null, username: null };

export const postCaseReq: CasePostRequest = {
description: 'This is a brand new case of a bad meanie defacing data',
title: 'Super Bad Security Issue',
Expand Down
113 changes: 91 additions & 22 deletions x-pack/test/case_api_integration/common/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type { KibanaClient } from '@elastic/elasticsearch/api/kibana';

import * as st from 'supertest';
import supertestAsPromised from 'supertest-as-promised';
import { ObjectRemover as ActionsRemover } from '../../../alerting_api_integration/common/lib';
import {
CASES_URL,
CASE_CONFIGURE_CONNECTORS_URL,
Expand Down Expand Up @@ -45,7 +46,7 @@ import {
CasesConfigurationsResponse,
CaseUserActionsResponse,
} from '../../../../plugins/cases/common/api';
import { postCollectionReq, postCommentGenAlertReq } from './mock';
import { getPostCaseRequest, postCollectionReq, postCommentGenAlertReq } from './mock';
import { getCaseUserActionUrl, getSubCasesUrl } from '../../../../plugins/cases/common/api/helpers';
import { ContextTypeGeneratedAlertType } from '../../../../plugins/cases/server/connectors';
import { SignalHit } from '../../../../plugins/security_solution/server/lib/detection_engine/signals/types';
Expand Down Expand Up @@ -539,6 +540,16 @@ export const deleteMappings = async (es: KibanaClient): Promise<void> => {
});
};

export const superUserSpace1Auth = getAuthWithSuperUser();

/**
* Returns an auth object with the specified space and user set as super user. The result can be passed to other utility
* functions.
*/
export function getAuthWithSuperUser(space: string = 'space1'): { user: User; space: string } {
return { user: superUser, space };
}

export const getSpaceUrlPrefix = (spaceId: string | undefined | null) => {
return spaceId && spaceId !== 'default' ? `/s/${spaceId}` : ``;
};
Expand All @@ -556,6 +567,72 @@ export const ensureSavedObjectIsAuthorized = (
entities.forEach((entity) => expect(owners.includes(entity.owner)).to.be(true));
};

export const createCaseWithConnector = async ({
supertest,
configureReq = {},
servicenowSimulatorURL,
actionsRemover,
auth = { user: superUser, space: null },
createCaseReq = getPostCaseRequest(),
}: {
supertest: st.SuperTest<supertestAsPromised.Test>;
servicenowSimulatorURL: string;
actionsRemover: ActionsRemover;
configureReq?: Record<string, unknown>;
auth?: { user: User; space: string | null };
createCaseReq?: CasePostRequest;
}): Promise<{
postedCase: CaseResponse;
connector: CreateConnectorResponse;
}> => {
const connector = await createConnector({
supertest,
req: {
...getServiceNowConnector(),
config: { apiUrl: servicenowSimulatorURL },
},
auth,
});

actionsRemover.add(auth.space ?? 'default', connector.id, 'action', 'actions');
await createConfiguration(
supertest,
{
...getConfigurationRequest({
id: connector.id,
name: connector.name,
type: connector.connector_type_id as ConnectorTypes,
}),
...configureReq,
},
200,
auth
);

const postedCase = await createCase(
supertest,
{
...createCaseReq,
connector: {
id: connector.id,
name: connector.name,
type: connector.connector_type_id,
fields: {
urgency: '2',
impact: '2',
severity: '2',
category: 'software',
subcategory: 'os',
},
} as CaseConnector,
},
200,
auth
);

return { postedCase, connector };
};

export const createCase = async (
supertest: st.SuperTest<supertestAsPromised.Test>,
params: CasePostRequest,
Expand Down Expand Up @@ -622,19 +699,6 @@ export const createComment = async ({
return theCase;
};

export const getAllUserAction = async (
supertest: st.SuperTest<supertestAsPromised.Test>,
caseId: string,
expectedHttpCode: number = 200
): Promise<CaseUserActionResponse[]> => {
const { body: userActions } = await supertest
.get(`${CASES_URL}/${caseId}/user_actions`)
.set('kbn-xsrf', 'true')
.expect(expectedHttpCode);

return userActions;
};

export const updateCase = async ({
supertest,
params,
Expand Down Expand Up @@ -742,13 +806,13 @@ export const getComment = async ({
caseId,
commentId,
expectedHttpCode = 200,
auth = { user: superUser },
auth = { user: superUser, space: null },
}: {
supertest: st.SuperTest<supertestAsPromised.Test>;
caseId: string;
commentId: string;
expectedHttpCode?: number;
auth?: { user: User; space?: string };
auth?: { user: User; space: string | null };
}): Promise<CommentResponse> => {
const { body: comment } = await supertest
.get(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments/${commentId}`)
Expand Down Expand Up @@ -843,13 +907,18 @@ export const createConnector = async ({
return connector;
};

export const getCaseConnectors = async (
supertest: st.SuperTest<supertestAsPromised.Test>,
expectedHttpCode: number = 200
): Promise<FindActionResult[]> => {
export const getCaseConnectors = async ({
supertest,
expectedHttpCode = 200,
auth = { user: superUser, space: null },
}: {
supertest: st.SuperTest<supertestAsPromised.Test>;
expectedHttpCode?: number;
auth?: { user: User; space: string | null };
}): Promise<FindActionResult[]> => {
const { body: connectors } = await supertest
.get(`${CASE_CONFIGURE_CONNECTORS_URL}/_find`)
.set('kbn-xsrf', 'true')
.get(`${getSpaceUrlPrefix(auth.space)}${CASE_CONFIGURE_CONNECTORS_URL}/_find`)
.auth(auth.user.username, auth.user.password)
.expect(expectedHttpCode);

return connectors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,10 @@ import {
deleteCases,
createComment,
getComment,
getAllUserAction,
removeServerGeneratedPropertiesFromUserAction,
getCase,
superUserSpace1Auth,
getCaseUserActions,
} from '../../../../common/lib/utils';
import { getSubCaseDetailsUrl } from '../../../../../../plugins/cases/common/api/helpers';
import { CaseResponse } from '../../../../../../plugins/cases/common/api';
Expand All @@ -39,8 +40,6 @@ import {
superUser,
} from '../../../../common/lib/authentication/users';

import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
const supertestWithoutAuth = getService('supertestWithoutAuth');
Expand Down Expand Up @@ -89,7 +88,7 @@ export default ({ getService }: FtrProviderContext): void => {
it('should create a user action when creating a case', async () => {
const postedCase = await createCase(supertest, getPostCaseRequest());
await deleteCases({ supertest, caseIDs: [postedCase.id] });
const userActions = await getAllUserAction(supertest, postedCase.id);
const userActions = await getCaseUserActions({ supertest, caseID: postedCase.id });
const creationUserAction = removeServerGeneratedPropertiesFromUserAction(userActions[1]);

expect(creationUserAction).to.eql({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ import {
createCase,
createComment,
updateCase,
getAllUserAction,
getCaseUserActions,
removeServerGeneratedPropertiesFromCase,
removeServerGeneratedPropertiesFromUserAction,
findCases,
superUserSpace1Auth,
} from '../../../../common/lib/utils';
import {
createSignalsIndex,
Expand All @@ -58,7 +59,6 @@ import {
secOnlyRead,
superUser,
} from '../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down Expand Up @@ -110,7 +110,7 @@ export default ({ getService }: FtrProviderContext): void => {
},
});

const userActions = await getAllUserAction(supertest, postedCase.id);
const userActions = await getCaseUserActions({ supertest, caseID: postedCase.id });
const statusUserAction = removeServerGeneratedPropertiesFromUserAction(userActions[1]);
const data = removeServerGeneratedPropertiesFromCase(patchedCases[0]);

Expand Down Expand Up @@ -149,7 +149,7 @@ export default ({ getService }: FtrProviderContext): void => {
},
});

const userActions = await getAllUserAction(supertest, postedCase.id);
const userActions = await getCaseUserActions({ supertest, caseID: postedCase.id });
const statusUserAction = removeServerGeneratedPropertiesFromUserAction(userActions[1]);
const data = removeServerGeneratedPropertiesFromCase(patchedCases[0]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
createCase,
removeServerGeneratedPropertiesFromCase,
removeServerGeneratedPropertiesFromUserAction,
getAllUserAction,
getCaseUserActions,
} from '../../../../common/lib/utils';
import {
secOnly,
Expand Down Expand Up @@ -106,7 +106,7 @@ export default ({ getService }: FtrProviderContext): void => {

it('should create a user action when creating a case', async () => {
const postedCase = await createCase(supertest, getPostCaseRequest());
const userActions = await getAllUserAction(supertest, postedCase.id);
const userActions = await getCaseUserActions({ supertest, caseID: postedCase.id });
const creationUserAction = removeServerGeneratedPropertiesFromUserAction(userActions[0]);

const { new_value, ...rest } = creationUserAction as CaseUserActionResponse;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
updateCase,
getAllCasesStatuses,
deleteAllCaseItems,
superUserSpace1Auth,
} from '../../../../../common/lib/utils';
import {
globalRead,
Expand All @@ -25,7 +26,6 @@ import {
secOnlyRead,
superUser,
} from '../../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
createComment,
deleteComment,
deleteAllComments,
superUserSpace1Auth,
} from '../../../../common/lib/utils';
import {
globalRead,
Expand All @@ -33,7 +34,6 @@ import {
secOnlyRead,
superUser,
} from '../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
ensureSavedObjectIsAuthorized,
getSpaceUrlPrefix,
createCase,
superUserSpace1Auth,
} from '../../../../common/lib/utils';

import {
Expand All @@ -40,7 +41,6 @@ import {
globalRead,
obsSecRead,
} from '../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
createCase,
createComment,
getAllComments,
superUserSpace1Auth,
} from '../../../../common/lib/utils';
import { CommentType } from '../../../../../../plugins/cases/common/api';
import {
Expand All @@ -31,7 +32,6 @@ import {
secOnlyRead,
superUser,
} from '../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
createCase,
createComment,
getComment,
superUserSpace1Auth,
} from '../../../../common/lib/utils';
import { CommentType } from '../../../../../../plugins/cases/common/api';
import {
Expand All @@ -30,7 +31,6 @@ import {
secOnlyRead,
superUser,
} from '../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
createCase,
createComment,
updateComment,
superUserSpace1Auth,
} from '../../../../common/lib/utils';
import {
globalRead,
Expand All @@ -45,7 +46,6 @@ import {
secOnlyRead,
superUser,
} from '../../../../common/lib/authentication/users';
import { superUserSpace1Auth } from '../../../../common/lib/authentication';

// eslint-disable-next-line import/no-default-export
export default ({ getService }: FtrProviderContext): void => {
Expand Down
Loading

0 comments on commit b910889

Please sign in to comment.