summaryrefslogtreecommitdiff
path: root/spec/workers/error_tracking_issue_link_worker_spec.rb
blob: 701d54b72f014c9803df7127f11049021690242b (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
# frozen_string_literal: true

require 'spec_helper'

describe ErrorTrackingIssueLinkWorker do
  let_it_be(:error_tracking) { create(:project_error_tracking_setting) }
  let_it_be(:project) { error_tracking.project }
  let_it_be(:issue) { create(:issue, project: project) }
  let_it_be(:sentry_issue) { create(:sentry_issue, issue: issue) }

  let(:repo) do
    Gitlab::ErrorTracking::Repo.new(
      status: 'active',
      integration_id: 66666,
      project_id: project.id
    )
  end

  subject { described_class.new.perform(issue.id) }

  describe '#perform' do
    it 'creates a link between an issue and a Sentry issue in Sentry' do
      expect_next_instance_of(Sentry::Client) do |client|
        expect(client).to receive(:repos).with('sentry-org').and_return([repo])
        expect(client)
          .to receive(:create_issue_link)
          .with(66666, sentry_issue.sentry_issue_identifier, issue)
          .and_return(true)
      end

      expect(subject).to be true
    end

    shared_examples_for 'makes no external API requests' do
      it 'takes no action' do
        expect_any_instance_of(Sentry::Client).not_to receive(:repos)
        expect_any_instance_of(Sentry::Client).not_to receive(:create_issue_link)

        expect(subject).to be nil
      end
    end

    shared_examples_for 'attempts to create a link via plugin' do
      it 'takes no action' do
        expect_next_instance_of(Sentry::Client) do |client|
          expect(client).to receive(:repos).with('sentry-org').and_return([repo])
          expect(client)
            .to receive(:create_issue_link)
            .with(nil, sentry_issue.sentry_issue_identifier, issue)
            .and_return(true)
        end

        expect(subject).to be true
      end
    end

    context 'when issue is unavailable' do
      let(:issue) { double('issue', id: -3) }

      it_behaves_like 'makes no external API requests'
    end

    context 'when project does not have error tracking configured' do
      let(:issue) { build(:project) }

      it_behaves_like 'makes no external API requests'
    end

    context 'when the issue is not linked to a Sentry issue in GitLab' do
      let(:issue) { build(:issue, project: project) }

      it_behaves_like 'makes no external API requests'
    end

    context 'when Sentry disabled the GitLab integration' do
      let(:repo) do
        Gitlab::ErrorTracking::Repo.new(
          status: 'inactive',
          integration_id: 66666,
          project_id: project.id
        )
      end

      it_behaves_like 'attempts to create a link via plugin'
    end

    context 'when Sentry the GitLab integration is for another project' do
      let(:repo) do
        Gitlab::ErrorTracking::Repo.new(
          status: 'active',
          integration_id: 66666,
          project_id: -3
        )
      end

      it_behaves_like 'attempts to create a link via plugin'
    end

    context 'when Sentry repos request errors' do
      it 'falls back to creating a link via plugin' do
        expect_next_instance_of(Sentry::Client) do |client|
          expect(client).to receive(:repos).with('sentry-org').and_raise(Sentry::Client::Error)
          expect(client)
            .to receive(:create_issue_link)
            .with(nil, sentry_issue.sentry_issue_identifier, issue)
            .and_return(true)
        end

        expect(subject).to be true
      end
    end
  end
end