Skip to content

Commit

Permalink
Pass default_url_options from mailer
Browse files Browse the repository at this point in the history
  • Loading branch information
djfpaagman committed Nov 28, 2023
1 parent cd0e0ed commit 714a154
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/mailers/passwordless/mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ def sign_in(session, token = nil)
session,
action: "confirm",
id: session.to_param,
token: @token
token: @token,
**default_url_options
)

email_field = session.authenticatable.class.passwordless_email_field
Expand Down
4 changes: 4 additions & 0 deletions test/dummy/app/mailers/application_mailer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@
class ApplicationMailer < ActionMailer::Base
default from: "from@example.com"
layout "mailer"

def default_url_options
{ host: "example.com" }
end
end
16 changes: 16 additions & 0 deletions test/mailers/passwordless/mailer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,20 @@ class Passwordless::MailerTest < ActionMailer::TestCase
assert_match /sign in: hello\n/, email.body.to_s
assert_match %r{/admins/sign_in/#{session.identifier}/hello}, email.body.to_s
end

test("uses default_url_options from config.action_mailer") do
session = Passwordless::Session.create!(authenticatable: users(:alice), token: "hello")
email = Passwordless::Mailer.sign_in(session, "hello")

assert_match %r{localhost:3000/users/sign_in/#{session.identifier}/hello}, email.body.to_s
end

test("uses default_url_options from mailer") do
WithConfig.with_config({parent_mailer: "ApplicationMailer"}) do
session = Passwordless::Session.create!(authenticatable: users(:alice), token: "hello")
email = Passwordless::Mailer.sign_in(session, "hello")

assert_match %r{example.com/users/sign_in/#{session.identifier}/hello}, email.body.to_s
end
end
end
5 changes: 5 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ def with_config(options)
end
end

# We need to reload the application, because the config can set a different
# parent_mailer class, which means `Passwordless::Mailer` needs to be
# reloaded. Simply reloading the whole application seems to be the easiest.
Rails.application.reloader.reload!

yield
ensure
Passwordless.reset_config!
Expand Down

0 comments on commit 714a154

Please sign in to comment.