Skip to content

Commit

Permalink
[Cases] Refactor integration helper functions (#149640)
Browse files Browse the repository at this point in the history
This PR refactors the cases integration test API helper functions so
that splitting up the `utils.ts` file will be easier in the future
because it will all be hidden under the `api/index.ts` import. That way
moving functions out of the `index.ts` file into their own will not
require a ton of test files to be updated.
  • Loading branch information
jonathan-buttner authored Feb 8, 2023
1 parent 820500f commit 375a863
Show file tree
Hide file tree
Showing 106 changed files with 666 additions and 621 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import { APP_ID as SECURITY_SOLUTION_APP_ID } from '@kbn/security-solution-plugi
import { observabilityFeatureId as OBSERVABILITY_APP_ID } from '@kbn/observability-plugin/common';
import { FtrProviderContext } from '../../ftr_provider_context';

import { deleteAllCaseItems } from '../../../cases_api_integration/common/lib/utils';
import { deleteAllCaseItems } from '../../../cases_api_integration/common/lib/api';
import {
bulkGetUserProfiles,
suggestUserProfiles,
} from '../../../cases_api_integration/common/lib/user_profiles';
} from '../../../cases_api_integration/common/lib/api/user_profiles';
import {
casesAllUser,
casesReadUser,
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/cases/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
deleteUsersAndRoles,
} from '../../../cases_api_integration/common/lib/authentication';

import { loginUsers } from '../../../cases_api_integration/common/lib/user_profiles';
import { loginUsers } from '../../../cases_api_integration/common/lib/api/user_profiles';
import { casesAllUser, obsCasesAllUser, secAllUser, users } from './common/users';
import { roles } from './common/roles';
import { FtrProviderContext } from '../../ftr_provider_context';
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/cases/privileges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
deleteAllCaseItems,
deleteCases,
getCase,
} from '../../../cases_api_integration/common/lib/utils';
} from '../../../cases_api_integration/common/lib/api';
import {
casesAllUser,
casesNoDeleteUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import { APP_ID as SECURITY_SOLUTION_APP_ID } from '@kbn/security-solution-plugi
import { observabilityFeatureId as OBSERVABILITY_APP_ID } from '@kbn/observability-plugin/common';
import { FtrProviderContext } from '../../ftr_provider_context';

import { deleteAllCaseItems } from '../../../cases_api_integration/common/lib/utils';
import { suggestUserProfiles } from '../../../cases_api_integration/common/lib/user_profiles';
import { deleteAllCaseItems } from '../../../cases_api_integration/common/lib/api';
import { suggestUserProfiles } from '../../../cases_api_integration/common/lib/api/user_profiles';
import {
casesAllUser,
casesOnlyDeleteUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
createCase,
deleteAllCaseItems,
getCase,
} from '../../../cases_api_integration/common/lib/utils';
} from '../../../cases_api_integration/common/lib/api';
import { getPostCaseRequest } from '../../../cases_api_integration/common/lib/mock';

const secAll: Role = {
Expand Down
260 changes: 260 additions & 0 deletions x-pack/test/cases_api_integration/common/lib/api/attachments.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import type SuperTest from 'supertest';
import { CASES_INTERNAL_URL, CASES_URL } from '@kbn/cases-plugin/common/constants';
import {
AllCommentsResponse,
BulkCreateCommentRequest,
BulkGetAttachmentsResponse,
CaseResponse,
CommentPatchRequest,
CommentRequest,
CommentResponse,
CommentType,
} from '@kbn/cases-plugin/common/api';
import { User } from '../authentication/types';
import { superUser } from '../authentication/users';
import { getSpaceUrlPrefix, setupAuth } from './helpers';
import { createCase } from './case';
import { postCaseReq } from '../mock';

export const bulkGetAttachments = async ({
supertest,
attachmentIds,
caseId,
expectedHttpCode = 200,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
attachmentIds: string[];
caseId: string;
auth?: { user: User; space: string | null };
expectedHttpCode?: number;
}): Promise<BulkGetAttachmentsResponse> => {
const { body: comments } = await supertest
.post(`${getSpaceUrlPrefix(auth.space)}${CASES_INTERNAL_URL}/${caseId}/attachments/_bulk_get`)
.send({ ids: attachmentIds })
.set('kbn-xsrf', 'abc')
.auth(auth.user.username, auth.user.password)
.expect(expectedHttpCode);

return comments;
};

export const createComment = async ({
supertest,
caseId,
params,
auth = { user: superUser, space: null },
expectedHttpCode = 200,
headers = {},
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
params: CommentRequest;
auth?: { user: User; space: string | null } | null;
expectedHttpCode?: number;
headers?: Record<string, unknown>;
}): Promise<CaseResponse> => {
const apiCall = supertest.post(
`${getSpaceUrlPrefix(auth?.space)}${CASES_URL}/${caseId}/comments`
);

setupAuth({ apiCall, headers, auth });

const { body: theCase } = await apiCall
.set('kbn-xsrf', 'true')
.set(headers)
.send(params)
.expect(expectedHttpCode);

return theCase;
};

export const bulkCreateAttachments = async ({
supertest,
caseId,
params,
auth = { user: superUser, space: null },
expectedHttpCode = 200,
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
params: BulkCreateCommentRequest;
auth?: { user: User; space: string | null };
expectedHttpCode?: number;
}): Promise<CaseResponse> => {
const { body: theCase } = await supertest
.post(
`${getSpaceUrlPrefix(auth.space)}${CASES_INTERNAL_URL}/${caseId}/attachments/_bulk_create`
)
.auth(auth.user.username, auth.user.password)
.set('kbn-xsrf', 'true')
.send(params)
.expect(expectedHttpCode);

return theCase;
};

export const createCaseAndBulkCreateAttachments = async ({
supertest,
numberOfAttachments = 3,
auth = { user: superUser, space: null },
expectedHttpCode = 200,
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
numberOfAttachments?: number;
auth?: { user: User; space: string | null };
expectedHttpCode?: number;
}): Promise<{ theCase: CaseResponse; attachments: BulkCreateCommentRequest }> => {
const postedCase = await createCase(supertest, postCaseReq);
const attachments = getAttachments(numberOfAttachments);
const patchedCase = await bulkCreateAttachments({
supertest,
caseId: postedCase.id,
params: attachments,
});

return { theCase: patchedCase, attachments };
};

export const getAttachments = (numberOfAttachments: number): BulkCreateCommentRequest => {
return [...Array(numberOfAttachments)].map((index) => {
if (index % 0) {
return {
type: CommentType.user,
comment: `Test ${index + 1}`,
owner: 'securitySolutionFixture',
};
}

return {
type: CommentType.alert,
alertId: `test-id-${index + 1}`,
index: `test-index-${index + 1}`,
rule: {
id: `rule-test-id-${index + 1}`,
name: `Test ${index + 1}`,
},
owner: 'securitySolutionFixture',
};
});
};

export const deleteComment = async ({
supertest,
caseId,
commentId,
expectedHttpCode = 204,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
commentId: string;
expectedHttpCode?: number;
auth?: { user: User; space: string | null };
}): Promise<{} | Error> => {
const { body: comment } = await supertest
.delete(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments/${commentId}`)
.set('kbn-xsrf', 'true')
.auth(auth.user.username, auth.user.password)
.expect(expectedHttpCode)
.send();

return comment;
};

export const deleteAllComments = async ({
supertest,
caseId,
expectedHttpCode = 204,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
expectedHttpCode?: number;
auth?: { user: User; space: string | null };
}): Promise<{} | Error> => {
const { body: comment } = await supertest
.delete(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments`)
.set('kbn-xsrf', 'true')
.auth(auth.user.username, auth.user.password)
.expect(expectedHttpCode)
.send();

return comment;
};

export const getAllComments = async ({
supertest,
caseId,
expectedHttpCode = 200,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
auth?: { user: User; space: string | null };
expectedHttpCode?: number;
}): Promise<AllCommentsResponse> => {
const { body: comments } = await supertest
.get(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments`)
.auth(auth.user.username, auth.user.password)
.expect(expectedHttpCode);

return comments;
};

export const getComment = async ({
supertest,
caseId,
commentId,
expectedHttpCode = 200,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
commentId: string;
expectedHttpCode?: number;
auth?: { user: User; space: string | null };
}): Promise<CommentResponse> => {
const { body: comment } = await supertest
.get(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}/${caseId}/comments/${commentId}`)
.auth(auth.user.username, auth.user.password)
.expect(expectedHttpCode);

return comment;
};

export const updateComment = async ({
supertest,
caseId,
req,
expectedHttpCode = 200,
auth = { user: superUser, space: null },
headers = {},
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseId: string;
req: CommentPatchRequest;
expectedHttpCode?: number;
auth?: { user: User; space: string | null } | null;
headers?: Record<string, unknown>;
}): Promise<CaseResponse> => {
const apiCall = supertest.patch(
`${getSpaceUrlPrefix(auth?.space)}${CASES_URL}/${caseId}/comments`
);

setupAuth({ apiCall, headers, auth });
const { body: res } = await apiCall
.set('kbn-xsrf', 'true')
.set(headers)
.send(req)
.expect(expectedHttpCode);

return res;
};
61 changes: 61 additions & 0 deletions x-pack/test/cases_api_integration/common/lib/api/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

import { CASES_URL } from '@kbn/cases-plugin/common';
import { CasePostRequest, CaseResponse } from '@kbn/cases-plugin/common/api';
import type SuperTest from 'supertest';
import { User } from '../authentication/types';

import { superUser } from '../authentication/users';
import { getSpaceUrlPrefix, setupAuth } from './helpers';

export const createCase = async (
supertest: SuperTest.SuperTest<SuperTest.Test>,
params: CasePostRequest,
expectedHttpCode: number = 200,
auth: { user: User; space: string | null } | null = { user: superUser, space: null },
headers: Record<string, unknown> = {}
): Promise<CaseResponse> => {
const apiCall = supertest.post(`${getSpaceUrlPrefix(auth?.space)}${CASES_URL}`);

setupAuth({ apiCall, headers, auth });

const { body: theCase } = await apiCall
.set('kbn-xsrf', 'true')
.set(headers)
.send(params)
.expect(expectedHttpCode);

return theCase;
};

/**
* Sends a delete request for the specified case IDs.
*/
export const deleteCases = async ({
supertest,
caseIDs,
expectedHttpCode = 204,
auth = { user: superUser, space: null },
}: {
supertest: SuperTest.SuperTest<SuperTest.Test>;
caseIDs: string[];
expectedHttpCode?: number;
auth?: { user: User; space: string | null };
}) => {
const { body } = await supertest
.delete(`${getSpaceUrlPrefix(auth.space)}${CASES_URL}`)
.auth(auth.user.username, auth.user.password)
// we need to json stringify here because just passing in the array of case IDs will cause a 400 with Kibana
// not being able to parse the array correctly. The format ids=["1", "2"] seems to work, which stringify outputs.
.query({ ids: JSON.stringify(caseIDs) })
.set('kbn-xsrf', 'true')
.send()
.expect(expectedHttpCode);

return body;
};
Loading

0 comments on commit 375a863

Please sign in to comment.