* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import * as React from 'react';
+import { getRuleDetails } from '../../../api/rules';
import { getSecurityHotspotDetails } from '../../../api/security-hotspots';
import { scrollToElement } from '../../../helpers/scrolling';
import {
HotspotStatusFilter,
HotspotStatusOption
} from '../../../types/security-hotspots';
-import { Component } from '../../../types/types';
+import { Component, RuleDescriptionSections } from '../../../types/types';
import { getStatusFilterFromStatusOption } from '../utils';
import HotspotViewerRenderer from './HotspotViewerRenderer';
this.mounted = false;
}
- fetchHotspot = () => {
+ fetchHotspot = async () => {
this.setState({ loading: true });
- return getSecurityHotspotDetails(this.props.hotspotKey)
- .then(hotspot => {
- if (this.mounted) {
- this.setState({ hotspot, loading: false });
- }
- return hotspot;
- })
- .catch(() => this.mounted && this.setState({ loading: false }));
+ try {
+ const hotspot = await getSecurityHotspotDetails(this.props.hotspotKey);
+ const ruleDetails = await getRuleDetails({ key: hotspot.rule.key }).then(r => r.rule);
+
+ if (this.mounted) {
+ hotspot.rule.riskDescription =
+ ruleDetails.descriptionSections?.find(section =>
+ [RuleDescriptionSections.DEFAULT, RuleDescriptionSections.ROOT_CAUSE].includes(
+ section.key
+ )
+ )?.content || hotspot.rule.riskDescription;
+
+ hotspot.rule.fixRecommendations =
+ ruleDetails.descriptionSections?.find(
+ section => RuleDescriptionSections.HOW_TO_FIX === section.key
+ )?.content || hotspot.rule.fixRecommendations;
+
+ hotspot.rule.vulnerabilityDescription =
+ ruleDetails.descriptionSections?.find(
+ section => RuleDescriptionSections.ASSESS_THE_PROBLEM === section.key
+ )?.content || hotspot.rule.vulnerabilityDescription;
+
+ this.setState({ hotspot, loading: false });
+ }
+ } catch (error) {
+ if (this.mounted) {
+ this.setState({ loading: false });
+ }
+ }
};
handleHotspotUpdate = async (statusUpdate = false, statusOption?: HotspotStatusOption) => {
import { shallow } from 'enzyme';
import { clone } from 'lodash';
import * as React from 'react';
+import { getRuleDetails } from '../../../../api/rules';
import { getSecurityHotspotDetails } from '../../../../api/security-hotspots';
import { mockComponent } from '../../../../helpers/mocks/component';
import { scrollToElement } from '../../../../helpers/scrolling';
+import { mockRuleDetails } from '../../../../helpers/testMocks';
import { waitAndUpdate } from '../../../../helpers/testUtils';
import { HotspotStatusOption } from '../../../../types/security-hotspots';
+import { RuleDescriptionSections } from '../../../../types/types';
import HotspotViewer from '../HotspotViewer';
import HotspotViewerRenderer from '../HotspotViewerRenderer';
const hotspotKey = 'hotspot-key';
jest.mock('../../../../api/security-hotspots', () => ({
- getSecurityHotspotDetails: jest.fn().mockResolvedValue({ id: `I am a detailled hotspot` })
+ getSecurityHotspotDetails: jest
+ .fn()
+ .mockResolvedValue({ id: `I am a detailled hotspot`, rule: {} })
+}));
+
+jest.mock('../../../../api/rules', () => ({
+ getRuleDetails: jest.fn().mockResolvedValue({ rule: { descriptionSections: [] } })
}));
jest.mock('../../../../helpers/scrolling', () => ({
expect(getSecurityHotspotDetails).toHaveBeenCalledWith(newHotspotKey);
});
+it('should render fetch rule details', async () => {
+ (getRuleDetails as jest.Mock).mockResolvedValueOnce({
+ rule: mockRuleDetails({
+ descriptionSections: [
+ {
+ key: RuleDescriptionSections.ASSESS_THE_PROBLEM,
+ content: 'assess'
+ },
+ {
+ key: RuleDescriptionSections.ROOT_CAUSE,
+ content: 'cause'
+ },
+ {
+ key: RuleDescriptionSections.HOW_TO_FIX,
+ content: 'how'
+ }
+ ]
+ })
+ });
+ const wrapper = shallowRender();
+ await waitAndUpdate(wrapper);
+
+ expect(wrapper.state().hotspot?.rule).toStrictEqual({
+ fixRecommendations: 'how',
+ riskDescription: 'cause',
+ vulnerabilityDescription: 'assess'
+ });
+});
+
it('should refresh hotspot list on status update', () => {
const onUpdateHotspot = jest.fn();
const wrapper = shallowRender({ onUpdateHotspot });