summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/sentry_spec.rb
blob: af8b059b984818339c851e89c0a44ffc8ab7ba07 (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
require 'spec_helper'

describe Gitlab::Sentry do
  describe '.context' do
    it 'adds the expected tags' do
      expect(described_class).to receive(:enabled?).and_return(true)
      allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')

      described_class.context(nil)

      expect(Raven.tags_context[:locale].to_s).to eq(I18n.locale.to_s)
      expect(Raven.tags_context[Labkit::Correlation::CorrelationId::LOG_KEY.to_sym].to_s)
        .to eq('cid')
    end
  end

  describe '.track_exception' do
    let(:exception) { RuntimeError.new('boom') }

    before do
      allow(described_class).to receive(:enabled?).and_return(true)
    end

    it 'raises the exception if it should' do
      expect(described_class).to receive(:should_raise_for_dev?).and_return(true)
      expect { described_class.track_exception(exception) }
        .to raise_error(RuntimeError)
    end

    context 'when exceptions should not be raised' do
      before do
        allow(described_class).to receive(:should_raise_for_dev?).and_return(false)
        allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
      end

      it 'logs the exception with all attributes passed' do
        expected_extras = {
          some_other_info: 'info',
          issue_url: 'http://gitlab.com/gitlab-org/gitlab-ce/issues/1'
        }

        expected_tags = {
          correlation_id: 'cid'
        }

        expect(Raven).to receive(:capture_exception)
                           .with(exception,
                            tags: a_hash_including(expected_tags),
                            extra: a_hash_including(expected_extras))

        described_class.track_exception(
          exception,
          issue_url: 'http://gitlab.com/gitlab-org/gitlab-ce/issues/1',
          extra: { some_other_info: 'info' }
        )
      end

      it 'sets the context' do
        expect(described_class).to receive(:context)

        described_class.track_exception(exception)
      end
    end
  end

  context '.track_acceptable_exception' do
    let(:exception) { RuntimeError.new('boom') }

    before do
      allow(described_class).to receive(:enabled?).and_return(true)
      allow(Labkit::Correlation::CorrelationId).to receive(:current_id).and_return('cid')
    end

    it 'calls Raven.capture_exception' do
      expected_extras = {
        some_other_info: 'info',
        issue_url: 'http://gitlab.com/gitlab-org/gitlab-ce/issues/1'
      }

      expected_tags = {
        correlation_id: 'cid'
      }

      expect(Raven).to receive(:capture_exception)
                         .with(exception,
                          tags: a_hash_including(expected_tags),
                          extra: a_hash_including(expected_extras))

      described_class.track_acceptable_exception(
        exception,
        issue_url: 'http://gitlab.com/gitlab-org/gitlab-ce/issues/1',
        extra: { some_other_info: 'info' }
      )
    end
  end
end