require 'test_helper' require 'hipchat' class HipchatNotifierTest < ActiveSupport::TestCase test "should send hipchat notification if properly configured" do options = { :api_token => 'good_token', :room_name => 'room_name', :color => 'yellow', } HipChat::Room.any_instance.expects(:send).with('Exception', fake_body, { :color => 'yellow' }) hipchat = ExceptionNotifier::HipchatNotifier.new(options) hipchat.call(fake_exception) end test "should send hipchat notification without backtrace info if properly configured" do options = { :api_token => 'good_token', :room_name => 'room_name', :color => 'yellow', } HipChat::Room.any_instance.expects(:send).with('Exception', fake_body_without_backtrace, { :color => 'yellow' }) hipchat = ExceptionNotifier::HipchatNotifier.new(options) hipchat.call(fake_exception_without_backtrace) end test "should allow custom from value if set" do options = { :api_token => 'good_token', :room_name => 'room_name', :from => 'TrollFace', } HipChat::Room.any_instance.expects(:send).with('TrollFace', fake_body, { :color => 'red' }) hipchat = ExceptionNotifier::HipchatNotifier.new(options) hipchat.call(fake_exception) end test "should not send hipchat notification if badly configured" do wrong_params = { :api_token => 'bad_token', :room_name => 'test_room' } HipChat::Client.stubs(:new).with('bad_token', {:api_version => 'v1'}).returns(nil) hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params) assert_nil hipchat.room end test "should not send hipchat notification if api_key is missing" do wrong_params = {:room_name => 'test_room'} HipChat::Client.stubs(:new).with(nil, {:api_version => 'v1'}).returns(nil) hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params) assert_nil hipchat.room end test "should not send hipchat notification if room_name is missing" do wrong_params = {:api_token => 'good_token'} HipChat::Client.stubs(:new).with('good_token', {:api_version => 'v1'}).returns({}) hipchat = ExceptionNotifier::HipchatNotifier.new(wrong_params) assert_nil hipchat.room end test "should send hipchat notification with message_template" do options = { :api_token => 'good_token', :room_name => 'room_name', :color => 'yellow', :message_template => ->(exception) { "This is custom message: '#{exception.message}'" } } HipChat::Room.any_instance.expects(:send).with('Exception', "This is custom message: '#{fake_exception.message}'", { :color => 'yellow' }) hipchat = ExceptionNotifier::HipchatNotifier.new(options) hipchat.call(fake_exception) end test "should use APIv1 if api_version is not specified" do options = { :api_token => 'good_token', :room_name => 'room_name', } HipChat::Client.stubs(:new).with('good_token', {:api_version => 'v1'}).returns({}) hipchat = ExceptionNotifier::HipchatNotifier.new(options) hipchat.call(fake_exception) end test "should use APIv2 when specified" do options = { :api_token => 'good_token', :room_name => 'room_name', :api_version => 'v2', } HipChat::Client.stubs(:new).with('good_token', {:api_version => 'v2'}).returns({}) hipchat = ExceptionNotifier::HipchatNotifier.new(options) hipchat.call(fake_exception) end private def fake_body "A new exception occurred: '#{fake_exception.message}' on '#{fake_exception.backtrace.first}'" end def fake_exception exception = begin 5/0 rescue Exception => e e end end def fake_body_without_backtrace "A new exception occurred: '#{fake_exception_without_backtrace.message}'" end def fake_exception_without_backtrace StandardError.new('my custom error') end end