2017-07-23 23:17:16 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-07 01:03:39 +08:00
|
|
|
require "abstract_unit"
|
|
|
|
require "mailers/base_mailer"
|
|
|
|
require "active_support/log_subscriber/test_helper"
|
|
|
|
require "action_mailer/log_subscriber"
|
2010-01-13 18:56:11 +08:00
|
|
|
|
2010-02-15 22:44:30 +08:00
|
|
|
class AMLogSubscriberTest < ActionMailer::TestCase
|
2018-12-28 17:23:27 +08:00
|
|
|
include ActiveSupport::LogSubscriber::TestHelper
|
2010-03-03 14:55:09 +08:00
|
|
|
|
|
|
|
def setup
|
|
|
|
super
|
2010-06-24 19:23:43 +08:00
|
|
|
ActionMailer::LogSubscriber.attach_to :action_mailer
|
2010-03-03 14:55:09 +08:00
|
|
|
end
|
2010-01-13 18:56:11 +08:00
|
|
|
|
2023-08-30 19:09:04 +08:00
|
|
|
class BogusDelivery
|
|
|
|
def initialize(*)
|
|
|
|
end
|
|
|
|
|
|
|
|
def deliver!(mail)
|
|
|
|
raise "failed"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-13 18:56:11 +08:00
|
|
|
def set_logger(logger)
|
|
|
|
ActionMailer::Base.logger = logger
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_deliver_is_notified
|
2019-02-13 22:55:38 +08:00
|
|
|
BaseMailer.welcome(message_id: "123@abc").deliver_now
|
2010-01-13 18:56:11 +08:00
|
|
|
wait
|
2013-10-16 16:32:06 +08:00
|
|
|
|
2010-01-25 00:31:18 +08:00
|
|
|
assert_equal(1, @logger.logged(:info).size)
|
2019-02-13 22:55:38 +08:00
|
|
|
assert_match(/Delivered mail 123@abc/, @logger.logged(:info).first)
|
2013-10-16 16:32:06 +08:00
|
|
|
|
|
|
|
assert_equal(2, @logger.logged(:debug).size)
|
|
|
|
assert_match(/BaseMailer#welcome: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first)
|
|
|
|
assert_match(/Welcome/, @logger.logged(:debug).second)
|
2014-05-13 13:01:14 +08:00
|
|
|
ensure
|
|
|
|
BaseMailer.deliveries.clear
|
2010-01-13 18:56:11 +08:00
|
|
|
end
|
|
|
|
|
2018-09-09 19:16:20 +08:00
|
|
|
def test_deliver_message_when_perform_deliveries_is_false
|
2019-02-13 22:55:38 +08:00
|
|
|
BaseMailer.welcome_without_deliveries(message_id: "123@abc").deliver_now
|
2018-09-08 16:14:12 +08:00
|
|
|
wait
|
|
|
|
|
2018-09-09 19:16:20 +08:00
|
|
|
assert_equal(1, @logger.logged(:info).size)
|
2019-02-13 22:55:38 +08:00
|
|
|
assert_match("Skipped delivery of mail 123@abc as `perform_deliveries` is false", @logger.logged(:info).first)
|
2018-09-09 19:16:20 +08:00
|
|
|
|
|
|
|
assert_equal(2, @logger.logged(:debug).size)
|
2018-09-08 16:14:12 +08:00
|
|
|
assert_match(/BaseMailer#welcome_without_deliveries: processed outbound mail in [\d.]+ms/, @logger.logged(:debug).first)
|
2018-09-09 19:16:20 +08:00
|
|
|
assert_match("Welcome", @logger.logged(:debug).second)
|
2018-09-08 16:14:12 +08:00
|
|
|
ensure
|
|
|
|
BaseMailer.deliveries.clear
|
|
|
|
end
|
2023-08-30 19:09:04 +08:00
|
|
|
|
|
|
|
def test_deliver_message_when_exception_happened
|
|
|
|
previous_delivery_method = BaseMailer.delivery_method
|
|
|
|
BaseMailer.delivery_method = BogusDelivery
|
|
|
|
|
|
|
|
assert_raises(RuntimeError) { BaseMailer.welcome(message_id: "123@abc").deliver_now }
|
|
|
|
wait
|
|
|
|
|
|
|
|
assert_equal(1, @logger.logged(:info).size)
|
|
|
|
assert_equal('Failed delivery of mail 123@abc error_class=RuntimeError error_message="failed"', @logger.logged(:info).first)
|
|
|
|
ensure
|
|
|
|
BaseMailer.delivery_method = previous_delivery_method
|
|
|
|
end
|
2013-10-16 16:32:06 +08:00
|
|
|
end
|