Commit eb9d9c24 authored by Zamir Martins Filho's avatar Zamir Martins Filho Committed by Paul Slaughter

Add All Environments option for environments

https://gitlab.com/gitlab-org/gitlab/-/merge_requests/40531
parent 06c6774a
<script>
import { mapActions, mapGetters, mapState } from 'vuex';
import { GlFormGroup, GlDeprecatedDropdown, GlDeprecatedDropdownItem } from '@gitlab/ui';
import { ALL_ENVIRONMENT_NAME } from '../constants';
export default {
components: {
......@@ -8,14 +9,27 @@ export default {
GlDeprecatedDropdown,
GlDeprecatedDropdownItem,
},
props: {
includeAll: {
type: Boolean,
required: false,
default: false,
},
},
computed: {
...mapState('threatMonitoring', ['environments', 'currentEnvironmentId']),
...mapState('threatMonitoring', ['environments', 'currentEnvironmentId', 'allEnvironments']),
...mapGetters('threatMonitoring', ['currentEnvironmentName', 'canChangeEnvironment']),
environmentName() {
return this.allEnvironments && this.includeAll
? ALL_ENVIRONMENT_NAME
: this.currentEnvironmentName;
},
},
methods: {
...mapActions('threatMonitoring', ['setCurrentEnvironmentId']),
...mapActions('threatMonitoring', ['setCurrentEnvironmentId', 'setAllEnvironments']),
},
environmentFilterId: 'threat-monitoring-environment-filter',
ALL_ENVIRONMENT_NAME,
};
</script>
......@@ -31,7 +45,7 @@ export default {
ref="environmentsDropdown"
class="mb-0 d-flex"
toggle-class="d-flex justify-content-between text-truncate"
:text="currentEnvironmentName"
:text="environmentName"
:disabled="!canChangeEnvironment"
>
<gl-deprecated-dropdown-item
......@@ -41,6 +55,12 @@ export default {
@click="setCurrentEnvironmentId(environment.id)"
>{{ environment.name }}</gl-deprecated-dropdown-item
>
<gl-deprecated-dropdown-item
v-if="includeAll"
ref="environmentsDropdownItem"
@click="setAllEnvironments"
>{{ $options.ALL_ENVIRONMENT_NAME }}</gl-deprecated-dropdown-item
>
</gl-deprecated-dropdown>
</gl-form-group>
</template>
......@@ -176,7 +176,7 @@ export default {
<div class="pt-3 px-3 bg-gray-light">
<div class="row justify-content-between align-items-center">
<environment-picker ref="environmentsPicker" />
<environment-picker ref="environmentsPicker" :include-all="true" />
<div class="col-sm-auto">
<gl-button
category="secondary"
......
import { s__ } from '~/locale';
export const INVALID_CURRENT_ENVIRONMENT_NAME = '';
export const PREDEFINED_NETWORK_POLICIES = [
......@@ -30,3 +32,5 @@ spec:
- port: 443`,
},
];
export const ALL_ENVIRONMENT_NAME = s__('ThreatMonitoring|All Environments');
......@@ -16,14 +16,20 @@ const commitReceivePoliciesError = (commit, payload) => {
};
export const fetchPolicies = ({ state, commit }, environmentId) => {
if (!state.policiesEndpoint || !environmentId) return commitReceivePoliciesError(commit);
if (!state.policiesEndpoint) return commitReceivePoliciesError(commit);
commit(types.REQUEST_POLICIES);
const params = environmentId ? { params: { environment_id: environmentId } } : {};
return axios
.get(state.policiesEndpoint, { params: { environment_id: environmentId } })
.get(state.policiesEndpoint, params)
.then(({ data }) => commit(types.RECEIVE_POLICIES_SUCCESS, data))
.catch(error => commitReceivePoliciesError(commit, error?.response?.data));
.catch(({ response: { data } }) => {
const payload = data?.payload?.length ? data.payload : [];
commit(types.RECEIVE_POLICIES_SUCCESS, payload);
commitReceivePoliciesError(commit, data);
});
};
const commitPolicyError = (commit, type, payload) => {
......
......@@ -70,3 +70,8 @@ export const setCurrentTimeWindow = ({ commit, dispatch }, timeWindow) => {
root: true,
});
};
export const setAllEnvironments = ({ commit, dispatch }) => {
commit(types.SET_ALL_ENVIRONMENTS);
dispatch(`networkPolicies/fetchPolicies`, null, { root: true });
};
......@@ -4,5 +4,6 @@ export const REQUEST_ENVIRONMENTS = 'REQUEST_ENVIRONMENTS';
export const RECEIVE_ENVIRONMENTS_SUCCESS = 'RECEIVE_ENVIRONMENTS_SUCCESS';
export const RECEIVE_ENVIRONMENTS_ERROR = 'RECEIVE_ENVIRONMENTS_ERROR';
export const SET_CURRENT_ENVIRONMENT_ID = 'SET_CURRENT_ENVIRONMENT_ID';
export const SET_ALL_ENVIRONMENTS = 'SET_ALL_ENVIRONMENTS';
export const SET_CURRENT_TIME_WINDOW = 'SET_CURRENT_TIME_WINDOW';
......@@ -21,8 +21,12 @@ export default {
},
[types.SET_CURRENT_ENVIRONMENT_ID](state, payload) {
state.currentEnvironmentId = payload;
state.allEnvironments = false;
},
[types.SET_CURRENT_TIME_WINDOW](state, payload) {
state.currentTimeWindow = payload;
},
[types.SET_ALL_ENVIRONMENTS](state) {
state.allEnvironments = true;
},
};
......@@ -7,4 +7,5 @@ export default () => ({
errorLoadingEnvironments: false,
currentEnvironmentId: -1,
currentTimeWindow: defaultTimeRange.name,
allEnvironments: false,
});
---
title: Add 'All Environments' option for environment dropdown if enabled
merge_request: 40531
author:
type: changed
......@@ -6,10 +6,10 @@ exports[`NetworkPolicyList component renders policies table 1`] = `
<table
aria-busy="false"
aria-colcount="3"
aria-describedby="__BVID__341__caption_"
aria-describedby="__BVID__359__caption_"
aria-multiselectable="false"
class="table b-table gl-table table-hover b-table-stacked-md b-table-selectable b-table-select-single"
id="__BVID__341"
id="__BVID__359"
role="table"
>
<!---->
......
import { shallowMount } from '@vue/test-utils';
import createStore from 'ee/threat_monitoring/store';
import EnvironmentPicker from 'ee/threat_monitoring/components/environment_picker.vue';
import { INVALID_CURRENT_ENVIRONMENT_NAME } from 'ee/threat_monitoring/constants';
import {
INVALID_CURRENT_ENVIRONMENT_NAME,
ALL_ENVIRONMENT_NAME,
} from 'ee/threat_monitoring/constants';
import { mockEnvironmentsResponse } from '../mock_data';
const mockEnvironments = mockEnvironmentsResponse.environments;
const currentEnvironment = mockEnvironments[1];
describe('EnvironmentPicker component', () => {
let store;
......@@ -44,7 +48,6 @@ describe('EnvironmentPicker component', () => {
});
});
describe('given there are environments', () => {
const currentEnvironment = mockEnvironments[1];
beforeEach(() => {
factory({
environments: mockEnvironments,
......@@ -73,6 +76,25 @@ describe('EnvironmentPicker component', () => {
});
});
});
describe('with includeAll enabled', () => {
beforeEach(() => {
factory({
environments: mockEnvironments,
currentEnvironmentId: currentEnvironment.id,
allEnvironments: true,
});
wrapper = shallowMount(EnvironmentPicker, {
propsData: {
includeAll: true,
},
store,
});
});
it('has text set to the all environment option', () => {
expect(findEnvironmentsDropdown().attributes().text).toBe(ALL_ENVIRONMENT_NAME);
});
});
});
describe.each`
......
......@@ -72,22 +72,44 @@ describe('Network Policy actions', () => {
],
[],
));
describe('without environment id', () => {
beforeEach(() => {
mock.onGet(networkPoliciesEndpoint, null).replyOnce(httpStatus.OK, mockPoliciesResponse);
});
it('should dispatch the request and success actions', () =>
testAction(
actions.fetchPolicies,
null,
state,
[
{ type: types.REQUEST_POLICIES },
{
type: types.RECEIVE_POLICIES_SUCCESS,
payload: mockPoliciesResponse,
},
],
[],
));
});
});
describe('on error', () => {
describe('without payload', () => {
const error = { error: 'foo' };
beforeEach(() => {
mock.onGet(networkPoliciesEndpoint).replyOnce(500, error);
});
it('should dispatch the request and error actions', () =>
it('should dispatch the request, error actions and updates payload', () =>
testAction(
actions.fetchPolicies,
environmentId,
state,
[
{ type: types.REQUEST_POLICIES },
{ type: types.RECEIVE_POLICIES_SUCCESS, payload: [] },
{ type: types.RECEIVE_POLICIES_ERROR, payload: 'foo' },
],
[],
......@@ -96,33 +118,39 @@ describe('Network Policy actions', () => {
}));
});
describe('with an empty endpoint', () => {
describe('with payload', () => {
const payload = { error: 'foo', payload: [policy] };
beforeEach(() => {
state.policiesEndpoint = '';
mock.onGet(networkPoliciesEndpoint).replyOnce(500, payload);
});
it('should dispatch RECEIVE_POLICES_ERROR', () =>
it('should dispatch the request, error actions and updates payload', () =>
testAction(
actions.fetchPolicies,
environmentId,
state,
[
{
type: types.RECEIVE_POLICIES_ERROR,
payload: s__('NetworkPolicies|Something went wrong, unable to fetch policies'),
},
{ type: types.REQUEST_POLICIES },
{ type: types.RECEIVE_POLICIES_SUCCESS, payload: [policy] },
{ type: types.RECEIVE_POLICIES_ERROR, payload: 'foo' },
],
[],
).then(() => {
expect(createFlash).toHaveBeenCalled();
}));
});
});
describe('without environment id', () => {
it('should dispatch RECEIVE_POLICIES_ERROR', () =>
describe('with an empty endpoint', () => {
beforeEach(() => {
state.policiesEndpoint = '';
});
it('should dispatch RECEIVE_POLICES_ERROR', () =>
testAction(
actions.fetchPolicies,
undefined,
environmentId,
state,
[
{
......
......@@ -237,4 +237,15 @@ describe('Threat Monitoring actions', () => {
],
));
});
describe('setAllEnvironments', () => {
it('commits the SET_ALL_ENVIRONMENTS mutation and dispatches Network Policy fetch action', () =>
testAction(
actions.setAllEnvironments,
null,
state,
[{ type: types.SET_ALL_ENVIRONMENTS }],
[{ type: 'networkPolicies/fetchPolicies', payload: null }],
));
});
});
......@@ -100,8 +100,9 @@ describe('Threat Monitoring mutations', () => {
mutations[types.SET_CURRENT_ENVIRONMENT_ID](state, environmentId);
});
it('sets currentEnvironmentId', () => {
it('sets currentEnvironmentId and allEnvironments', () => {
expect(state.currentEnvironmentId).toBe(environmentId);
expect(state.allEnvironments).toBe(false);
});
});
......@@ -116,4 +117,14 @@ describe('Threat Monitoring mutations', () => {
expect(state.currentTimeWindow).toBe(timeWindow);
});
});
describe(types.SET_ALL_ENVIRONMENTS, () => {
beforeEach(() => {
mutations[types.SET_ALL_ENVIRONMENTS](state);
});
it('sets allEnvironments', () => {
expect(state.allEnvironments).toBe(true);
});
});
});
......@@ -26166,6 +26166,9 @@ msgstr ""
msgid "Threat Monitoring"
msgstr ""
msgid "ThreatMonitoring|All Environments"
msgstr ""
msgid "ThreatMonitoring|Anomalous Requests"
msgstr ""
......
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