require "spec_helper"

describe InboundMailProcessor do
  subject { InboundMailProcessor }

  it "should be on the inbound mail queue" do
    subject.queue.should == :inbound_mail
  end

  it "should respond to perform" do
    subject.should respond_to(:perform)
  end

  context "thread reply mail" do
    let(:thread) { FactoryGirl.create(:message_thread) }
    let(:email_recipient) { "thread-#{thread.public_token}@cyclescape.org" }
    let(:inbound_mail) { FactoryGirl.create(:inbound_mail, to: email_recipient) }

    before do
      subject.perform(inbound_mail.id)
    end

    it "should create a new message on the thread" do
      thread.should have(1).message
    end

    it "should have the same text as the email" do
      thread.messages.first.body.should == inbound_mail.message.body.to_s
    end

    it "should have be created by a new user with the email address" do
      thread.messages.first.created_by.email.should == inbound_mail.message.from.first
    end
  end
end

require "spec_helper"

describe InboundMailProcessor do
  subject { InboundMailProcessor }

  it "should be on the inbound mail queue" do
    subject.queue.should == :inbound_mail
  end

  it "should respond to perform" do
    subject.should respond_to(:perform)
  end

  context "thread reply mail" do
    let(:thread) { FactoryGirl.create(:message_thread) }
    let(:email_recipient) { "thread-#{thread.public_token}@cyclescape.org" }
    let(:inbound_mail) { FactoryGirl.create(:inbound_mail, to: email_recipient) }

    before do
      subject.perform(inbound_mail.id)
    end

    it "should create a new message on the thread" do
      thread.should have(1).message
    end

    it "should have the same text as the email" do
      # There are weird newline issues here, each \r is duplicated in the model's response
      thread.messages.first.body.gsub(/\n|\r/, '').should == inbound_mail.message.body.to_s.gsub(/\n|\r/, '')
    end

    it "should have be created by a new user with the email address" do
      thread.messages.first.created_by.email.should == inbound_mail.message.from.first
    end
  end
end

Patch test to workaround odd newline error in output, not worth debugging now.
Patch test to workaround odd newline error in output, not worth debugging now.
