Add more tests, undo some changes to fixture files

Was causing Email::Reciever tests to fail
This commit is contained in:
riking 2014-08-01 12:40:28 -07:00
parent 0faea8ee0b
commit 63cdde3d96
7 changed files with 97 additions and 97 deletions

View File

@ -67,6 +67,19 @@ module Email
end end
end end
def is_in_email?
@allow_strangers = false
return false unless SiteSetting.email_in
category = Category.find_by_email(@message.to.first)
return false unless category
@category_id = category.id
@allow_strangers = category.email_in_allow_strangers
true
end
private private
def parse_body def parse_body
@ -135,19 +148,6 @@ module Email
@body.strip! @body.strip!
end end
def is_in_email?
@allow_strangers = false
return false unless SiteSetting.email_in
category = Category.find_by_email(@message.to.first)
return false unless category
@category_id = category.id
@allow_strangers = category.email_in_allow_strangers
true
end
def wrap_body_in_quote def wrap_body_in_quote
@body = "[quote=\"#{@message.from.first}\"] @body = "[quote=\"#{@message.from.first}\"]
#{@body} #{@body}

BIN
spec/fixtures/emails/bottom_reply.eml vendored Normal file

Binary file not shown.

BIN
spec/fixtures/emails/empty.eml vendored Normal file

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
spec/fixtures/emails/wrong_reply_key.eml vendored Normal file

Binary file not shown.

View File

@ -64,7 +64,11 @@ describe Jobs::PollMailbox do
end end
def expect_success def expect_success
Jobs::PollMailbox.expects(:handle_failure).never poller.expects(:handle_failure).never
end
def expect_exception(clazz)
poller.expects(:handle_failure).with(anything, instance_of(clazz))
end end
describe "processing emails" do describe "processing emails" do
@ -73,30 +77,65 @@ describe Jobs::PollMailbox do
before do before do
SiteSetting.email_in = true SiteSetting.email_in = true
SiteSetting.reply_by_email_address = 'reply+%{reply_key}@discourse.example.com' SiteSetting.reply_by_email_address = "reply+%{reply_key}@appmail.adventuretime.ooo"
category.email_in = 'incoming+amazing@discourse.example.com' category.email_in = 'incoming+amazing@appmail.adventuretime.ooo'
category.save category.save
user.change_trust_level! :regular user.change_trust_level! :regular
user.username = 'Jake' user.username = 'Jake'
user.email = 'jake@email.example.com' user.email = 'jake@adventuretime.ooo'
user.save user.save
end end
describe "a valid incoming email" do describe "a valid incoming email" do
let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_incoming.eml')} let(:email) {
# this string replacing is kinda dumb
str = fixture_file('emails/valid_incoming.eml')
str = str.gsub("FROM", 'jake@adventuretime.ooo').gsub("TO", 'incoming+amazing@appmail.adventuretime.ooo')
MockPop3EmailObject.new str
}
let(:expected_post) { fixture_file('emails/valid_incoming.cooked') } let(:expected_post) { fixture_file('emails/valid_incoming.cooked') }
it "posts a new topic" do it "posts a new topic with the correct content" do
expect_success expect_success
poller.handle_mail(email) poller.handle_mail(email)
topic = Topic.where(category: category).where.not(id: category.topic_id).first topic = Topic.where(category: category).where.not(id: category.topic_id).last
assert topic.present? topic.should be_present
post = topic.posts.first topic.title.should == "We should have a post-by-email-feature"
assert_equal expected_post.strip, post.cooked.strip
assert email.deleted? post = topic.posts.first
post.cooked.strip.should == expected_post.strip
email.should be_deleted
end
describe "with insufficient trust" do
before do
user.change_trust_level! :newuser
end
it "raises a UserNotSufficientTrustLevelError" do
expect_exception Email::Receiver::UserNotSufficientTrustLevelError
poller.handle_mail(email)
end
it "posts the topic if allow_strangers is true" do
begin
category.email_in_allow_strangers = true
category.save
expect_success
poller.handle_mail(email)
topic = Topic.where(category: category).where.not(id: category.topic_id).last
topic.should be_present
topic.title.should == "We should have a post-by-email-feature"
ensure
category.email_in_allow_strangers = false
category.save
end
end
end end
end end
@ -125,86 +164,47 @@ describe Jobs::PollMailbox do
assert new_post.present? assert new_post.present?
assert_equal expected_post.strip, new_post.cooked.strip assert_equal expected_post.strip, new_post.cooked.strip
assert email.deleted? email.should be_deleted
end end
end
describe "without an email log" do describe "with the wrong reply key" do
let(:email) { MockPop3EmailObject.new fixture_file('emails/valid_reply.eml')} let(:email) { MockPop3EmailObject.new fixture_file('emails/wrong_reply_key.eml')}
it "handles an EmailLogNotFound error" do it "raises an EmailLogNotFound error" do
poller.expects(:handle_failure).with { |mail_string, ex| ex.is_a? Email::Receiver::EmailLogNotFound } expect_exception Email::Receiver::EmailLogNotFound
poller.handle_mail(email)
assert email.deleted?
end
end
end
describe "processing email A" do
let!(:receiver) { mock }
let!(:email_string) { fixture_file("emails/valid_incoming.eml") }
let!(:email) { mock }
before do
email.stubs(:pop).returns(email_string)
Email::Receiver.expects(:new).with(email_string).returns(receiver)
end
describe "all goes fine" do
it "email gets deleted" do
receiver.expects(:process)
email.expects(:delete)
poller.handle_mail(email)
end
end
describe "raises Untrusted error" do
it "sends a reply and deletes the email" do
receiver.expects(:process).raises(Email::Receiver::UserNotSufficientTrustLevelError)
email.expects(:delete)
message = Mail::Message.new(email_string)
Mail::Message.expects(:new).with(email_string).returns(message)
client_message = mock
sender_object = mock
RejectionMailer.expects(:send_rejection).with(
message.from, message.body, message.subject, message.to, :email_reject_trust_level
).returns(client_message)
Email::Sender.expects(:new).with(client_message, :email_reject_trust_level).returns(sender_object)
sender_object.expects(:send)
poller.handle_mail(email)
end
end
describe "raises error" do
[ Email::Receiver::ProcessingError,
Email::Receiver::EmailUnparsableError,
Email::Receiver::EmptyEmailError,
Email::Receiver::UserNotFoundError,
Email::Receiver::EmailLogNotFound,
ActiveRecord::Rollback,
TypeError
].each do |exception|
it "deletes email on #{exception}" do
receiver.expects(:process).raises(exception)
email.expects(:delete)
Discourse.stubs(:handle_exception)
poller.handle_mail(email) poller.handle_mail(email)
email.should be_deleted
end end
end end
end
describe "in failure conditions" do
it "a valid reply without an email log raises an EmailLogNotFound error" do
email = MockPop3EmailObject.new fixture_file('emails/valid_reply.eml')
expect_exception Email::Receiver::EmailLogNotFound
poller.handle_mail(email)
email.should be_deleted
end
it "a no content reply raises an EmailUnparsableError" do
email = MockPop3EmailObject.new fixture_file('emails/no_content_reply.eml')
expect_exception Email::Receiver::EmailUnparsableError
poller.handle_mail(email)
email.should be_deleted
end
it "a fully empty email raises an EmptyEmailError" do
email = MockPop3EmailObject.new fixture_file('emails/empty.eml')
expect_exception Email::Receiver::EmptyEmailError
poller.handle_mail(email)
email.should be_deleted
end
end end
end end