Commit 8fb38237 authored by Mehmet Emin INAC's avatar Mehmet Emin INAC

Refactor `Gitlab::ErrorTracking.track_exception specs

parent b9590f39
...@@ -198,47 +198,39 @@ RSpec.describe Gitlab::ErrorTracking do ...@@ -198,47 +198,39 @@ RSpec.describe Gitlab::ErrorTracking do
end end
describe '.track_exception' do describe '.track_exception' do
it 'calls Raven.capture_exception' do let(:extra) { { issue_url: issue_url, some_other_info: 'info' } }
expected_extras = {
some_other_info: 'info',
issue_url: issue_url
}
expected_tags = { subject(:track_exception) { described_class.track_exception(exception, extra) }
correlation_id: 'cid'
}
expect(Raven).to receive(:capture_exception) before do
.with(exception, allow(Raven).to receive(:capture_exception).and_call_original
tags: a_hash_including(expected_tags), allow(Gitlab::ErrorTracking::Logger).to receive(:error)
extra: a_hash_including(expected_extras)) end
described_class.track_exception( it 'calls Raven.capture_exception' do
exception, track_exception
issue_url: issue_url,
some_other_info: 'info' expect(Raven).to have_received(:capture_exception)
) .with(exception,
tags: a_hash_including(correlation_id: 'cid'),
extra: a_hash_including(some_other_info: 'info', issue_url: issue_url))
end end
it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do it 'calls Gitlab::ErrorTracking::Logger.error with formatted payload' do
expect(Gitlab::ErrorTracking::Logger).to receive(:error) track_exception
.with(a_hash_including(*expected_payload_includes))
described_class.track_exception( expect(Gitlab::ErrorTracking::Logger).to have_received(:error)
exception, .with(a_hash_including(*expected_payload_includes))
issue_url: issue_url,
some_other_info: 'info'
)
end end
context 'with filterable parameters' do context 'with filterable parameters' do
let(:extra) { { test: 1, my_token: 'test' } } let(:extra) { { test: 1, my_token: 'test' } }
it 'filters parameters' do it 'filters parameters' do
expect(Gitlab::ErrorTracking::Logger).to receive(:error).with( track_exception
hash_including({ 'extra.test' => 1, 'extra.my_token' => '[FILTERED]' }))
described_class.track_exception(exception, extra) expect(Gitlab::ErrorTracking::Logger).to have_received(:error)
.with(hash_including({ 'extra.test' => 1, 'extra.my_token' => '[FILTERED]' }))
end end
end end
...@@ -247,56 +239,55 @@ RSpec.describe Gitlab::ErrorTracking do ...@@ -247,56 +239,55 @@ RSpec.describe Gitlab::ErrorTracking do
let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info, backtrace: caller) } let(:exception) { double(message: 'bang!', sentry_extra_data: extra_info, backtrace: caller) }
it 'includes the extra data from the exception in the tracking information' do it 'includes the extra data from the exception in the tracking information' do
expect(Raven).to receive(:capture_exception) track_exception
.with(exception, a_hash_including(extra: a_hash_including(extra_info)))
described_class.track_exception(exception) expect(Raven).to have_received(:capture_exception)
.with(exception, a_hash_including(extra: a_hash_including(extra_info)))
end end
end end
context 'the exception implements :sentry_extra_data, which returns nil' do context 'the exception implements :sentry_extra_data, which returns nil' do
let(:exception) { double(message: 'bang!', sentry_extra_data: nil, backtrace: caller) } let(:exception) { double(message: 'bang!', sentry_extra_data: nil, backtrace: caller) }
let(:extra) { { issue_url: issue_url } }
it 'just includes the other extra info' do it 'just includes the other extra info' do
extra_info = { issue_url: issue_url } track_exception
expect(Raven).to receive(:capture_exception)
.with(exception, a_hash_including(extra: a_hash_including(extra_info)))
described_class.track_exception(exception, extra_info) expect(Raven).to have_received(:capture_exception)
.with(exception, a_hash_including(extra: a_hash_including(extra)))
end end
end end
context 'with sidekiq args' do context 'with sidekiq args' do
it 'ensures extra.sidekiq.args is a string' do context 'when the args does not have anything sensitive' do
extra = { sidekiq: { 'class' => 'PostReceive', 'args' => [1, { 'id' => 2, 'name' => 'hello' }, 'some-value', 'another-value'] } } let(:extra) { { sidekiq: { 'class' => 'PostReceive', 'args' => [1, { 'id' => 2, 'name' => 'hello' }, 'some-value', 'another-value'] } } }
expect(Gitlab::ErrorTracking::Logger).to receive(:error).with( it 'ensures extra.sidekiq.args is a string' do
hash_including({ 'extra.sidekiq' => { 'class' => 'PostReceive', 'args' => ['1', '{"id"=>2, "name"=>"hello"}', 'some-value', 'another-value'] } })) track_exception
described_class.track_exception(exception, extra) expect(Gitlab::ErrorTracking::Logger).to have_received(:error).with(
hash_including({ 'extra.sidekiq' => { 'class' => 'PostReceive', 'args' => ['1', '{"id"=>2, "name"=>"hello"}', 'some-value', 'another-value'] } }))
end
end end
it 'filters sensitive arguments before sending' do context 'when the args has sensitive information' do
extra = { sidekiq: { 'class' => 'UnknownWorker', 'args' => ['sensitive string', 1, 2] } } let(:extra) { { sidekiq: { 'class' => 'UnknownWorker', 'args' => ['sensitive string', 1, 2] } } }
expect(Gitlab::ErrorTracking::Logger).to receive(:error).with(
hash_including('extra.sidekiq' => { 'class' => 'UnknownWorker', 'args' => ['[FILTERED]', '1', '2'] }))
described_class.track_exception(exception, extra) it 'filters sensitive arguments before sending' do
track_exception
expect(sentry_event.dig('extra', 'sidekiq', 'args')).to eq(['[FILTERED]', 1, 2]) expect(sentry_event.dig('extra', 'sidekiq', 'args')).to eq(['[FILTERED]', 1, 2])
expect(Gitlab::ErrorTracking::Logger).to have_received(:error).with(
hash_including('extra.sidekiq' => { 'class' => 'UnknownWorker', 'args' => ['[FILTERED]', '1', '2'] }))
end
end end
end end
context 'when the error is kind of an `ActiveRecord::StatementInvalid`' do context 'when the error is kind of an `ActiveRecord::StatementInvalid`' do
let(:exception) { ActiveRecord::StatementInvalid.new(sql: :foo) } let(:exception) { ActiveRecord::StatementInvalid.new(sql: :foo) }
before do
allow(Raven).to receive(:capture_exception)
end
it 'injects the sql query into extra' do it 'injects the sql query into extra' do
described_class.track_exception(exception) track_exception
expect(Raven).to have_received(:capture_exception) expect(Raven).to have_received(:capture_exception)
.with(exception, a_hash_including(extra: a_hash_including(sql: :foo))) .with(exception, a_hash_including(extra: a_hash_including(sql: :foo)))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment