2019-01-09 16:04:27 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Sentry::Client do
|
|
|
|
let(:sentry_url) { 'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project' }
|
|
|
|
let(:token) { 'test-token' }
|
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
let(:issues_sample_response) do
|
2019-01-09 16:04:27 -05:00
|
|
|
Gitlab::Utils.deep_indifferent_access(
|
2019-01-31 05:05:29 -05:00
|
|
|
JSON.parse(fixture_file('sentry/issues_sample_response.json'))
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:projects_sample_response) do
|
|
|
|
Gitlab::Utils.deep_indifferent_access(
|
|
|
|
JSON.parse(fixture_file('sentry/list_projects_sample_response.json'))
|
2019-01-09 16:04:27 -05:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
subject(:client) { described_class.new(sentry_url, token) }
|
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
# Requires sentry_api_url and subject to be defined
|
|
|
|
shared_examples 'no redirects' do
|
|
|
|
let(:redirect_to) { 'https://redirected.example.com' }
|
|
|
|
let(:other_url) { 'https://other.example.org' }
|
|
|
|
|
|
|
|
let!(:redirected_req_stub) { stub_sentry_request(other_url) }
|
|
|
|
|
|
|
|
let!(:redirect_req_stub) do
|
|
|
|
stub_sentry_request(
|
|
|
|
sentry_api_url,
|
|
|
|
status: 302,
|
|
|
|
headers: { location: redirect_to }
|
|
|
|
)
|
|
|
|
end
|
2019-01-09 16:04:27 -05:00
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
it 'does not follow redirects' do
|
2019-02-21 06:19:51 -05:00
|
|
|
expect { subject }.to raise_exception(Sentry::Client::Error, 'Sentry response status code: 302')
|
2019-01-31 05:05:29 -05:00
|
|
|
expect(redirect_req_stub).to have_been_requested
|
|
|
|
expect(redirected_req_stub).not_to have_been_requested
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
2019-01-09 16:04:27 -05:00
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
shared_examples 'has correct return type' do |klass|
|
|
|
|
it "returns objects of type #{klass}" do
|
|
|
|
expect(subject).to all( be_a(klass) )
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples 'has correct length' do |length|
|
|
|
|
it { expect(subject.length).to eq(length) }
|
|
|
|
end
|
|
|
|
|
|
|
|
# Requires sentry_api_request and subject to be defined
|
|
|
|
shared_examples 'calls sentry api' do
|
|
|
|
it 'calls sentry api' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(sentry_api_request).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
shared_examples 'maps exceptions' do
|
|
|
|
exceptions = {
|
|
|
|
HTTParty::Error => 'Error when connecting to Sentry',
|
|
|
|
Net::OpenTimeout => 'Connection to Sentry timed out',
|
|
|
|
SocketError => 'Received SocketError when trying to connect to Sentry',
|
|
|
|
OpenSSL::SSL::SSLError => 'Sentry returned invalid SSL data',
|
|
|
|
Errno::ECONNREFUSED => 'Connection refused',
|
|
|
|
StandardError => 'Sentry request failed due to StandardError'
|
|
|
|
}
|
|
|
|
|
|
|
|
exceptions.each do |exception, message|
|
|
|
|
context "#{exception}" do
|
|
|
|
before do
|
|
|
|
stub_request(:get, sentry_request_url).to_raise(exception)
|
|
|
|
end
|
|
|
|
|
|
|
|
it do
|
|
|
|
expect { subject }
|
|
|
|
.to raise_exception(Sentry::Client::Error, message)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
describe '#list_issues' do
|
|
|
|
let(:issue_status) { 'unresolved' }
|
|
|
|
let(:limit) { 20 }
|
2019-03-29 10:53:40 -04:00
|
|
|
let(:sentry_api_response) { issues_sample_response }
|
2019-04-07 03:51:36 -04:00
|
|
|
let(:sentry_request_url) { sentry_url + '/issues/?limit=20&query=is:unresolved' }
|
2019-03-29 10:53:40 -04:00
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
let!(:sentry_api_request) { stub_sentry_request(sentry_request_url, body: sentry_api_response) }
|
2019-01-31 05:05:29 -05:00
|
|
|
|
|
|
|
subject { client.list_issues(issue_status: issue_status, limit: limit) }
|
|
|
|
|
|
|
|
it_behaves_like 'calls sentry api'
|
|
|
|
|
|
|
|
it_behaves_like 'has correct return type', Gitlab::ErrorTracking::Error
|
|
|
|
it_behaves_like 'has correct length', 1
|
2019-01-09 16:04:27 -05:00
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
shared_examples 'has correct external_url' do
|
|
|
|
context 'external_url' do
|
|
|
|
it 'is constructed correctly' do
|
|
|
|
expect(subject[0].external_url).to eq('https://sentrytest.gitlab.com/sentry-org/sentry-project/issues/11')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-01-09 16:04:27 -05:00
|
|
|
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
|
2019-03-29 10:53:40 -04:00
|
|
|
it { expect(subject[0].public_send(error_object)).to eq(sentry_api_response[0].dig(*sentry_response)) }
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
it_behaves_like 'has correct external_url'
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'redirects' do
|
2019-01-31 05:05:29 -05:00
|
|
|
let(:sentry_api_url) { sentry_url + '/issues/?limit=20&query=is:unresolved' }
|
2019-01-09 16:04:27 -05:00
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
it_behaves_like 'no redirects'
|
2019-01-09 16:04:27 -05:00
|
|
|
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/' }
|
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
let(:sentry_request_url) do
|
|
|
|
'https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/' \
|
2019-01-09 16:04:27 -05:00
|
|
|
'issues/?limit=20&query=is:unresolved'
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes extra slashes in api url' do
|
2019-04-07 03:51:36 -04:00
|
|
|
expect(client.url).to eq(sentry_url)
|
2019-01-09 16:04:27 -05:00
|
|
|
expect(Gitlab::HTTP).to receive(:get).with(
|
|
|
|
URI('https://sentrytest.gitlab.com/api/0/projects/sentry-org/sentry-project/issues/'),
|
|
|
|
anything
|
|
|
|
).and_call_original
|
|
|
|
|
2019-01-31 05:05:29 -05:00
|
|
|
subject
|
|
|
|
|
2019-04-07 03:51:36 -04:00
|
|
|
expect(sentry_api_request).to have_been_requested
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
|
|
|
end
|
2019-03-29 10:53:40 -04:00
|
|
|
|
|
|
|
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 'has correct return type', Gitlab::ErrorTracking::Error
|
|
|
|
it_behaves_like '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
|
2019-04-07 03:51:36 -04:00
|
|
|
|
|
|
|
it_behaves_like 'maps exceptions'
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#list_projects' do
|
|
|
|
let(:sentry_list_projects_url) { 'https://sentrytest.gitlab.com/api/0/projects/' }
|
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
let(:sentry_api_response) { projects_sample_response }
|
|
|
|
|
|
|
|
let!(:sentry_api_request) { stub_sentry_request(sentry_list_projects_url, body: sentry_api_response) }
|
2019-01-31 05:05:29 -05:00
|
|
|
|
|
|
|
subject { client.list_projects }
|
|
|
|
|
|
|
|
it_behaves_like 'calls sentry api'
|
|
|
|
|
|
|
|
it_behaves_like 'has correct return type', Gitlab::ErrorTracking::Project
|
|
|
|
it_behaves_like 'has correct length', 2
|
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
context 'essential keys missing in API response' do
|
|
|
|
let(:sentry_api_response) do
|
|
|
|
projects_sample_response[0...1].map do |project|
|
|
|
|
project.except(:slug)
|
|
|
|
end
|
|
|
|
end
|
2019-01-31 05:05:29 -05:00
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
it 'raises exception' do
|
|
|
|
expect { subject }.to raise_error(Sentry::Client::MissingKeysError, 'Sentry API response is missing keys. key not found: "slug"')
|
|
|
|
end
|
|
|
|
end
|
2019-01-31 05:05:29 -05:00
|
|
|
|
2019-03-29 10:53:40 -04:00
|
|
|
context 'optional keys missing in sentry response' do
|
|
|
|
let(:sentry_api_response) do
|
|
|
|
projects_sample_response[0...1].map do |project|
|
|
|
|
project[:organization].delete(:id)
|
|
|
|
project.delete(:id)
|
|
|
|
project.except(:status)
|
|
|
|
end
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
2019-03-29 10:53:40 -04:00
|
|
|
|
|
|
|
it_behaves_like 'calls sentry api'
|
|
|
|
|
|
|
|
it_behaves_like 'has correct return type', Gitlab::ErrorTracking::Project
|
|
|
|
it_behaves_like 'has correct length', 1
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'error object created from sentry response' do
|
|
|
|
using RSpec::Parameterized::TableSyntax
|
|
|
|
|
|
|
|
where(:sentry_project_object, :sentry_response) do
|
|
|
|
:id | :id
|
|
|
|
:name | :name
|
|
|
|
:status | :status
|
|
|
|
:slug | :slug
|
|
|
|
:organization_name | [:organization, :name]
|
|
|
|
:organization_id | [:organization, :id]
|
|
|
|
:organization_slug | [:organization, :slug]
|
|
|
|
end
|
|
|
|
|
|
|
|
with_them do
|
2019-03-29 10:53:40 -04:00
|
|
|
it do
|
|
|
|
expect(subject[0].public_send(sentry_project_object)).to(
|
|
|
|
eq(sentry_api_response[0].dig(*sentry_response))
|
|
|
|
)
|
|
|
|
end
|
2019-01-31 05:05:29 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'redirects' do
|
|
|
|
let(:sentry_api_url) { sentry_list_projects_url }
|
|
|
|
|
|
|
|
it_behaves_like 'no 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//' }
|
|
|
|
let(:client) { described_class.new(sentry_url, token) }
|
|
|
|
|
|
|
|
let!(:valid_req_stub) do
|
|
|
|
stub_sentry_request(sentry_list_projects_url)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'removes extra slashes in api url' do
|
|
|
|
expect(Gitlab::HTTP).to receive(:get).with(
|
|
|
|
URI(sentry_list_projects_url),
|
|
|
|
anything
|
|
|
|
).and_call_original
|
|
|
|
|
|
|
|
subject
|
2019-01-09 16:04:27 -05:00
|
|
|
|
|
|
|
expect(valid_req_stub).to have_been_requested
|
|
|
|
end
|
|
|
|
end
|
2019-04-07 03:51:36 -04:00
|
|
|
|
|
|
|
context 'when exception is raised' do
|
|
|
|
let(:sentry_request_url) { sentry_list_projects_url }
|
|
|
|
|
|
|
|
it_behaves_like 'maps exceptions'
|
|
|
|
end
|
2019-01-09 16:04:27 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def stub_sentry_request(url, body: {}, status: 200, headers: {})
|
2019-04-07 03:51:36 -04:00
|
|
|
stub_request(:get, url)
|
2019-01-09 16:04:27 -05:00
|
|
|
.to_return(
|
|
|
|
status: status,
|
|
|
|
headers: { 'Content-Type' => 'application/json' }.merge(headers),
|
|
|
|
body: body.to_json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|