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] Custom fields feature fix tests #167472

Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
0a67b83
Add custom field user action type
cnasikas Sep 22, 2023
dab8dbc
Merge branch 'cases-custom-fields-feature-branch' into custom_fields_…
cnasikas Sep 22, 2023
b6b39a6
Add custom fields to the create user action
cnasikas Sep 22, 2023
815c168
Rename types
cnasikas Sep 24, 2023
4ec0ca7
Create customFields builder
cnasikas Sep 24, 2023
9a09fbd
Merge branch 'cases-custom-fields-feature-branch' into custom_fields_…
cnasikas Sep 24, 2023
9b2b1cd
Move custom fields type to dedicated domain file
cnasikas Sep 25, 2023
1fb4348
Accept an array of custom fields in the payload
cnasikas Sep 25, 2023
dfa0495
Create custom fields user action builder in the UI
cnasikas Sep 25, 2023
af5b0f9
Merge branch 'cases-custom-fields-feature-branch' into custom_fields_…
cnasikas Sep 25, 2023
e0d0622
Handle delete user actions
cnasikas Sep 25, 2023
c9bc8d3
Add more tests
cnasikas Sep 25, 2023
24bfc06
Merge branch 'cases-custom-fields-feature-branch' into custom_fields_…
cnasikas Sep 26, 2023
18d39dd
Merge branch 'cases-custom-fields-feature-branch' into custom_fields_…
cnasikas Sep 26, 2023
98e025a
Remove delete user action
cnasikas Sep 26, 2023
57ffcfc
Fix i18n
cnasikas Sep 26, 2023
dd4c36b
Fix empty user action label
cnasikas Sep 26, 2023
77e8545
Fix bug when updating custom fields
cnasikas Sep 26, 2023
5706bc8
Fix bugs when adding/removing custom field configs
cnasikas Sep 26, 2023
2ef7158
Fix types and tests
cnasikas Sep 26, 2023
f4efc20
Change schema
cnasikas Sep 26, 2023
6aae78d
Fix types and tests
cnasikas Sep 26, 2023
4d0d554
Allow only slugish keys
cnasikas Sep 27, 2023
d500dff
Fill out missing custom fields
cnasikas Sep 27, 2023
503ccc4
Chane the value schema for the toggle
cnasikas Sep 27, 2023
18ecb86
Merge branch 'cases-custom-fields-feature-branch' into custom_fields_…
cnasikas Sep 27, 2023
90f8561
Fix types and tests
cnasikas Sep 27, 2023
3bf2f42
Make types generic
cnasikas Sep 28, 2023
4979cc8
fix configure and import export tests
js-jankisalvi Sep 28, 2023
df339ed
update security solution case and configure API response with custom …
js-jankisalvi Sep 28, 2023
af8d52c
Merge remote-tracking branch 'cnasikas/custom_fields_new_schema' into…
js-jankisalvi Sep 28, 2023
debf8fb
Merge remote-tracking branch 'upstream/cases-custom-fields-feature-br…
js-jankisalvi Sep 28, 2023
fa5b1ed
patch case fix
js-jankisalvi Sep 28, 2023
2628c94
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Sep 28, 2023
0b2d3b3
Merge remote-tracking branch 'upstream/cases-custom-fields-feature-br…
js-jankisalvi Sep 28, 2023
01ee8c8
Merge branch 'cases-custom-fields-feature-branch' into custom-fields-…
js-jankisalvi Sep 28, 2023
e2ce598
Merge branch 'custom-fields-feature-fix-tests' of https://github.com/…
js-jankisalvi Sep 28, 2023
f10dd1b
Fix required custom fields validation.
adcoelho Sep 28, 2023
79c3de0
Merge branch 'cases-custom-fields-feature-branch' into custom-fields-…
js-jankisalvi Sep 28, 2023
28304c1
Merge branch 'custom-fields-feature-fix-tests' of https://github.com/…
js-jankisalvi Sep 28, 2023
35df316
fix jest tests
js-jankisalvi Sep 28, 2023
28209db
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Sep 28, 2023
0c2de5e
removed unnecessary change
js-jankisalvi Sep 28, 2023
75d2589
Merge branch 'custom-fields-feature-fix-tests' of https://github.com/…
js-jankisalvi Sep 28, 2023
0b05b5c
add empty customfields to postCase api mock, serverless service
js-jankisalvi Sep 28, 2023
e63c07f
fix ui jest test
js-jankisalvi Sep 28, 2023
b5f2ffb
update resolve case args
js-jankisalvi Sep 29, 2023
df19932
Merge branch 'cases-custom-fields-feature-branch' into custom-fields-…
js-jankisalvi Sep 29, 2023
e5ceb36
PR feedback
js-jankisalvi Sep 29, 2023
2249e67
[CI] Auto-commit changed files from 'node scripts/precommit_hook.js -…
kibanamachine Sep 29, 2023
4035897
added empty customfields to postCaseResp
js-jankisalvi Sep 29, 2023
756405f
Merge branch 'custom-fields-feature-fix-tests' of https://github.com/…
js-jankisalvi Sep 29, 2023
f254ca7
[CI] Auto-commit changed files from 'node scripts/eslint --no-cache -…
kibanamachine Sep 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 6 additions & 26 deletions x-pack/plugins/cases/server/client/cases/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -495,31 +495,11 @@ describe('create', () => {
);
});

it('should not throw an error and fill out missing customFields when they are undefined', async () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep this test and the new test you added ("should throw an error when required `customFields are undefined"). For this test, we need to make all fields in the configuration optional.

await expect(create({ ...theCase }, clientArgs, casesClient)).resolves.not.toThrow();

expect(clientArgs.services.caseService.postNewCase).toHaveBeenCalledWith(
expect.objectContaining({
attributes: {
...theCase,
closed_by: null,
closed_at: null,
category: null,
created_at: expect.any(String),
created_by: expect.any(Object),
updated_at: null,
updated_by: null,
external_service: null,
duration: null,
status: CaseStatuses.open,
customFields: [
{ key: 'first_key', type: 'text', value: null },
{ key: 'second_key', type: 'toggle', value: null },
],
},
id: expect.any(String),
refresh: false,
})
it('should throw an error when required customFields are undefined', async () => {
await expect(
create({ ...theCase }, clientArgs, casesClient)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to create case: Error: Missing required custom fields: first_key"`
);
});

Expand All @@ -534,7 +514,7 @@ describe('create', () => {
casesClient
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to create case: Error: The length of the field customFields is too long. Array must be of length <= 5."`
`"Failed to create case: Error: The length of the field customFields is too long. Array must be of length <= 10."`
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved
);
});

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/cases/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1068,7 +1068,7 @@ describe('update', () => {
casesClient
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to update case, ids: [{\\"id\\":\\"mock-id-1\\",\\"version\\":\\"WzAsMV0=\\"}]: Error: The length of the field customFields is too long. Array must be of length <= 5."`
`"Failed to update case, ids: [{\\"id\\":\\"mock-id-1\\",\\"version\\":\\"WzAsMV0=\\"}]: Error: The length of the field customFields is too long. Array must be of length <= 10."`
);
});

Expand Down
12 changes: 6 additions & 6 deletions x-pack/plugins/cases/server/client/cases/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ export const validateRequiredCustomFields = ({
requestCustomFields,
customFieldsConfiguration,
}: CustomFieldValidationParams) => {
if (!Array.isArray(requestCustomFields) || !requestCustomFields.length) {
return;
}

if (customFieldsConfiguration === undefined) {
throw Boom.badRequest('No custom fields configured.');
if (!Array.isArray(requestCustomFields) || !requestCustomFields.length) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am a bit confused about this change. What scenario are we trying to cover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is strange, this doesn't seem to be my change 😄

Copy link
Member

@cnasikas cnasikas Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that @adcoelho tried to fix some tests in f10dd1b (#167472)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that if there is no configuration or no custom fields we should return. The function validates only required custom fields so no configuration means no custom fields are required.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We discussed offline and we decided to leave it as it is.

return;
} else {
throw Boom.badRequest('No custom fields configured.');
}
}

const requiredCustomFields = customFieldsConfiguration.filter(
Expand All @@ -109,7 +109,7 @@ export const validateRequiredCustomFields = ({

const invalidCustomFieldKeys = differenceWith(
requiredCustomFields,
requestCustomFields,
requestCustomFields ?? [],
(requiredVal, requestedVal) => requiredVal.key === requestedVal.key
).map((e) => e.key);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@ export default ({ getService }: FtrProviderContext): void => {
const data = removeServerGeneratedPropertiesFromCase(patchedCases[0]);
expect(data).to.eql({
...postCaseResp(),
customFields: [
{
key: 'test_custom_field',
type: CustomFieldTypes.TEXT,
value: null,
},
],
title: 'new title',
updated_by: defaultUser,
});
Expand Down Expand Up @@ -329,7 +336,16 @@ export default ({ getService }: FtrProviderContext): void => {
})
);

const postedCase = await createCase(supertest, postCaseReq);
const postedCase = await createCase(supertest, {
...postCaseReq,
customFields: [
{
key: 'test_custom_field_2',
type: CustomFieldTypes.TOGGLE,
value: true,
},
],
});
const patchedCases = await updateCase({
supertest,
params: {
Expand Down Expand Up @@ -368,7 +384,7 @@ export default ({ getService }: FtrProviderContext): void => {
]);
});

it('should fill out missing custom fields', async () => {
it('should fill out missing optional custom fields', async () => {
await createConfiguration(
supertest,
getConfigurationRequest({
Expand All @@ -391,7 +407,17 @@ export default ({ getService }: FtrProviderContext): void => {
})
);

const postedCase = await createCase(supertest, postCaseReq);
const postedCase = await createCase(supertest, {
...postCaseReq,
customFields: [
{
key: 'test_custom_field_2',
type: CustomFieldTypes.TOGGLE,
value: true,
},
],
});

const patchedCases = await updateCase({
supertest,
params: {
Expand Down Expand Up @@ -1016,7 +1042,7 @@ export default ({ getService }: FtrProviderContext): void => {
});
});

it('400s when trying to create case with a missing required custom field', async () => {
it('400s when trying to patch a case with a missing required custom field', async () => {
await createConfiguration(
supertest,
getConfigurationRequest({
Expand All @@ -1032,7 +1058,16 @@ export default ({ getService }: FtrProviderContext): void => {
},
})
);
const postedCase = await createCase(supertest, postCaseReq);
const postedCase = await createCase(supertest, {
...postCaseReq,
customFields: [
{
key: 'test_custom_field',
type: CustomFieldTypes.TEXT,
value: ['hello'],
},
],
});

await updateCase({
supertest,
Expand All @@ -1049,7 +1084,7 @@ export default ({ getService }: FtrProviderContext): void => {
});
});

it('400s when trying to create case with a custom field with the wrong type', async () => {
it('400s when trying to patch a case with a custom field with the wrong type', async () => {
await createConfiguration(
supertest,
getConfigurationRequest({
Expand All @@ -1059,7 +1094,7 @@ export default ({ getService }: FtrProviderContext): void => {
key: 'test_custom_field',
label: 'text',
type: CustomFieldTypes.TEXT,
required: true,
required: false,
},
],
},
Expand All @@ -1078,7 +1113,7 @@ export default ({ getService }: FtrProviderContext): void => {
{
key: 'test_custom_field',
type: CustomFieldTypes.TOGGLE,
value: true,
value: false,
},
],
},
Expand Down
1 change: 0 additions & 1 deletion x-pack/test/functional/services/cases/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ export function CasesCommonServiceProvider({ getService, getPageObject }: FtrPro
const radioGroup = await testSubjects.find(testSubject);
const label = await radioGroup.findByCssSelector(`label[for="${value}"]`);
await label.click();
await this.assertRadioGroupValue(testSubject, value);
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved
},

async selectSeverity(severity: CaseSeverity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default ({ getPageObject, getService }: FtrProviderContext) => {
const testSubjects = getService('testSubjects');
const cases = getService('cases');
const toasts = getService('toasts');
const header = getPageObject('header');

describe('Configure', function () {
before(async () => {
Expand All @@ -25,14 +26,17 @@ export default ({ getPageObject, getService }: FtrProviderContext) => {

describe('Closure options', function () {
it('defaults the closure option correctly', async () => {
await header.waitUntilLoadingHasFinished();
js-jankisalvi marked this conversation as resolved.
Show resolved Hide resolved
await cases.common.assertRadioGroupValue('closure-options-radio-group', 'close-by-user');
});

it('change closure option successfully', async () => {
await header.waitUntilLoadingHasFinished();
await cases.common.selectRadioGroupValue('closure-options-radio-group', 'close-by-pushing');
const toast = await toasts.getToastElement(1);
expect(await toast.getVisibleText()).to.be('Saved external connection settings');
await toasts.dismissAllToasts();
await cases.common.assertRadioGroupValue('closure-options-radio-group', 'close-by-pushing');
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('Cases connectors', { tags: ['@ess', '@serverless'] }, () => {
error: null,
updated_at: null,
updated_by: null,
customFields: [],
mappings: [
{ source: 'title', target: 'short_description', action_type: 'overwrite' },
{ source: 'description', target: 'description', action_type: 'overwrite' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export const getCaseResponse = (): Case => ({
status: CaseStatuses.open,
severity: CaseSeverity.HIGH,
assignees: [],
customFields: [],
settings: {
syncAlerts: false,
},
Expand Down