From eea60433620819417a45838401340d4d5f2cf9a1 Mon Sep 17 00:00:00 2001 From: Jonathan Boler Date: Thu, 16 Aug 2018 16:26:01 -0500 Subject: [PATCH 1/3] Ensure user_attributes returns a Hash --- lib/airbrake_api/v3/notice_parser.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/airbrake_api/v3/notice_parser.rb b/lib/airbrake_api/v3/notice_parser.rb index d74361c50..bb316c2a0 100644 --- a/lib/airbrake_api/v3/notice_parser.rb +++ b/lib/airbrake_api/v3/notice_parser.rb @@ -69,7 +69,8 @@ def request end def user_attributes - return context['user'] if context['user'] + user = context['user'] + return user.is_a?(Hash) ? user : { user: user } if user { 'id' => context['userId'], From ffcc82d971dbbd31ca8bcec0b97196c44590c2e2 Mon Sep 17 00:00:00 2001 From: Jonathan Boler Date: Fri, 17 Aug 2018 17:08:32 -0500 Subject: [PATCH 2/3] Add user_attributes specs --- spec/lib/airbrake_api/v3/notice_parser_spec.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/spec/lib/airbrake_api/v3/notice_parser_spec.rb b/spec/lib/airbrake_api/v3/notice_parser_spec.rb index cfc8dd84d..a6a25c837 100644 --- a/spec/lib/airbrake_api/v3/notice_parser_spec.rb +++ b/spec/lib/airbrake_api/v3/notice_parser_spec.rb @@ -105,6 +105,20 @@ expect(parser.attributes[:server_environment]['hostname']).to eq('app01.infra.example.com') end + describe '#user_attributes' do + it 'returns a user context hash' do + user_hash = { id: 1, name: 'John Doe' } + parser = described_class.new({ 'context' => { 'user' => user_hash } }) + expect(parser.send(:user_attributes)).to eq(user_hash) + end + + it 'returns a hash for a user context string' do + user_string = '[Filtered]' + parser = described_class.new({ 'context' => { 'user' => user_string } }) + expect(parser.send(:user_attributes)).to eq({ user: user_string }) + end + end + def build_params_for(fixture, options = {}) json = Rails.root.join('spec', 'fixtures', fixture).read data = JSON.parse(json) From 38e1cd6e4d4c83e060021d6cb6fbb1b89a70ee50 Mon Sep 17 00:00:00 2001 From: Jonathan Boler Date: Fri, 17 Aug 2018 17:14:18 -0500 Subject: [PATCH 3/3] Fixes for rubocop --- spec/lib/airbrake_api/v3/notice_parser_spec.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/lib/airbrake_api/v3/notice_parser_spec.rb b/spec/lib/airbrake_api/v3/notice_parser_spec.rb index a6a25c837..cd903c906 100644 --- a/spec/lib/airbrake_api/v3/notice_parser_spec.rb +++ b/spec/lib/airbrake_api/v3/notice_parser_spec.rb @@ -108,14 +108,14 @@ describe '#user_attributes' do it 'returns a user context hash' do user_hash = { id: 1, name: 'John Doe' } - parser = described_class.new({ 'context' => { 'user' => user_hash } }) + parser = described_class.new('context' => { 'user' => user_hash }) expect(parser.send(:user_attributes)).to eq(user_hash) end it 'returns a hash for a user context string' do user_string = '[Filtered]' - parser = described_class.new({ 'context' => { 'user' => user_string } }) - expect(parser.send(:user_attributes)).to eq({ user: user_string }) + parser = described_class.new('context' => { 'user' => user_string }) + expect(parser.send(:user_attributes)).to eq(user: user_string) end end