Fix scan execution policy selection

This cleans up the policy selection handling by directly storing the
selected policy object instead of computing it based on the policy'
name. This fixes a bug where the policy drawer would remain empty when
selecting a scan execution policy as the `selectedPolicy` object was
being computed based on `networkPolicies` only.

This also fixes a bug where editing a network policy would lead to an
error 404. Including the policy kind in the edit URL should resolve the
issue as the backend now expects this parameter to load policy data to
be passed to the frontend.

Changelog: fixed
EE: true
parent 7db9f4c6
...@@ -98,9 +98,7 @@ export default { ...@@ -98,9 +98,7 @@ export default {
}, },
data() { data() {
return { return {
selectedPolicyName: null, selectedPolicy: null,
initialManifest: null,
initialEnforcementStatus: null,
networkPolicies: [], networkPolicies: [],
scanExecutionPolicies: [], scanExecutionPolicies: [],
}; };
...@@ -129,11 +127,7 @@ export default { ...@@ -129,11 +127,7 @@ export default {
); );
}, },
hasSelectedPolicy() { hasSelectedPolicy() {
return Boolean(this.selectedPolicyName); return Boolean(this.selectedPolicy);
},
selectedPolicy() {
if (!this.hasSelectedPolicy) return null;
return this.networkPolicies.find((policy) => policy.name === this.selectedPolicyName);
}, },
hasAutoDevopsPolicy() { hasAutoDevopsPolicy() {
return Boolean(this.networkPolicies?.some((policy) => policy.fromAutoDevops)); return Boolean(this.networkPolicies?.some((policy) => policy.fromAutoDevops));
...@@ -141,8 +135,10 @@ export default { ...@@ -141,8 +135,10 @@ export default {
editPolicyPath() { editPolicyPath() {
return this.hasSelectedPolicy return this.hasSelectedPolicy
? mergeUrlParams( ? mergeUrlParams(
{ environment_id: this.currentEnvironmentId }, !this.selectedPolicy.kind
this.newPolicyPath.replace('new', `${this.selectedPolicyName}/edit`), ? { environment_id: this.currentEnvironmentId }
: { environment_id: this.currentEnvironmentId, kind: this.selectedPolicy.kind },
this.newPolicyPath.replace('new', `${this.selectedPolicy.name}/edit`),
) )
: ''; : '';
}, },
...@@ -191,12 +187,10 @@ export default { ...@@ -191,12 +187,10 @@ export default {
if (rows.length === 0) return; if (rows.length === 0) return;
const [selectedPolicy] = rows; const [selectedPolicy] = rows;
this.selectedPolicyName = selectedPolicy?.name; this.selectedPolicy = selectedPolicy;
this.initialManifest = selectedPolicy?.yaml;
this.initialEnforcementStatus = selectedPolicy?.enabled;
}, },
deselectPolicy() { deselectPolicy() {
this.selectedPolicyName = null; this.selectedPolicy = null;
const bTable = this.$refs.policiesTable.$children[0]; const bTable = this.$refs.policiesTable.$children[0];
bTable.clearSelected(); bTable.clearSelected();
......
...@@ -3,6 +3,7 @@ query networkPolicies($fullPath: ID!, $environmentId: EnvironmentID) { ...@@ -3,6 +3,7 @@ query networkPolicies($fullPath: ID!, $environmentId: EnvironmentID) {
networkPolicies(environmentId: $environmentId) { networkPolicies(environmentId: $environmentId) {
nodes { nodes {
name name
kind
yaml yaml
enabled enabled
fromAutoDevops fromAutoDevops
......
...@@ -2,11 +2,13 @@ import { GlTable, GlDrawer } from '@gitlab/ui'; ...@@ -2,11 +2,13 @@ import { GlTable, GlDrawer } from '@gitlab/ui';
import { createLocalVue } from '@vue/test-utils'; import { createLocalVue } from '@vue/test-utils';
import { merge } from 'lodash'; import { merge } from 'lodash';
import VueApollo from 'vue-apollo'; import VueApollo from 'vue-apollo';
import PolicyDrawer from 'ee/threat_monitoring/components/policy_drawer/policy_drawer.vue';
import PolicyList from 'ee/threat_monitoring/components/policy_list.vue'; import PolicyList from 'ee/threat_monitoring/components/policy_list.vue';
import networkPoliciesQuery from 'ee/threat_monitoring/graphql/queries/network_policies.query.graphql'; import networkPoliciesQuery from 'ee/threat_monitoring/graphql/queries/network_policies.query.graphql';
import scanExecutionPoliciesQuery from 'ee/threat_monitoring/graphql/queries/scan_execution_policies.query.graphql'; import scanExecutionPoliciesQuery from 'ee/threat_monitoring/graphql/queries/scan_execution_policies.query.graphql';
import createStore from 'ee/threat_monitoring/store'; import createStore from 'ee/threat_monitoring/store';
import createMockApollo from 'helpers/mock_apollo_helper'; import createMockApollo from 'helpers/mock_apollo_helper';
import { stubComponent } from 'helpers/stub_component';
import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper'; import { mountExtended, shallowMountExtended } from 'helpers/vue_test_utils_helper';
import waitForPromises from 'helpers/wait_for_promises'; import waitForPromises from 'helpers/wait_for_promises';
import { networkPolicies, scanExecutionPolicies } from '../mocks/mock_apollo'; import { networkPolicies, scanExecutionPolicies } from '../mocks/mock_apollo';
...@@ -64,7 +66,12 @@ describe('PolicyList component', () => { ...@@ -64,7 +66,12 @@ describe('PolicyList component', () => {
[scanExecutionPoliciesQuery, requestHandlers.scanExecutionPolicies], [scanExecutionPoliciesQuery, requestHandlers.scanExecutionPolicies],
]), ]),
stubs: { stubs: {
PolicyDrawer: GlDrawer, PolicyDrawer: stubComponent(PolicyDrawer, {
props: {
...PolicyDrawer.props,
...GlDrawer.props,
},
}),
}, },
localVue, localVue,
}, },
...@@ -203,16 +210,21 @@ describe('PolicyList component', () => { ...@@ -203,16 +210,21 @@ describe('PolicyList component', () => {
}); });
}); });
describe('given there is a selected policy', () => { describe.each`
description | policy
${'network'} | ${mockNetworkPoliciesResponse[0]}
${'scan execution'} | ${mockScanExecutionPoliciesResponse[0]}
`('given there is a $description policy selected', ({ policy }) => {
beforeEach(() => { beforeEach(() => {
mountShallowWrapper(); mountShallowWrapper();
findPoliciesTable().vm.$emit('row-selected', [mockNetworkPoliciesResponse[0]]); findPoliciesTable().vm.$emit('row-selected', [policy]);
}); });
it('renders opened editor drawer', () => { it('renders opened editor drawer', () => {
const editorDrawer = findPolicyDrawer(); const editorDrawer = findPolicyDrawer();
expect(editorDrawer.exists()).toBe(true); expect(editorDrawer.exists()).toBe(true);
expect(editorDrawer.props('open')).toBe(true); expect(editorDrawer.props('open')).toBe(true);
expect(editorDrawer.props('policy')).toBe(policy);
}); });
}); });
......
...@@ -18,6 +18,7 @@ export const mockEnvironmentsResponse = { ...@@ -18,6 +18,7 @@ export const mockEnvironmentsResponse = {
export const mockNetworkPoliciesResponse = [ export const mockNetworkPoliciesResponse = [
{ {
name: 'policy', name: 'policy',
kind: 'NetworkPolicy',
yaml: `--- yaml: `---
apiVersion: networking.k8s.io/v1 apiVersion: networking.k8s.io/v1
kind: NetworkPolicy kind: NetworkPolicy
......
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