summaryrefslogtreecommitdiff
path: root/spec/services/incident_management/incidents/create_service_spec.rb
blob: 851b21e12272c299bfc64b5becca32b9ebff82da (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IncidentManagement::Incidents::CreateService do
  let_it_be(:project) { create(:project) }
  let_it_be(:user) { User.alert_bot }

  let(:description) { 'Incident description' }

  describe '#execute' do
    subject(:create_incident) { described_class.new(project, user, title: title, description: description).execute }

    context 'when incident has title and description' do
      let(:title) { 'Incident title' }
      let(:new_issue) { Issue.last! }

      it 'responds with success' do
        expect(create_incident).to be_success
      end

      it 'creates an incident issue' do
        expect { create_incident }.to change(Issue, :count).by(1)
      end

      it 'created issue has correct attributes', :aggregate_failures do
        create_incident

        expect(new_issue.title).to eq(title)
        expect(new_issue.description).to eq(description)
        expect(new_issue.author).to eq(user)
      end

      it_behaves_like 'incident issue' do
        before do
          create_incident
        end

        let(:issue) { new_issue }
      end

      context 'with default severity' do
        it 'sets the correct severity level to "unknown"' do
          create_incident
          expect(new_issue.severity).to eq(IssuableSeverity::DEFAULT)
        end
      end

      context 'with severity' do
        using RSpec::Parameterized::TableSyntax

        subject(:create_incident) { described_class.new(project, user, title: title, description: description, severity: severity).execute }

        where(:severity, :incident_severity) do
          'critical' | 'critical'
          'high'     | 'high'
          'medium'   | 'medium'
          'low'      | 'low'
          'unknown'  | 'unknown'
        end

        with_them do
          it 'sets the correct severity level' do
            create_incident
            expect(new_issue.severity).to eq(incident_severity)
          end
        end
      end
    end

    context 'when incident has no title' do
      let(:title) { '' }

      it 'does not create an issue' do
        expect { create_incident }.not_to change(Issue, :count)
      end

      it 'responds with errors' do
        expect(create_incident).to be_error
        expect(create_incident.errors).to contain_exactly("Title can't be blank")
      end

      it 'result payload contains an Issue object' do
        expect(create_incident.payload[:issue]).to be_kind_of(Issue)
      end

      context 'with alert' do
        let(:alert) { create(:alert_management_alert, project: project) }

        subject(:create_incident) { described_class.new(project, user, title: title, description: description, alert: alert).execute }

        it 'associates the alert with the incident' do
          expect(create_incident[:issue].alert_management_alert).to eq(alert)
        end

        context 'the alert prevents the issue from saving' do
          let(:alert) { create(:alert_management_alert, :with_validation_errors, project: project) }

          it 'responds with errors' do
            expect(create_incident).to be_error
            expect(create_incident.errors).to contain_exactly('Hosts hosts array is over 255 chars')
          end
        end
      end
    end
  end
end