Skip to content

Commit

Permalink
Adds hash validation on UI. Display as many error messages as they ar…
Browse files Browse the repository at this point in the history
…e instead of displaying just the first one on entry fields. Updates related unit test
  • Loading branch information
dasansol92 committed Mar 18, 2021
1 parent 52a1ce1 commit 2805afe
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,11 @@ describe('When showing the Trusted App Create Form', () => {
expect(renderResult.getByText('Name is required'));
});

it('should validate invalid Hash value', () => {
setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
expect(renderResult.getByText('[1] Invalid hash value'));
});

it('should validate that a condition value has a non empty space value', () => {
setTextFieldValue(getConditionValue(getCondition(renderResult)), ' ');
expect(renderResult.getByText('[1] Field entry must have a value'));
Expand All @@ -281,13 +286,27 @@ describe('When showing the Trusted App Create Form', () => {
setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
expect(renderResult.getByText('[2] Field entry must have a value'));
});

it('should validate multiple errors in form', () => {
const andButton = getConditionBuilderAndButton(renderResult);
reactTestingLibrary.act(() => {
fireEvent.click(andButton, { button: 1 });
});

setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
expect(renderResult.getByText('[1] Invalid hash value'));
expect(renderResult.getByText('[2] Field entry must have a value'));
});
});

describe('and all required data passes validation', () => {
it('should call change callback with isValid set to true and contain the new item', () => {
const renderResult = render();
setTextFieldValue(getNameField(renderResult), 'Some Process');
setTextFieldValue(getConditionValue(getCondition(renderResult)), 'someHASH');
setTextFieldValue(
getConditionValue(getCondition(renderResult)),
'e50fb1a0e5fff590ece385082edc6c41'
);
setTextFieldValue(getDescriptionField(renderResult), 'some description');

expect(getAllValidationErrors(renderResult)).toHaveLength(0);
Expand All @@ -300,7 +319,7 @@ describe('When showing the Trusted App Create Form', () => {
field: ConditionEntryField.HASH,
operator: 'included',
type: 'match',
value: 'someHASH',
value: 'e50fb1a0e5fff590ece385082edc6c41',
},
],
name: 'Some Process',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ import {
import { i18n } from '@kbn/i18n';
import { EuiFormProps } from '@elastic/eui/src/components/form/form';
import {
ConditionEntryField,
MacosLinuxConditionEntry,
NewTrustedApp,
OperatingSystem,
} from '../../../../../../common/endpoint/types';
import { isValidHash } from '../../../../../../common/endpoint/validation/trusted_apps';

import {
isMacosLinuxTrustedAppCondition,
isWindowsTrustedAppCondition,
Expand Down Expand Up @@ -113,7 +116,7 @@ const validateFormValues = (values: NewTrustedApp): ValidationResult => {
})
);
} else {
values.entries.some((entry, index) => {
values.entries.forEach((entry, index) => {
if (!entry.field || !entry.value.trim()) {
isValid = false;
addResultToValidation(
Expand All @@ -128,9 +131,18 @@ const validateFormValues = (values: NewTrustedApp): ValidationResult => {
}
)
);
return true;
} else if (entry.field === ConditionEntryField.HASH && !isValidHash(entry.value)) {
isValid = false;
addResultToValidation(
validation,
'entries',
'errors',
i18n.translate('xpack.securitySolution.trustedapps.create.conditionFieldInvalidHashMsg', {
defaultMessage: '[{row}] Invalid hash value',
values: { row: index + 1 },
})
);
}
return false;
});
}

Expand Down

0 comments on commit 2805afe

Please sign in to comment.