Commit 95a09020 authored by Alexander Turinske's avatar Alexander Turinske

Fix not being able to delete unparseable policies

- pass yamlEditorValue instead of trying
  to parse the unparseable yaml
- update tests to be more robust
- abstract mock data

Changelog: fixed
EE: true
parent 5d2c6277
...@@ -180,7 +180,7 @@ export default { ...@@ -180,7 +180,7 @@ export default {
}); });
}, },
removePolicy() { removePolicy() {
const policy = { name: this.existingPolicy.name, manifest: toYaml(this.policy) }; const policy = { name: this.existingPolicy.name, manifest: this.yamlEditorValue };
return this.deletePolicy({ environmentId: this.currentEnvironmentId, policy }).then(() => { return this.deletePolicy({ environmentId: this.currentEnvironmentId, policy }).then(() => {
if (!this.errorRemovingPolicy) redirectTo(this.threatMonitoringPath); if (!this.errorRemovingPolicy) redirectTo(this.threatMonitoringPath);
......
...@@ -6,7 +6,6 @@ import { ...@@ -6,7 +6,6 @@ import {
RuleTypeEndpoint, RuleTypeEndpoint,
EndpointMatchModeLabel, EndpointMatchModeLabel,
fromYaml, fromYaml,
buildRule,
toYaml, toYaml,
} from 'ee/threat_monitoring/components/policy_editor/network_policy/lib'; } from 'ee/threat_monitoring/components/policy_editor/network_policy/lib';
import NetworkPolicyEditor from 'ee/threat_monitoring/components/policy_editor/network_policy/network_policy_editor.vue'; import NetworkPolicyEditor from 'ee/threat_monitoring/components/policy_editor/network_policy/network_policy_editor.vue';
...@@ -17,7 +16,7 @@ import PolicyPreview from 'ee/threat_monitoring/components/policy_editor/policy_ ...@@ -17,7 +16,7 @@ import PolicyPreview from 'ee/threat_monitoring/components/policy_editor/policy_
import createStore from 'ee/threat_monitoring/store'; import createStore from 'ee/threat_monitoring/store';
import { shallowMountExtended } from 'helpers/vue_test_utils_helper'; import { shallowMountExtended } from 'helpers/vue_test_utils_helper';
import { redirectTo } from '~/lib/utils/url_utility'; import { redirectTo } from '~/lib/utils/url_utility';
import { mockL3Manifest, mockL7Manifest } from '../../../mocks/mock_data'; import { mockExistingL3Policy, mockExistingL7Policy } from '../../../mocks/mock_data';
jest.mock('~/lib/utils/url_utility'); jest.mock('~/lib/utils/url_utility');
...@@ -27,7 +26,7 @@ describe('NetworkPolicyEditor component', () => { ...@@ -27,7 +26,7 @@ describe('NetworkPolicyEditor component', () => {
const defaultStore = { threatMonitoring: { environments: [{ id: 1 }], currentEnvironmentId: 1 } }; const defaultStore = { threatMonitoring: { environments: [{ id: 1 }], currentEnvironmentId: 1 } };
const factory = ({ propsData, provide = {}, updatedStore = defaultStore, data } = {}) => { const factory = ({ propsData, provide = {}, updatedStore = defaultStore } = {}) => {
store = createStore(); store = createStore();
store.replaceState({ store.replaceState({
...@@ -57,7 +56,6 @@ describe('NetworkPolicyEditor component', () => { ...@@ -57,7 +56,6 @@ describe('NetworkPolicyEditor component', () => {
...provide, ...provide,
}, },
store, store,
data,
stubs: { PolicyYamlEditor: true }, stubs: { PolicyYamlEditor: true },
}); });
}; };
...@@ -117,48 +115,22 @@ describe('NetworkPolicyEditor component', () => { ...@@ -117,48 +115,22 @@ describe('NetworkPolicyEditor component', () => {
expect(findComponent().exists()).toBe(state); expect(findComponent().exists()).toBe(state);
}); });
describe('given .yaml editor mode is enabled', () => { it('updates policy on yaml editor value change', async () => {
beforeEach(() => { findPolicyEditorLayout().vm.$emit('update-yaml', mockExistingL3Policy.manifest);
factory({
data: () => ({ expect(wrapper.vm.policy).toMatchObject({
editorMode: EDITOR_MODE_YAML, name: 'test-policy-02',
}), description: 'test description',
}); isEnabled: true,
}); endpointMatchMode: EndpointMatchModeLabel,
endpointLabels: 'foo:bar',
it('updates policy on yaml editor value change', async () => { rules: [
findPolicyEditorLayout().vm.$emit('update-yaml', mockL3Manifest); {
ruleType: RuleTypeEndpoint,
expect(wrapper.vm.policy).toMatchObject({ matchLabels: 'foo:bar',
name: 'test-policy-02', },
description: 'test description', ],
isEnabled: true, labels: { 'app.gitlab.com/proj': '21' },
endpointMatchMode: EndpointMatchModeLabel,
endpointLabels: 'foo:bar',
rules: [
{
ruleType: RuleTypeEndpoint,
matchLabels: 'foo:bar',
},
],
labels: { 'app.gitlab.com/proj': '21' },
});
});
it('saves L7 policies', async () => {
factory({
data: () => ({
editorMode: EDITOR_MODE_YAML,
yamlEditorValue: mockL7Manifest,
}),
});
await findPolicyEditorLayout().vm.$emit('save-policy', EDITOR_MODE_YAML);
expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/createPolicy', {
environmentId: 1,
policy: { manifest: mockL7Manifest },
});
expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring');
}); });
}); });
...@@ -213,11 +185,7 @@ describe('NetworkPolicyEditor component', () => { ...@@ -213,11 +185,7 @@ describe('NetworkPolicyEditor component', () => {
describe('given there is a yaml parsing error', () => { describe('given there is a yaml parsing error', () => {
beforeEach(() => { beforeEach(() => {
factory({ factory({ propsData: { existingPolicy: mockExistingL7Policy } });
data: () => ({
yamlEditorError: {},
}),
});
}); });
it('disables policy name field', () => { it('disables policy name field', () => {
...@@ -252,7 +220,28 @@ describe('NetworkPolicyEditor component', () => { ...@@ -252,7 +220,28 @@ describe('NetworkPolicyEditor component', () => {
findPolicyName().vm.$emit('input', 'test-policy'); findPolicyName().vm.$emit('input', 'test-policy');
const policyEditorLayout = findPolicyEditorLayout(); const policyEditorLayout = findPolicyEditorLayout();
await policyEditorLayout.vm.$emit('update-editor-mode', EDITOR_MODE_YAML); await policyEditorLayout.vm.$emit('update-editor-mode', EDITOR_MODE_YAML);
expect(policyEditorLayout.attributes('yamleditorvalue')).toEqual(''); expect(policyEditorLayout.attributes('yamleditorvalue')).toEqual(
mockExistingL7Policy.manifest,
);
});
it('saves unparseable policy', async () => {
await findPolicyEditorLayout().vm.$emit('save-policy', EDITOR_MODE_YAML);
expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/createPolicy', {
environmentId: 1,
policy: { manifest: mockExistingL7Policy.manifest },
});
expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring');
});
it('removes unparseable policy', async () => {
await findPolicyEditorLayout().vm.$emit('remove-policy');
expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/deletePolicy', {
environmentId: 1,
policy: { name: mockExistingL7Policy.name, manifest: mockExistingL7Policy.manifest },
});
expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring');
}); });
}); });
...@@ -279,23 +268,17 @@ describe('NetworkPolicyEditor component', () => { ...@@ -279,23 +268,17 @@ describe('NetworkPolicyEditor component', () => {
}); });
describe('editing a policy', () => { describe('editing a policy', () => {
const manifest = toYaml({
name: 'policy',
endpointLabels: '',
rules: [buildRule()],
});
beforeEach(() => { beforeEach(() => {
factory({ factory({
propsData: { propsData: {
existingPolicy: { name: 'policy', manifest }, existingPolicy: mockExistingL3Policy,
isEditing: true, isEditing: true,
}, },
}); });
}); });
it('presents existing policy', () => { it('presents existing policy', () => {
expect(findPolicyName().attributes().value).toEqual('policy'); expect(findPolicyName().attributes().value).toEqual(mockExistingL3Policy.name);
expect(wrapper.findAllComponents(PolicyRuleBuilder)).toHaveLength(1); expect(wrapper.findAllComponents(PolicyRuleBuilder)).toHaveLength(1);
}); });
...@@ -303,7 +286,10 @@ describe('NetworkPolicyEditor component', () => { ...@@ -303,7 +286,10 @@ describe('NetworkPolicyEditor component', () => {
await findPolicyEditorLayout().vm.$emit('save-policy'); await findPolicyEditorLayout().vm.$emit('save-policy');
expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/updatePolicy', { expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/updatePolicy', {
environmentId: 1, environmentId: 1,
policy: { name: 'policy', manifest: toYaml(wrapper.vm.policy) }, policy: {
name: mockExistingL3Policy.name,
manifest: toYaml(fromYaml(mockExistingL3Policy.manifest)),
},
}); });
expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring'); expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring');
}); });
...@@ -311,7 +297,7 @@ describe('NetworkPolicyEditor component', () => { ...@@ -311,7 +297,7 @@ describe('NetworkPolicyEditor component', () => {
describe('given there is a updatePolicy error', () => { describe('given there is a updatePolicy error', () => {
beforeEach(() => { beforeEach(() => {
factory({ factory({
propsData: { existingPolicy: { name: 'policy', manifest } }, propsData: { existingPolicy: mockExistingL3Policy },
updatedStore: { networkPolicies: { errorUpdatingPolicy: true }, ...defaultStore }, updatedStore: { networkPolicies: { errorUpdatingPolicy: true }, ...defaultStore },
}); });
}); });
...@@ -328,7 +314,10 @@ describe('NetworkPolicyEditor component', () => { ...@@ -328,7 +314,10 @@ describe('NetworkPolicyEditor component', () => {
expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/deletePolicy', { expect(store.dispatch).toHaveBeenCalledWith('networkPolicies/deletePolicy', {
environmentId: 1, environmentId: 1,
policy: { name: 'policy', manifest: toYaml(wrapper.vm.policy) }, policy: {
name: mockExistingL3Policy.name,
manifest: mockExistingL3Policy.manifest,
},
}); });
expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring'); expect(redirectTo).toHaveBeenCalledWith('/threat-monitoring');
}); });
......
...@@ -147,6 +147,16 @@ spec: ...@@ -147,6 +147,16 @@ spec:
fromEntities: fromEntities:
- cluster`; - cluster`;
export const mockExistingL3Policy = {
name: 'test-policy-02',
manifest: mockL3Manifest,
};
export const mockExistingL7Policy = {
name: 'limit-inbound-ip',
manifest: mockL7Manifest,
};
export const mockCiliumPolicy = { export const mockCiliumPolicy = {
__typename: 'NetworkPolicy', __typename: 'NetworkPolicy',
name: 'test-policy-03', name: 'test-policy-03',
......
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