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

client_spec.rb « sentry « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07547a92fb9fc262d99858f0ed91f7bac9d3f6ec (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# frozen_string_literal: true

require 'spec_helper'

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

  subject(:client) { described_class.new(sentry_url, token) }

  shared_examples 'issues has correct return type' do |klass|
    it "returns objects of type #{klass}" do
      expect(subject[:issues]).to all( be_a(klass) )
    end
  end

  shared_examples 'issues has correct length' do |length|
    it { expect(subject[:issues].length).to eq(length) }
  end

  describe '#list_issues' do
    let(:issues_sample_response) do
      Gitlab::Utils.deep_indifferent_access(
        JSON.parse(fixture_file('sentry/issues_sample_response.json'))
      )
    end

    let(:issue_status) { 'unresolved' }
    let(:limit) { 20 }
    let(:search_term) { '' }
    let(:cursor) { nil }
    let(:sort) { 'last_seen' }
    let(:sentry_api_response) { issues_sample_response }
    let(:sentry_request_url) { sentry_url + '/issues/?limit=20&query=is:unresolved' }

    let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }

    subject { client.list_issues(issue_status: issue_status, limit: limit, search_term: search_term, sort: sort, cursor: cursor) }

    it_behaves_like 'calls sentry api'

    it_behaves_like 'issues has correct return type', Gitlab::ErrorTracking::Error
    it_behaves_like 'issues has correct length', 1

    shared_examples 'has correct external_url' do
      context 'external_url' do
        it 'is constructed correctly' do
          expect(subject[:issues][0].external_url).to eq('https://sentrytest.gitlab.com/sentry-org/sentry-project/issues/11')
        end
      end
    end

    context 'when response has a pagination info' do
      let(:headers) do
        {
          link: '<https://sentrytest.gitlab.com>; rel="previous"; results="true"; cursor="1573556671000:0:1", <https://sentrytest.gitlab.com>; rel="next"; results="true"; cursor="1572959139000:0:0"'
        }
      end
      let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response, headers: headers) }

      it 'parses the pagination' do
        expect(subject[:pagination]).to eq(
          'previous' => { 'cursor' => '1573556671000:0:1' },
          'next' => { 'cursor' => '1572959139000:0:0' }
        )
      end
    end

    context 'error object created from sentry response' do
      using RSpec::Parameterized::TableSyntax

      where(:error_object, :sentry_response) do
        :id           | :id
        :first_seen   | :firstSeen
        :last_seen    | :lastSeen
        :title        | :title
        :type         | :type
        :user_count   | :userCount
        :count        | :count
        :message      | [:metadata, :value]
        :culprit      | :culprit
        :short_id     | :shortId
        :status       | :status
        :frequency    | [:stats, '24h']
        :project_id   | [:project, :id]
        :project_name | [:project, :name]
        :project_slug | [:project, :slug]
      end

      with_them do
        it { expect(subject[:issues][0].public_send(error_object)).to eq(sentry_api_response[0].dig(*sentry_response)) }
      end

      it_behaves_like 'has correct external_url'
    end

    context 'redirects' do
      let(:sentry_api_url) { sentry_url + '/issues/?limit=20&query=is:unresolved' }

      it_behaves_like 'no Sentry redirects'
    end

    # Sentry API returns 404 if there are extra slashes in the URL!
    context 'extra slashes in URL' do
      let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects//sentry-org/sentry-project/' }

      let(:sentry_request_url) do
        'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/' \
          'issues/?limit=20&query=is:unresolved'
      end

      it 'removes extra slashes in api url' do
        expect(client.url).to eq(sentry_url)
        expect(Gitlab::HTTP).to receive(:get).with(
          URI('https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/issues/'),
          anything
        ).and_call_original

        subject

        expect(sentry_api_request).to have_been_requested
      end
    end

    context 'requests with sort parameter in sentry api' do
      let(:sentry_request_url) do
        'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/' \
          'issues/?limit=20&query=is:unresolved&sort=freq'
      end
      let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }

      subject { client.list_issues(issue_status: issue_status, limit: limit, sort: 'frequency') }

      it 'calls the sentry api with sort params' do
        expect(Gitlab::HTTP).to receive(:get).with(
          URI("#{sentry_url}/issues/"),
          default_httparty_options.merge(query: { limit: 20, query: "is:unresolved", sort: "freq" })
        ).and_call_original

        subject

        expect(sentry_api_request).to have_been_requested
      end
    end

    context 'with invalid sort params' do
      subject { client.list_issues(issue_status: issue_status, limit: limit, sort: 'fish') }

      it 'throws an error' do
        expect { subject }.to raise_error(Sentry::Client::BadRequestError, 'Invalid value for sort param')
      end
    end

    context 'Older sentry versions where keys are not present' do
      let(:sentry_api_response) do
        issues_sample_response[0...1].map do |issue|
          issue[:project].delete(:id)
          issue
        end
      end

      it_behaves_like 'calls sentry api'

      it_behaves_like 'issues has correct return type', Gitlab::ErrorTracking::Error
      it_behaves_like 'issues has correct length', 1

      it_behaves_like 'has correct external_url'
    end

    context 'essential keys missing in API response' do
      let(:sentry_api_response) do
        issues_sample_response[0...1].map do |issue|
          issue.except(:id)
        end
      end

      it 'raises exception' do
        expect { subject }.to raise_error(Sentry::Client::MissingKeysError, 'Sentry API response is missing keys. key not found: "id"')
      end
    end

    context 'sentry api response too large' do
      it 'raises exception' do
        deep_size = double('Gitlab::Utils::DeepSize', valid?: false)
        allow(Gitlab::Utils::DeepSize).to receive(:new).with(sentry_api_response).and_return(deep_size)

        expect { subject }.to raise_error(Sentry::Client::ResponseInvalidSizeError, 'Sentry API response is too big. Limit is 1 MB.')
      end
    end

    it_behaves_like 'maps Sentry exceptions'

    context 'when search term is present' do
      let(:search_term) { 'NoMethodError' }
      let(:sentry_request_url) { "#{sentry_url}/issues/?limit=20&query=is:unresolved NoMethodError" }

      it_behaves_like 'calls sentry api'

      it_behaves_like 'issues has correct return type', Gitlab::ErrorTracking::Error
      it_behaves_like 'issues has correct length', 1
    end

    context 'when cursor is present' do
      let(:cursor) { '1572959139000:0:0' }
      let(:sentry_request_url) { "#{sentry_url}/issues/?limit=20&cursor=#{cursor}&query=is:unresolved" }

      it_behaves_like 'calls sentry api'

      it_behaves_like 'issues has correct return type', Gitlab::ErrorTracking::Error
      it_behaves_like 'issues has correct length', 1
    end
  end

  describe '#issue_latest_event' do
    let(:sample_response) do
      Gitlab::Utils.deep_indifferent_access(
        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