spec/notifier/hipchat_spec.rb in backup-3.0.20 vs spec/notifier/hipchat_spec.rb in backup-3.0.21

- old
+ new

@@ -1,193 +1,188 @@ # encoding: utf-8 require File.expand_path('../../spec_helper.rb', __FILE__) describe Backup::Notifier::Hipchat do + let(:model) { Backup::Model.new(:test_trigger, 'test label') } let(:notifier) do - Backup::Notifier::Hipchat.new do |notifier| - notifier.from = 'application' + Backup::Notifier::Hipchat.new(model) do |notifier| notifier.token = 'token' + notifier.from = 'application' notifier.rooms_notified = ['room1', 'room2'] - notifier.notify_users = true end end describe '#initialize' do - it "sets the correct defaults" do - notifier.from.should == 'application' - notifier.token.should == 'token' - notifier.success_color.should == 'yellow' - notifier.warning_color.should == 'yellow' - notifier.failure_color.should == 'yellow' + it 'should set the correct values and defaults' do + notifier.token.should == 'token' + notifier.from.should == 'application' + notifier.rooms_notified.should == ['room1', 'room2'] + notifier.notify_users.should == false + notifier.success_color.should == 'yellow' + notifier.warning_color.should == 'yellow' + notifier.failure_color.should == 'yellow' + notifier.on_success.should == true notifier.on_warning.should == true notifier.on_failure.should == true end - it 'uses and overrides configuration defaults' do - Backup::Configuration::Notifier::Hipchat.defaults do |notifier| - notifier.token = 'old' - notifier.from = 'before' - notifier.success_color = 'green' - end - hipchat = Backup::Notifier::Hipchat.new do |notifier| - notifier.token = 'new' - notifier.from = 'after' - notifier.failure_color = 'red' - end + context 'when setting configuration defaults' do + after { Backup::Configuration::Notifier::Hipchat.clear_defaults! } - hipchat.token.should == 'new' - hipchat.from.should == 'after' - hipchat.success_color.should == 'green' - hipchat.warning_color.should == 'yellow' - hipchat.failure_color.should == 'red' - hipchat.on_success.should == true - hipchat.on_warning.should == true - hipchat.on_failure.should == true - end + it 'should use the configuration defaults' do + Backup::Configuration::Notifier::Hipchat.defaults do |notifier| + notifier.token = 'old' + notifier.from = 'before' + notifier.success_color = 'green' - end # describe '#initialize' + notifier.on_failure = false + end + hipchat = Backup::Notifier::Hipchat.new(model) - describe '#perform!' do - let(:model) { Backup::Model.new('trigger', 'label') {} } - let(:hipchat_mock) { mock } - let(:hipchat_client) { HipChat::Client.any_instance } - let(:message) { '[Backup::%s] label (trigger)' } + hipchat.token.should == 'old' + hipchat.from.should == 'before' + hipchat.rooms_notified.should == [] + hipchat.notify_users.should == false + hipchat.success_color.should == 'green' + hipchat.warning_color.should == 'yellow' + hipchat.failure_color.should == 'yellow' - before do - notifier.on_success = false - notifier.on_warning = false - notifier.on_failure = false - notifier.success_color = 'green' - notifier.warning_color = 'yellow' - notifier.failure_color = 'red' - end + hipchat.on_success.should == true + hipchat.on_warning.should == true + hipchat.on_failure.should == false + end - context 'success' do + it 'should override the configuration defaults' do + Backup::Configuration::Notifier::Hipchat.defaults do |notifier| + notifier.token = 'old' + notifier.from = 'before' + notifier.success_color = 'green' - context 'when on_success is true' do - before { notifier.on_success = true } + notifier.on_failure = false + end + hipchat = Backup::Notifier::Hipchat.new(model) do |notifier| + notifier.token = 'new' + notifier.from = 'after' + notifier.failure_color = 'red' - it 'sends success message' do - notifier.expects(:log!) - hipchat_client.expects(:[]).with('room1').returns(hipchat_mock) - hipchat_client.expects(:[]).with('room2').returns(hipchat_mock) - hipchat_mock.expects(:send).twice.with {|user, msg, hash| - (user.should == notifier.from) && - (msg.should == message % 'Success') && - (hash[:color].should == notifier.success_color) && - (hash[:notify].should == notifier.notify_users) - } - - notifier.perform!(model) + notifier.on_success = false + notifier.on_failure = true end - end - context 'when on_success is false' do - it 'sends no message' do - notifier.expects(:log!).never - notifier.expects(:notify!).never - hipchat_client.expects(:[]).never + hipchat.token.should == 'new' + hipchat.from.should == 'after' + hipchat.success_color.should == 'green' + hipchat.warning_color.should == 'yellow' + hipchat.failure_color.should == 'red' - notifier.perform!(model) - end + hipchat.on_success.should == false + hipchat.on_warning.should == true + hipchat.on_failure.should == true end + end # context 'when setting configuration defaults' - end # context 'success' + end # describe '#initialize' - context 'warning' do - before { Backup::Logger.stubs(:has_warnings?).returns(true) } + describe '#notify!' do + before do + notifier.success_color = 'green' + notifier.warning_color = 'yellow' + notifier.failure_color = 'red' + end - context 'when on_success is true' do - before { notifier.on_success = true } - - it 'sends warning message' do - notifier.expects(:log!) - hipchat_client.expects(:[]).with('room1').returns(hipchat_mock) - hipchat_client.expects(:[]).with('room2').returns(hipchat_mock) - hipchat_mock.expects(:send).twice.with {|user, msg, hash| - (user.should == notifier.from) && - (msg.should == message % 'Warning') && - (hash[:color].should == notifier.warning_color) && - (hash[:notify].should == notifier.notify_users) - } - - notifier.perform!(model) - end + context 'when status is :success' do + it 'should send Success message' do + notifier.expects(:send_message).with( + '[Backup::Success] test label (test_trigger)', 'green' + ) + notifier.send(:notify!, :success) end + end - context 'when on_warning is true' do - before { notifier.on_warning = true } - - it 'sends warning message' do - notifier.expects(:log!) - hipchat_client.expects(:[]).with('room1').returns(hipchat_mock) - hipchat_client.expects(:[]).with('room2').returns(hipchat_mock) - hipchat_mock.expects(:send).twice.with {|user, msg, hash| - (user.should == notifier.from) && - (msg.should == message % 'Warning') && - (hash[:color].should == notifier.warning_color) && - (hash[:notify].should == notifier.notify_users) - } - - notifier.perform!(model) - end + context 'when status is :warning' do + it 'should send Warning message' do + notifier.expects(:send_message).with( + '[Backup::Warning] test label (test_trigger)', 'yellow' + ) + notifier.send(:notify!, :warning) end + end - context 'when on_success and on_warning are false' do - it 'sends no message' do - notifier.expects(:log!).never - notifier.expects(:notify!).never - hipchat_client.expects(:[]).never - - notifier.perform!(model) - end + context 'when status is :failure' do + it 'should send Failure message' do + notifier.expects(:send_message).with( + '[Backup::Failure] test label (test_trigger)', 'red' + ) + notifier.send(:notify!, :failure) end + end + end # describe '#notify!' - end # context 'warning' + describe '#send_message' do + let(:client) { mock } + let(:room) { mock } - context 'failure' do + it 'should handle rooms_notified being set as a single room string' do + notifier.rooms_notified = 'a_room' + HipChat::Client.expects(:new).with('token').returns(client) + client.expects(:[]).with('a_room').returns(room) + room.expects(:send).with( + 'application', + 'a message', + {:color => 'a color', :notify => false} + ) - context 'when on_failure is true' do - before { notifier.on_failure = true } + notifier.send(:send_message, 'a message', 'a color') + end - it 'sends failure message' do - notifier.expects(:log!) - hipchat_client.expects(:[]).with('room1').returns(hipchat_mock) - hipchat_client.expects(:[]).with('room2').returns(hipchat_mock) - hipchat_mock.expects(:send).twice.with {|user, msg, hash| - (user.should == notifier.from) && - (msg.should == message % 'Failure') && - (hash[:color].should == notifier.failure_color) && - (hash[:notify].should == notifier.notify_users) - } + context 'when notify_users is set to true' do + before { notifier.notify_users = true } - notifier.perform!(model, Exception.new) - end - end + it 'should notify rooms with :notify => true' do + HipChat::Client.expects(:new).with('token').returns(client) - context 'when on_failure is false' do - it 'sends no message' do - notifier.expects(:log!).never - notifier.expects(:notify!).never - hipchat_client.expects(:[]).never + client.expects(:[]).with('room1').returns(room) + room.expects(:send).with( + 'application', + 'a message', + {:color => 'a color', :notify => true} + ) - notifier.perform!(model, Exception.new) - end + client.expects(:[]).with('room2').returns(room) + room.expects(:send).with( + 'application', + 'a message', + {:color => 'a color', :notify => true} + ) + + notifier.send(:send_message, 'a message', 'a color') end + end - end # context 'failure' + context 'when notify_users is set to false' do + before { notifier.notify_users = false } - it 'will convert a single room param to an array' do - notifier.on_success = true - notifier.rooms_notified = 'one_room' + it 'should notify rooms with :notify => false' do + HipChat::Client.expects(:new).with('token').returns(client) - hipchat_client.expects(:[]).with('one_room').returns(stub(:send)) + client.expects(:[]).with('room1').returns(room) + room.expects(:send).with( + 'application', + 'a message', + {:color => 'a color', :notify => false} + ) - notifier.perform!(model) - notifier.rooms_notified.should == ['one_room'] - end + client.expects(:[]).with('room2').returns(room) + room.expects(:send).with( + 'application', + 'a message', + {:color => 'a color', :notify => false} + ) - end # describe '#perform!' + notifier.send(:send_message, 'a message', 'a color') + end + end + end end