-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cases] Refactor integration helper functions (#149640)
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
1 parent
820500f
commit 375a863
Showing
106 changed files
with
666 additions
and
621 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
260 changes: 260 additions & 0 deletions
260
x-pack/test/cases_api_integration/common/lib/api/attachments.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
Oops, something went wrong.