summaryrefslogtreecommitdiff
path: root/spec/lib/sentry/client/event_spec.rb
blob: af1e28d09bbb8005443cfb1d04959df35d5ebf93 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Sentry::Client do
  include SentryClientHelpers

  let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
  let(:token) { 'test-token' }
  let(:default_httparty_options) do
    {
      follow_redirects: false,
      headers: { "Authorization" => "Bearer test-token" }
    }
  end
  let(:client) { described_class.new(sentry_url, token) }

  describe '#issue_latest_event' do
    let(:sample_response) do
      Gitlab::Utils.deep_indifferent_access(
        Gitlab::Json.parse(fixture_file('sentry/issue_latest_event_sample_response.json'))
      )
    end
    let(:issue_id) { '1234' }
    let(:sentry_api_response) { sample_response }
    let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0' }
    let(:sentry_request_url) { "#{sentry_url}/issues/#{issue_id}/events/latest/" }
    let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }

    subject { client.issue_latest_event(issue_id: issue_id) }

    it_behaves_like 'calls sentry api'

    it 'has correct return type' do
      expect(subject).to be_a(Gitlab::ErrorTracking::ErrorEvent)
    end

    shared_examples 'assigns error tracking event correctly' do
      using RSpec::Parameterized::TableSyntax

      where(:event_object, :sentry_response) do
        :issue_id      | :groupID
        :date_received | :dateReceived
      end

      with_them do
        it { expect(subject.public_send(event_object)).to eq(sentry_api_response.dig(*sentry_response)) }
      end
    end

    context 'error object created from sentry response' do
      it_behaves_like 'assigns error tracking event correctly'

      it 'parses the stack trace' do
        expect(subject.stack_trace_entries).to be_a Array
        expect(subject.stack_trace_entries).not_to be_empty
      end

      context 'error without stack trace' do
        before do
          sample_response['entries'] = []
          stub_sentry_request(sentry_request_url, body: sample_response)
        end

        it_behaves_like 'assigns error tracking event correctly'

        it 'returns an empty array for stack_trace_entries' do
          expect(subject.stack_trace_entries).to eq []
        end
      end
    end
  end
end