Skip to content

Commit

Permalink
Adds error message when there are duplicated entries. Also adds unit …
Browse files Browse the repository at this point in the history
…test for that use case (#123812)
  • Loading branch information
dasansol92 authored Jan 27, 2022
1 parent 94e64e4 commit cc082bc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,18 @@ describe('When using the Trusted App Form', () => {
expect(renderResult.getByText('[2] Field entry must have a value'));
});

it('should validate duplicated conditions', () => {
const andButton = getConditionBuilderAndButton();
reactTestingLibrary.act(() => {
fireEvent.click(andButton, { button: 1 });
});

setTextFieldValue(getConditionValue(getCondition()), '');
rerenderWithLatestTrustedApp();

expect(renderResult.getByText('Hash cannot be added more than once'));
});

it('should validate multiple errors in form', () => {
const andButton = getConditionBuilderAndButton();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import { i18n } from '@kbn/i18n';
import { EuiFormProps } from '@elastic/eui/src/components/form/form';
import {
ConditionEntry,
ConditionEntryField,
EffectScope,
MacosLinuxConditionEntry,
Expand All @@ -31,6 +32,7 @@ import {
isValidHash,
isPathValid,
hasSimpleExecutableName,
getDuplicateFields,
} from '../../../../../../common/endpoint/service/trusted_apps/validations';

import {
Expand All @@ -40,7 +42,7 @@ import {
isWindowsTrustedAppCondition,
} from '../../state/type_guards';
import { defaultConditionEntry } from '../../store/builders';
import { OS_TITLES } from '../translations';
import { CONDITION_FIELD_TITLE, OS_TITLES } from '../translations';
import { LogicalConditionBuilder, LogicalConditionBuilderProps } from './logical_condition';
import { useTestIdGenerator } from '../../../../components/hooks/use_test_id_generator';
import { useLicense } from '../../../../../common/hooks/use_license';
Expand Down Expand Up @@ -135,6 +137,21 @@ const validateFormValues = (values: MaybeImmutable<NewTrustedApp>): ValidationRe
})
);
} else {
const duplicated = getDuplicateFields(values.entries as ConditionEntry[]);
if (duplicated.length) {
isValid = false;
duplicated.forEach((field) => {
addResultToValidation(
validation,
'entries',
'errors',
i18n.translate('xpack.securitySolution.trustedapps.create.conditionFieldDuplicatedMsg', {
defaultMessage: '{field} cannot be added more than once',
values: { field: CONDITION_FIELD_TITLE[field] },
})
);
});
}
values.entries.forEach((entry, index) => {
const isValidPathEntry = isPathValid({
os: values.os,
Expand Down

0 comments on commit cc082bc

Please sign in to comment.