-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apply comments + refactor useAlertSuppression
- Loading branch information
Showing
6 changed files
with
91 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 0 additions & 55 deletions
55
...c/detection_engine/rule_management/components/rule_details/use_alert_suppression.test.tsx
This file was deleted.
Oops, something went wrong.
34 changes: 0 additions & 34 deletions
34
...public/detection_engine/rule_management/components/rule_details/use_alert_suppression.tsx
This file was deleted.
Oops, something went wrong.
51 changes: 51 additions & 0 deletions
51
...ity_solution/public/detection_engine/rule_management/hooks/use_alert_suppression.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { renderHook } from '@testing-library/react-hooks'; | ||
import type { Type } from '@kbn/securitysolution-io-ts-alerting-types'; | ||
import { SuppressibleAlertRules } from '../../../../common/detection_engine/constants'; | ||
import * as useIsExperimentalFeatureEnabledMock from '@kbn/security-solution-plugin/public/common/hooks/use_experimental_features'; | ||
import { useAlertSuppression } from './use_alert_suppression'; | ||
|
||
describe('useAlertSuppression', () => { | ||
it('should return the correct isSuppressionEnabled value if rule Type exists in SuppressibleAlertRules and Feature Flag is enabled', () => { | ||
jest | ||
.spyOn(useIsExperimentalFeatureEnabledMock, 'useIsExperimentalFeatureEnabled') | ||
.mockImplementation((featureFlagName: string) => { | ||
return featureFlagName === 'alertSuppressionForIndicatorMatchRuleEnabled'; | ||
}); | ||
const { result } = renderHook(() => useAlertSuppression(SuppressibleAlertRules.THREAT_MATCH)); | ||
|
||
expect(result.current.isSuppressionEnabled).toBe(true); | ||
}); | ||
it('should return the correct isSuppressionEnabled value if rule Type exists in SuppressibleAlertRules and Feature Flag is disabled', () => { | ||
jest | ||
.spyOn(useIsExperimentalFeatureEnabledMock, 'useIsExperimentalFeatureEnabled') | ||
.mockImplementation((featureFlagName: string) => { | ||
return featureFlagName !== 'alertSuppressionForIndicatorMatchRuleEnabled'; | ||
}); | ||
const { result } = renderHook(() => useAlertSuppression(SuppressibleAlertRules.THREAT_MATCH)); | ||
|
||
expect(result.current.isSuppressionEnabled).toBe(false); | ||
}); | ||
|
||
it('should return the correct isSuppressionEnabled value if rule Type exists in SuppressibleAlertRules', () => { | ||
const { result } = renderHook(() => useAlertSuppression(SuppressibleAlertRules.QUERY)); | ||
|
||
expect(result.current.isSuppressionEnabled).toBe(true); | ||
}); | ||
|
||
it('should return false if rule type is not set', () => { | ||
const { result } = renderHook(() => useAlertSuppression()); | ||
expect(result.current.isSuppressionEnabled).toBe(false); | ||
}); | ||
|
||
it('should return false if rule type is not THREAT_MATCH', () => { | ||
const { result } = renderHook(() => useAlertSuppression('OTHER_RULE_TYPE' as Type)); | ||
|
||
expect(result.current.isSuppressionEnabled).toBe(false); | ||
}); | ||
}); |
32 changes: 32 additions & 0 deletions
32
...security_solution/public/detection_engine/rule_management/hooks/use_alert_suppression.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { useCallback } from 'react'; | ||
import { Type } from '@kbn/securitysolution-io-ts-alerting-types'; | ||
import { isSuppressibleAlertRule } from '../../../../common/detection_engine/utils'; | ||
import { SuppressibleAlertRules } from '../../../../common/detection_engine/constants'; | ||
import { useIsExperimentalFeatureEnabled } from '../../../common/hooks/use_experimental_features'; | ||
export interface UseAlertSuppressionReturn { | ||
isSuppressionEnabled: boolean; | ||
} | ||
|
||
export const useAlertSuppression = (ruleType?: Type): UseAlertSuppressionReturn => { | ||
const isThreatMatchRuleFFEnabled = useIsExperimentalFeatureEnabled( | ||
'alertSuppressionForIndicatorMatchRuleEnabled' | ||
); | ||
|
||
const isSuppressionEnabledForRuleType = useCallback(() => { | ||
if (!ruleType) return false; | ||
|
||
if (ruleType === SuppressibleAlertRules.THREAT_MATCH) return isThreatMatchRuleFFEnabled; | ||
|
||
return isSuppressibleAlertRule(ruleType); | ||
}, [ruleType]); | ||
|
||
return { | ||
isSuppressionEnabled: isSuppressionEnabledForRuleType(), | ||
}; | ||
}; |