Skip to content

Commit

Permalink
[Cases] Custom fields feature fix tests (#167472)
Browse files Browse the repository at this point in the history
## Summary

Connected to #160236

This PR fixes tests for custom field mvp feature

### Checklist

- [x] [Unit or functional
tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html)
were updated or added to match the most common scenarios

---------

Co-authored-by: Christos Nasikas <[email protected]>
Co-authored-by: kibanamachine <[email protected]>
Co-authored-by: adcoelho <[email protected]>
  • Loading branch information
4 people authored Sep 29, 2023
1 parent 764bb51 commit 60cdab5
Show file tree
Hide file tree
Showing 11 changed files with 117 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,17 @@ describe('CustomFields', () => {
});

it('shows error when custom fields reaches the limit', async () => {
const customFields = [
...customFieldsConfigurationMock,
{
key: 'third_field_key',
label: 'My third custom label',
const generatedMockCustomFields = [];

for (let i = 0; i < 8; i++) {
generatedMockCustomFields.push({
key: `field_key_${i + 1}`,
label: `My custom label ${i + 1}`,
type: CustomFieldTypes.TEXT,
required: false,
},
{
key: 'fourth_field_key',
label: 'My fourth custom label',
type: CustomFieldTypes.TOGGLE,
required: true,
},
{
key: 'fifth_field_key',
label: 'My fifth custom label',
type: CustomFieldTypes.TEXT,
required: true,
},
];
});
}
const customFields = [...customFieldsConfigurationMock, ...generatedMockCustomFields];

appMockRender.render(<CustomFields {...{ ...props, customFields }} />);

Expand Down
49 changes: 48 additions & 1 deletion x-pack/plugins/cases/server/client/cases/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,25 @@ describe('create', () => {
});

it('should not throw an error and fill out missing customFields when they are undefined', async () => {
casesClient.configure.get = jest.fn().mockResolvedValue([
{
owner: theCase.owner,
customFields: [
{
key: 'first_key',
type: CustomFieldTypes.TEXT,
label: 'foo',
required: false,
},
{
key: 'second_key',
type: CustomFieldTypes.TOGGLE,
label: 'foo',
required: false,
},
],
},
]);
await expect(create({ ...theCase }, clientArgs, casesClient)).resolves.not.toThrow();

expect(clientArgs.services.caseService.postNewCase).toHaveBeenCalledWith(
Expand Down Expand Up @@ -523,6 +542,34 @@ describe('create', () => {
);
});

it('should throw an error when required customFields are undefined', async () => {
casesClient.configure.get = jest.fn().mockResolvedValue([
{
owner: theCase.owner,
customFields: [
{
key: 'first_key',
type: CustomFieldTypes.TEXT,
label: 'foo',
required: true,
},
{
key: 'second_key',
type: CustomFieldTypes.TOGGLE,
label: 'foo',
required: false,
},
],
},
]);

await expect(
create({ ...theCase }, clientArgs, casesClient)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to create case: Error: Missing required custom fields: first_key"`
);
});

it('throws error when the customFields array is too long', async () => {
await expect(
create(
Expand All @@ -534,7 +581,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."`
);
});

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) {
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 @@ -79,7 +79,7 @@ export default ({ getService }: FtrProviderContext): void => {

expect(objects).to.have.length(4);

const expectedCaseRequest = { ...caseRequest, category: null }; // added default value
const expectedCaseRequest = { ...caseRequest, category: null, customFields: [] }; // added default value

expectExportToHaveCaseSavedObject(objects, expectedCaseRequest);
expectExportToHaveUserActions(objects, expectedCaseRequest);
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: 1 addition & 0 deletions x-pack/test/functional/services/cases/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function CasesCommonServiceProvider({ getService, getPageObject }: FtrPro
const radioGroup = await testSubjects.find(testSubject);
const label = await radioGroup.findByCssSelector(`label[for="${value}"]`);
await label.click();
await header.waitUntilLoadingHasFinished();
await this.assertRadioGroupValue(testSubject, value);
},

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 @@ -24,6 +25,9 @@ export default ({ getPageObject, getService }: FtrProviderContext) => {
});

describe('Closure options', function () {
this.beforeEach(async () => {
await header.waitUntilLoadingHasFinished();
});
it('defaults the closure option correctly', async () => {
await cases.common.assertRadioGroupValue('closure-options-radio-group', 'close-by-user');
});
Expand All @@ -33,6 +37,7 @@ export default ({ getPageObject, getService }: FtrProviderContext) => {
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
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ export function SvlCasesApiServiceProvider({ getService }: FtrProviderContext) {
status: CaseStatuses.open,
updated_by: null,
category: null,
customFields: [],
};
},

Expand Down

0 comments on commit 60cdab5

Please sign in to comment.