Skip to content

Commit

Permalink
Merge pull request #268 from alphagov/disable_unless_configured
Browse files Browse the repository at this point in the history
Only send notices if configured with an api_key
  • Loading branch information
alif committed May 2, 2014
2 parents c720737 + ce0d0f1 commit e7bf49d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/airbrake.rb
Expand Up @@ -147,7 +147,7 @@ def build_lookup_hash_for(exception, options = {})
private

def send_notice(notice)
if configuration.public?
if configuration.configured? && configuration.public?
if configuration.async?
configuration.async.call(notice)
nil # make sure we never set env["airbrake.error_id"] for async notices
Expand Down
4 changes: 4 additions & 0 deletions lib/airbrake/configuration.rb
Expand Up @@ -256,6 +256,10 @@ def merge(hash)
to_hash.merge(hash)
end

def configured?
!api_key.nil? && !api_key.empty?
end

# Determines if the notifier will send notices.
# @return [Boolean] Returns +false+ if in a development environment, +true+ otherwise.
def public?
Expand Down
21 changes: 21 additions & 0 deletions test/configuration_test.rb
Expand Up @@ -183,6 +183,27 @@ class ConfigurationTest < Test::Unit::TestCase
assert_same_elements %w(development test cucumber), config.development_environments
end

context "configured?" do
setup do
@config = Airbrake::Configuration.new
end

should "be true if given an api_key" do
@config.api_key = "1234"
assert @config.configured?
end

should "be false with a nil api_key" do
@config.api_key = nil
assert !@config.configured?
end

should "be false with a blank api_key" do
@config.api_key = ''
assert !@config.configured?
end
end

should "be public in a public environment" do
config = Airbrake::Configuration.new
config.development_environments = %w(development)
Expand Down
7 changes: 7 additions & 0 deletions test/integration/catcher_test.rb
Expand Up @@ -270,6 +270,13 @@ def test_not_deliver_notices_from_exceptions_in_development_environments
assert_caught_and_not_sent
end

def test_not_deliver_notices_from_exceptions_with_no_api_key
Airbrake.configuration.api_key = nil
@app = AirbrakeTestController.action(:boom)
get '/'
assert_caught_and_not_sent
end

def test_not_deliver_notices_from_actions_that_dont_raise
@app = AirbrakeTestController.action(:hello)
get '/'
Expand Down
2 changes: 1 addition & 1 deletion test/notifier_test.rb
Expand Up @@ -187,7 +187,7 @@ def set_development_env
config_opts = { 'one' => 'two', 'three' => 'four' }
stub_notice!
stub_sender!
Airbrake.configuration = stub('config', :merge => config_opts, :public? => true,:async? => nil)
Airbrake.configuration = stub('config', :merge => config_opts, :configured? => true, :public? => true,:async? => nil)

Airbrake.notify(exception)

Expand Down

0 comments on commit e7bf49d

Please sign in to comment.