Skip to content

Commit

Permalink
Update TheHiveTLP enum to be safer in the future
Browse files Browse the repository at this point in the history
  • Loading branch information
brijesh-elastic committed Sep 10, 2024
1 parent 63b8bd6 commit 0b5da8a
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ describe('TheHive Cases Fields', () => {
);

userEvent.selectOptions(screen.getByTestId('tlp-field'), '4');
expect(await screen.findByTestId('tlp-field')).toHaveValue(
Object.values(TheHiveTLP).indexOf(TheHiveTLP.RED).toString()
);
expect(await screen.findByTestId('tlp-field')).toHaveValue(TheHiveTLP.RED.toString());
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ import { TheHiveTLP } from './types';

const { emptyField } = fieldValidators;

const tlpOptions: Array<{ text: string; value: number }> = Object.entries(TheHiveTLP).map(
([_, value], index) => ({
text: value,
value: index,
})
const tlpOptions = Object.entries(TheHiveTLP).reduce<Array<{ text: string; value: number }>>(
(acc, [key, value]) => (typeof value === 'number' ? [...acc, { text: key, value }] : acc),
[]
);

const TheHiveFieldsComponent: React.FunctionComponent<ConnectorFieldsProps> = () => {
Expand All @@ -41,7 +39,7 @@ const TheHiveFieldsComponent: React.FunctionComponent<ConnectorFieldsProps> = ()
validator: emptyField(i18n.TLP_REQUIRED),
},
],
defaultValue: tlpOptions[2].value,
defaultValue: TheHiveTLP.AMBER,
}}
onChange={onTLPChange}
componentProps={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import * as i18n from './translations';
import { TheHiveTLP } from './types';

const mapTLP = (tlpValue: number): string => {
const tlpValues = Object.values(TheHiveTLP);
return tlpValues[tlpValue] || TheHiveTLP.AMBER;
const entry = Object.entries(TheHiveTLP).find(([_, value]) => value === tlpValue);
return entry?.[0] ?? 'AMBER';
};

const TheHiveFieldsPreviewComponent: React.FunctionComponent<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/

export enum TheHiveTLP {
CLEAR = 'CLEAR',
GREEN = 'GREEN',
AMBER = 'AMBER',
AMBER_STRICT = 'AMBER+STRICT',
RED = 'RED',
CLEAR = 0,
GREEN = 1,
AMBER = 2,
'AMBER+STRICT' = 3,
RED = 4,
}

0 comments on commit 0b5da8a

Please sign in to comment.