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

destroy_service_spec.rb « link_alerts « incident_management « services « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 13885ab7d5de32eaeacac00441a6773912db5631 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe IncidentManagement::LinkAlerts::DestroyService, feature_category: :incident_management do
  let_it_be(:project) { create(:project) }
  let_it_be(:another_project) { create(:project) }
  let_it_be(:developer) { create(:user) }
  let_it_be(:guest) { create(:user) }
  let_it_be(:incident) { create(:incident, project: project) }
  let_it_be(:another_incident) { create(:incident, project: project) }
  let_it_be(:internal_alert) { create(:alert_management_alert, project: project, issue: incident) }
  let_it_be(:external_alert) { create(:alert_management_alert, project: another_project, issue: incident) }
  let_it_be(:unrelated_alert) { create(:alert_management_alert, project: project, issue: another_incident) }

  before_all do
    project.add_guest(guest)
    project.add_developer(developer)
  end

  describe '#execute' do
    subject(:execute) { described_class.new(incident, current_user, alert).execute }

    let(:alert) { internal_alert }

    context 'when current user is a guest' do
      let(:current_user) { guest }

      it 'responds with error', :aggregate_failures do
        response = execute

        expect(response).to be_error
        expect(response.message).to eq('You have insufficient permissions to manage alerts for this project')
      end

      it 'does not unlink alert from the incident' do
        expect { execute }.not_to change { incident.reload.alert_management_alerts.to_a }
      end
    end

    context 'when current user is a developer' do
      let(:current_user) { developer }

      it 'responds with success', :aggregate_failures do
        response = execute

        expect(response).to be_success
        expect(response.payload[:incident]).to eq(incident)
      end

      context 'when unlinking internal alert' do
        let(:alert) { internal_alert }

        it 'unlinks the alert' do
          expect { execute }
            .to change { incident.reload.alert_management_alerts.to_a }
            .to match_array([external_alert])
        end
      end

      context 'when unlinking external alert' do
        let(:alert) { external_alert }

        it 'unlinks the alert' do
          expect { execute }
            .to change { incident.reload.alert_management_alerts.to_a }
            .to match_array([internal_alert])
        end
      end

      context 'when unlinking an alert not related to the incident' do
        let(:alert) { unrelated_alert }

        it "does not change the incident's alerts" do
          expect { execute }.not_to change { incident.reload.alert_management_alerts.to_a }
        end

        it "does not change another incident's alerts" do
          expect { execute }.not_to change { another_incident.reload.alert_management_alerts.to_a }
        end

        it "does not change the alert's incident" do
          expect { execute }.not_to change { unrelated_alert.reload.issue }
        end
      end
    end
  end
end