Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cases] Fix fields query parameter in the _find API #128143

Merged
merged 6 commits into from Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/common/api/cases/case.ts
Expand Up @@ -153,7 +153,7 @@ export const CasesFindRequestRt = rt.partial({
/**
* The fields in the entity to return in the response
*/
fields: rt.array(rt.string),
fields: rt.union([rt.array(rt.string), rt.string]),
/**
* The page of objects to return
*/
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/cases/server/authorization/utils.test.ts
Expand Up @@ -174,8 +174,8 @@ describe('utils', () => {
expect(includeFieldsRequiredForAuthentication()).toBeUndefined();
});

it('returns an array with a single entry containing the owner field', () => {
expect(includeFieldsRequiredForAuthentication([])).toStrictEqual([OWNER_FIELD]);
it('returns undefined when the fields parameter is an empty array', () => {
expect(includeFieldsRequiredForAuthentication([])).toBeUndefined();
});

it('returns an array without duplicates and including the owner field', () => {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/authorization/utils.ts
Expand Up @@ -58,7 +58,7 @@ export const ensureFieldIsSafeForQuery = (field: string, value: string): boolean
};

export const includeFieldsRequiredForAuthentication = (fields?: string[]): string[] | undefined => {
if (fields === undefined) {
if (fields === undefined || fields.length === 0) {
return;
}
return uniq([...fields, OWNER_FIELD]);
Expand Down
6 changes: 4 additions & 2 deletions x-pack/plugins/cases/server/client/cases/find.ts
Expand Up @@ -38,8 +38,10 @@ export const find = async (
const { caseService, authorization, logger } = clientArgs;

try {
const fields = asArray(params.fields);

const queryParams = pipe(
excess(CasesFindRequestRt).decode(params),
excess(CasesFindRequestRt).decode({ ...params, fields }),
fold(throwErrors(Boom.badRequest), identity)
);

Expand Down Expand Up @@ -67,7 +69,7 @@ export const find = async (
...queryParams,
...caseQueryOptions,
searchFields: asArray(queryParams.searchFields),
fields: includeFieldsRequiredForAuthentication(queryParams.fields),
fields: includeFieldsRequiredForAuthentication(fields),
},
}),
caseService.getCaseStatusStats({
Expand Down
Expand Up @@ -195,6 +195,46 @@ export default ({ getService }: FtrProviderContext): void => {
expect(cases.count_in_progress_cases).to.eql(1);
});

it('returns the correct fields', async () => {
const postedCase = await createCase(supertest, postCaseReq);
const queryFields: Array<keyof CaseResponse | Array<keyof CaseResponse>> = [
'title',
['title', 'description'],
];

for (const fields of queryFields) {
const cases = await findCases({ supertest, query: { fields } });
const fieldsAsArray = Array.isArray(fields) ? fields : [fields];

const expectedValues = fieldsAsArray.reduce(
(theCase, field) => ({
...theCase,
[field]: postedCase[field],
}),
{}
);

expect(cases).to.eql({
...findCasesResp,
total: 1,
cases: [
{
id: postedCase.id,
version: postedCase.version,
external_service: postedCase.external_service,
owner: postedCase.owner,
connector: postedCase.connector,
comments: [],
totalAlerts: 0,
totalComment: 0,
...expectedValues,
},
],
count_open_cases: 1,
});
}
});

it('unhappy path - 400s when bad query supplied', async () => {
await findCases({ supertest, query: { perPage: true }, expectedHttpCode: 400 });
});
Expand Down