Commit fdfb1710 authored by Alexander Turinske's avatar Alexander Turinske

Update policy type method to be more robust

- use GraphQLs `__typename` field to determine
  type of policy instead of searching the string
- update tests
parent 666351f2
......@@ -24,21 +24,22 @@ export { dateFormats as DATE_FORMATS } from '~/analytics/shared/constants';
export const POLICY_TYPE_COMPONENT_OPTIONS = {
container: {
component: 'network-policy-editor',
kind: {
cilium: 'CiliumNetworkPolicy',
network: 'NetworkPolicy',
},
shouldShowEnvironmentPicker: true,
text: s__('SecurityOrchestration|Network'),
typeName: 'NetworkPolicy',
urlParameter: 'container_policy',
value: 'container',
yamlIndicator: {
cilium: 'CiliumNetworkPolicy',
network: 'NetworkPolicy',
},
},
scanExecution: {
component: 'scan-execution-policy-editor',
text: s__('SecurityOrchestration|Scan Execution'),
typeName: 'ScanExecutionPolicy',
urlParameter: 'scan_execution_policy',
value: 'scanExecution',
yamlIndicator: 'scanner_profile',
},
};
......
......@@ -167,7 +167,8 @@ export default {
return '';
},
policyType() {
return this.selectedPolicy ? getPolicyType(this.selectedPolicy.yaml) : 'container';
// eslint-disable-next-line no-underscore-dangle
return this.selectedPolicy ? getPolicyType(this.selectedPolicy.__typename) : 'container';
},
fields() {
const environments = {
......
......@@ -48,7 +48,7 @@ export default function toYaml(policy) {
const policySpec = {
apiVersion: 'cilium.io/v2',
kind: POLICY_TYPE_COMPONENT_OPTIONS.container.yamlIndicator.cilium,
kind: POLICY_TYPE_COMPONENT_OPTIONS.container.kind.cilium,
};
if (description?.length > 0) {
......
......@@ -168,7 +168,8 @@ export default {
: '';
},
policyType() {
return this.selectedPolicy ? getPolicyType(this.selectedPolicy.yaml) : '';
// eslint-disable-next-line no-underscore-dangle
return this.selectedPolicy ? getPolicyType(this.selectedPolicy.__typename) : '';
},
fields() {
const environments = {
......
import createGqClient from '~/lib/graphql';
import { POLICY_TYPE_COMPONENT_OPTIONS } from './components/constants';
/**
* Determines if the yaml passed in is of the type `container`
* @param {String} yaml the policy in yaml form
* @returns {Boolean}
*/
const isContainerPolicyYaml = (yaml) => {
const containerYamlIndicator = Object.values(
POLICY_TYPE_COMPONENT_OPTIONS.container.yamlIndicator,
);
return containerYamlIndicator.some((str) => yaml?.includes(str));
};
/**
* Get the height of the wrapper page element
* This height can be used to determine where the highest element goes in a page
......@@ -30,11 +18,11 @@ export const getContentWrapperHeight = (contentWrapperClass) => {
* @param {String} yaml policy's YAML manifest
* @returns {String|null} policy type if available
*/
export const getPolicyType = (yaml = '') => {
if (isContainerPolicyYaml(yaml)) {
export const getPolicyType = (typeName = '') => {
if (typeName === POLICY_TYPE_COMPONENT_OPTIONS.container.typeName) {
return POLICY_TYPE_COMPONENT_OPTIONS.container.value;
}
if (yaml?.includes(POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.yamlIndicator)) {
if (typeName === POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.typeName) {
return POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value;
}
return null;
......
......@@ -114,6 +114,7 @@ spec:
- cluster`;
export const mockCiliumPolicy = {
__typename: 'NetworkPolicy',
name: 'test-policy-03',
updatedAt: new Date('2021-06-07T00:00:00.000Z'),
yaml: mockCiliumManifest,
......@@ -121,6 +122,7 @@ export const mockCiliumPolicy = {
export const mockNetworkPoliciesResponse = [
{
__typename: 'NetworkPolicy',
name: 'policy',
kind: 'NetworkPolicy',
yaml: mockNetworkManifest,
......@@ -132,6 +134,7 @@ export const mockNetworkPoliciesResponse = [
},
},
{
__typename: 'NetworkPolicy',
name: 'test-policy-02',
kind: 'CiliumNetworkPolicy',
yaml: mockL3Manifest,
......@@ -145,6 +148,7 @@ export const mockNetworkPoliciesResponse = [
];
export const mockScanExecutionPolicy = {
__typename: 'ScanExecutionPolicy',
name: 'Scheduled DAST scan',
updatedAt: new Date('2021-06-07T00:00:00.000Z'),
yaml: mockDastScanExecutionManifest,
......
/* eslint-disable no-underscore-dangle */
import { POLICY_TYPE_COMPONENT_OPTIONS } from 'ee/threat_monitoring/components/constants';
import {
getContentWrapperHeight,
......@@ -5,11 +6,7 @@ import {
removeUnnecessaryDashes,
} from 'ee/threat_monitoring/utils';
import { setHTMLFixture } from 'helpers/fixtures';
import {
mockDastScanExecutionManifest,
mockCiliumManifest,
mockNetworkManifest,
} from './mocks/mock_data';
import { mockScanExecutionPolicy, mockNetworkPoliciesResponse } from './mocks/mock_data';
describe('Threat Monitoring Utils', () => {
describe('getContentWrapperHeight', () => {
......@@ -36,12 +33,12 @@ describe('Threat Monitoring Utils', () => {
describe('getPolicyType', () => {
it.each`
input | output
${''} | ${null}
${'random string'} | ${null}
${mockNetworkManifest} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value}
${mockCiliumManifest} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value}
${mockDastScanExecutionManifest} | ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value}
input | output
${''} | ${null}
${'UnknownPolicyType'} | ${null}
${mockNetworkPoliciesResponse[0].__typename} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value}
${mockNetworkPoliciesResponse[1].__typename} | ${POLICY_TYPE_COMPONENT_OPTIONS.container.value}
${mockScanExecutionPolicy.__typename} | ${POLICY_TYPE_COMPONENT_OPTIONS.scanExecution.value}
`('returns $output when used on $input', ({ input, output }) => {
expect(getPolicyType(input)).toBe(output);
});
......
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