summaryrefslogtreecommitdiff
path: root/spec/services/incident_management/issuable_escalation_statuses/after_update_service_spec.rb
blob: 731406613dd6adee389b53d25e314e45d46aa2db (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IncidentManagement::IssuableEscalationStatuses::AfterUpdateService do
  let_it_be(:current_user) { create(:user) }
  let_it_be(:escalation_status, reload: true) { create(:incident_management_issuable_escalation_status, :triggered) }
  let_it_be(:issue, reload: true) { escalation_status.issue }
  let_it_be(:project) { issue.project }
  let_it_be(:alert) { create(:alert_management_alert, issue: issue, project: project) }

  let(:status_event) { :acknowledge }
  let(:update_params) { { incident_management_issuable_escalation_status_attributes: { status_event: status_event } } }
  let(:service) { IncidentManagement::IssuableEscalationStatuses::AfterUpdateService.new(issue, current_user) }

  subject(:result) do
    issue.update!(update_params)
    service.execute
  end

  before do
    issue.project.add_developer(current_user)
  end

  shared_examples 'does not attempt to update the alert' do
    specify do
      expect(::AlertManagement::Alerts::UpdateService).not_to receive(:new)

      expect(result).to be_success
    end
  end

  shared_examples 'adds a status change system note' do
    specify do
      expect { result }.to change { issue.reload.notes.count }.by(1)
    end
  end

  context 'with status attributes' do
    it_behaves_like 'adds a status change system note'

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

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

    context 'when incident is not associated with an alert' do
      before do
        alert.destroy!
      end

      it_behaves_like 'does not attempt to update the alert'
      it_behaves_like 'adds a status change system note'
    end

    context 'when new status matches the current status' do
      let(:status_event) { :trigger }

      it_behaves_like 'does not attempt to update the alert'

      specify { expect { result }.not_to change { issue.reload.notes.count } }
    end
  end
end