summaryrefslogtreecommitdiff
path: root/spec/services/error_tracking/list_issues_service_spec.rb
blob: 9d4fc62f9230011cf0e5f7887f0a81d0a76f4642 (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'

describe ErrorTracking::ListIssuesService do
  set(:user) { create(:user) }
  set(:project) { create(:project) }

  let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
  let(:token) { 'test-token' }
  let(:result) { subject.execute }

  let(:error_tracking_setting) do
    create(:project_error_tracking_setting, api_url: sentry_url, token: token, project: project)
  end

  subject { described_class.new(project, user) }

  before do
    expect(project).to receive(:error_tracking_setting).at_least(:once).and_return(error_tracking_setting)

    project.add_reporter(user)
  end

  describe '#execute' do
    context 'with authorized user' do
      context 'when list_sentry_issues returns issues' do
        let(:issues) { [:list, :of, :issues] }

        before do
          expect(error_tracking_setting)
            .to receive(:list_sentry_issues).and_return(issues: issues)
        end

        it 'returns the issues' do
          expect(result).to eq(status: :success, issues: issues)
        end
      end

      context 'when list_sentry_issues returns nil' do
        before do
          expect(error_tracking_setting)
            .to receive(:list_sentry_issues).and_return(nil)
        end

        it 'result is not ready' do
          expect(result).to eq(
            status: :error, http_status: :no_content, message: 'Not ready. Try again later')
        end
      end

      context 'when list_sentry_issues returns error' do
        before do
          allow(error_tracking_setting)
            .to receive(:list_sentry_issues)
            .and_return(error: 'Sentry response status code: 401')
        end

        it 'returns the error' do
          expect(result).to eq(
            status: :error,
            http_status: :bad_request,
            message: 'Sentry response status code: 401'
          )
        end
      end
    end

    context 'with unauthorized user' do
      let(:unauthorized_user) { create(:user) }

      subject { described_class.new(project, unauthorized_user) }

      it 'returns error' do
        result = subject.execute

        expect(result).to include(
          status: :error,
          message: 'Access denied',
          http_status: :unauthorized
        )
      end
    end

    context 'with error tracking disabled' do
      before do
        error_tracking_setting.enabled = false
      end

      it 'raises error' do
        result = subject.execute

        expect(result).to include(status: :error, message: 'Error Tracking is not enabled')
      end
    end
  end

  describe '#sentry_external_url' do
    let(:external_url) { 'https://sentrytest.gitlab.com/sentry-org/sentry-project' }

    it 'calls ErrorTracking::ProjectErrorTrackingSetting' do
      expect(error_tracking_setting).to receive(:sentry_external_url).and_call_original

      subject.external_url
    end
  end
end