Commit c2b6131a authored by Alexander Turinske's avatar Alexander Turinske

Add policy name filter to threat alerts

- add input field for policy name searching
- update tests
parent d2dc00ba
<script> <script>
import { GlFormCheckbox, GlFormGroup } from '@gitlab/ui'; import { GlFormCheckbox, GlFormGroup, GlSearchBoxByType } from '@gitlab/ui';
import { s__ } from '~/locale'; import { s__ } from '~/locale';
import { DEFAULT_FILTERS } from './constants'; import { DEFAULT_FILTERS } from './constants';
export default { export default {
DEFAULT_DISMISSED_FILTER: true, DEFAULT_DISMISSED_FILTER: true,
components: { GlFormCheckbox, GlFormGroup }, components: { GlFormCheckbox, GlFormGroup, GlSearchBoxByType },
props: {
filters: {
type: Object,
required: false,
default: () => {},
},
},
data() {
return {
showMinimumSearchQueryMessage: false,
};
},
i18n: { i18n: {
HIDE_DISMISSED_TITLE: s__('ThreatMonitoring|Hide dismissed alerts'), HIDE_DISMISSED_TITLE: s__('ThreatMonitoring|Hide dismissed alerts'),
POLICY_NAME_FILTER_PLACEHOLDER: s__('NetworkPolicy|Search by policy name'),
POLICY_NAME_FILTER_TITLE: s__('NetworkPolicy|Policy'),
}, },
methods: { methods: {
changeDismissedFilter(filtered) { changeDismissedFilter(filtered) {
const newFilters = filtered ? DEFAULT_FILTERS : {}; const newFilters = filtered ? DEFAULT_FILTERS : { statuses: [] };
this.$emit('filter-change', newFilters); this.handleFilterChange(newFilters);
},
handleSearch(searchTerm) {
const newFilters = { searchTerm };
this.handleFilterChange(newFilters);
},
handleFilterChange(newFilters) {
this.$emit('filter-change', { ...this.filters, ...newFilters });
}, },
}, },
}; };
...@@ -20,9 +41,17 @@ export default { ...@@ -20,9 +41,17 @@ export default {
<template> <template>
<div <div
class="gl-pt-3 gl-px-3 gl-bg-gray-10 gl-display-flex gl-justify-content-end gl-align-items-center" class="gl-p-4 gl-bg-gray-10 gl-display-flex gl-justify-content-space-between gl-align-items-center"
> >
<gl-form-group label-size="sm"> <div>
<h5 class="gl-mt-0">{{ $options.i18n.POLICY_NAME_FILTER_TITLE }}</h5>
<gl-search-box-by-type
debounce="250"
:placeholder="$options.i18n.POLICY_NAME_FILTER_PLACEHOLDER"
@input="handleSearch"
/>
</div>
<gl-form-group label-size="sm" class="gl-mb-0">
<gl-form-checkbox <gl-form-checkbox
class="gl-mt-3" class="gl-mt-3"
:checked="$options.DEFAULT_DISMISSED_FILTER" :checked="$options.DEFAULT_DISMISSED_FILTER"
......
...@@ -137,7 +137,7 @@ export default { ...@@ -137,7 +137,7 @@ export default {
</script> </script>
<template> <template>
<div> <div>
<alert-filters @filter-change="handleFilterChange" /> <alert-filters :filters="filters" @filter-change="handleFilterChange" />
<gl-alert v-if="showNoAlertsMsg" data-testid="threat-alerts-unconfigured" :dismissible="false"> <gl-alert v-if="showNoAlertsMsg" data-testid="threat-alerts-unconfigured" :dismissible="false">
<gl-sprintf :message="$options.i18n.MESSAGES.CONFIGURE"> <gl-sprintf :message="$options.i18n.MESSAGES.CONFIGURE">
<template #link="{ content }"> <template #link="{ content }">
......
---
title: Add policy name filter to threat alerts
merge_request: 56708
author:
type: added
import { GlFormCheckbox } from '@gitlab/ui'; import { GlFormCheckbox, GlSearchBoxByType } from '@gitlab/ui';
import { shallowMount } from '@vue/test-utils'; import { shallowMount } from '@vue/test-utils';
import AlertFilters from 'ee/threat_monitoring/components/alerts/alert_filters.vue'; import AlertFilters from 'ee/threat_monitoring/components/alerts/alert_filters.vue';
import { DEFAULT_FILTERS } from 'ee/threat_monitoring/components/alerts/constants'; import { DEFAULT_FILTERS } from 'ee/threat_monitoring/components/alerts/constants';
...@@ -7,9 +7,10 @@ describe('AlertFilters component', () => { ...@@ -7,9 +7,10 @@ describe('AlertFilters component', () => {
let wrapper; let wrapper;
const findGlFormCheckbox = () => wrapper.find(GlFormCheckbox); const findGlFormCheckbox = () => wrapper.find(GlFormCheckbox);
const findGlSearch = () => wrapper.find(GlSearchBoxByType);
const createWrapper = () => { const createWrapper = (filters = DEFAULT_FILTERS) => {
wrapper = shallowMount(AlertFilters); wrapper = shallowMount(AlertFilters, { propsData: { filters } });
}; };
afterEach(() => { afterEach(() => {
...@@ -17,6 +18,31 @@ describe('AlertFilters component', () => { ...@@ -17,6 +18,31 @@ describe('AlertFilters component', () => {
wrapper = null; wrapper = null;
}); });
describe('Policy Name Filter', () => {
beforeEach(() => {
createWrapper();
});
describe('default state', () => {
it('shows policy name search box', () => {
const search = findGlSearch();
expect(search.exists()).toBe(true);
expect(search.attributes('value')).toBe('');
});
it('does emit an event with a user-defined string', async () => {
const term = 'abc';
const search = findGlSearch();
search.vm.$emit('input', term);
await wrapper.vm.$nextTick();
expect(wrapper.emitted('filter-change')).toStrictEqual([
[{ ...DEFAULT_FILTERS, searchTerm: 'abc' }],
]);
});
});
});
describe('Hide Dismissed Filter', () => {
describe('default state', () => { describe('default state', () => {
it('"hide dismissed checkbox" is checked', () => { it('"hide dismissed checkbox" is checked', () => {
createWrapper(); createWrapper();
...@@ -27,22 +53,21 @@ describe('AlertFilters component', () => { ...@@ -27,22 +53,21 @@ describe('AlertFilters component', () => {
}); });
describe('dismissed alerts filter', () => { describe('dismissed alerts filter', () => {
beforeEach(() => {
createWrapper();
});
it('emits an event with no filters on filter deselect', async () => { it('emits an event with no filters on filter deselect', async () => {
createWrapper();
const checkbox = findGlFormCheckbox(); const checkbox = findGlFormCheckbox();
checkbox.vm.$emit('change', false); checkbox.vm.$emit('change', false);
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
expect(wrapper.emitted('filter-change')).toEqual([[{}]]); expect(wrapper.emitted('filter-change')).toStrictEqual([[{ statuses: [] }]]);
}); });
it('emits an event with the default filters on filter select', async () => { it('emits an event with the default filters on filter select', async () => {
createWrapper({});
const checkbox = findGlFormCheckbox(); const checkbox = findGlFormCheckbox();
checkbox.vm.$emit('change', true); checkbox.vm.$emit('change', true);
await wrapper.vm.$nextTick(); await wrapper.vm.$nextTick();
expect(wrapper.emitted('filter-change')).toEqual([[DEFAULT_FILTERS]]); expect(wrapper.emitted('filter-change')).toEqual([[DEFAULT_FILTERS]]);
}); });
}); });
});
}); });
...@@ -101,6 +101,7 @@ describe('AlertsList component', () => { ...@@ -101,6 +101,7 @@ describe('AlertsList component', () => {
it('shows threat monitoring alert filters', () => { it('shows threat monitoring alert filters', () => {
expect(findAlertFilters().exists()).toBe(true); expect(findAlertFilters().exists()).toBe(true);
expect(findAlertFilters().props('filters')).toBe(DEFAULT_FILTERS);
}); });
it('does have the default filters initially', () => { it('does have the default filters initially', () => {
......
...@@ -20394,6 +20394,12 @@ msgstr "" ...@@ -20394,6 +20394,12 @@ msgstr ""
msgid "NetworkPolicies|ports/protocols" msgid "NetworkPolicies|ports/protocols"
msgstr "" msgstr ""
msgid "NetworkPolicy|Policy"
msgstr ""
msgid "NetworkPolicy|Search by policy name"
msgstr ""
msgid "Never" msgid "Never"
msgstr "" 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