2018-09-18 19:42:38 -04:00
|
|
|
require_relative '../../test_helper'
|
|
|
|
|
2018-09-28 15:19:43 -04:00
|
|
|
class CallbackMailbox < ActionMailbox::Base
|
2018-09-18 19:42:38 -04:00
|
|
|
before_processing { $before_processing = "Ran that!" }
|
|
|
|
after_processing { $after_processing = "Ran that too!" }
|
|
|
|
around_processing ->(r, block) { block.call; $around_processing = "Ran that as well!" }
|
|
|
|
|
|
|
|
def process
|
|
|
|
$processed = mail.subject
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-03 15:36:40 -04:00
|
|
|
class BouncingCallbackMailbox < ActionMailbox::Base
|
|
|
|
before_processing { $before_processing = [ "Pre-bounce" ] }
|
|
|
|
|
|
|
|
before_processing do
|
|
|
|
bounce_with BounceMailer.bounce(to: mail.from)
|
|
|
|
$before_processing << "Bounce"
|
|
|
|
end
|
|
|
|
|
|
|
|
before_processing { $before_processing << "Post-bounce" }
|
|
|
|
|
2018-10-03 16:04:33 -04:00
|
|
|
after_processing { $after_processing = true }
|
|
|
|
|
2018-10-03 15:36:40 -04:00
|
|
|
def process
|
2018-10-03 16:04:33 -04:00
|
|
|
$processed = true
|
2018-10-03 15:36:40 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-10-06 22:15:36 -04:00
|
|
|
class DiscardingCallbackMailbox < ActionMailbox::Base
|
|
|
|
before_processing { $before_processing = [ "Pre-discard" ] }
|
|
|
|
|
|
|
|
before_processing do
|
|
|
|
delivered!
|
|
|
|
$before_processing << "Discard"
|
|
|
|
end
|
|
|
|
|
|
|
|
before_processing { $before_processing << "Post-discard" }
|
|
|
|
|
|
|
|
after_processing { $after_processing = true }
|
|
|
|
|
|
|
|
def process
|
|
|
|
$processed = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-09-28 15:19:43 -04:00
|
|
|
class ActionMailbox::Base::CallbacksTest < ActiveSupport::TestCase
|
2018-09-18 19:42:38 -04:00
|
|
|
setup do
|
|
|
|
$before_processing = $after_processing = $around_processing = $processed = false
|
2018-09-19 20:08:35 -04:00
|
|
|
@inbound_email = create_inbound_email_from_fixture("welcome.eml")
|
2018-09-18 19:42:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test "all callback types" do
|
|
|
|
CallbackMailbox.receive @inbound_email
|
|
|
|
assert_equal "Ran that!", $before_processing
|
|
|
|
assert_equal "Ran that too!", $after_processing
|
|
|
|
assert_equal "Ran that as well!", $around_processing
|
|
|
|
end
|
2018-10-03 15:36:40 -04:00
|
|
|
|
|
|
|
test "bouncing in a callback terminates processing" do
|
|
|
|
BouncingCallbackMailbox.receive @inbound_email
|
|
|
|
assert @inbound_email.bounced?
|
|
|
|
assert_equal [ "Pre-bounce", "Bounce" ], $before_processing
|
|
|
|
assert_not $processed
|
2018-10-03 16:04:33 -04:00
|
|
|
assert_not $after_processing
|
2018-10-03 15:36:40 -04:00
|
|
|
end
|
2018-10-06 22:15:36 -04:00
|
|
|
|
|
|
|
test "marking the inbound email as delivered in a callback terminates processing" do
|
|
|
|
DiscardingCallbackMailbox.receive @inbound_email
|
|
|
|
assert @inbound_email.delivered?
|
|
|
|
assert_equal [ "Pre-discard", "Discard" ], $before_processing
|
|
|
|
assert_not $processed
|
|
|
|
assert_not $after_processing
|
|
|
|
end
|
2018-09-18 19:42:38 -04:00
|
|
|
end
|