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

require 'spec_helper'

RSpec.describe IncidentManagement::IssuableEscalationStatuses::PrepareUpdateService do
  let_it_be(:escalation_status) { create(:incident_management_issuable_escalation_status, :triggered) }
  let_it_be(:user_with_permissions) { create(:user) }

  let(:current_user) { user_with_permissions }
  let(:issue) { escalation_status.issue }
  let(:status) { :acknowledged }
  let(:params) { { status: status } }
  let(:service) { IncidentManagement::IssuableEscalationStatuses::PrepareUpdateService.new(issue, current_user, params) }

  subject(:result) { service.execute }

  before do
    issue.project.add_developer(user_with_permissions)
  end

  shared_examples 'successful response' do |payload|
    it 'returns valid parameters which can be used to update the issue' do
      expect(result).to be_success
      expect(result.payload).to eq(escalation_status: payload)
    end
  end

  shared_examples 'error response' do |message|
    specify do
      expect(result).to be_error
      expect(result.message).to eq(message)
    end
  end

  shared_examples 'availability error response' do
    include_examples 'error response', 'Escalation status updates are not available for this issue, user, or project.'
  end

  shared_examples 'invalid params error response' do
    include_examples 'error response', 'Invalid value was provided for parameters: status'
  end

  it_behaves_like 'successful response', { status_event: :acknowledge }

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

    it_behaves_like 'availability error response'
  end

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

    it_behaves_like 'availability error response'
  end

  context 'when user does not have permissions' do
    let(:current_user) { create(:user) }

    it_behaves_like 'availability error response'
  end

  context 'when called with an unsupported issue type' do
    let(:issue) { create(:issue) }

    it_behaves_like 'availability error response'
  end

  context 'when an IssuableEscalationStatus record for the issue does not exist' do
    let(:issue) { create(:incident) }

    it_behaves_like 'successful response', { status_event: :acknowledge }

    it 'initializes an issuable escalation status record' do
      expect { result }.not_to change(::IncidentManagement::IssuableEscalationStatus, :count)
      expect(issue.escalation_status).to be_present
    end
  end

  context 'when called without params' do
    let(:params) { nil }

    it_behaves_like 'successful response', {}
  end

  context 'when called with unsupported params' do
    let(:params) { { escalations_started_at: Time.current } }

    it_behaves_like 'successful response', {}
  end

  context 'with status param' do
    context 'when status matches the current status' do
      let(:params) { { status: :triggered } }

      it_behaves_like 'successful response', {}
    end

    context 'when status is unsupported' do
      let(:params) { { status: :mitigated } }

      it_behaves_like 'invalid params error response'
    end

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

      it_behaves_like 'successful response', { status_event: :acknowledge }
    end
  end

  context 'with status_change_reason param' do
    let(:params) { { status_change_reason: ' by changing the incident status' } }

    it_behaves_like 'successful response', { status_change_reason: ' by changing the incident status' }
  end
end