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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
|
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe IncidentManagement::TimelineEvents::CreateService do
let_it_be(:user_with_permissions) { create(:user) }
let_it_be(:user_without_permissions) { create(:user) }
let_it_be(:project) { create(:project) }
let_it_be_with_refind(:incident) { create(:incident, project: project) }
let_it_be(:comment) { create(:note, project: project, noteable: incident) }
let_it_be(:timeline_event_tag) do
create(:incident_management_timeline_event_tag, name: 'Test tag 1', project: project)
end
let(:args) do
{
note: 'note',
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment
}
end
let(:editable) { false }
let(:current_user) { user_with_permissions }
let(:service) { described_class.new(incident, current_user, args) }
before_all do
project.add_developer(user_with_permissions)
project.add_reporter(user_without_permissions)
end
describe '#execute' do
shared_examples 'error response' do |message|
it 'has an informative message' do
expect(execute).to be_error
expect(execute.message).to eq(message)
end
it_behaves_like 'does not track incident management event', :incident_management_timeline_event_created
end
shared_examples 'success response' do
it 'has timeline event', :aggregate_failures do
expect(execute).to be_success
result = execute.payload[:timeline_event]
expect(result).to be_a(::IncidentManagement::TimelineEvent)
expect(result.author).to eq(current_user)
expect(result.incident).to eq(incident)
expect(result.project).to eq(project)
expect(result.note).to eq(args[:note])
expect(result.promoted_from_note).to eq(comment)
expect(result.editable).to eq(editable)
end
it_behaves_like 'an incident management tracked event', :incident_management_timeline_event_created
it_behaves_like 'Snowplow event tracking with RedisHLL context' do
let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
let(:namespace) { project.namespace.reload }
let(:category) { described_class.to_s }
let(:user) { current_user }
let(:action) { 'incident_management_timeline_event_created' }
let(:label) { 'redis_hll_counters.incident_management.incident_management_total_unique_counts_monthly' }
end
end
subject(:execute) { service.execute }
context 'when current user is blank' do
let(:current_user) { nil }
it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident'
end
context 'when user does not have permissions to create timeline events' do
let(:current_user) { user_without_permissions }
it_behaves_like 'error response', 'You have insufficient permissions to manage timeline events for this incident'
end
context 'when error occurs during creation' do
let(:args) { {} }
it_behaves_like 'error response', "Occurred at can't be blank and Timeline text can't be blank"
end
context 'with default action' do
let(:args) { { note: 'note', occurred_at: Time.current, promoted_from_note: comment } }
it_behaves_like 'success response'
it 'matches the default action', :aggregate_failures do
result = execute.payload[:timeline_event]
expect(result.action).to eq(IncidentManagement::TimelineEvents::DEFAULT_ACTION)
end
it 'creates a system note' do
expect { execute }.to change { incident.notes.reload.count }.by(1)
end
context 'with auto_created param' do
let(:args) do
{
note: 'note',
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment,
auto_created: auto_created
}
end
context 'when auto_created is true' do
let(:auto_created) { true }
it 'does not create a system note' do
expect { execute }.not_to change { incident.notes.reload.count }
end
context 'when user does not have permissions' do
let(:current_user) { user_without_permissions }
it_behaves_like 'success response'
end
end
context 'when auto_created is false' do
let(:auto_created) { false }
it 'creates a system note' do
expect { execute }.to change { incident.notes.reload.count }.by(1)
end
end
end
end
context 'with non_default action' do
it_behaves_like 'success response'
it 'matches the action from arguments', :aggregate_failures do
result = execute.payload[:timeline_event]
expect(result.action).to eq(args[:action])
end
end
context 'when timeline event tag names are passed' do
let(:args) do
{
note: 'note',
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment,
timeline_event_tag_names: ['Test tag 1']
}
end
it_behaves_like 'success response'
it 'matches the tag name' do
result = execute.payload[:timeline_event]
expect(result.timeline_event_tags.first).to eq(timeline_event_tag)
end
context 'when predefined tags are passed' do
let(:args) do
{
note: 'note',
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment,
timeline_event_tag_names: ['start time', 'end time', 'Impact mitigated']
}
end
it_behaves_like 'success response'
it 'matches the two tags on the event and creates on project' do
result = execute.payload[:timeline_event]
expect(result.timeline_event_tags.count).to eq(3)
expect(result.timeline_event_tags.by_names(['Start time', 'End time', 'Impact mitigated']).pluck_names)
.to match_array(['Start time', 'End time', 'Impact mitigated'])
expect(project.incident_management_timeline_event_tags.pluck_names)
.to include('Start time', 'End time', 'Impact mitigated')
end
end
context 'when invalid tag names are passed' do
let(:args) do
{
note: 'note',
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment,
timeline_event_tag_names: ['some other time']
}
end
it_behaves_like 'error response', "Following tags don't exist: [\"some other time\"]"
it 'does not create timeline event' do
expect { execute }.not_to change(IncidentManagement::TimelineEvent, :count)
end
end
end
context 'with editable param' do
let(:args) do
{
note: 'note',
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment,
editable: editable
}
end
context 'when editable is true' do
let(:editable) { true }
it_behaves_like 'success response'
end
context 'when editable is false' do
let(:editable) { false }
it_behaves_like 'success response'
end
end
it 'successfully creates a database record', :aggregate_failures do
expect { execute }.to change { ::IncidentManagement::TimelineEvent.count }.by(1)
end
context 'when note is more than 280 characters long' do
let(:args) do
{
note: 'a' * 281,
occurred_at: Time.current,
action: 'new comment',
promoted_from_note: comment,
auto_created: auto_created
}
end
let(:auto_created) { false }
context 'when was not promoted from note' do
let(:comment) { nil }
context 'when auto_created is true' do
let(:auto_created) { true }
it_behaves_like 'success response'
end
context 'when auto_created is false' do
it_behaves_like 'error response', 'Timeline text is too long (maximum is 280 characters)'
end
end
context 'when promoted from note' do
it_behaves_like 'success response'
end
end
end
describe 'automatically created timeline events' do
shared_examples 'successfully created timeline event' do
it 'creates a timeline event', :aggregate_failures do
expect(execute).to be_success
result = execute.payload[:timeline_event]
expect(result).to be_a(::IncidentManagement::TimelineEvent)
expect(result.author).to eq(current_user)
expect(result.incident).to eq(incident)
expect(result.project).to eq(project)
expect(result.note).to eq(expected_note)
expect(result.editable).to eq(false)
expect(result.action).to eq(expected_action)
end
it_behaves_like 'an incident management tracked event', :incident_management_timeline_event_created
it_behaves_like 'Snowplow event tracking with RedisHLL context' do
let(:feature_flag_name) { :route_hll_to_snowplow_phase2 }
let(:namespace) { project.namespace.reload }
let(:category) { described_class.to_s }
let(:user) { current_user }
let(:action) { 'incident_management_timeline_event_created' }
let(:label) { 'redis_hll_counters.incident_management.incident_management_total_unique_counts_monthly' }
end
it 'successfully creates a database record', :aggregate_failures do
expect { execute }.to change { ::IncidentManagement::TimelineEvent.count }.by(1)
end
it 'does not create a system note' do
expect { execute }.not_to change { incident.notes.reload.count }
end
end
describe '.create_incident' do
subject(:execute) { described_class.create_incident(incident, current_user) }
let(:expected_note) { "@#{current_user.username} created the incident" }
let(:expected_action) { 'issues' }
it_behaves_like 'successfully created timeline event'
end
describe '.reopen_incident' do
subject(:execute) { described_class.reopen_incident(incident, current_user) }
let(:expected_note) { "@#{current_user.username} reopened the incident" }
let(:expected_action) { 'issues' }
it_behaves_like 'successfully created timeline event'
end
describe '.resolve_incident' do
subject(:execute) { described_class.resolve_incident(incident, current_user) }
let(:expected_note) { "@#{current_user.username} resolved the incident" }
let(:expected_action) { 'status' }
it_behaves_like 'successfully created timeline event'
end
describe '.change_incident_status' do
subject(:execute) { described_class.change_incident_status(incident, current_user, escalation_status) }
let(:escalation_status) do
instance_double('IncidentManagement::IssuableEscalationStatus', status_name: 'acknowledged')
end
let(:expected_note) { "@#{current_user.username} changed the incident status to **Acknowledged**" }
let(:expected_action) { 'status' }
it_behaves_like 'successfully created timeline event'
end
describe '.change_severity' do
subject(:execute) { described_class.change_severity(incident, current_user) }
let_it_be(:severity) { create(:issuable_severity, severity: :critical, issue: incident) }
let(:expected_note) { "@#{current_user.username} changed the incident severity to **Critical - S1**" }
let(:expected_action) { 'severity' }
it_behaves_like 'successfully created timeline event'
end
describe '.change_labels' do
subject(:execute) do
described_class.change_labels(incident, current_user, added_labels: added, removed_labels: removed)
end
let_it_be(:labels) { create_list(:label, 4, project: project) }
let(:expected_action) { 'label' }
context 'when there are neither added nor removed labels' do
let(:added) { [] }
let(:removed) { [] }
it 'responds with error', :aggregate_failures do
expect(execute).to be_error
expect(execute.message).to eq(_('There are no changed labels'))
end
it 'does not create timeline event' do
expect { execute }.not_to change { incident.incident_management_timeline_events.count }
end
end
context 'when there are only added labels' do
let(:added) { [labels[0], labels[1]] }
let(:removed) { [] }
let(:expected_note) { "@#{current_user.username} added #{added.map(&:to_reference).join(' ')} labels" }
it_behaves_like 'successfully created timeline event'
end
context 'when there are only removed labels' do
let(:added) { [] }
let(:removed) { [labels[2], labels[3]] }
let(:expected_note) { "@#{current_user.username} removed #{removed.map(&:to_reference).join(' ')} labels" }
it_behaves_like 'successfully created timeline event'
end
context 'when there are both added and removed labels' do
let(:added) { [labels[0], labels[1]] }
let(:removed) { [labels[2], labels[3]] }
let(:expected_note) do
added_note = "added #{added.map(&:to_reference).join(' ')} labels"
removed_note = "removed #{removed.map(&:to_reference).join(' ')} labels"
"@#{current_user.username} #{added_note} and #{removed_note}"
end
it_behaves_like 'successfully created timeline event'
end
context 'when there is a single added and single removed labels' do
let(:added) { [labels[0]] }
let(:removed) { [labels[3]] }
let(:expected_note) do
added_note = "added #{added.first.to_reference} label"
removed_note = "removed #{removed.first.to_reference} label"
"@#{current_user.username} #{added_note} and #{removed_note}"
end
it_behaves_like 'successfully created timeline event'
end
context 'when feature flag is disabled' do
let(:added) { [labels[0], labels[1]] }
let(:removed) { [labels[2], labels[3]] }
before do
stub_feature_flags(incident_timeline_events_from_labels: false)
end
it 'does not create timeline event' do
expect { execute }.not_to change { incident.incident_management_timeline_events.count }
end
end
end
end
end
|