|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | RSpec.describe CaptureError do |
| 4 | + describe '.rake_task_invocation?' do |
| 5 | + subject { described_class.rake_task_invocation? } |
| 6 | + |
| 7 | + context 'when Rake is not loaded' do |
| 8 | + before { hide_const('Rake') } |
| 9 | + |
| 10 | + it { is_expected.to be false } |
| 11 | + end |
| 12 | + |
| 13 | + context 'when Rake is loaded but no top-level tasks (puma, scheduler, console)' do |
| 14 | + before do |
| 15 | + allow(Rake.application).to receive(:top_level_tasks).and_return([]) |
| 16 | + end |
| 17 | + |
| 18 | + it { is_expected.to be false } |
| 19 | + end |
| 20 | + |
| 21 | + context 'when Rake is executing a top-level task' do |
| 22 | + before do |
| 23 | + allow(Rake.application).to receive(:top_level_tasks).and_return(['db:migrate']) |
| 24 | + end |
| 25 | + |
| 26 | + it { is_expected.to be true } |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe '#build_before_send (private)' do |
| 31 | + subject(:before_send) { described_class.send(:build_before_send) } |
| 32 | + |
| 33 | + let(:configuration) do |
| 34 | + config = Sentry::Configuration.new |
| 35 | + config.dsn = 'http://public@example.invalid/1' |
| 36 | + config |
| 37 | + end |
| 38 | + let(:event) { Sentry::ErrorEvent.new(configuration: configuration) } |
| 39 | + |
| 40 | + before do |
| 41 | + allow(Rails.application.config).to receive(:filter_parameters).and_return([:password]) |
| 42 | + end |
| 43 | + |
| 44 | + it 'should return the same Sentry::ErrorEvent object (not a Hash)' do |
| 45 | + result = before_send.call(event) |
| 46 | + expect(result).to be(event) |
| 47 | + expect(result).to be_a(Sentry::ErrorEvent) |
| 48 | + end |
| 49 | + |
| 50 | + it 'should mask filter_parameters keys in event.extra and keep other keys' do |
| 51 | + event.extra = { password: 'secret', visible: 'ok' } |
| 52 | + result = before_send.call(event) |
| 53 | + expect(result.extra).to eq(password: '[FILTERED]', visible: 'ok') |
| 54 | + end |
| 55 | + |
| 56 | + it 'should mask Authorization in event.tags' do |
| 57 | + event.tags = { 'Authorization' => 'Bearer abc', 'probe' => 'on' } |
| 58 | + result = before_send.call(event) |
| 59 | + expect(result.tags).to eq('Authorization' => '[FILTERED]', 'probe' => 'on') |
| 60 | + end |
| 61 | + |
| 62 | + it 'should mask filter_parameters keys in event.user and event.contexts' do |
| 63 | + event.user = { id: 1, password: 'pw' } |
| 64 | + event.contexts = { extra_ctx: { password: 'pw2', note: 'visible' } } |
| 65 | + result = before_send.call(event) |
| 66 | + expect(result.user).to eq(id: 1, password: '[FILTERED]') |
| 67 | + expect(result.contexts).to eq(extra_ctx: { password: '[FILTERED]', note: 'visible' }) |
| 68 | + end |
| 69 | + |
| 70 | + it 'should mask filter_parameters keys and Authorization in event.request fields' do |
| 71 | + request = Sentry::RequestInterface.allocate |
| 72 | + request.data = { password: 'pw', visible: 'ok' } |
| 73 | + request.headers = { 'Authorization' => 'Bearer xyz', 'X-Trace' => 't1' } |
| 74 | + request.env = { 'HTTP_AUTHORIZATION' => 'Bearer xyz', 'REMOTE_ADDR' => '127.0.0.1' } |
| 75 | + event.instance_variable_set(:@request, request) |
| 76 | + |
| 77 | + result = before_send.call(event) |
| 78 | + expect(result.request.data).to eq(password: '[FILTERED]', visible: 'ok') |
| 79 | + expect(result.request.headers).to eq('Authorization' => '[FILTERED]', 'X-Trace' => 't1') |
| 80 | + expect(result.request.env).to eq('HTTP_AUTHORIZATION' => '[FILTERED]', 'REMOTE_ADDR' => '127.0.0.1') |
| 81 | + end |
| 82 | + |
| 83 | + it 'should not raise when event.request is nil' do |
| 84 | + event.extra = { password: 'pw' } |
| 85 | + expect { before_send.call(event) }.not_to raise_error |
| 86 | + end |
| 87 | + |
| 88 | + it 'should not raise when event fields are nil' do |
| 89 | + event.extra = nil |
| 90 | + event.tags = nil |
| 91 | + event.user = nil |
| 92 | + event.contexts = nil |
| 93 | + expect { before_send.call(event) }.not_to raise_error |
| 94 | + end |
| 95 | + end |
| 96 | + |
4 | 97 | describe '.with_clean_context' do |
5 | 98 | subject do |
6 | 99 | described_class.with_clean_context(context) { block_result } |
|
0 commit comments