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) => {
.catch(error => commitReceivePoliciesError(commit, error?.response?.data));
};
const commitUpdatePolicyError = (commit, payload) => {
const commitPolicyError = (commit, type, payload) => {
const error =
payload?.error || s__('NetworkPolicies|Something went wrong, failed to update policy');
commit(types.RECEIVE_UPDATE_POLICY_ERROR, error);
commit(type, 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 }) => {
if (!state.policiesEndpoint || !environmentId || !policy) {
return commitUpdatePolicyError(commit);
return commitPolicyError(commit, types.RECEIVE_UPDATE_POLICY_ERROR);
}
commit(types.REQUEST_UPDATE_POLICY);
......@@ -58,5 +84,7 @@ export const updatePolicy = ({ state, commit }, { environmentId, policy }) => {
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';
export const RECEIVE_POLICIES_SUCCESS = 'RECEIVE_POLICIES_SUCCESS';
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 RECEIVE_UPDATE_POLICY_SUCCESS = 'RECEIVE_UPDATE_POLICY_SUCCESS';
export const RECEIVE_UPDATE_POLICY_ERROR = 'RECEIVE_UPDATE_POLICY_ERROR';
......@@ -18,6 +18,20 @@ export default {
state.isLoadingPolicies = false;
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) {
state.isUpdatingPolicy = true;
state.errorUpdatingPolicy = false;
......
......@@ -18,13 +18,19 @@ const networkPoliciesEndpoint = 'networkPoliciesEndpoint';
describe('Network Policy actions', () => {
let state;
let mock;
const environmentId = 3;
const policy = { name: 'policy', manifest: 'foo', isEnabled: true };
beforeEach(() => {
state = getInitialState();
state.policiesEndpoint = networkPoliciesEndpoint;
mock = new MockAdapter(axios);
});
afterEach(() => {
createFlash.mockClear();
mock.restore();
});
describe('setEndpoints', () => {
......@@ -44,23 +50,11 @@ describe('Network Policy actions', () => {
});
describe('fetchPolicies', () => {
let mock;
const currentEnvironmentId = 3;
beforeEach(() => {
state.policiesEndpoint = networkPoliciesEndpoint;
mock = new MockAdapter(axios);
});
afterEach(() => {
mock.restore();
});
describe('on success', () => {
beforeEach(() => {
mock
.onGet(networkPoliciesEndpoint, {
params: { environment_id: currentEnvironmentId },
params: { environment_id: environmentId },
})
.replyOnce(httpStatus.OK, mockPoliciesResponse);
});
......@@ -68,7 +62,7 @@ describe('Network Policy actions', () => {
it('should dispatch the request and success actions', () =>
testAction(
actions.fetchPolicies,
currentEnvironmentId,
environmentId,
state,
[
{ type: types.REQUEST_POLICIES },
......@@ -91,14 +85,16 @@ describe('Network Policy actions', () => {
it('should dispatch the request and error actions', () =>
testAction(
actions.fetchPolicies,
currentEnvironmentId,
environmentId,
state,
[
{ type: types.REQUEST_POLICIES },
{ type: types.RECEIVE_POLICIES_ERROR, payload: 'foo' },
],
[],
));
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('with an empty endpoint', () => {
......@@ -109,7 +105,7 @@ describe('Network Policy actions', () => {
it('should dispatch RECEIVE_POLICES_ERROR', () =>
testAction(
actions.fetchPolicies,
currentEnvironmentId,
environmentId,
state,
[
{
......@@ -118,7 +114,9 @@ describe('Network Policy actions', () => {
},
],
[],
));
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('without environment id', () => {
......@@ -134,25 +132,117 @@ describe('Network Policy actions', () => {
},
],
[],
));
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
});
describe('updatePolicy', () => {
let mock;
const environmentId = 3;
const policy = { name: 'policy', manifest: 'foo', isEnabled: true };
const updatedPolicy = { name: 'policy', manifest: 'bar', isEnabled: true };
describe('createPolicy', () => {
const createdPolicy = { name: 'policy', manifest: 'bar', isEnabled: true };
beforeEach(() => {
state.policiesEndpoint = networkPoliciesEndpoint;
mock = new MockAdapter(axios);
describe('on success', () => {
beforeEach(() => {
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(() => {
mock.restore();
describe('on error', () => {
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', () => {
beforeEach(() => {
mock
......@@ -203,7 +293,9 @@ describe('Network Policy actions', () => {
{ type: types.RECEIVE_UPDATE_POLICY_ERROR, payload: 'foo' },
],
[],
));
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('with an empty endpoint', () => {
......@@ -223,7 +315,9 @@ describe('Network Policy actions', () => {
},
],
[],
));
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
describe('without environment id', () => {
......@@ -242,7 +336,9 @@ describe('Network Policy actions', () => {
},
],
[],
));
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
});
});
......@@ -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, () => {
beforeEach(() => {
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