1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe AlertManagement::AlertsFinder, '#execute' do
let_it_be(:current_user) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be(:resolved_alert) { create(:alert_management_alert, :all_fields, :resolved, project: project, ended_at: 1.year.ago, events: 2, severity: :high) }
let_it_be(:ignored_alert) { create(:alert_management_alert, :all_fields, :ignored, project: project, events: 1, severity: :critical) }
let_it_be(:triggered_alert) { create(:alert_management_alert, :all_fields) }
let_it_be(:threat_monitroing_alert) { create(:alert_management_alert, domain: 'threat_monitoring') }
let(:params) { {} }
describe '#execute' do
subject(:execute) { described_class.new(current_user, project, params).execute }
context 'user is not a developer or above' do
it { is_expected.to be_empty }
end
context 'user is developer' do
before do
project.add_developer(current_user)
end
context 'domain' do
context 'domain is threat management' do
let(:params) { { domain: 'threat_management' } }
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
end
context 'domain is unknown' do
let(:params) { { domain: 'unkown' } }
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
end
context 'domain is missing' do
let(:params) { {} }
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
end
context 'skips domain if iid is given' do
let(:params) { { iid: resolved_alert.iid, domain: 'threat_monitoring' } }
it { is_expected.to contain_exactly(resolved_alert) }
end
end
context 'empty params' do
it { is_expected.to contain_exactly(resolved_alert, ignored_alert) }
end
context 'iid given' do
let(:params) { { iid: resolved_alert.iid } }
it { is_expected.to match_array(resolved_alert) }
context 'unknown iid' do
let(:params) { { iid: 'unknown' } }
it { is_expected.to be_empty }
end
end
context 'status given' do
let(:params) { { status: :resolved } }
it { is_expected.to match_array(resolved_alert) }
context 'with an array of statuses' do
let(:triggered_alert) { create(:alert_management_alert) }
let(:params) { { status: [:resolved] } }
it { is_expected.to match_array(resolved_alert) }
end
context 'with no alerts of status' do
let(:params) { { status: :acknowledged } }
it { is_expected.to be_empty }
end
context 'with an empty status array' do
let(:params) { { status: [] } }
it { is_expected.to match_array([resolved_alert, ignored_alert]) }
end
context 'with an nil status' do
let(:params) { { status: nil } }
it { is_expected.to match_array([resolved_alert, ignored_alert]) }
end
end
describe 'sorting' do
context 'when sorting by created' do
context 'sorts alerts ascending' do
let(:params) { { sort: 'created_asc' } }
it { is_expected.to eq [resolved_alert, ignored_alert] }
end
context 'sorts alerts descending' do
let(:params) { { sort: 'created_desc' } }
it { is_expected.to eq [ignored_alert, resolved_alert] }
end
end
context 'when sorting by updated' do
context 'sorts alerts ascending' do
let(:params) { { sort: 'updated_asc' } }
it { is_expected.to eq [resolved_alert, ignored_alert] }
end
context 'sorts alerts descending' do
let(:params) { { sort: 'updated_desc' } }
it { is_expected.to eq [ignored_alert, resolved_alert] }
end
end
context 'when sorting by start time' do
context 'sorts alerts ascending' do
let(:params) { { sort: 'started_at_asc' } }
it { is_expected.to eq [resolved_alert, ignored_alert] }
end
context 'sorts alerts descending' do
let(:params) { { sort: 'started_at_desc' } }
it { is_expected.to eq [ignored_alert, resolved_alert] }
end
end
context 'when sorting by end time' do
context 'sorts alerts ascending' do
let(:params) { { sort: 'ended_at_asc' } }
it { is_expected.to eq [resolved_alert, ignored_alert] }
end
context 'sorts alerts descending' do
let(:params) { { sort: 'ended_at_desc' } }
it { is_expected.to eq [ignored_alert, resolved_alert] }
end
end
context 'when sorting by events count' do
let_it_be(:alert_count_6) { create(:alert_management_alert, project: project, events: 6) }
let_it_be(:alert_count_3) { create(:alert_management_alert, project: project, events: 3) }
context 'sorts alerts ascending' do
let(:params) { { sort: 'event_count_asc' } }
it { is_expected.to eq [ignored_alert, resolved_alert, alert_count_3, alert_count_6] }
end
context 'sorts alerts descending' do
let(:params) { { sort: 'event_count_desc' } }
it { is_expected.to eq [alert_count_6, alert_count_3, resolved_alert, ignored_alert] }
end
end
context 'when sorting by severity' do
let_it_be(:alert_critical) { create(:alert_management_alert, :critical, project: project) }
let_it_be(:alert_high) { create(:alert_management_alert, :high, project: project) }
let_it_be(:alert_medium) { create(:alert_management_alert, :medium, project: project) }
let_it_be(:alert_low) { create(:alert_management_alert, :low, project: project) }
let_it_be(:alert_info) { create(:alert_management_alert, :info, project: project) }
let_it_be(:alert_unknown) { create(:alert_management_alert, :unknown, project: project) }
context 'with ascending sort order' do
let(:params) { { sort: 'severity_asc' } }
it 'sorts alerts by severity from less critical to more critical' do
expect(execute.pluck(:severity).uniq).to eq(%w(unknown info low medium high critical))
end
end
context 'with descending sort order' do
let(:params) { { sort: 'severity_desc' } }
it 'sorts alerts by severity from more critical to less critical' do
expect(execute.pluck(:severity).uniq).to eq(%w(critical high medium low info unknown))
end
end
end
context 'when sorting by status' do
let_it_be(:alert_triggered) { create(:alert_management_alert, project: project) }
let_it_be(:alert_acknowledged) { create(:alert_management_alert, :acknowledged, project: project) }
let_it_be(:alert_resolved) { create(:alert_management_alert, :resolved, project: project) }
let_it_be(:alert_ignored) { create(:alert_management_alert, :ignored, project: project) }
context 'with ascending sort order' do
let(:params) { { sort: 'status_asc' } }
it 'sorts by status: Ignored > Resolved > Acknowledged > Triggered' do
expect(execute.map(&:status_name).uniq).to eq([:ignored, :resolved, :acknowledged, :triggered])
end
end
context 'with descending sort order' do
let(:params) { { sort: 'status_desc' } }
it 'sorts by status: Triggered > Acknowledged > Resolved > Ignored' do
expect(execute.map(&:status_name).uniq).to eq([:triggered, :acknowledged, :resolved, :ignored])
end
end
end
end
context 'search query given' do
let_it_be(:alert) do
create(
:alert_management_alert,
:with_fingerprint,
project: project,
title: 'Title',
description: 'Desc',
service: 'Service',
monitoring_tool: 'Monitor'
)
end
context 'searching title' do
let(:params) { { search: alert.title } }
it { is_expected.to match_array([alert]) }
end
context 'searching description' do
let(:params) { { search: alert.description } }
it { is_expected.to match_array([alert]) }
end
context 'searching service' do
let(:params) { { search: alert.service } }
it { is_expected.to match_array([alert]) }
end
context 'searching monitoring tool' do
let(:params) { { search: alert.monitoring_tool } }
it { is_expected.to match_array([alert]) }
end
context 'searching something else' do
let(:params) { { search: alert.fingerprint } }
it { is_expected.to be_empty }
end
end
context 'assignee username given' do
let_it_be(:assignee) { create(:user) }
let_it_be(:alert) { create(:alert_management_alert, project: project, assignees: [assignee]) }
let(:params) { { assignee_username: username } }
context 'with valid assignee_username' do
let(:username) { assignee.username }
it { is_expected.to match_array([alert]) }
end
context 'with invalid assignee_username' do
let(:username) { 'unknown username' }
it { is_expected.to be_empty }
end
end
end
end
describe '.counts_by_status' do
subject { described_class.counts_by_status(current_user, project, params) }
before do
project.add_developer(current_user)
end
it { is_expected.to match(resolved: 1, ignored: 1) }
context 'when filtering params are included' do
let(:params) { { status: :resolved } }
it { is_expected.to match(resolved: 1) }
end
end
end
|