Commit 42f0b9df authored by ap4y's avatar ap4y

Add createPolicy action to the network_policy store

This commit adds a new "createPolicy" action to the networkPolcies
store that allows us to create new policies.
parent 25ff795f
...@@ -26,16 +26,42 @@ export const fetchPolicies = ({ state, commit }, environmentId) => { ...@@ -26,16 +26,42 @@ export const fetchPolicies = ({ state, commit }, environmentId) => {
.catch(error => commitReceivePoliciesError(commit, error?.response?.data)); .catch(error => commitReceivePoliciesError(commit, error?.response?.data));
}; };
const commitUpdatePolicyError = (commit, payload) => { const commitPolicyError = (commit, type, payload) => {
const error = const error =
payload?.error || s__('NetworkPolicies|Something went wrong, failed to update policy'); payload?.error || s__('NetworkPolicies|Something went wrong, failed to update policy');
commit(types.RECEIVE_UPDATE_POLICY_ERROR, error); commit(type, error);
createFlash(error); createFlash(error);
}; };
export const createPolicy = ({ state, commit }, { environmentId, policy }) => {
if (!state.policiesEndpoint || !environmentId || !policy) {
return commitPolicyError(commit, types.RECEIVE_CREATE_POLICY_ERROR);
}
commit(types.REQUEST_CREATE_POLICY);
return axios
.post(state.policiesEndpoint, {
environment_id: environmentId,
manifest: policy.manifest,
})
.then(({ data }) => {
commit(types.RECEIVE_CREATE_POLICY_SUCCESS, data);
createFlash(
sprintf(s__('NetworkPolicies|Policy %{policyName} was successfully changed'), {
policyName: policy.name,
}),
FLASH_TYPES.SUCCESS,
);
})
.catch(error =>
commitPolicyError(commit, types.RECEIVE_CREATE_POLICY_ERROR, error?.response?.data),
);
};
export const updatePolicy = ({ state, commit }, { environmentId, policy }) => { export const updatePolicy = ({ state, commit }, { environmentId, policy }) => {
if (!state.policiesEndpoint || !environmentId || !policy) { if (!state.policiesEndpoint || !environmentId || !policy) {
return commitUpdatePolicyError(commit); return commitPolicyError(commit, types.RECEIVE_UPDATE_POLICY_ERROR);
} }
commit(types.REQUEST_UPDATE_POLICY); commit(types.REQUEST_UPDATE_POLICY);
...@@ -58,5 +84,7 @@ export const updatePolicy = ({ state, commit }, { environmentId, policy }) => { ...@@ -58,5 +84,7 @@ export const updatePolicy = ({ state, commit }, { environmentId, policy }) => {
FLASH_TYPES.SUCCESS, FLASH_TYPES.SUCCESS,
); );
}) })
.catch(error => commitUpdatePolicyError(commit, error?.response?.data)); .catch(error =>
commitPolicyError(commit, types.RECEIVE_UPDATE_POLICY_ERROR, error?.response?.data),
);
}; };
...@@ -4,6 +4,10 @@ export const REQUEST_POLICIES = 'REQUEST_POLICIES'; ...@@ -4,6 +4,10 @@ export const REQUEST_POLICIES = 'REQUEST_POLICIES';
export const RECEIVE_POLICIES_SUCCESS = 'RECEIVE_POLICIES_SUCCESS'; export const RECEIVE_POLICIES_SUCCESS = 'RECEIVE_POLICIES_SUCCESS';
export const RECEIVE_POLICIES_ERROR = 'RECEIVE_POLICIES_ERROR'; export const RECEIVE_POLICIES_ERROR = 'RECEIVE_POLICIES_ERROR';
export const REQUEST_CREATE_POLICY = 'REQUEST_CREATE_POLICY';
export const RECEIVE_CREATE_POLICY_SUCCESS = 'RECEIVE_CREATE_POLICY_SUCCESS';
export const RECEIVE_CREATE_POLICY_ERROR = 'RECEIVE_CREATE_POLICY_ERROR';
export const REQUEST_UPDATE_POLICY = 'REQUEST_UPDATE_POLICY'; export const REQUEST_UPDATE_POLICY = 'REQUEST_UPDATE_POLICY';
export const RECEIVE_UPDATE_POLICY_SUCCESS = 'RECEIVE_UPDATE_POLICY_SUCCESS'; export const RECEIVE_UPDATE_POLICY_SUCCESS = 'RECEIVE_UPDATE_POLICY_SUCCESS';
export const RECEIVE_UPDATE_POLICY_ERROR = 'RECEIVE_UPDATE_POLICY_ERROR'; export const RECEIVE_UPDATE_POLICY_ERROR = 'RECEIVE_UPDATE_POLICY_ERROR';
...@@ -18,6 +18,20 @@ export default { ...@@ -18,6 +18,20 @@ export default {
state.isLoadingPolicies = false; state.isLoadingPolicies = false;
state.errorLoadingPolicies = true; state.errorLoadingPolicies = true;
}, },
[types.REQUEST_CREATE_POLICY](state) {
state.isUpdatingPolicy = true;
state.errorUpdatingPolicy = false;
},
[types.RECEIVE_CREATE_POLICY_SUCCESS](state, policy) {
const newPolicy = convertObjectPropsToCamelCase(policy);
state.policies = [...state.policies, newPolicy];
state.isUpdatingPolicy = false;
state.errorUpdatingPolicy = false;
},
[types.RECEIVE_CREATE_POLICY_ERROR](state) {
state.isUpdatingPolicy = false;
state.errorUpdatingPolicy = true;
},
[types.REQUEST_UPDATE_POLICY](state) { [types.REQUEST_UPDATE_POLICY](state) {
state.isUpdatingPolicy = true; state.isUpdatingPolicy = true;
state.errorUpdatingPolicy = false; state.errorUpdatingPolicy = false;
......
...@@ -18,13 +18,19 @@ const networkPoliciesEndpoint = 'networkPoliciesEndpoint'; ...@@ -18,13 +18,19 @@ const networkPoliciesEndpoint = 'networkPoliciesEndpoint';
describe('Network Policy actions', () => { describe('Network Policy actions', () => {
let state; let state;
let mock;
const environmentId = 3;
const policy = { name: 'policy', manifest: 'foo', isEnabled: true };
beforeEach(() => { beforeEach(() => {
state = getInitialState(); state = getInitialState();
state.policiesEndpoint = networkPoliciesEndpoint;
mock = new MockAdapter(axios);
}); });
afterEach(() => { afterEach(() => {
createFlash.mockClear(); createFlash.mockClear();
mock.restore();
}); });
describe('setEndpoints', () => { describe('setEndpoints', () => {
...@@ -44,23 +50,11 @@ describe('Network Policy actions', () => { ...@@ -44,23 +50,11 @@ describe('Network Policy actions', () => {
}); });
describe('fetchPolicies', () => { describe('fetchPolicies', () => {
let mock;
const currentEnvironmentId = 3;
beforeEach(() => {
state.policiesEndpoint = networkPoliciesEndpoint;
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('on success', () => { describe('on success', () => {
beforeEach(() => { beforeEach(() => {
mock mock
.onGet(networkPoliciesEndpoint, { .onGet(networkPoliciesEndpoint, {
params: { environment_id: currentEnvironmentId }, params: { environment_id: environmentId },
}) })
.replyOnce(httpStatus.OK, mockPoliciesResponse); .replyOnce(httpStatus.OK, mockPoliciesResponse);
}); });
...@@ -68,7 +62,7 @@ describe('Network Policy actions', () => { ...@@ -68,7 +62,7 @@ describe('Network Policy actions', () => {
it('should dispatch the request and success actions', () => it('should dispatch the request and success actions', () =>
testAction( testAction(
actions.fetchPolicies, actions.fetchPolicies,
currentEnvironmentId, environmentId,
state, state,
[ [
{ type: types.REQUEST_POLICIES }, { type: types.REQUEST_POLICIES },
...@@ -91,14 +85,16 @@ describe('Network Policy actions', () => { ...@@ -91,14 +85,16 @@ describe('Network Policy actions', () => {
it('should dispatch the request and error actions', () => it('should dispatch the request and error actions', () =>
testAction( testAction(
actions.fetchPolicies, actions.fetchPolicies,
currentEnvironmentId, environmentId,
state, state,
[ [
{ type: types.REQUEST_POLICIES }, { type: types.REQUEST_POLICIES },
{ type: types.RECEIVE_POLICIES_ERROR, payload: 'foo' }, { type: types.RECEIVE_POLICIES_ERROR, payload: 'foo' },
], ],
[], [],
)); ).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
describe('with an empty endpoint', () => { describe('with an empty endpoint', () => {
...@@ -109,7 +105,7 @@ describe('Network Policy actions', () => { ...@@ -109,7 +105,7 @@ describe('Network Policy actions', () => {
it('should dispatch RECEIVE_POLICES_ERROR', () => it('should dispatch RECEIVE_POLICES_ERROR', () =>
testAction( testAction(
actions.fetchPolicies, actions.fetchPolicies,
currentEnvironmentId, environmentId,
state, state,
[ [
{ {
...@@ -118,7 +114,9 @@ describe('Network Policy actions', () => { ...@@ -118,7 +114,9 @@ describe('Network Policy actions', () => {
}, },
], ],
[], [],
)); ).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
describe('without environment id', () => { describe('without environment id', () => {
...@@ -134,25 +132,117 @@ describe('Network Policy actions', () => { ...@@ -134,25 +132,117 @@ describe('Network Policy actions', () => {
}, },
], ],
[], [],
)); ).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
}); });
describe('updatePolicy', () => { describe('createPolicy', () => {
let mock; const createdPolicy = { name: 'policy', manifest: 'bar', isEnabled: true };
const environmentId = 3;
const policy = { name: 'policy', manifest: 'foo', isEnabled: true };
const updatedPolicy = { name: 'policy', manifest: 'bar', isEnabled: true };
beforeEach(() => { describe('on success', () => {
state.policiesEndpoint = networkPoliciesEndpoint; beforeEach(() => {
mock = new MockAdapter(axios); mock
.onPost(networkPoliciesEndpoint, {
environment_id: environmentId,
manifest: policy.manifest,
})
.replyOnce(httpStatus.OK, createdPolicy);
});
it('should dispatch the request and success actions', () =>
testAction(
actions.createPolicy,
{ environmentId, policy },
state,
[
{ type: types.REQUEST_CREATE_POLICY },
{
type: types.RECEIVE_CREATE_POLICY_SUCCESS,
payload: createdPolicy,
},
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
afterEach(() => { describe('on error', () => {
mock.restore(); const error = { error: 'foo' };
beforeEach(() => {
mock
.onPost(networkPoliciesEndpoint, {
environment_id: environmentId,
manifest: policy.manifest,
})
.replyOnce(500, error);
});
it('should dispatch the request and error actions', () =>
testAction(
actions.createPolicy,
{ environmentId, policy },
state,
[
{ type: types.REQUEST_CREATE_POLICY },
{ type: types.RECEIVE_CREATE_POLICY_ERROR, payload: 'foo' },
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
describe('with an empty endpoint', () => {
beforeEach(() => {
state.policiesEndpoint = '';
});
it('should dispatch RECEIVE_CREATE_POLICY_ERROR', () =>
testAction(
actions.createPolicy,
{ environmentId, policy },
state,
[
{
type: types.RECEIVE_CREATE_POLICY_ERROR,
payload: s__('NetworkPolicies|Something went wrong, failed to update policy'),
},
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('without environment id', () => {
it('should dispatch RECEIVE_CREATE_POLICY_ERROR', () =>
testAction(
actions.createPolicy,
{
environmentId: undefined,
policy,
},
state,
[
{
type: types.RECEIVE_CREATE_POLICY_ERROR,
payload: s__('NetworkPolicies|Something went wrong, failed to update policy'),
},
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
});
describe('updatePolicy', () => {
const updatedPolicy = { name: 'policy', manifest: 'bar', isEnabled: true };
describe('on success', () => { describe('on success', () => {
beforeEach(() => { beforeEach(() => {
mock mock
...@@ -203,7 +293,9 @@ describe('Network Policy actions', () => { ...@@ -203,7 +293,9 @@ describe('Network Policy actions', () => {
{ type: types.RECEIVE_UPDATE_POLICY_ERROR, payload: 'foo' }, { type: types.RECEIVE_UPDATE_POLICY_ERROR, payload: 'foo' },
], ],
[], [],
)); ).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
describe('with an empty endpoint', () => { describe('with an empty endpoint', () => {
...@@ -223,7 +315,9 @@ describe('Network Policy actions', () => { ...@@ -223,7 +315,9 @@ describe('Network Policy actions', () => {
}, },
], ],
[], [],
)); ).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
describe('without environment id', () => { describe('without environment id', () => {
...@@ -242,7 +336,9 @@ describe('Network Policy actions', () => { ...@@ -242,7 +336,9 @@ describe('Network Policy actions', () => {
}, },
], ],
[], [],
)); ).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
}); });
}); });
}); });
...@@ -55,6 +55,45 @@ describe('Network Policies mutations', () => { ...@@ -55,6 +55,45 @@ describe('Network Policies mutations', () => {
}); });
}); });
describe(types.REQUEST_CREATE_POLICY, () => {
beforeEach(() => {
mutations[types.REQUEST_CREATE_POLICY](state);
});
it('sets isUpdatingPolicy to true and sets errorUpdatingPolicy to false', () => {
expect(state.isUpdatingPolicy).toBe(true);
expect(state.errorUpdatingPolicy).toBe(false);
});
});
describe(types.RECEIVE_CREATE_POLICY_SUCCESS, () => {
const policy = { id: 1, name: 'production', manifest: 'foo' };
beforeEach(() => {
mutations[types.RECEIVE_CREATE_POLICY_SUCCESS](state, policy);
});
it('adds a policy to the store', () => {
expect(state.policies).toEqual(expect.objectContaining([policy]));
});
it('sets isUpdatingPolicy to false and sets errorUpdatingPolicy to false', () => {
expect(state.isUpdatingPolicy).toBe(false);
expect(state.errorUpdatingPolicy).toBe(false);
});
});
describe(types.RECEIVE_CREATE_POLICY_ERROR, () => {
beforeEach(() => {
mutations[types.RECEIVE_CREATE_POLICY_ERROR](state);
});
it('sets isUpdatingPolicy to false and sets errorUpdatingPolicy to true', () => {
expect(state.isUpdatingPolicy).toBe(false);
expect(state.errorUpdatingPolicy).toBe(true);
});
});
describe(types.REQUEST_UPDATE_POLICY, () => { describe(types.REQUEST_UPDATE_POLICY, () => {
beforeEach(() => { beforeEach(() => {
mutations[types.REQUEST_UPDATE_POLICY](state); mutations[types.REQUEST_UPDATE_POLICY](state);
......
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