spec/mandrill/web_hook/processor_spec.rb in mandrill-rails-1.1.1 vs spec/mandrill/web_hook/processor_spec.rb in mandrill-rails-1.2.0

- old
+ new

@@ -5,27 +5,27 @@ let(:params) { {} } let(:processor_class) { Mandrill::WebHook::Processor } let(:processor) { processor_class.new(params) } describe "#run!" do - context "with inbound events" do + + context "when handler methods are present" do before do - processor_class.stub(:handle_inbound) + allow(processor_class).to receive(:handle_inbound) + allow(processor_class).to receive(:handle_click) end let(:event1) { { "event" => "inbound" } } - let(:event2) { { "event" => "inbound" } } + let(:event2) { { "event" => "click" } } let(:params) { { "mandrill_events" => [event1,event2].to_json } } - it "should pass event payload to the handler" do - processor.should_receive(:handle_inbound).twice + it "should pass all event payloads to the handler" do + expect(processor).to receive(:handle_inbound) + expect(processor).to receive(:handle_click) processor.run! end end - context "with callback host" do - shared_examples_for 'pass event payload to the handler' do - end - + context "with callback host" do let(:callback_host) { callback_host_class.new } let(:processor) { processor_class.new(params,callback_host) } let(:event1) { { "event" => "inbound" } } let(:event2) { { "event" => "inbound" } } let(:params) { { "mandrill_events" => [event1,event2].to_json } } @@ -38,11 +38,11 @@ def handle_inbound; end end end it "should pass event payload to the handler" do - callback_host.should_receive(:handle_inbound).twice + expect(callback_host).to receive(:handle_inbound).twice processor.run! end end context "with handler method as protected" do let(:callback_host_class) do @@ -52,11 +52,11 @@ def handle_inbound; end end end it "should pass event payload to the handler" do - callback_host.should_receive(:handle_inbound).twice + expect(callback_host).to receive(:handle_inbound).twice processor.run! end end context "with handler method as private" do let(:callback_host_class) do @@ -66,31 +66,65 @@ def handle_inbound; end end end it "should pass event payload to the handler" do - callback_host.should_receive(:handle_inbound).twice + expect(callback_host).to receive(:handle_inbound).twice processor.run! end end - end - context "without handler method" do - let(:event1) { { "event" => "inbound" } } - let(:event2) { { "event" => "inbound" } } - let(:params) { { "mandrill_events" => [event1,event2].to_json } } - it "raises error on run!" do - expect { processor.run! } - .to raise_error(Mandrill::Rails::Errors::MissingEventHandler) + context "with unhandled event" do + let(:callback_host_class) do + Class.new do + end + end + context "and default missing handler behaviour" do + before do + class ::Rails + end + end + after do + Object.send(:remove_const, :Rails) + end + it "logs an error" do + processor.on_unhandled_mandrill_events = :log + logger = double() + expect(logger).to receive(:error).twice + expect(Rails).to receive(:logger).twice.and_return(logger) + expect { processor.run! }.to_not raise_error + end + end + + context "and ignore missing handler behaviour" do + it "logs an error" do + processor.on_unhandled_mandrill_events = :ignore + expect { processor.run! }.to_not raise_error + end + end + + context "and raise_exception missing handler behaviour" do + it "raises an error" do + processor.on_unhandled_mandrill_events = :raise_exception + expect { processor.run! } + .to raise_error(Mandrill::Rails::Errors::MissingEventHandler) + end + end + end + end + + end describe "#wrap_payload" do let(:raw_payload) { {} } subject { processor.wrap_payload(raw_payload) } - its(:class) { should eql(Mandrill::WebHook::EventDecorator) } + it "returns a decorated hash" do + expect(subject.class).to eql(Mandrill::WebHook::EventDecorator) + end end describe "##authentic?" do let(:example_payload) { webhook_example_event('click_with_signature') } let(:expected_signature) { example_payload['headers']['X-Mandrill-Signature'] } @@ -98,23 +132,23 @@ let(:webhook_key) { example_payload['private_key'] } let(:mandrill_webhook_keys) { [webhook_key] } let(:params) { example_payload['raw_params'] } subject { processor_class.authentic?(expected_signature, mandrill_webhook_keys, original_url, params) } context "when valid" do - it { should be_true } + it { should eql(true) } end context "when no keys" do let(:mandrill_webhook_keys) { [] } - it { should be_true } + it { should eql(true) } end context "when keys don't match" do let(:mandrill_webhook_keys) { ['bogative'] } - it { should be_false } + it { should eql(false) } end context "when signature don't match" do let(:expected_signature) { 'bogative' } - it { should be_false } + it { should eql(false) } end end @@ -123,9 +157,11 @@ let(:expected_signature) { example_payload['headers']['X-Mandrill-Signature'] } let(:original_url) { example_payload['original_url'] } let(:webhook_key) { example_payload['private_key'] } let(:params) { example_payload['raw_params'] } subject { processor_class.generate_signature(webhook_key, original_url, params) } - it { should eql(expected_signature) } + it "matches expected signature" do + expect(subject).to eql(expected_signature) + end end end