Skip to content

Commit e6dcde3

Browse files
authored
Testing coverage improvement (#410)
* Added unit tests for middleware stack * Added rack middleware test * Moved integration specs into folder, added rake spec * Added warden_user_spec * Added clearance user middleware tests * Moved rake middleware test into rake integration spec * Added unset request data test * Added shoryuken spec * Removed rack requirement * Fix issue with cal;led function * Added mailman spec * Added resque integration spec * Updated mailman spec * Added que tests
1 parent 5247fdc commit e6dcde3

12 files changed

Lines changed: 769 additions & 86 deletions

spec/integration_spec.rb

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,6 @@
2323

2424
let(:request) { JSON.parse(queue.pop) }
2525

26-
it 'should run the rake middleware when rake tasks crash' do
27-
#Skips this test in ruby 1.9.3 with travis
28-
unless ENV['TRAVIS'] && RUBY_VERSION == "1.9.3"
29-
ENV['BUGSNAG_TEST_SERVER_PORT'] = server.config[:Port].to_s
30-
task_fixtures_path = File.join(File.dirname(__FILE__), 'fixtures', 'tasks')
31-
Dir.chdir(task_fixtures_path) do
32-
system("bundle exec rake test:crash > /dev/null 2>&1")
33-
end
34-
35-
result = request()
36-
expect(result["events"][0]["metaData"]["rake_task"]).not_to be_nil
37-
expect(result["events"][0]["metaData"]["rake_task"]["name"]).to eq("test:crash")
38-
end
39-
end
40-
4126
it 'should send notifications over the wire' do
4227
Bugsnag.configure do |config|
4328
config.endpoint = "http://localhost:#{server.config[:Port]}"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
require "spec_helper"
2+
3+
describe Bugsnag::Middleware::ClearanceUser do
4+
it "updates the reports user with warden parameters" do
5+
callback = double
6+
7+
user = double
8+
allow(user).to receive_messages(
9+
:email => "TEST_EMAIL",
10+
:name => "TEST_NAME",
11+
:created_at => "TEST_NOW"
12+
)
13+
14+
clearance = double
15+
allow(clearance).to receive_messages(
16+
:signed_in? => true,
17+
:current_user => user
18+
)
19+
20+
report = double("Bugsnag::Report")
21+
expect(report).to receive(:request_data).exactly(5).times.and_return({
22+
:rack_env => {
23+
:clearance => clearance
24+
}
25+
})
26+
27+
expect(report).to receive(:user=).with({
28+
:email => "TEST_EMAIL",
29+
:name => "TEST_NAME",
30+
:created_at => "TEST_NOW"
31+
})
32+
33+
expect(callback).to receive(:call).with(report)
34+
35+
middleware = Bugsnag::Middleware::ClearanceUser.new(callback)
36+
middleware.call(report)
37+
end
38+
end

spec/integrations/mailman_spec.rb

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
require "spec_helper"
2+
3+
describe 'Bugsnag::Mailman', :order => :defined do
4+
before do
5+
unless defined?(::Mailman)
6+
@mocked_mailman = true
7+
class Mailman
8+
end
9+
module Kernel
10+
alias_method :old_require, :require
11+
def require(path)
12+
old_require(path) unless /^mailman/.match(path)
13+
end
14+
end
15+
end
16+
end
17+
18+
it "should load Bugsnag::Mailman" do
19+
config = double('mailman-config')
20+
allow(Mailman).to receive(:config).and_return(config)
21+
expect(config).to receive(:respond_to?).with(:middleware).and_return(true)
22+
middleware = double('mailman-config-middleware')
23+
expect(config).to receive(:middleware).and_return(middleware)
24+
expect(middleware).to receive(:add).with(any_args)
25+
26+
#Kick off
27+
require './lib/bugsnag/integrations/mailman'
28+
end
29+
30+
it "can be called" do
31+
config = double('config')
32+
allow(Bugsnag).to receive(:configuration).and_return(config)
33+
int_middleware = double('internal_middleware')
34+
expect(config).to receive(:internal_middleware).and_return(int_middleware)
35+
expect(int_middleware).to receive(:use).with(Bugsnag::Middleware::Mailman)
36+
expect(config).to receive(:app_type=).with("mailman")
37+
38+
integration = Bugsnag::Mailman.new
39+
40+
mail = double('mail')
41+
expect(config).to receive(:set_request_data).with(:mailman_msg, mail)
42+
expect(mail).to receive(:to_s).and_return(mail)
43+
allow(config).to receive(:clear_request_data)
44+
45+
exception = RuntimeError.new('oops')
46+
report = double('report')
47+
expect(Bugsnag).to receive(:notify).with(exception, true).and_yield(report)
48+
expect(report).to receive(:severity=).with('error')
49+
expect(report).to receive(:severity_reason=).with({
50+
:type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
51+
:attributes => Bugsnag::Mailman::FRAMEWORK_ATTRIBUTES
52+
})
53+
expect{integration.call(mail) {raise exception}}.to raise_error(exception)
54+
end
55+
56+
after do
57+
Object.send(:remove_const, :Mailman) if @mocked_mailman
58+
module Kernel
59+
alias_method :require, :old_require
60+
end
61+
end
62+
end
63+
64+
65+
describe Bugsnag::Middleware::Mailman do
66+
it "adds mailman message to the metadata" do
67+
callback = double
68+
69+
report = double("Bugsnag::Report")
70+
expect(report).to receive(:request_data).and_return({
71+
:mailman_msg => "test message"
72+
})
73+
74+
expect(report).to receive(:add_tab).with(:mailman, {
75+
"message" => "test message"
76+
})
77+
78+
expect(callback).to receive(:call).with(report)
79+
80+
middleware = Bugsnag::Middleware::Mailman.new(callback)
81+
middleware.call(report)
82+
end
83+
end

spec/integrations/que_spec.rb

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# encoding: utf-8
2+
require 'spec_helper'
3+
4+
describe 'Bugsnag::Que', :order => :defined do
5+
before do
6+
unless defined?(::Que)
7+
@mocked_que = true
8+
class ::Que
9+
end
10+
end
11+
end
12+
13+
it "should create and register a que handler" do
14+
error = RuntimeError.new("oops")
15+
job = double('que_job')
16+
expect(job).to receive(:dup).and_return({
17+
:error_count => 0,
18+
:job_class => 'ActiveJob::QueueAdapters::QueAdapter::JobWrapper',
19+
:args => [{"queue_name" => "foo", "arguments" => "bar"}],
20+
:job_id => "ID"
21+
})
22+
23+
report = double('report')
24+
expect(Bugsnag).to receive(:notify).with(error, true).and_yield(report)
25+
expect(report).to receive(:add_tab).with(:job, {
26+
:error_count => 1,
27+
:job_class => 'ActiveJob::QueueAdapters::QueAdapter::JobWrapper',
28+
:args => [{"queue_name" => "foo", "arguments" => "bar"}],
29+
:job_id => "ID",
30+
:wrapper_job_class => 'ActiveJob::QueueAdapters::QueAdapter::JobWrapper',
31+
:wrapper_job_id => "ID",
32+
:queue => "foo",
33+
:args => "bar"
34+
})
35+
expect(report).to receive(:severity=).with("error")
36+
expect(report).to receive(:severity_reason=).with({
37+
:type => Bugsnag::Report::UNHANDLED_EXCEPTION_MIDDLEWARE,
38+
:attributes => {
39+
:framework => 'Que'
40+
}
41+
})
42+
43+
allow(Que).to receive(:respond_to?).with(:error_notifier=).and_return(true)
44+
config = double('config')
45+
allow(Bugsnag).to receive(:configuration).and_return(config)
46+
expect(config).to receive(:app_type)
47+
expect(config).to receive(:app_type=).with('que')
48+
allow(config).to receive(:clear_request_data)
49+
expect(Que).to receive(:error_notifier=) do |handler|
50+
handler.call(error, job)
51+
end
52+
53+
#Kick off
54+
load './lib/bugsnag/integrations/que.rb'
55+
end
56+
57+
after do
58+
Object.send(:remove_const, :Que) if @mocked_que
59+
end
60+
end

spec/integrations/rack_spec.rb

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
require 'spec_helper'
2+
3+
describe Bugsnag::Rack do
4+
it "calls the upstream rack app with the environment" do
5+
rack_env = {"key" => "value"}
6+
app = lambda { |env| ['response', {}, env] }
7+
rack_stack = Bugsnag::Rack.new(app)
8+
9+
response = rack_stack.call(rack_env)
10+
11+
expect(response).to eq(['response', {}, rack_env])
12+
end
13+
14+
context "when an exception is raised in rack middleware" do
15+
# Build a fake crashing rack app
16+
exception = BugsnagTestException.new("It crashed")
17+
rack_env = {"key" => "value"}
18+
app = lambda { |env| raise exception }
19+
rack_stack = Bugsnag::Rack.new(app)
20+
21+
it "re-raises the exception" do
22+
expect { rack_stack.call(rack_env) }.to raise_error(BugsnagTestException)
23+
end
24+
25+
it "delivers an exception if auto_notify is enabled" do
26+
rack_stack.call(rack_env) rescue nil
27+
28+
expect(Bugsnag).to have_sent_notification{ |payload, headers|
29+
exception_class = payload["events"].first["exceptions"].first["errorClass"]
30+
expect(exception_class).to eq(exception.class.to_s)
31+
}
32+
33+
end
34+
35+
it "applies the correct severity reason" do
36+
rack_stack.call(rack_env) rescue nil
37+
38+
expect(Bugsnag).to have_sent_notification{ |payload, headers|
39+
event = get_event_from_payload(payload)
40+
expect(event["unhandled"]).to be true
41+
expect(event["severityReason"]).to eq({
42+
"type" => "unhandledExceptionMiddleware",
43+
"attributes" => {
44+
"framework" => "Rack"
45+
}
46+
})
47+
}
48+
end
49+
50+
it "does not deliver an exception if auto_notify is disabled" do
51+
Bugsnag.configure do |config|
52+
config.auto_notify = false
53+
end
54+
55+
rack_stack.call(rack_env) rescue nil
56+
57+
expect(Bugsnag).not_to have_sent_notification
58+
end
59+
end
60+
61+
context "when running against the middleware" do
62+
before do
63+
unless defined?(::Rack)
64+
@mocked_rack = true
65+
class ::Rack
66+
class ::Request
67+
end
68+
end
69+
end
70+
end
71+
72+
it "correctly extracts data from rack middleware" do
73+
callback = double
74+
rack_env = {
75+
:env => true,
76+
:HTTP_test_key => "test_key",
77+
"rack.session" => {
78+
:session => true
79+
}
80+
}
81+
82+
rack_request = double
83+
rack_params = {
84+
:param => 'test'
85+
}
86+
allow(rack_request).to receive_messages(
87+
:params => rack_params,
88+
:ip => "rack_ip",
89+
:request_method => "TEST",
90+
:path => "/TEST_PATH",
91+
:scheme => "http",
92+
:host => "test_host",
93+
:port => 80,
94+
:referer => "referer",
95+
:fullpath => "/TEST_PATH"
96+
)
97+
expect(::Rack::Request).to receive(:new).with(rack_env).and_return(rack_request)
98+
99+
report = double("Bugsnag::Report")
100+
allow(report).to receive(:request_data).and_return({
101+
:rack_env => rack_env
102+
})
103+
expect(report).to receive(:context=).with("TEST /TEST_PATH")
104+
expect(report).to receive(:user).and_return({})
105+
106+
config = double
107+
allow(config).to receive(:send_environment).and_return(true)
108+
allow(config).to receive(:meta_data_filters).and_return(nil)
109+
allow(report).to receive(:configuration).and_return(config)
110+
expect(report).to receive(:add_tab).once.with(:environment, rack_env)
111+
expect(report).to receive(:add_tab).once.with(:request, {
112+
:url => "http://test_host/TEST_PATH",
113+
:httpMethod => "TEST",
114+
:params => rack_params,
115+
:referer => "referer",
116+
:clientIp => "rack_ip",
117+
:headers => {
118+
"Test-Key" => "test_key"
119+
}
120+
})
121+
expect(report).to receive(:add_tab).once.with(:session, {
122+
:session => true
123+
})
124+
125+
expect(callback).to receive(:call).with(report)
126+
127+
middleware = Bugsnag::Middleware::RackRequest.new(callback)
128+
middleware.call(report)
129+
end
130+
131+
after do
132+
Object.send(:remove_const, :Rack) if @mocked_rack
133+
end
134+
135+
end
136+
137+
it "don't mess with middlewares list on each req" do
138+
stub_const('Rack', nil)
139+
app = lambda { |env| ['200', {}, ['']] }
140+
141+
Bugsnag::Rack.new(app)
142+
143+
expect { 2.times { Bugsnag::Rack.new(app) } }.not_to change {
144+
Bugsnag.configuration.middleware.instance_variable_get(:@middlewares)
145+
}
146+
end
147+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,21 @@
2323
}
2424
end
2525

26+
it "unsets request metadata" do
27+
Bugsnag.configuration.set_request_data(:rack_env, {
28+
"action_dispatch.remote_ip" => "10.2.2.224",
29+
"action_dispatch.request_id" => "5",
30+
})
31+
Bugsnag.configuration.unset_request_data(:rack_env, nil)
32+
Bugsnag.notify(BugsnagTestException.new('Grimbles'))
33+
34+
expect(Bugsnag).to have_sent_notification { |payload, headers|
35+
event = get_event_from_payload(payload)
36+
puts event["metaData"].inspect
37+
expect(event["metaData"]["request"]).to be nil
38+
}
39+
end
40+
2641
context "the Remote IP will throw when serialized" do
2742

2843
it "sets the client IP metdata to [SPOOF]" do

0 commit comments

Comments
 (0)