Refactor out handle_failure method in PollMailbox

This commit is contained in:
riking 2014-08-01 11:03:55 -07:00
parent d7df4e5979
commit c0b2b9b341
2 changed files with 47 additions and 40 deletions

View File

@ -24,49 +24,52 @@ module Jobs
mail_string = mail.pop mail_string = mail.pop
Email::Receiver.new(mail_string).process Email::Receiver.new(mail_string).process
rescue => e rescue => e
message_template = nil handle_failure(mail_string, e)
template_args = {}
case e
when Email::Receiver::UserNotSufficientTrustLevelError
message_template = :email_reject_trust_level
when Email::Receiver::UserNotFoundError
message_template = :email_reject_no_account
when Email::Receiver::EmptyEmailError
message_template = :email_reject_empty
when Email::Receiver::EmailUnparsableError
message_template = :email_reject_parsing
when Email::Receiver::EmailLogNotFound
message_template = :email_reject_reply_key
when ActiveRecord::Rollback
message_template = :email_reject_post_error
when Email::Receiver::InvalidPost
if e.message.length < 6
message_template = :email_reject_post_error
else
message_template = :email_reject_post_error_specified
template_args[:post_error] = e.message
end
else
message_template = nil
end
if message_template
# inform the user about the rejection
message = Mail::Message.new(mail_string)
template_args[:former_title] = message.subject
template_args[:destination] = message.to
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
Email::Sender.new(client_message, message_template).send
else
Discourse.handle_exception(e, error_context(@args, "Unrecognized error type when processing incoming email", mail: mail_string))
end
ensure ensure
mail.delete mail.delete
end end
end end
def handle_failure(mail_string, e)
template_args = {}
case e
when Email::Receiver::UserNotSufficientTrustLevelError
message_template = :email_reject_trust_level
when Email::Receiver::UserNotFoundError
message_template = :email_reject_no_account
when Email::Receiver::EmptyEmailError
message_template = :email_reject_empty
when Email::Receiver::EmailUnparsableError
message_template = :email_reject_parsing
when Email::Receiver::EmailLogNotFound
message_template = :email_reject_reply_key
when ActiveRecord::Rollback
message_template = :email_reject_post_error
when Email::Receiver::InvalidPost
if e.message.length < 6
message_template = :email_reject_post_error
else
message_template = :email_reject_post_error_specified
template_args[:post_error] = e.message
end
else
message_template = nil
end
if message_template
# inform the user about the rejection
message = Mail::Message.new(mail_string)
template_args[:former_title] = message.subject
template_args[:destination] = message.to
client_message = RejectionMailer.send_rejection(message_template, message.from, template_args)
Email::Sender.new(client_message, message_template).send
else
Discourse.handle_exception(e, error_context(@args, "Unrecognized error type when processing incoming email", mail: mail_string))
end
end
def poll_pop3s def poll_pop3s
if !SiteSetting.pop3s_polling_insecure if !SiteSetting.pop3s_polling_insecure
Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE)

View File

@ -63,6 +63,10 @@ describe Jobs::PollMailbox do
end end
end end
def expect_success
Jobs::PollMailbox.expects(:handle_failure).never
end
describe "processing email B" do describe "processing email B" do
let(:category) { Fabricate(:category) } let(:category) { Fabricate(:category) }
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
@ -83,6 +87,7 @@ describe Jobs::PollMailbox do
let(:expected_post) { fixture_file('emails/valid_incoming.cooked') } let(:expected_post) { fixture_file('emails/valid_incoming.cooked') }
it "posts a new topic with the correct content" do it "posts a new topic with the correct content" do
expect_success
poller.handle_mail(email) poller.handle_mail(email)
@ -111,8 +116,7 @@ describe Jobs::PollMailbox do
end end
pending "creates a new post with the correct content" do pending "creates a new post with the correct content" do
RejectionMailer.expects(:send_rejection).never expect_success
Discourse.expects(:handle_exception).never
poller.handle_mail(email) poller.handle_mail(email)