<commit_before>require 'spec_helper'

describe Mandrill::WebHook::Processor do

  let(:params) { {} }
  let(:processor_class) { Mandrill::WebHook::Processor }
  let(:processor) { processor_class.new(params) }

  describe "#run!" do
    context "with inbound events" do
      before do
        processor_class.stub(:handle_inbound)
      end
      let(:event1) { { "event" => "inbound" } }
      let(:event2) { { "event" => "inbound" } }
      let(:params) { { "mandrill_events" => [event1,event2].to_json } }
      it "should pass event payload to the handler" do
        processor.should_receive(:handle_inbound).twice
        processor.run!
      end
    end
    context "with callback host" do
      let(:callback_host) do
        host = double()
        host.stub(:handle_inbound)
        host
      end
      let(:processor) { processor_class.new(params,callback_host) }
      let(:event1) { { "event" => "inbound" } }
      let(:event2) { { "event" => "inbound" } }
      let(:params) { { "mandrill_events" => [event1,event2].to_json } }
      it "should pass event payload to the handler" do
        callback_host.should_receive(:handle_inbound).twice
        processor.run!
      end
    end
  end

  describe "#wrap_payload" do
    let(:raw_payload) { {} }
    subject { processor.wrap_payload(raw_payload) }
    its(:class) { should eql(Mandrill::WebHook::EventDecorator) }
  end

  describe "##authentic?" do
    let(:example_payload) { webhook_example_event('click_with_signature') }
    let(:expected_signature) { example_payload['headers']['X-Mandrill-Signature'] }
    let(:original_url) { example_payload['original_url'] }
    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 }
    end
    context "when no keys" do
      let(:mandrill_webhook_keys) { [] }
      it { should be_true }
    end
    context "when keys don't match" do<commit_msg>+ Add spec for handler methods with different visibility in callback_host<commit_after>require 'spec_helper'

describe Mandrill::WebHook::Processor do

  let(:params) { {} }
  let(:processor_class) { Mandrill::WebHook::Processor }
  let(:processor) { processor_class.new(params) }

  describe "#run!" do
    context "with inbound events" do
      before do
        processor_class.stub(:handle_inbound)
      end
      let(:event1) { { "event" => "inbound" } }
      let(:event2) { { "event" => "inbound" } }
      let(:params) { { "mandrill_events" => [event1,event2].to_json } }
      it "should pass event payload to the handler" do
        processor.should_receive(:handle_inbound).twice
        processor.run!
      end
    end
    context "with callback host" do
      shared_examples_for 'pass event payload to the handler' do

      end

      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 } }

      context "with handler method as public" do
        let(:callback_host_class) do
          Class.new do
            public

            def handle_inbound; end
          end
        end

        it "should pass event payload to the handler" do
          callback_host.should_receive(:handle_inbound).twice
          processor.run!
        end
      end
      context "with handler method as protected" do
        let(:callback_host_class) do
          Class.new do
            protected

            def handle_inbound; end
          end
        end

        it "should pass event payload to the handler" do
          callback_host.should_receive(:handle_inbound).twice
          processor.run!
        end
      end
      context "with handler method as private" do
        let(:callback_host_class) do
          Class.new do
            private

            def handle_inbound; end
          end
        end

        it "should pass event payload to the handler" do
          callback_host.should_receive(:handle_inbound).twice
          processor.run!
        end
      end
    end
  end

  describe "#wrap_payload" do
    let(:raw_payload) { {} }
    subject { processor.wrap_payload(raw_payload) }
    its(:class) { should eql(Mandrill::WebHook::EventDecorator) }
  end

  describe "##authentic?" do
    let(:example_payload) { webhook_example_event('click_with_signature') }
    let(:expected_signature) { example_payload['headers']['X-Mandrill-Signature'] }
    let(:original_url) { example_payload['original_url'] }
    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 }
    end
    context "when no keys" do
      let(:mandrill_webhook_keys) { [] }
      it { should be_true }
    end
    context "when keys don't match" do
{'fraction_non_alphanumeric': 0.11353711790393013, 'fraction_numerical': 0.003037782418834251, 'mean_word_length': 2.8621700879765397, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6947289', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1578, 'n_words': 361}