2014-08-12 14:24:19 -04:00
|
|
|
# encoding: utf-8
|
|
|
|
require 'abstract_unit'
|
2014-08-27 13:38:04 -04:00
|
|
|
require 'active_job'
|
2014-08-12 14:24:19 -04:00
|
|
|
require 'minitest/mock'
|
2014-08-27 13:38:04 -04:00
|
|
|
require 'mailers/delayed_mailer'
|
2014-08-25 10:34:50 -04:00
|
|
|
require 'active_support/core_ext/numeric/time'
|
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
|
|
|
|
ActionMailer::Base.delivery_method = :test
|
2014-08-14 19:33:40 -04:00
|
|
|
ActiveJob::Base.logger = Logger.new(nil)
|
2014-08-12 14:24:19 -04:00
|
|
|
@mail = DelayedMailer.test_message(1, 2, 3)
|
|
|
|
ActionMailer::Base.deliveries.clear
|
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
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveJob::Base.logger = @previous_logger
|
|
|
|
ActionMailer::Base.delivery_method = @previous_delivery_method
|
|
|
|
end
|
|
|
|
|
2014-08-14 19:33:40 -04:00
|
|
|
test 'should have a message' do
|
|
|
|
assert @mail.message
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
2014-08-14 19:33:40 -04:00
|
|
|
test 'its message should be a Mail::Message' do
|
|
|
|
assert_equal Mail::Message , @mail.message.class
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
test 'should respond to .deliver_later' do
|
|
|
|
assert_respond_to @mail, :deliver_later
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should respond to .deliver_later!' do
|
|
|
|
assert_respond_to @mail, :deliver_later!
|
|
|
|
end
|
|
|
|
|
2014-08-20 04:34:57 -04:00
|
|
|
test 'should respond to .deliver_now' do
|
|
|
|
assert_respond_to @mail, :deliver_now
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should respond to .deliver_now!' do
|
|
|
|
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
|
|
|
|
ensure
|
|
|
|
ActionMailer::Base.deliveries.clear
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
|
2014-08-25 10:34:50 -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
|
|
|
|
|
2014-08-25 10:34:50 -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
|
|
|
|
|
|
|
|
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
|
|
|
|
assert_performed_with(job: ActionMailer::DeliveryJob, at: Time.current.to_f+600.seconds, args: ['DelayedMailer', 'test_message', 'deliver_now', 1, 2, 3]) do
|
|
|
|
@mail.deliver_later wait: 600.seconds
|
|
|
|
end
|
2014-08-12 14:24:19 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
test 'should enqueue a delivery at a specific time' do
|
2014-08-25 10:34:50 -04:00
|
|
|
later_time = Time.now.to_f + 3600
|
|
|
|
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
|
|
|
|
|
|
|
|
end
|