dfg-airbrake 5.6.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (37) hide show
  1. checksums.yaml +7 -0
  2. data/lib/airbrake.rb +57 -0
  3. data/lib/airbrake/capistrano/tasks.rb +65 -0
  4. data/lib/airbrake/delayed_job/plugin1.rb +52 -0
  5. data/lib/airbrake/rack/middleware.rb +59 -0
  6. data/lib/airbrake/rack/notice_builder.rb +125 -0
  7. data/lib/airbrake/rack/user.rb +61 -0
  8. data/lib/airbrake/rails/action_controller.rb +37 -0
  9. data/lib/airbrake/rails/active_job.rb +35 -0
  10. data/lib/airbrake/rails/active_record.rb +36 -0
  11. data/lib/airbrake/rails/railtie.rb +79 -0
  12. data/lib/airbrake/rake/task_ext.rb +66 -0
  13. data/lib/airbrake/rake/tasks.rb +118 -0
  14. data/lib/airbrake/resque/failure.rb +19 -0
  15. data/lib/airbrake/sidekiq/error_handler.rb +35 -0
  16. data/lib/airbrake/version.rb +6 -0
  17. data/lib/generators/airbrake_generator.rb +25 -0
  18. data/lib/generators/airbrake_initializer.rb.erb +68 -0
  19. data/spec/airbrake_spec.rb +17 -0
  20. data/spec/apps/rack/dummy_app.rb +17 -0
  21. data/spec/apps/rails/dummy_app.rb +156 -0
  22. data/spec/apps/rails/dummy_task.rake +20 -0
  23. data/spec/apps/sinatra/composite_app/sinatra_app1.rb +11 -0
  24. data/spec/apps/sinatra/composite_app/sinatra_app2.rb +11 -0
  25. data/spec/apps/sinatra/dummy_app.rb +12 -0
  26. data/spec/integration/rack/rack_spec.rb +17 -0
  27. data/spec/integration/rails/rails_spec.rb +216 -0
  28. data/spec/integration/rails/rake_spec.rb +160 -0
  29. data/spec/integration/shared_examples/rack_examples.rb +126 -0
  30. data/spec/integration/sinatra/sinatra_spec.rb +77 -0
  31. data/spec/spec_helper.rb +116 -0
  32. data/spec/unit/rack/middleware_spec.rb +136 -0
  33. data/spec/unit/rack/notice_builder_spec.rb +157 -0
  34. data/spec/unit/rack/user_spec.rb +172 -0
  35. data/spec/unit/rake/tasks_spec.rb +67 -0
  36. data/spec/unit/sidekiq/error_handler_spec.rb +33 -0
  37. metadata +247 -0
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe "Rake integration" do
4
+ let(:endpoint) do
5
+ 'https://airbrake.io/api/v3/projects/113743/notices?key=fd04e13d806a90f96614ad8e529b2822'
6
+ end
7
+
8
+ def wait_for_a_request_with_body(body)
9
+ wait_for(a_request(:post, endpoint).with(body: body)).to have_been_made.once
10
+ end
11
+
12
+ def expect_no_requests_with_body(body)
13
+ sleep 1
14
+ expect(a_request(:post, endpoint).with(body: body)).not_to have_been_made
15
+ end
16
+
17
+ before do
18
+ Rails.application.load_tasks
19
+ stub_request(:post, endpoint).to_return(status: 201, body: '{}')
20
+ expect { faulty_task.invoke }.to raise_error(AirbrakeTestError)
21
+ end
22
+
23
+ after do
24
+ # Rake ensures that each task is executed only once per session. For testing
25
+ # purposes, we run the task multiple times.
26
+ faulty_task.reenable
27
+ end
28
+
29
+ describe "a task with maximum information, which raises an exception" do
30
+ let(:faulty_task) { Rake::Task['bingo:bango'] }
31
+
32
+ it "sends the exception to Airbrake" do
33
+ wait_for_a_request_with_body(/"errors":\[{"type":"AirbrakeTestError"/)
34
+ end
35
+
36
+ describe "contains the context payload, which" do
37
+ it "includes correct component" do
38
+ wait_for_a_request_with_body(/"context":{.*"component":"rake".*}/)
39
+ end
40
+
41
+ it "includes correct action" do
42
+ wait_for_a_request_with_body(
43
+ /"context":{.*"action":"bingo:bango".*/
44
+ )
45
+ end
46
+ end
47
+
48
+ describe "contains the params payload, which" do
49
+ it "includes a task name" do
50
+ wait_for_a_request_with_body(
51
+ /"params":{.*"rake_task":{.*"name":"bingo:bango".*}.*}/
52
+ )
53
+ end
54
+
55
+ it "includes a timestamp" do
56
+ wait_for_a_request_with_body(
57
+ /"params":{.*"rake_task":{.*"timestamp":"201\d\-\d\d-\d\d.+".*}.*}/
58
+ )
59
+ end
60
+
61
+ it "includes investigation" do
62
+ # rubocop:disable Metrics/LineLength
63
+ wait_for_a_request_with_body(
64
+ /"params":{.*"rake_task":{.*"investigation":".+Investigating bingo:bango.+".*}.*}/
65
+ )
66
+ # rubocop:enable Metrics/LineLength
67
+ end
68
+
69
+ it "includes full comment" do
70
+ wait_for_a_request_with_body(
71
+ /"params":{.*"rake_task":{.*"full_comment":"Dummy description".*}.*}/
72
+ )
73
+ end
74
+
75
+ it "includes arg names" do
76
+ wait_for_a_request_with_body(
77
+ /"params":{.*"rake_task":{.*"arg_names":\["dummy_arg"\].*}.*}/
78
+ )
79
+ end
80
+
81
+ it "includes arg description" do
82
+ wait_for_a_request_with_body(
83
+ /"params":{.*"rake_task":{.*"arg_description":"\[dummy_arg\]".*}.*}/
84
+ )
85
+ end
86
+
87
+ it "includes locations" do
88
+ # rubocop:disable Metrics/LineLength
89
+ wait_for_a_request_with_body(
90
+ %r("params":{.*"rake_task":{.*"locations":\[".+spec/apps/rails/dummy_task.rake:\d+:in.+"\].*}.*})
91
+ )
92
+ # rubocop:enable Metrics/LineLength
93
+ end
94
+
95
+ it "includes sources" do
96
+ wait_for_a_request_with_body(
97
+ /"params":{.*"rake_task":{.*"sources":\["environment"\].*}.*}/
98
+ )
99
+ end
100
+
101
+ it "includes prerequisite tasks" do
102
+ # rubocop:disable Metrics/LineLength
103
+ wait_for_a_request_with_body(
104
+ /"params":{.*"rake_task":{.*"prerequisite_tasks":\[{"name":"bingo:environment".+\].*}.*}/
105
+ )
106
+ # rubocop:enable Metrics/LineLength
107
+ end
108
+
109
+ it "includes argv info" do
110
+ wait_for_a_request_with_body(
111
+ %r("params":{.*"argv":"--pattern spec/integration/rails/\*_spec.rb".*})
112
+ )
113
+ end
114
+
115
+ it "includes #execute args" do
116
+ wait_for_a_request_with_body(
117
+ /"params":{.*"execute_args":\[\].*}/
118
+ )
119
+ end
120
+ end
121
+ end
122
+
123
+ describe "a task with minimum information, which raises an exception" do
124
+ let(:faulty_task) { Rake::Task['bingo:bongo'] }
125
+
126
+ describe "doesn't contain in the params payload" do
127
+ it "full comment" do
128
+ expect_no_requests_with_body(
129
+ /"params":{.*"rake_task":{.*"full_comment":"Dummy description".*}.*}/
130
+ )
131
+ end
132
+
133
+ it "arg names" do
134
+ expect_no_requests_with_body(
135
+ /"params":{.*"rake_task":{.*"arg_names":\["dummy_arg"\].*}.*}/
136
+ )
137
+ end
138
+
139
+ it "arg description" do
140
+ expect_no_requests_with_body(
141
+ /"params":{.*"rake_task":{.*"arg_description":"\[dummy_arg\]".*}.*}/
142
+ )
143
+ end
144
+
145
+ it "sources" do
146
+ expect_no_requests_with_body(
147
+ /"params":{.*"rake_task":{.*"sources":\["environment"\].*}.*}/
148
+ )
149
+ end
150
+
151
+ it "prerequisite tasks" do
152
+ # rubocop:disable Metrics/LineLength
153
+ expect_no_requests_with_body(
154
+ /"params":{.*"rake_task":{.*"prerequisite_tasks":\[{"name":"bingo:environment".+\].*}.*}/
155
+ )
156
+ # rubocop:enable Metrics/LineLength
157
+ end
158
+ end
159
+ end
160
+ end
@@ -0,0 +1,126 @@
1
+ RSpec.shared_examples 'rack examples' do
2
+ include Warden::Test::Helpers
3
+
4
+ after { Warden.test_reset! }
5
+
6
+ let(:endpoint) do
7
+ 'https://airbrake.io/api/v3/projects/113743/notices?key=fd04e13d806a90f96614ad8e529b2822'
8
+ end
9
+
10
+ def wait_for_a_request_with_body(body)
11
+ wait_for(a_request(:post, endpoint).with(body: body)).to have_been_made.once
12
+ end
13
+
14
+ before do
15
+ stub_request(:post, endpoint).to_return(status: 201, body: '{}')
16
+ end
17
+
18
+ describe "application routes" do
19
+ describe "/index" do
20
+ it "successfully returns 200 and body" do
21
+ get '/'
22
+
23
+ expect(last_response.status).to eq(200)
24
+ expect(last_response.body).to eq('Hello from index')
25
+
26
+ wait_for(a_request(:post, endpoint)).not_to have_been_made
27
+ end
28
+ end
29
+
30
+ describe "/crash" do
31
+ it "returns 500 and sends a notice to Airbrake" do
32
+ get '/crash'
33
+
34
+ expect(last_response.status).to eq(500)
35
+ wait_for_a_request_with_body(/"errors":\[{"type":"AirbrakeTestError"/)
36
+ end
37
+ end
38
+ end
39
+
40
+ describe "context payload" do
41
+ context "when the user is present" do
42
+ let(:common_user_params) do
43
+ { id: 1, email: 'qa@example.com', username: 'qa-dept' }
44
+ end
45
+
46
+ before do
47
+ login_as(OpenStruct.new(user_params))
48
+ get '/crash'
49
+ end
50
+
51
+ context "when the user has first and last names" do
52
+ let(:user_params) do
53
+ common_user_params.merge(first_name: 'Bingo', last_name: 'Bongo')
54
+ end
55
+
56
+ it "reports the user's first and last names" do
57
+ wait_for_a_request_with_body(/
58
+ "context":{.*
59
+ "user":{
60
+ "id":"1",
61
+ "name":"Bingo\sBongo",
62
+ "username":"qa-dept",
63
+ "email":"qa@example.com"}
64
+ /x)
65
+ end
66
+ end
67
+
68
+ context "when the user has only name" do
69
+ let(:user_params) do
70
+ common_user_params.merge(name: 'Bingo')
71
+ end
72
+
73
+ it "reports the user's name" do
74
+ wait_for_a_request_with_body(/
75
+ "context":{.*
76
+ "user":{
77
+ "id":"1",
78
+ "name":"Bingo",
79
+ "username":"qa-dept",
80
+ "email":"qa@example.com"}
81
+ /x)
82
+ end
83
+ end
84
+ end
85
+
86
+ context "when additional parameters present" do
87
+ before do
88
+ get '/crash', nil, 'HTTP_USER_AGENT' => 'Bot', 'HTTP_REFERER' => 'bingo.com'
89
+ end
90
+
91
+ it "features url" do
92
+ wait_for_a_request_with_body(
93
+ %r("context":{.*"url":"http://example\.org/crash".*})
94
+ )
95
+ end
96
+
97
+ it "features hostname" do
98
+ wait_for_a_request_with_body(/"context":{.*"hostname":".+".*}/)
99
+ end
100
+
101
+ it "features userAgent" do
102
+ wait_for_a_request_with_body(/"context":{.*"userAgent":"Bot".*}/)
103
+ end
104
+ end
105
+ end
106
+
107
+ describe "environment payload" do
108
+ before do
109
+ get '/crash', nil, 'HTTP_REFERER' => 'bingo.com'
110
+ end
111
+
112
+ it "features referer" do
113
+ wait_for_a_request_with_body(/"environment":{.*"referer":"bingo.com".*}/)
114
+ end
115
+
116
+ it "contains HTTP headers" do
117
+ wait_for_a_request_with_body(
118
+ /"environment":{.*"headers":{.*"CONTENT_LENGTH":"0".*}/
119
+ )
120
+ end
121
+
122
+ it "contains HTTP method" do
123
+ wait_for_a_request_with_body(/"environment":{.*"httpMethod":"GET".*}/)
124
+ end
125
+ end
126
+ end
@@ -0,0 +1,77 @@
1
+ require 'sinatra'
2
+ require 'spec_helper'
3
+
4
+ require 'apps/sinatra/dummy_app'
5
+ require 'apps/sinatra/composite_app/sinatra_app1'
6
+ require 'apps/sinatra/composite_app/sinatra_app2'
7
+
8
+ require 'integration/shared_examples/rack_examples'
9
+
10
+ RSpec.describe "Sinatra integration specs" do
11
+ let(:app) { DummyApp }
12
+
13
+ include_examples 'rack examples'
14
+
15
+ describe "context payload" do
16
+ it "includes version" do
17
+ get '/crash'
18
+ wait_for_a_request_with_body(/"context":{.*"version":"1.2.3 Sinatra/)
19
+ end
20
+ end
21
+
22
+ context "when multiple apps are mounted" do
23
+ let(:endpoint1) do
24
+ 'https://airbrake.io/api/v3/projects/113743/notices?key=fd04e13d806a90f96614ad8e529b2822'
25
+ end
26
+
27
+ let(:endpoint2) do
28
+ 'https://airbrake.io/api/v3/projects/99123/notices?key=ad04e13d806a90f96614ad8e529b2821'
29
+ end
30
+
31
+ def env_for(url, opts = {})
32
+ Rack::MockRequest.env_for(url, opts)
33
+ end
34
+
35
+ before do
36
+ stub_request(:post, endpoint1).to_return(status: 201, body: '{}')
37
+ stub_request(:post, endpoint2).to_return(status: 201, body: '{}')
38
+ end
39
+
40
+ context "and when both apps use their own notifiers and middlewares" do
41
+ let(:app) do
42
+ Rack::Builder.new do
43
+ map('/app1') do
44
+ use Airbrake::Rack::Middleware, SinatraApp1
45
+ run SinatraApp1.new
46
+ end
47
+
48
+ map '/app2' do
49
+ use Airbrake::Rack::Middleware, SinatraApp2
50
+ run SinatraApp2.new
51
+ end
52
+ end
53
+ end
54
+
55
+ it "reports errors from SinatraApp1 notifier" do
56
+ get '/app1'
57
+
58
+ body = %r|"backtrace":\[{"file":".+apps/sinatra/composite_app/sinatra_app1.rb"|
59
+
60
+ wait_for(
61
+ a_request(:post, endpoint1).
62
+ with(body: body)
63
+ ).to have_been_made.once
64
+ end
65
+
66
+ it "reports errors from SinatraApp2 notifier" do
67
+ get '/app2'
68
+
69
+ body = %r|"backtrace":\[{"file":".+apps/sinatra/composite_app/sinatra_app2.rb"|
70
+ wait_for(
71
+ a_request(:post, endpoint2).
72
+ with(body: body)
73
+ ).to have_been_made.once
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,116 @@
1
+ # Gems from the gemspec.
2
+ require 'webmock'
3
+ require 'webmock/rspec'
4
+ require 'rspec/wait'
5
+ require 'pry'
6
+ require 'rack'
7
+ require 'rack/test'
8
+ require 'rake'
9
+
10
+ require 'airbrake'
11
+ require 'airbrake/rake/tasks'
12
+
13
+ # Load integration tests only when they're run through appraisals.
14
+ if ENV['APPRAISAL_INITIALIZED']
15
+ # Gems from appraisals that every application uses.
16
+ require 'warden'
17
+
18
+ # Load a Rails app or skip.
19
+ begin
20
+ ENV['RAILS_ENV'] = 'test'
21
+
22
+ if RUBY_ENGINE == 'jruby'
23
+ require 'activerecord-jdbcsqlite3-adapter'
24
+ else
25
+ require 'sqlite3'
26
+ end
27
+
28
+ require 'rails'
29
+
30
+ rails_vsn = Gem::Version.new(Rails.version)
31
+
32
+ ENV['DATABASE_URL'] = if rails_vsn <= Gem::Version.new('4.2')
33
+ 'sqlite3:///:memory:'
34
+ else
35
+ 'sqlite3::memory:'
36
+ end
37
+
38
+ require 'action_controller'
39
+ require 'action_view'
40
+ require 'action_view/testing/resolvers'
41
+ require 'active_record/railtie'
42
+ if rails_vsn >= Gem::Version.new('4.2')
43
+ require 'active_job'
44
+
45
+ # Silence logger.
46
+ ActiveJob::Base.logger.level = 99
47
+ end
48
+
49
+ require 'resque'
50
+ require 'resque_spec'
51
+ require 'airbrake/resque/failure'
52
+ Resque::Failure.backend = Resque::Failure::Airbrake
53
+
54
+ require 'delayed_job'
55
+ require 'delayed_job_active_record'
56
+ # 2016-12-19
57
+ # https://meta.discourse.org/t/54462
58
+ # «The Plugin::Instance.find_all method incorrectly treats every file with the «plugin.rb» name
59
+ # as a Discourse plugin».
60
+ require 'airbrake/delayed_job/plugin1'
61
+ Delayed::Worker.delay_jobs = false
62
+
63
+ require 'airbrake/rails/railtie'
64
+
65
+ load 'apps/rails/dummy_task.rake'
66
+ require 'apps/rails/dummy_app'
67
+ rescue LoadError
68
+ puts '** Skipped Rails specs'
69
+ end
70
+
71
+ # Load a Rack app or skip.
72
+ begin
73
+ # Don't load the Rack app since we want to test Sinatra if it's loaded.
74
+ raise LoadError if defined?(Sinatra)
75
+
76
+ require 'apps/rack/dummy_app'
77
+ rescue LoadError
78
+ puts '** Skipped Rack specs'
79
+ end
80
+ end
81
+
82
+ RSpec.configure do |c|
83
+ c.order = 'random'
84
+ c.color = true
85
+ c.disable_monkey_patching!
86
+ c.wait_timeout = 3
87
+
88
+ c.include Rack::Test::Methods
89
+ end
90
+
91
+ Airbrake.configure do |c|
92
+ c.project_id = 113743
93
+ c.project_key = 'fd04e13d806a90f96614ad8e529b2822'
94
+ c.logger = Logger.new('/dev/null')
95
+ c.app_version = '1.2.3'
96
+ c.workers = 5
97
+ end
98
+
99
+ # Make sure tests that use async requests fail.
100
+ Thread.abort_on_exception = true
101
+
102
+ AirbrakeTestError = Class.new(StandardError)
103
+
104
+ # Print header with versions information. This simplifies debugging of build
105
+ # failures on CircleCI.
106
+ versions = <<EOS
107
+ #{'#' * 80}
108
+ # RUBY_VERSION: #{RUBY_VERSION}
109
+ # RUBY_ENGINE: #{RUBY_ENGINE}
110
+ EOS
111
+ versions << "# JRUBY_VERSION #{JRUBY_VERSION}\n" if defined?(JRUBY_VERSION)
112
+ versions << "# Rails version: #{Rails.version}\n" if defined?(Rails)
113
+ versions << "# Rack release: #{Rack.release}\n"
114
+ versions << '#' * 80
115
+
116
+ puts versions