diff options
author | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-18 16:26:30 -0700 |
---|---|---|
committer | David Heinemeier Hansson <david@loudthinking.com> | 2018-09-18 16:26:30 -0700 |
commit | 016ba4dbfa4946658c4ec6200bad73757f30be30 (patch) | |
tree | 0a2a763b771b80033167a1fb871d49047e7a3a48 | |
parent | 0cb3245b4da59f9f77f5797d37214cb7844772fd (diff) | |
download | rails-016ba4dbfa4946658c4ec6200bad73757f30be30.tar.gz rails-016ba4dbfa4946658c4ec6200bad73757f30be30.tar.bz2 rails-016ba4dbfa4946658c4ec6200bad73757f30be30.zip |
Process inbound emails with state and exceptions
-rw-r--r-- | lib/action_mailroom/mailbox.rb | 16 | ||||
-rw-r--r-- | test/unit/mailbox_test.rb | 36 |
2 files changed, 51 insertions, 1 deletions
diff --git a/lib/action_mailroom/mailbox.rb b/lib/action_mailroom/mailbox.rb index e873b55544..7f16165221 100644 --- a/lib/action_mailroom/mailbox.rb +++ b/lib/action_mailroom/mailbox.rb @@ -1,7 +1,11 @@ +require "active_support/rescuable" + class ActionMailroom::Mailbox + include ActiveSupport::Rescuable + class << self def receive(inbound_email) - new(inbound_email).process + new(inbound_email).process_with_state_and_exception_handling end def routing(routes) @@ -16,6 +20,16 @@ class ActionMailroom::Mailbox @inbound_email = inbound_email end + def process_with_state_and_exception_handling + inbound_email.processing! + process + inbound_email.delivered! + rescue => exception + inbound_email.failed! + rescue_with_handler(exception) || raise + end + def process + # Overwrite in subclasses end end diff --git a/test/unit/mailbox_test.rb b/test/unit/mailbox_test.rb new file mode 100644 index 0000000000..505c1f370a --- /dev/null +++ b/test/unit/mailbox_test.rb @@ -0,0 +1,36 @@ +require_relative '../test_helper' + +class SuccessfulMailbox < ActionMailroom::Mailbox + def process + $processed = mail.subject + end +end + +class UnsuccessfulMailbox < ActionMailroom::Mailbox + rescue_from(RuntimeError) { $processed = :failure } + + def process + raise "No way!" + end +end + +module ActionMailroom + class MailboxTest < ActiveSupport::TestCase + setup do + $processed = false + @inbound_email = create_inbound_email("welcome.eml") + end + + test "successful mailbox processing leaves inbound email in delivered state" do + SuccessfulMailbox.receive @inbound_email + assert @inbound_email.delivered? + assert_equal "Discussion: Let's debate these attachments", $processed + end + + test "unsuccessful mailbox processing leaves inbound email in failed state" do + UnsuccessfulMailbox.receive @inbound_email + assert @inbound_email.failed? + assert_equal :failure, $processed + end + end +end |