Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 277 lines (213 sloc) 7.474 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
require File.expand_path '../helper', __FILE__

class NotifierTest < Test::Unit::TestCase

  class OriginalException < Exception
  end

  class ContinuedException < Exception
  end

  include DefinesConstants

  def setup
    super
    reset_config
  end

  def assert_sent(notice, notice_args)
    assert_received(Airbrake::Notice, :new) {|expect| expect.with(has_entries(notice_args)) }
    assert_received(Airbrake.sender, :send_to_airbrake) {|expect| expect.with(notice) }
  end

  def set_public_env
    Airbrake.configure { |config| config.environment_name = 'production' }
  end

  def set_development_env
    Airbrake.configure { |config| config.environment_name = 'development' }
  end

  should "yield and save a configuration when configuring" do
    yielded_configuration = nil
    Airbrake.configure do |config|
      yielded_configuration = config
    end

    assert_kind_of Airbrake::Configuration, yielded_configuration
    assert_equal yielded_configuration, Airbrake.configuration
  end

  should "not remove existing config options when configuring twice" do
    first_config = nil
    Airbrake.configure do |config|
      first_config = config
    end
    Airbrake.configure do |config|
      assert_equal first_config, config
    end
  end

  should "configure the sender" do
    sender = stub_sender
    Airbrake::Sender.stubs(:new => sender)
    configuration = nil

    Airbrake.configure { |yielded_config| configuration = yielded_config }

    assert_received(Airbrake::Sender, :new) { |expect| expect.with(configuration) }
    assert_equal sender, Airbrake.sender
  end

  should "create and send a notice for an exception" do
    set_public_env
    exception = build_exception
    stub_sender!
    notice = stub_notice!

    Airbrake.notify(exception)

    assert_sent notice, :exception => exception
  end

  should "create and send a notice for a hash" do
    set_public_env
    notice = stub_notice!
    notice_args = { :error_message => 'uh oh' }
    stub_sender!

    Airbrake.notify(notice_args)

    assert_sent(notice, notice_args)
  end

  should "not pass the hash as an exception when sending a notice for it" do
    set_public_env
    notice = stub_notice!
    notice_args = { :error_message => 'uh oh' }
    stub_sender!

    Airbrake.notify(notice_args)

    assert_received(Airbrake::Notice, :new) {|expect| expect.with(Not(has_key(:exception))) }
  end

  should "create and send a notice for an exception that responds to to_hash" do
    set_public_env
    exception = build_exception
    notice = stub_notice!
    notice_args = { :error_message => 'uh oh' }
    exception.stubs(:to_hash).returns(notice_args)
    stub_sender!

    Airbrake.notify(exception)

    assert_sent(notice, notice_args.merge(:exception => exception))
  end

  should "create and sent a notice for an exception and hash" do
    set_public_env
    exception = build_exception
    notice = stub_notice!
    notice_args = { :error_message => 'uh oh' }
    stub_sender!

    Airbrake.notify(exception, notice_args)

    assert_sent(notice, notice_args.merge(:exception => exception))
  end

  should "not create a notice in a development environment" do
    set_development_env
    sender = stub_sender!

    Airbrake.notify(build_exception)
    Airbrake.notify_or_ignore(build_exception)

    assert_received(sender, :send_to_airbrake) {|expect| expect.never }
  end

  should "not deliver an ignored exception when notifying implicitly" do
    set_public_env
    exception = build_exception
    sender = stub_sender!
    notice = stub_notice!
    notice.stubs(:ignore? => true)

    Airbrake.notify_or_ignore(exception)

    assert_received(sender, :send_to_airbrake) {|expect| expect.never }
  end

  should "deliver exception in async-mode" do
    Airbrake.configure do |config|
      config.environment_name = 'production'
      config.async do |notice|
        Airbrake.sender.send_to_airbrake(notice)
      end
    end
    exception = build_exception
    sender = stub_sender!
    notice = stub_notice!

    Airbrake.notify(exception)

    assert_sent(notice, :exception => exception)
  end

  should "pass notice in async-mode" do
    received_notice = nil
    Airbrake.configure do |config|
      config.environment_name = 'production'
      config.async {|notice| received_notice = notice}
    end
    exception = build_exception
    sender = stub_sender!
    notice = stub_notice!

    Airbrake.notify(exception)

    assert_equal received_notice, notice
  end

  should "deliver an ignored exception when notifying manually" do
    set_public_env
    exception = build_exception
    sender = stub_sender!
    notice = stub_notice!
    notice.stubs(:ignore? => true)

    Airbrake.notify(exception)

    assert_sent(notice, :exception => exception)
  end

  should "pass config to created notices" do
    exception = build_exception
    config_opts = { 'one' => 'two', 'three' => 'four' }
    notice = stub_notice!
    stub_sender!
    Airbrake.configuration = stub('config', :merge => config_opts, :public? => true,:async? => nil)

    Airbrake.notify(exception)

    assert_received(Airbrake::Notice, :new) do |expect|
      expect.with(has_entries(config_opts))
    end
  end

  context "building notice JSON for an exception" do
    setup do
      @params = { :controller => "users", :action => "create" }
      @exception = build_exception
      @hash = Airbrake.build_lookup_hash_for(@exception, @params)
    end

    should "set action" do
      assert_equal @params[:action], @hash[:action]
    end

    should "set controller" do
      assert_equal @params[:controller], @hash[:component]
    end

    should "set line number" do
      assert @hash[:line_number] =~ /\d+/
    end

    should "set file" do
      assert_match /test\/helper\.rb$/, @hash[:file]
    end

    should "set rails_env to production" do
      assert_equal 'production', @hash[:environment_name]
    end

    should "set error class" do
      assert_equal @exception.class.to_s, @hash[:error_class]
    end

    should "not set file or line number with no backtrace" do
      @exception.stubs(:backtrace).returns([])

      @hash = Airbrake.build_lookup_hash_for(@exception)

      assert_nil @hash[:line_number]
      assert_nil @hash[:file]
    end

    should "not set action or controller when not provided" do
      @hash = Airbrake.build_lookup_hash_for(@exception)

      assert_nil @hash[:action]
      assert_nil @hash[:controller]
    end

    context "when an exception that provides #original_exception is raised" do
      setup do
        @exception.stubs(:original_exception).returns(begin
          raise NotifierTest::OriginalException.new
        rescue Exception => e
          e
        end)
      end

      should "unwrap exceptions that provide #original_exception" do
        @hash = Airbrake.build_lookup_hash_for(@exception)
        assert_equal "NotifierTest::OriginalException", @hash[:error_class]
      end
    end

    context "when an exception that provides #continued_exception is raised" do
      setup do
        @exception.stubs(:continued_exception).returns(begin
          raise NotifierTest::ContinuedException.new
        rescue Exception => e
          e
        end)
      end

      should "unwrap exceptions that provide #continued_exception" do
        @hash = Airbrake.build_lookup_hash_for(@exception)
        assert_equal "NotifierTest::ContinuedException", @hash[:error_class]
      end
    end
  end
end
Something went wrong with that request. Please try again.