Commit 1875242904f9d6923a5e8f37909bd8aab2673d1a

Authored by Stephen Crosby
2 parents faab6964 d5c07b65
Exists in master

Merge pull request #1027 from svoop/new_user_payload

Add support for new user payload
lib/airbrake_api/v3/notice_parser.rb
... ... @@ -69,8 +69,14 @@ module AirbrakeApi
69 69 end
70 70  
71 71 def user_attributes
72   - hash = context.slice('userId', 'userUsername', 'userName', 'userEmail')
73   - Hash[hash.map { |key, value| [key.sub(/^user/, ''), value] }]
  72 + return context['user'] if context['user']
  73 +
  74 + {
  75 + 'id' => context['userId'],
  76 + 'name' => context['userName'],
  77 + 'email' => context['userEmail'],
  78 + 'username' => context['userUsername']
  79 + }.compact
74 80 end
75 81  
76 82 def url
... ...
spec/fixtures/api_v3_request.json
... ... @@ -22,10 +22,12 @@
22 22 "sourceMapEnabled":true,
23 23 "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36",
24 24 "url":"http://localhost:3000/kontakt",
25   - "userId":1,"userUsername":"john",
26   - "userName":"John Doe",
27   - "userUsername": "john",
28   - "userEmail":"john.doe@example.org",
  25 + "user": {
  26 + "id":1,"userUsername":"john",
  27 + "name":"John Doe",
  28 + "username": "john",
  29 + "email":"john.doe@example.org"
  30 + },
29 31 "version":"1.0",
30 32 "component":"ContactsController",
31 33 "action":"show"
... ...
spec/fixtures/api_v3_request_with_deprecated_user_keys.json 0 → 100644
... ... @@ -0,0 +1,36 @@
  1 +{
  2 + "notifier":{"name":"airbrake-js-v8","version":"0.3.10","url":"https://github.com/airbrake/airbrake-js"},
  3 + "errors":[
  4 + {
  5 + "type":"Error",
  6 + "message":"Error: TestError",
  7 + "backtrace":[
  8 + {"function":"d","file":"http://localhost:3000/assets/application.js","line":11234,"column":24},
  9 + {"function":"c","file":"http://localhost:3000/assets/application.js","line":11233,"column":18},
  10 + {"function":"b","file":"http://localhost:3000/assets/application.js","line":11232,"column":18},
  11 + {"function":"a","file":"http://localhost:3000/assets/application.js","line":11231,"column":18},
  12 + {"function":"HTMLDocument.<anonymous>","file":"http://localhost:3000/assets/application.js","line":11236,"column":3},
  13 + {"function":"fire","file":"http://localhost:3000/assets/application.js","line":1018,"column":34},
  14 + {"function":"Object.self.fireWith [as resolveWith]","file":"http://localhost:3000/assets/application.js","line":1128,"column":13},
  15 + {"function":"Function.jQuery.extend.ready","file":"http://localhost:3000/assets/application.js","line":417,"column":15},
  16 + {"function":"HTMLDocument.DOMContentLoaded","file":"http://localhost:3000/assets/application.js","line":93,"column":14}
  17 + ]
  18 + }
  19 + ],
  20 + "context":{
  21 + "language":"JavaScript",
  22 + "sourceMapEnabled":true,
  23 + "userAgent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36",
  24 + "url":"http://localhost:3000/kontakt",
  25 + "userId":1,
  26 + "userName":"John Doe",
  27 + "userUsername": "john",
  28 + "userEmail":"john.doe@example.org",
  29 + "version":"1.0",
  30 + "component":"ContactsController",
  31 + "action":"show"
  32 + },
  33 + "params":{"returnTo":"dashboard"},
  34 + "environment":{"navigator_vendor":"Google Inc."},
  35 + "session":{"isAdmin":true}
  36 +}
... ...
spec/lib/airbrake_api/v3/notice_parser_spec.rb
... ... @@ -19,7 +19,7 @@ describe AirbrakeApi::V3::NoticeParser do
19 19 end
20 20  
21 21 it 'parses JSON payload and returns ErrorReport' do
22   - params = build_params(key: app.api_key)
  22 + params = build_params_for('api_v3_request.json', key: app.api_key)
23 23  
24 24 report = described_class.new(params).report
25 25 notice = report.generate_notice!
... ... @@ -28,10 +28,10 @@ describe AirbrakeApi::V3::NoticeParser do
28 28 expect(report.message).to eq('Error: TestError')
29 29 expect(report.backtrace.lines.size).to eq(9)
30 30 expect(notice.user_attributes).to include(
31   - 'Id' => 1,
32   - 'Name' => 'John Doe',
33   - 'Email' => 'john.doe@example.org',
34   - 'Username' => 'john'
  31 + 'id' => 1,
  32 + 'name' => 'John Doe',
  33 + 'email' => 'john.doe@example.org',
  34 + 'username' => 'john'
35 35 )
36 36 expect(notice.session).to include('isAdmin' => true)
37 37 expect(notice.params).to include('returnTo' => 'dashboard')
... ... @@ -42,7 +42,7 @@ describe AirbrakeApi::V3::NoticeParser do
42 42 end
43 43  
44 44 it 'parses JSON payload when api_key is missing but project_id is present' do
45   - params = build_params(key: nil, project_id: app.api_key)
  45 + params = build_params_for('api_v3_request.json', key: nil, project_id: app.api_key)
46 46  
47 47 report = described_class.new(params).report
48 48 expect(report).to be_valid
... ... @@ -61,6 +61,20 @@ describe AirbrakeApi::V3::NoticeParser do
61 61 expect(report.backtrace.lines.size).to eq(0)
62 62 end
63 63  
  64 + it 'parses JSON payload with deprecated user keys' do
  65 + params = build_params_for('api_v3_request_with_deprecated_user_keys.json', key: app.api_key)
  66 +
  67 + report = AirbrakeApi::V3::NoticeParser.new(params).report
  68 + notice = report.generate_notice!
  69 +
  70 + expect(notice.user_attributes).to include(
  71 + 'id' => 1,
  72 + 'name' => 'John Doe',
  73 + 'email' => 'john.doe@example.org',
  74 + 'username' => 'john'
  75 + )
  76 + end
  77 +
64 78 it 'takes the notifier from root' do
65 79 parser = described_class.new(
66 80 'errors' => ['MyError'],
... ... @@ -77,8 +91,8 @@ describe AirbrakeApi::V3::NoticeParser do
77 91 expect(parser.attributes[:notifier]).to eq(notifier_params)
78 92 end
79 93  
80   - def build_params(options = {})
81   - json = Rails.root.join('spec', 'fixtures', 'api_v3_request.json').read
  94 + def build_params_for(fixture, options = {})
  95 + json = Rails.root.join('spec', 'fixtures', fixture).read
82 96 data = JSON.parse(json)
83 97  
84 98 data['key'] = options[:key] if options.key?(:key)
... ...