2017-07-23 11:17:16 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-01-28 05:20:46 -05:00
|
|
|
require "abstract_unit"
|
|
|
|
require "active_job"
|
|
|
|
require "mailers/params_mailer"
|
|
|
|
|
|
|
|
class ParameterizedTest < ActiveSupport::TestCase
|
|
|
|
include ActiveJob::TestHelper
|
|
|
|
|
2018-11-30 17:33:35 -05:00
|
|
|
class DummyDeliveryJob < ActionMailer::MailDeliveryJob
|
2018-11-01 16:32:01 -04:00
|
|
|
end
|
|
|
|
|
2017-01-28 05:20:46 -05:00
|
|
|
setup do
|
|
|
|
@previous_logger = ActiveJob::Base.logger
|
|
|
|
ActiveJob::Base.logger = Logger.new(nil)
|
|
|
|
|
2017-01-28 10:47:16 -05:00
|
|
|
@previous_delivery_method = ActionMailer::Base.delivery_method
|
2017-01-28 05:20:46 -05:00
|
|
|
ActionMailer::Base.delivery_method = :test
|
|
|
|
|
|
|
|
@previous_deliver_later_queue_name = ActionMailer::Base.deliver_later_queue_name
|
|
|
|
ActionMailer::Base.deliver_later_queue_name = :test_queue
|
|
|
|
|
|
|
|
@mail = ParamsMailer.with(inviter: "david@basecamp.com", invitee: "jason@basecamp.com").invitation
|
|
|
|
end
|
|
|
|
|
|
|
|
teardown do
|
|
|
|
ActiveJob::Base.logger = @previous_logger
|
|
|
|
ParamsMailer.deliveries.clear
|
|
|
|
|
|
|
|
ActionMailer::Base.delivery_method = @previous_delivery_method
|
|
|
|
ActionMailer::Base.deliver_later_queue_name = @previous_deliver_later_queue_name
|
|
|
|
end
|
|
|
|
|
|
|
|
test "parameterized headers" do
|
|
|
|
assert_equal(["jason@basecamp.com"], @mail.to)
|
|
|
|
assert_equal(["david@basecamp.com"], @mail.from)
|
|
|
|
assert_equal("So says david@basecamp.com", @mail.body.encoded)
|
|
|
|
end
|
|
|
|
|
2017-01-30 13:28:31 -05:00
|
|
|
test "enqueue the email with params" do
|
2018-11-01 16:32:01 -04:00
|
|
|
args = [
|
|
|
|
"ParamsMailer",
|
|
|
|
"invitation",
|
|
|
|
"deliver_now",
|
2018-11-30 17:33:35 -05:00
|
|
|
params: { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" },
|
|
|
|
args: [],
|
2018-11-01 16:32:01 -04:00
|
|
|
]
|
2018-11-30 17:33:35 -05:00
|
|
|
assert_performed_with(job: ActionMailer::MailDeliveryJob, args: args) do
|
2017-01-28 05:20:46 -05:00
|
|
|
@mail.deliver_later
|
|
|
|
end
|
|
|
|
end
|
2017-01-30 13:28:14 -05:00
|
|
|
|
|
|
|
test "respond_to?" do
|
|
|
|
mailer = ParamsMailer.with(inviter: "david@basecamp.com", invitee: "jason@basecamp.com")
|
|
|
|
|
|
|
|
assert_respond_to mailer, :invitation
|
|
|
|
assert_not_respond_to mailer, :anything
|
|
|
|
|
|
|
|
invitation = mailer.method(:invitation)
|
|
|
|
assert_equal Method, invitation.class
|
|
|
|
|
|
|
|
assert_raises(NameError) do
|
|
|
|
invitation = mailer.method(:anything)
|
|
|
|
end
|
|
|
|
end
|
2018-10-05 11:53:28 -04:00
|
|
|
|
|
|
|
test "should enqueue a parameterized request with the correct delivery job" do
|
2018-11-01 16:32:01 -04:00
|
|
|
args = [
|
|
|
|
"ParamsMailer",
|
|
|
|
"invitation",
|
|
|
|
"deliver_now",
|
2018-11-30 17:33:35 -05:00
|
|
|
params: { inviter: "david@basecamp.com", invitee: "jason@basecamp.com" },
|
|
|
|
args: [],
|
2018-11-01 16:32:01 -04:00
|
|
|
]
|
|
|
|
|
|
|
|
with_delivery_job DummyDeliveryJob do
|
|
|
|
assert_performed_with(job: DummyDeliveryJob, args: args) do
|
|
|
|
@mail.deliver_later
|
|
|
|
end
|
2018-10-05 11:53:28 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-01 16:32:01 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def with_delivery_job(job)
|
|
|
|
old_delivery_job = ParamsMailer.delivery_job
|
|
|
|
ParamsMailer.delivery_job = job
|
2018-11-30 17:37:00 -05:00
|
|
|
yield
|
2018-11-01 16:32:01 -04:00
|
|
|
ensure
|
|
|
|
ParamsMailer.delivery_job = old_delivery_job
|
|
|
|
end
|
2017-01-28 05:20:46 -05:00
|
|
|
end
|