Welcome to mirror list, hosted at ThFree Co, Russian Federation.

update_service_spec.rb « alerts « alert_management « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 882543fd701e2cd19e271b0f82bc62708e8122e2 (plain)
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe AlertManagement::Alerts::UpdateService do
  let_it_be(:user_with_permissions) { create(:user) }
  let_it_be(:other_user_with_permissions) { create(:user) }
  let_it_be(:user_without_permissions) { create(:user) }
  let_it_be(:project) { create(:project) }
  let_it_be(:alert, reload: true) { create(:alert_management_alert, :triggered, project: project) }

  let(:current_user) { user_with_permissions }
  let(:params) { {} }

  let(:service) { described_class.new(alert, current_user, params) }

  before_all do
    project.add_developer(user_with_permissions)
    project.add_developer(other_user_with_permissions)
  end

  describe '#execute' do
    shared_examples 'does not add a todo' do
      specify { expect { response }.not_to change(Todo, :count) }
    end

    shared_examples 'does not add a system note' do
      specify { expect { response }.not_to change(Note, :count) }
    end

    shared_examples 'adds a system note' do |note_matcher = nil|
      specify do
        expect { response }.to change { alert.reload.notes.count }.by(1)
        expect(alert.notes.last.note).to match(note_matcher) if note_matcher
      end
    end

    shared_examples 'error response' do |message|
      it_behaves_like 'does not add a todo'
      it_behaves_like 'does not add a system note'

      it 'has an informative message' do
        expect(response).to be_error
        expect(response.message).to eq(message)
      end
    end

    subject(:response) { service.execute }

    context 'when the current_user is nil' do
      let(:current_user) { nil }

      it_behaves_like 'error response', 'You have no permissions'
    end

    context 'when current_user does not have permission to update alerts' do
      let(:current_user) { user_without_permissions }

      it_behaves_like 'error response', 'You have no permissions'
    end

    context 'when no parameters are included' do
      it_behaves_like 'error response', 'Please provide attributes to update'
    end

    context 'when an error occurs during update' do
      let(:params) { { title: nil } }

      it_behaves_like 'error response', "Title can't be blank"
    end

    shared_examples 'title update' do
      it_behaves_like 'does not add a todo'
      it_behaves_like 'does not add a system note'

      it 'updates the attribute' do
        original_title = alert.title

        expect { response }.to change { alert.title }.from(original_title).to(expected_title)
        expect(response).to be_success
      end
    end

    context 'when a model attribute is included without assignees' do
      let(:params) { { title: 'This is an updated alert.' } }
      let(:expected_title) { params[:title] }

      it_behaves_like 'title update'
    end

    context 'when alert is resolved and another existing open alert' do
      let!(:alert) { create(:alert_management_alert, :resolved, project: project) }
      let!(:existing_alert) { create(:alert_management_alert, :triggered, project: project) }

      let(:params) { { title: 'This is an updated alert.' } }
      let(:expected_title) { params[:title] }

      it_behaves_like 'title update'
    end

    context 'when assignees are included' do
      shared_examples 'adds a todo' do
        let(:assignee) { expected_assignees.first }

        specify do
          expect { response }.to change { assignee.reload.todos.count }.by(1)
          expect(assignee.todos.last.author).to eq(current_user)
        end
      end

      shared_examples 'successful assignment' do
        it_behaves_like 'adds a system note'
        it_behaves_like 'adds a todo'

        after do
          alert.assignees = []
        end

        specify do
          expect { response }.to change { alert.reload.assignees }.from([]).to(expected_assignees)
          expect(response).to be_success
        end
      end

      let(:expected_assignees) { params[:assignees] }

      context 'when the assignee is the current user' do
        let(:params) { { assignees: [current_user] } }

        it_behaves_like 'successful assignment'
      end

      context 'when the assignee has read permissions' do
        let(:params) { { assignees: [other_user_with_permissions] } }

        it_behaves_like 'successful assignment'
      end

      context 'when the assignee does not have read permissions' do
        let(:params) { { assignees: [user_without_permissions] } }

        it_behaves_like 'error response', 'Assignee has no permissions'
      end

      context 'when user is already assigned' do
        let(:params) { { assignees: [user_with_permissions] } }

        before do
          alert.assignees << user_with_permissions
        end

        it_behaves_like 'does not add a system note'
        it_behaves_like 'does not add a todo'
      end

      context 'with multiple users included' do
        let(:params) { { assignees: [user_with_permissions, user_without_permissions] } }
        let(:expected_assignees) { [user_with_permissions] }

        it_behaves_like 'successful assignment'
      end
    end

    context 'when a status is included' do
      let(:params) { { status: new_status } }
      let(:new_status) { :acknowledged }

      it 'successfully changes the status' do
        expect { response }.to change { alert.acknowledged? }.to(true)
        expect(response).to be_success
        expect(response.payload[:alert]).to eq(alert)
      end

      it_behaves_like 'adds a system note'

      context 'with unknown status' do
        let(:new_status) { :unknown_status }

        it_behaves_like 'error response', 'Invalid status'
      end

      context 'with resolving status' do
        let(:new_status) { :resolved }

        it 'changes the status' do
          expect { response }.to change { alert.resolved? }.to(true)
        end

        it "resolves the current user's related todos" do
          todo = create(:todo, :pending, target: alert, user: current_user, project: alert.project)

          expect { response }.to change { todo.reload.state }.from('pending').to('done')
        end
      end

      context 'with an opening status and existing open alert' do
        let_it_be(:alert) { create(:alert_management_alert, :resolved, :with_fingerprint, project: project) }
        let_it_be(:existing_alert) { create(:alert_management_alert, :triggered, fingerprint: alert.fingerprint, project: project) }
        let_it_be(:url) { Gitlab::Routing.url_helpers.details_project_alert_management_path(project, existing_alert) }
        let_it_be(:link) { ActionController::Base.helpers.link_to(_('alert'), url) }

        let(:message) do
          "An #{link} with the same fingerprint is already open. " \
          'To change the status of this alert, resolve the linked alert.'
        end

        it_behaves_like 'does not add a todo'
        it_behaves_like 'does not add a system note'

        it 'has an informative message' do
          expect(response).to be_error
          expect(response.message).to eq(message)
        end

        context 'fingerprints are blank' do
          let_it_be(:alert) { create(:alert_management_alert, :resolved, project: project, fingerprint: nil) }
          let_it_be(:existing_alert) { create(:alert_management_alert, :triggered, fingerprint: alert.fingerprint, project: project) }

          it 'successfully changes the status' do
            expect { response }.to change { alert.acknowledged? }.to(true)
            expect(response).to be_success
            expect(response.payload[:alert]).to eq(alert)
          end

          it_behaves_like 'adds a system note'
        end
      end

      context 'two existing closed alerts' do
        let_it_be(:alert) { create(:alert_management_alert, :resolved, :with_fingerprint, project: project) }
        let_it_be(:existing_alert) { create(:alert_management_alert, :resolved, fingerprint: alert.fingerprint, project: project) }

        it 'successfully changes the status' do
          expect { response }.to change { alert.acknowledged? }.to(true)
          expect(response).to be_success
          expect(response.payload[:alert]).to eq(alert)
        end

        it_behaves_like 'adds a system note'
      end

      context 'with an associated issue' do
        let_it_be(:issue, reload: true) { create(:issue, project: project) }

        before do
          alert.update!(issue: issue)
        end

        shared_examples 'does not sync with the incident status' do
          specify do
            expect(::Issues::UpdateService).not_to receive(:new)
            expect { response }.to change { alert.acknowledged? }.to(true)
          end
        end

        it_behaves_like 'does not sync with the incident status'

        context 'when the issue is an incident' do
          before do
            issue.update!(issue_type: Issue.issue_types[:incident])
          end

          it_behaves_like 'does not sync with the incident status'

          context 'when the incident has an escalation status' do
            let_it_be(:escalation_status, reload: true) { create(:incident_management_issuable_escalation_status, issue: issue) }

            it 'updates the incident escalation status with the new alert status' do
              expect(::Issues::UpdateService).to receive(:new).once.and_call_original
              expect(described_class).to receive(:new).once.and_call_original

              expect { response }.to change { escalation_status.reload.acknowledged? }.to(true)
                                 .and change { alert.reload.acknowledged? }.to(true)
            end

            context 'when the statuses match' do
              before do
                escalation_status.update!(status_event: :acknowledge)
              end

              it_behaves_like 'does not sync with the incident status'
            end

            context 'when feature flag is disabled' do
              before do
                stub_feature_flags(incident_escalations: false)
              end

              it_behaves_like 'does not sync with the incident status'
            end
          end
        end
      end

      context 'when a status change reason is included' do
        let(:params) { { status: new_status, status_change_reason: ' by changing the incident status' } }

        it_behaves_like 'adds a system note', /changed the status to \*\*Acknowledged\*\* by changing the incident status/
      end
    end
  end
end