Skip to content

Commit

Permalink
[Security Solution][Endpoint][Trusted Apps] Show warning for invalid …
Browse files Browse the repository at this point in the history
…hash when editing a Trusted App (elastic#140647)

* Re-evaluate TrustedApp conditions on field change during editing
* small refactor regarding getElement test helper
* improve visited state handler function naming
  • Loading branch information
gergoabraham authored Sep 15, 2022
1 parent 47fe9e7 commit 5f95f58
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,19 @@ describe('Condition entry input', () => {
onVisitedMock = jest.fn();
});

interface GetElementProps {
os?: OperatingSystem;
isRemoveDisabled?: boolean;
entry?: TrustedAppConditionEntry;
}

const getElement = (
subject: string,
os: OperatingSystem = OperatingSystem.WINDOWS,
isRemoveDisabled: boolean = false,
entry: TrustedAppConditionEntry = baseEntry
{
os = OperatingSystem.WINDOWS,
isRemoveDisabled = false,
entry = baseEntry,
}: GetElementProps = {}
) => (
<ConditionEntryInput
os={os}
Expand Down Expand Up @@ -80,7 +88,7 @@ describe('Condition entry input', () => {
});

it('should not be able to call on remove for field input because disabled', () => {
const element = mount(getElement('testOnRemove', OperatingSystem.WINDOWS, true));
const element = mount(getElement('testOnRemove', { isRemoveDisabled: true }));
expect(onRemoveMock).toHaveBeenCalledTimes(0);
element.find('[data-test-subj="testOnRemove-remove"]').first().simulate('click');
expect(onRemoveMock).toHaveBeenCalledTimes(0);
Expand All @@ -94,6 +102,21 @@ describe('Condition entry input', () => {
expect(onVisitedMock).toHaveBeenCalledWith(baseEntry);
});

it('should not call on visited for field change if value is empty', () => {
const emptyEntry = { ...baseEntry, value: '' };
const element = shallow(getElement('testOnVisited', { entry: emptyEntry }));
expect(onVisitedMock).toHaveBeenCalledTimes(0);
element.find('[data-test-subj="testOnVisited-field"]').first().simulate('change');
expect(onVisitedMock).toHaveBeenCalledTimes(0);
});

it('should call on visited for field change if value is not empty', () => {
const element = shallow(getElement('testOnVisited'));
expect(onVisitedMock).toHaveBeenCalledTimes(0);
element.find('[data-test-subj="testOnVisited-field"]').first().simulate('change');
expect(onVisitedMock).toHaveBeenCalledTimes(1);
});

it('should change value for field input', () => {
const element = shallow(getElement('testOnChange'));
expect(onChangeMock).toHaveBeenCalledTimes(0);
Expand Down Expand Up @@ -121,7 +144,7 @@ describe('Condition entry input', () => {
});

it('should be able to select two options when LINUX OS', () => {
const element = mount(getElement('testCheckSignatureOption', OperatingSystem.LINUX));
const element = mount(getElement('testCheckSignatureOption', { os: OperatingSystem.LINUX }));
const superSelectProps = element
.find('[data-test-subj="testCheckSignatureOption-field"]')
.first()
Expand All @@ -130,7 +153,7 @@ describe('Condition entry input', () => {
});

it('should be able to select two options when MAC OS', () => {
const element = mount(getElement('testCheckSignatureOption', OperatingSystem.MAC));
const element = mount(getElement('testCheckSignatureOption', { os: OperatingSystem.MAC }));
const superSelectProps = element
.find('[data-test-subj="testCheckSignatureOption-field"]')
.first()
Expand All @@ -144,11 +167,10 @@ describe('Condition entry input', () => {
expect(inputField.contains('is'));
});

it('should show operator dorpdown with two values when field is PATH', () => {
it('should show operator dropdown with two values when field is PATH', () => {
const element = shallow(
getElement('testOperatorOptions', undefined, undefined, {
...baseEntry,
field: ConditionEntryField.PATH,
getElement('testOperatorOptions', {
entry: { ...baseEntry, field: ConditionEntryField.PATH },
})
);
const superSelectProps = element
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,14 @@ export const ConditionEntryInput = memo<ConditionEntryInputProps>(
const getTestId = useTestIdGenerator(dataTestSubj);
const [isVisited, setIsVisited] = useState(false);

const handleVisited = useCallback(() => {
onVisited?.(entry);

if (!isVisited) {
setIsVisited(true);
}
}, [entry, isVisited, onVisited]);

const fieldOptions = useMemo<Array<EuiSuperSelectOption<string>>>(() => {
const getDropdownDisplay = (field: ConditionEntryField) => (
<>
Expand Down Expand Up @@ -132,8 +140,14 @@ export const ConditionEntryInput = memo<ConditionEntryInputProps>(
);

const handleFieldUpdate = useCallback(
(newField) => onChange({ ...entry, field: newField }, entry),
[entry, onChange]
(newField) => {
onChange({ ...entry, field: newField }, entry);

if (entry.value) {
handleVisited();
}
},
[handleVisited, entry, onChange]
);

const handleOperatorUpdate = useCallback(
Expand All @@ -144,13 +158,8 @@ export const ConditionEntryInput = memo<ConditionEntryInputProps>(
const handleRemoveClick = useCallback(() => onRemove(entry), [entry, onRemove]);

const handleValueOnBlur = useCallback(() => {
if (onVisited) {
onVisited(entry);
}
if (!isVisited) {
setIsVisited(true);
}
}, [entry, onVisited, isVisited]);
handleVisited();
}, [handleVisited]);

return (
<InputGroup data-test-subj={dataTestSubj}>
Expand Down

0 comments on commit 5f95f58

Please sign in to comment.