Commit 40a5c7c6 authored by Alexander Turinske's avatar Alexander Turinske

Fix bug where a rule does not have branches

- it is currently allowed on the backend to create a policy
  with a rule, but now branches, which will do nothing
- update the sidebar to display an empty text for this use
  case
- update tests
parent 219f5799
......@@ -5,6 +5,7 @@ export const DEFAULT_MR_TITLE = s__('SecurityOrchestration|Update scan execution
export const GRAPHQL_ERROR_MESSAGE = s__(
'SecurityOrchestration|There was a problem creating the new security policy',
);
export const NO_RULE_MESSAGE = s__('SecurityOrhestration|No rules defined - policy will not run.');
export const SECURITY_POLICY_ACTIONS = {
APPEND: 'APPEND',
......
import { convertToTitleCase, humanize } from '~/lib/utils/text_utility';
import { sprintf, s__, n__ } from '~/locale';
import { NO_RULE_MESSAGE } from './constants';
const getActionText = (scanType) =>
sprintf(s__('SecurityOrchestration|Executes a %{scanType} scan'), {
......@@ -16,7 +17,7 @@ const humanizeBranches = (originalBranches) => {
const plural = n__('branch', 'branches', branches.length);
if (branches.length <= 1) {
if (branches.length === 1) {
return sprintf(s__('SecurityOrchestration|%{branches} %{plural}'), {
branches: branches.join(','),
plural,
......@@ -69,5 +70,9 @@ export const humanizeActions = (actions) => {
* @returns {Array}
*/
export const humanizeRules = (rules) => {
return rules.map((r) => HUMANIZE_RULES_METHODS[r.type](r) || '');
const humanizedRules = rules.reduce((acc, curr) => {
return curr.branches ? [...acc, HUMANIZE_RULES_METHODS[curr.type](curr)] : acc;
}, []);
return humanizedRules.length ? humanizedRules : [NO_RULE_MESSAGE];
};
......@@ -33,13 +33,13 @@ describe('ScanExecutionPolicy component', () => {
});
it.each`
component | findComponent | text
component | finder | text
${'actions'} | ${findActions} | ${''}
${'rules'} | ${findRules} | ${''}
${'description'} | ${findDescription} | ${'This policy enforces pipeline configuration to have a job with DAST scan'}
${'latest scan'} | ${findLatestScan} | ${''}
`('does render the policy $component', ({ findComponent, text }) => {
const component = findComponent();
`('does render the policy $component', ({ finder, text }) => {
const component = finder();
expect(component.exists()).toBe(true);
if (text) {
expect(component.text()).toBe(text);
......@@ -61,11 +61,11 @@ describe('ScanExecutionPolicy component', () => {
});
it.each`
component | findComponent
component | finder
${'description'} | ${findDescription}
${'latest scan'} | ${findLatestScan}
`('does render the policy $component', ({ findComponent }) => {
expect(findComponent().exists()).toBe(false);
`('does render the policy $component', ({ finder }) => {
expect(finder().exists()).toBe(false);
});
});
});
import {
humanizeActions,
humanizeRules,
NO_RULE_MESSAGE,
} from 'ee/threat_monitoring/components/policy_editor/scan_execution_policy/lib';
const mockActions = [
......@@ -13,6 +14,7 @@ const mockRules = [
{ type: 'schedule', cadence: '*/10 * * * *', branches: ['main'] },
{ type: 'pipeline', branches: ['release/*', 'staging'] },
{ type: 'pipeline', branches: ['release/1.*', 'canary', 'staging'] },
{ type: 'pipeline' },
];
describe('humanizeActions', () => {
......@@ -32,8 +34,12 @@ describe('humanizeActions', () => {
});
describe('humanizeRules', () => {
it('returns an empty Array of rules as an empty Array', () => {
expect(humanizeRules([])).toStrictEqual([]);
it('returns the empty rules message in an Array if no rules are specified', () => {
expect(humanizeRules([])).toStrictEqual([NO_RULE_MESSAGE]);
});
it('returns the empty rules message in an Array if a single rule is passed in without a branch', () => {
expect(humanizeRules([])).toStrictEqual([NO_RULE_MESSAGE]);
});
it('returns a single rule as a human-readable string', () => {
......
......@@ -29760,6 +29760,9 @@ msgstr ""
msgid "SecurityOrchestration|view results"
msgstr ""
msgid "SecurityOrhestration|No rules defined - policy will not run."
msgstr ""
msgid "SecurityPolicies|+%{count} more"
msgstr ""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment