<commit_before>
require 'spec_helper'
require 'pipeline_helper'

describe RailsPipeline::Subscriber do
  before do
    @test_emitter = TestEmitter.new({foo: "bar"}, without_protection: true)

    @test_message = @test_emitter.create_message("2_0", RailsPipeline::EncryptedMessage::EventType::CREATED)
    @subscriber = TestSubscriber.new
    TestSubscriber.handler_method_cache = {}
    OtherSubscriber.handler_method_cache = {}
  end


  context "when there is no compatible version registered for the message" do
      before do
        RailsPipeline::Subscriber.register(TestEmitter_2_0, TestModel)
      end

      it "should handle correct messages" do
          expect(@subscriber).to receive(:handle_payload).once
          @subscriber.handle_envelope(@test_message)
      end
  end

  context "when there is a compatible version registered for the message" do
      it "should log the inability to process the message" do
          expect(RailsPipeline.logger).to receive(:info).once
          @subscriber.handle_envelope(@test_message)
      end
  end

  it "should raise exception on malformed messages" do
    @test_message = RailsPipeline::EncryptedMessage.new(salt: "jhkjehd", iv: "khdkjehdkejhdkjehdkjhed")
    expect(@subscriber).not_to receive(:handle_payload)
    expect{@subscriber.handle_envelope(@test_message)}.to raise_error
  end

  describe 'api_key' do
    before do
      @test_message = @test_emitter.create_message("2_0", RailsPipeline::EncryptedMessage::EventType::CREATED)
    end

    context 'with wrong api key' do
      it "should drop messages" do
        @test_message.api_key = '123XYZ'
        expect{@subscriber.handle_envelope(@test_message)}.to raise_error(RailsPipeline::Subscriber::WrongApiKeyError)
      end
    end

    context 'with no api key' do
      it "should drop messages" do
        @test_message.api_key = nil<commit_msg>Removing order dependent nature of test<commit_after>
require 'spec_helper'
require 'pipeline_helper'

describe RailsPipeline::Subscriber do
  before do
    @test_emitter = TestEmitter.new({foo: "bar"}, without_protection: true)

    @test_message = @test_emitter.create_message("2_0", RailsPipeline::EncryptedMessage::EventType::CREATED)
    @subscriber = TestSubscriber.new
    TestSubscriber.handler_method_cache = {}
    OtherSubscriber.handler_method_cache = {}
  end


  context "when there is no compatible version registered for the message" do
      before do
        RailsPipeline::Subscriber.register(TestEmitter_2_0, TestModel)
      end

      it "should handle correct messages" do
          expect(@subscriber).to receive(:handle_payload).once
          @subscriber.handle_envelope(@test_message)
      end
  end

  context "when there is a compatible version registered for the message" do
      before do
          @test_emitter = TestEmitter.new({foo: "bar"}, without_protection: true)
          RailsPipeline::Subscriber.stub(:registered_handlers){{}}
      end

      it "should log the inability to process the message" do
          expect(RailsPipeline.logger).to receive(:info).once
          @subscriber.handle_envelope(@test_message)
      end
  end

  it "should raise exception on malformed messages" do
    @test_message = RailsPipeline::EncryptedMessage.new(salt: "jhkjehd", iv: "khdkjehdkejhdkjehdkjhed")
    expect(@subscriber).not_to receive(:handle_payload)
    expect{@subscriber.handle_envelope(@test_message)}.to raise_error
  end

  describe 'api_key' do
    before do
      @test_message = @test_emitter.create_message("2_0", RailsPipeline::EncryptedMessage::EventType::CREATED)
    end

    context 'with wrong api key' do
      it "should drop messages" do
        @test_message.api_key = '123XYZ'
        expect{@subscriber.handle_envelope(@test_message)}.to raise_error(RailsPipeline::Subscriber::WrongApiKeyError)
      end
    end

    context 'with no api key' do
      it "should drop messages" do
        @test_message.api_key = nil
{'fraction_non_alphanumeric': 0.08897876643073811, 'fraction_numerical': 0.00455005055611729, 'mean_word_length': 3.8912237330037085, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6924587', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1142, 'n_words': 283}