Commit 9668f3dc authored by ap4y's avatar ap4y

Add deletePolicy action to the networkPolicy store

This commit adds a new action for removing network policies.
parent 8e4df1a6
......@@ -88,3 +88,27 @@ export const updatePolicy = ({ state, commit }, { environmentId, policy }) => {
commitPolicyError(commit, types.RECEIVE_UPDATE_POLICY_ERROR, error?.response?.data),
);
};
export const deletePolicy = ({ state, commit }, { environmentId, policy }) => {
if (!state.policiesEndpoint || !environmentId || !policy) {
return commitPolicyError(commit, types.RECEIVE_DELETE_POLICY_ERROR);
}
commit(types.REQUEST_DELETE_POLICY);
return axios
.delete(joinPaths(state.policiesEndpoint, policy.name), {
params: {
environment_id: environmentId,
manifest: policy.manifest,
},
})
.then(() => {
commit(types.RECEIVE_DELETE_POLICY_SUCCESS, {
policy,
});
})
.catch(error =>
commitPolicyError(commit, types.RECEIVE_DELETE_POLICY_ERROR, error?.response?.data),
);
};
......@@ -11,3 +11,7 @@ export const RECEIVE_CREATE_POLICY_ERROR = 'RECEIVE_CREATE_POLICY_ERROR';
export const REQUEST_UPDATE_POLICY = 'REQUEST_UPDATE_POLICY';
export const RECEIVE_UPDATE_POLICY_SUCCESS = 'RECEIVE_UPDATE_POLICY_SUCCESS';
export const RECEIVE_UPDATE_POLICY_ERROR = 'RECEIVE_UPDATE_POLICY_ERROR';
export const REQUEST_DELETE_POLICY = 'REQUEST_DELETE_POLICY';
export const RECEIVE_DELETE_POLICY_SUCCESS = 'RECEIVE_DELETE_POLICY_SUCCESS';
export const RECEIVE_DELETE_POLICY_ERROR = 'RECEIVE_DELETE_POLICY_ERROR';
......@@ -46,4 +46,17 @@ export default {
state.isUpdatingPolicy = false;
state.errorUpdatingPolicy = true;
},
[types.REQUEST_DELETE_POLICY](state) {
state.isRemovingPolicy = true;
state.errorRemovingPolicy = false;
},
[types.RECEIVE_DELETE_POLICY_SUCCESS](state, { policy }) {
state.policies = state.policies.filter(({ name }) => name !== policy.name);
state.isRemovingPolicy = false;
state.errorRemovingPolicy = false;
},
[types.RECEIVE_DELETE_POLICY_ERROR](state) {
state.isRemovingPolicy = false;
state.errorRemovingPolicy = true;
},
};
......@@ -5,4 +5,6 @@ export default () => ({
errorLoadingPolicies: false,
isUpdatingPolicy: false,
errorUpdatingPolicy: false,
isRemovingPolicy: false,
errorRemovingPolicy: false,
});
......@@ -226,6 +226,8 @@ spec:
Create policy
</gl-button-stub>
<!---->
<gl-button-stub
category="secondary"
href="/threat-monitoring"
......@@ -237,5 +239,19 @@ spec:
</gl-button-stub>
</div>
</div>
<gl-modal-stub
actioncancel="[object Object]"
actionsecondary="[object Object]"
modalclass=""
modalid="delete-modal"
size="md"
title="Delete policy: "
titletag="h4"
>
Are you sure you want to delete this policy? This action cannot be undone.
</gl-modal-stub>
</section>
`;
......@@ -340,4 +340,106 @@ describe('Network Policy actions', () => {
}));
});
});
describe('deletePolicy', () => {
describe('on success', () => {
beforeEach(() => {
mock
.onDelete(joinPaths(networkPoliciesEndpoint, policy.name), {
params: {
environment_id: environmentId,
manifest: policy.manifest,
},
})
.replyOnce(httpStatus.OK);
});
it('should dispatch the request and success actions', () =>
testAction(
actions.deletePolicy,
{ environmentId, policy },
state,
[
{ type: types.REQUEST_DELETE_POLICY },
{
type: types.RECEIVE_DELETE_POLICY_SUCCESS,
payload: { policy },
},
],
[],
));
});
describe('on error', () => {
const error = { error: 'foo' };
beforeEach(() => {
mock
.onDelete(joinPaths(networkPoliciesEndpoint, policy.name), {
params: {
environment_id: environmentId,
manifest: policy.manifest,
},
})
.replyOnce(500, error);
});
it('should dispatch the request and error actions', () =>
testAction(
actions.deletePolicy,
{ environmentId, policy },
state,
[
{ type: types.REQUEST_DELETE_POLICY },
{ type: types.RECEIVE_DELETE_POLICY_ERROR, payload: 'foo' },
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('with an empty endpoint', () => {
beforeEach(() => {
state.policiesEndpoint = '';
});
it('should dispatch RECEIVE_DELETE_POLICY_ERROR', () =>
testAction(
actions.deletePolicy,
{ environmentId, policy },
state,
[
{
type: types.RECEIVE_DELETE_POLICY_ERROR,
payload: s__('NetworkPolicies|Something went wrong, failed to update policy'),
},
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('without environment id', () => {
it('should dispatch RECEIVE_DELETE_POLICY_ERROR', () =>
testAction(
actions.deletePolicy,
{
environmentId: undefined,
policy,
},
state,
[
{
type: types.RECEIVE_DELETE_POLICY_ERROR,
payload: s__('NetworkPolicies|Something went wrong, failed to update policy'),
},
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
});
});
......@@ -138,4 +138,46 @@ describe('Network Policies mutations', () => {
expect(state.errorUpdatingPolicy).toBe(true);
});
});
describe(types.REQUEST_DELETE_POLICY, () => {
beforeEach(() => {
mutations[types.REQUEST_DELETE_POLICY](state);
});
it('sets isRemovingPolicy to true and sets errorRemovingPolicy to false', () => {
expect(state.isRemovingPolicy).toBe(true);
expect(state.errorRemovingPolicy).toBe(false);
});
});
describe(types.RECEIVE_DELETE_POLICY_SUCCESS, () => {
const policy = { id: 1, name: 'production', manifest: 'foo' };
beforeEach(() => {
state.policies.push(policy);
mutations[types.RECEIVE_DELETE_POLICY_SUCCESS](state, {
policy,
});
});
it('removes the policy', () => {
expect(state.policies).not.toEqual(expect.objectContaining(policy));
});
it('sets isRemovingPolicy to false and sets errorRemovingPolicy to false', () => {
expect(state.isRemovingPolicy).toBe(false);
expect(state.errorRemovingPolicy).toBe(false);
});
});
describe(types.RECEIVE_DELETE_POLICY_ERROR, () => {
beforeEach(() => {
mutations[types.RECEIVE_DELETE_POLICY_ERROR](state);
});
it('sets isRemovingPolicy to false and sets errorRemovingPolicy to true', () => {
expect(state.isRemovingPolicy).toBe(false);
expect(state.errorRemovingPolicy).toBe(true);
});
});
});
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