From c0b2b9b341254d0e98b2f2b3a86488812fd6e42a Mon Sep 17 00:00:00 2001 From: riking Date: Fri, 1 Aug 2014 11:03:55 -0700 Subject: [PATCH] Refactor out handle_failure method in PollMailbox --- app/jobs/scheduled/poll_mailbox.rb | 79 ++++++++++++++++-------------- spec/jobs/poll_mailbox_spec.rb | 8 ++- 2 files changed, 47 insertions(+), 40 deletions(-) diff --git a/app/jobs/scheduled/poll_mailbox.rb b/app/jobs/scheduled/poll_mailbox.rb index 36cd759f981..749dd67748e 100644 --- a/app/jobs/scheduled/poll_mailbox.rb +++ b/app/jobs/scheduled/poll_mailbox.rb @@ -24,49 +24,52 @@ module Jobs mail_string = mail.pop Email::Receiver.new(mail_string).process rescue => e - message_template = nil - 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 + handle_failure(mail_string, e) ensure mail.delete 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 if !SiteSetting.pop3s_polling_insecure Net::POP3.enable_ssl(OpenSSL::SSL::VERIFY_NONE) diff --git a/spec/jobs/poll_mailbox_spec.rb b/spec/jobs/poll_mailbox_spec.rb index 90c8ec9bea9..4875ead98f0 100644 --- a/spec/jobs/poll_mailbox_spec.rb +++ b/spec/jobs/poll_mailbox_spec.rb @@ -63,6 +63,10 @@ describe Jobs::PollMailbox do end end + def expect_success + Jobs::PollMailbox.expects(:handle_failure).never + end + describe "processing email B" do let(:category) { Fabricate(:category) } let(:user) { Fabricate(:user) } @@ -83,6 +87,7 @@ describe Jobs::PollMailbox do let(:expected_post) { fixture_file('emails/valid_incoming.cooked') } it "posts a new topic with the correct content" do + expect_success poller.handle_mail(email) @@ -111,8 +116,7 @@ describe Jobs::PollMailbox do end pending "creates a new post with the correct content" do - RejectionMailer.expects(:send_rejection).never - Discourse.expects(:handle_exception).never + expect_success poller.handle_mail(email)