2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_job"
|
|
|
|
require "mailers/delayed_mailer"
|
2014-08-12 14:24:19 -04:00
|
|
|
|
2014-08-13 17:50:38 -04:00
|
|
|
class MessageDeliveryTest < ActiveSupport::TestCase
|
2014-08-25 10:34:50 -04:00
|
|
|
include ActiveJob::TestHelper
|
2014-08-12 14:24:19 -04:00
|
|
|
|
|
|
|
setup do
|
|
|
|
@previous_logger = ActiveJob::Base.logger
|
|
|
|
@previous_delivery_method = ActionMailer::Base.delivery_method
|
2015-01-18 17:34:01 -05:00
|
|
|
@previous_deliver_later_queue_name = ActionMailer::Base.deliver_later_queue_name
|
|
|
|
ActionMailer::Base.deliver_later_queue_name = :test_queue
|
2014-08-12 14:24:19 -04:00
|
|
|
ActionMailer::Base.delivery_method = :test
|
2014-08-14 19:33:40 -04:00
|
|
|
ActiveJob::Base.logger = Logger.new(nil)
|
2014-08-25 10:34:50 -04:00
|
|
|
ActiveJob::Base.queue_adapter.perform_enqueued_at_jobs = true
|
|
|
|
ActiveJob::Base.queue_adapter.perform_enqueued_jobs = true
|
2016-05-13 20:43:48 -04:00
|
|
|
|
|
|
|
DelayedMailer.last_error = nil
|
|
|
|
DelayedMailer.last_rescue_from_instance = nil
|
|
|
|
|
|
|
|
@mail = DelayedMailer.test_message(1, 2, 3)
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
Clear mail after test
If clear it before the test, the mail of the last executed test will not
be correctly cleared.
Therefore, executing the test with seed below will result in an error.
```
./bin/test -w --seed 55480
Run options: --seed 55480
# Running:
...........................................................................................................................................................F
Failure:
MailDeliveryTest#test_does_not_increment_the_deliveries_collection_on_error [/home/yaginuma/program/rails/master_y_yagi/rails/actionmailer/test/delivery_methods_test.rb:221]:
--- expected
+++ actual
@@ -1 +1 @@
-[]
+[#<Mail::Message:47011389364640, Multipart: false, Headers: <Date: Mon, 14 Aug 2017 07:48:40 +0900>, <From: test-sender@test.com>, <To: test-receiver@test.com>, <Message-ID: <5990d748ea5b2_29342ac1af8bcf40886f7@yaginuma.mail>>, <Subject: Test Subject>, <Mime-Version: 1.0>, <Content-Type: text/plain>, <Content-Transfer-Encoding: 7bit>>]
bin/test test/delivery_methods_test.rb:216
```
2017-08-13 18:47:13 -04:00
|
|
|
ActionMailer::Base.deliveries.clear
|
|
|
|
|
2014-08-12 14:24:19 -04:00
|
|
|
ActiveJob::Base.logger = @previous_logger
|
|
|
|
ActionMailer::Base.delivery_method = @previous_delivery_method
|
2015-01-18 17:34:01 -05:00
|
|
|
ActionMailer::Base.deliver_later_queue_name = @previous_deliver_later_queue_name
|
2016-05-13 20:43:48 -04:00
|
|
|
|
|
|
|
DelayedMailer.last_error = nil
|
|
|
|
DelayedMailer.last_rescue_from_instance = nil
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should have a message" do
|
2014-08-14 19:33:40 -04:00
|
|
|
assert @mail.message
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "its message should be a Mail::Message" do
|
2017-12-12 06:00:50 -05:00
|
|
|
assert_equal Mail::Message, @mail.message.class
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should respond to .deliver_later" do
|
2014-08-12 14:24:19 -04:00
|
|
|
assert_respond_to @mail, :deliver_later
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should respond to .deliver_later!" do
|
2014-08-12 14:24:19 -04:00
|
|
|
assert_respond_to @mail, :deliver_later!
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should respond to .deliver_now" do
|
2014-08-20 04:34:57 -04:00
|
|
|
assert_respond_to @mail, :deliver_now
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should respond to .deliver_now!" do
|
2014-08-20 04:34:57 -04:00
|
|
|
assert_respond_to @mail, :deliver_now!
|
|
|
|
end
|
|
|
|
|
2014-08-20 17:40:00 -04:00
|
|
|
def test_should_enqueue_and_run_correctly_in_activejob
|
2014-08-12 14:24:19 -04:00
|
|
|
@mail.deliver_later!
|
2014-08-20 06:07:03 -04:00
|
|
|
assert_equal 1, ActionMailer::Base.deliveries.size
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should enqueue the email with :deliver_now delivery method" do
|
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
|
2014-08-12 14:24:19 -04:00
|
|
|
@mail.deliver_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should enqueue the email with :deliver_now! delivery method" do
|
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now!", 1, 2, 3]) do
|
2014-08-12 14:24:19 -04:00
|
|
|
@mail.deliver_later!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should enqueue a delivery with a delay" do
|
2014-09-04 01:08:06 -04:00
|
|
|
travel_to Time.new(2004, 11, 24, 01, 04, 44) do
|
2017-03-15 10:52:42 -04:00
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, at: Time.current + 10.minutes, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
|
|
|
|
@mail.deliver_later wait: 10.minutes
|
2014-09-04 01:08:06 -04:00
|
|
|
end
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should enqueue a delivery at a specific time" do
|
2017-03-15 04:19:27 -04:00
|
|
|
later_time = Time.current + 1.hour
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, at: later_time, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
|
2014-09-04 01:08:06 -04:00
|
|
|
@mail.deliver_later wait_until: later_time
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "should enqueue the job on the correct queue" do
|
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3], queue: "test_queue") do
|
2015-01-18 17:34:01 -05:00
|
|
|
@mail.deliver_later
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-15 03:12:44 -04:00
|
|
|
test "should enqueue the job with the correct delivery job" do
|
|
|
|
old_delivery_job = DelayedMailer.delivery_job
|
|
|
|
DelayedMailer.delivery_job = DummyJob
|
|
|
|
|
|
|
|
assert_performed_with(job: DummyJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3]) do
|
|
|
|
@mail.deliver_later
|
|
|
|
end
|
|
|
|
|
|
|
|
DelayedMailer.delivery_job = old_delivery_job
|
|
|
|
end
|
|
|
|
|
|
|
|
class DummyJob < ActionMailer::DeliveryJob; end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "can override the queue when enqueuing mail" do
|
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, args: ["DelayedMailer", "test_message", "deliver_now", 1, 2, 3], queue: "another_queue") do
|
2015-01-18 17:34:01 -05:00
|
|
|
@mail.deliver_later(queue: :another_queue)
|
|
|
|
end
|
|
|
|
end
|
2016-04-07 16:16:54 -04:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "deliver_later after accessing the message is disallowed" do
|
2016-04-07 16:16:54 -04:00
|
|
|
@mail.message # Load the message, which calls the mailer method.
|
|
|
|
|
|
|
|
assert_raise RuntimeError do
|
|
|
|
@mail.deliver_later
|
|
|
|
end
|
|
|
|
end
|
2016-05-13 20:43:48 -04:00
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "job delegates error handling to mailer" do
|
2016-05-13 20:43:48 -04:00
|
|
|
# Superclass not rescued by mailer's rescue_from RuntimeError
|
2016-08-06 13:03:39 -04:00
|
|
|
message = DelayedMailer.test_raise("StandardError")
|
2016-05-13 20:43:48 -04:00
|
|
|
assert_raise(StandardError) { message.deliver_later }
|
|
|
|
assert_nil DelayedMailer.last_error
|
|
|
|
assert_nil DelayedMailer.last_rescue_from_instance
|
|
|
|
|
|
|
|
# Rescued by mailer's rescue_from RuntimeError
|
2016-08-06 13:03:39 -04:00
|
|
|
message = DelayedMailer.test_raise("DelayedMailerError")
|
2016-05-13 20:43:48 -04:00
|
|
|
assert_nothing_raised { message.deliver_later }
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal "boom", DelayedMailer.last_error.message
|
2016-05-13 20:43:48 -04:00
|
|
|
assert_kind_of DelayedMailer, DelayedMailer.last_rescue_from_instance
|
|
|
|
end
|
|
|
|
|
|
|
|
class DeserializationErrorFixture
|
|
|
|
include GlobalID::Identification
|
|
|
|
|
|
|
|
def self.find(id)
|
2016-08-06 13:03:39 -04:00
|
|
|
raise "boom, missing find"
|
2016-05-13 20:43:48 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :id
|
|
|
|
def initialize(id = 1)
|
|
|
|
@id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
def to_global_id(options = {})
|
2016-08-06 13:03:39 -04:00
|
|
|
super app: "foo"
|
2016-05-13 20:43:48 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-06 13:03:39 -04:00
|
|
|
test "job delegates deserialization errors to mailer class" do
|
2016-05-13 20:43:48 -04:00
|
|
|
# Inject an argument that can't be deserialized.
|
|
|
|
message = DelayedMailer.test_message(DeserializationErrorFixture.new)
|
|
|
|
|
|
|
|
# DeserializationError is raised, rescued, and delegated to the handler
|
|
|
|
# on the mailer class.
|
|
|
|
assert_nothing_raised { message.deliver_later }
|
|
|
|
assert_equal DelayedMailer, DelayedMailer.last_rescue_from_instance
|
2016-08-06 13:03:39 -04:00
|
|
|
assert_equal "Error while trying to deserialize arguments: boom, missing find", DelayedMailer.last_error.message
|
2016-05-13 20:43:48 -04:00
|
|
|
end
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|