emails: fix exception when user signs up without an email.

Fix an `ActionView::Template::Error: undefined method 'verification_key'
for nil` error in the welcome_user mailer when a user signs up without
an email address.

Caused by the fact that we now render mail templates regardless of
whether the user has an email address, and then skip sending the email
only after the mail template is rendered.
This commit is contained in:
evazion
2022-01-04 17:57:16 -06:00
parent 1ccc74adca
commit 7ce81ceccb
2 changed files with 13 additions and 1 deletions

View File

@@ -21,6 +21,6 @@ module UsersHelper
end
def email_verification_url(user)
verify_user_email_url(user, email_verification_key: user.email_address.verification_key)
verify_user_email_url(user, email_verification_key: user.email_address&.verification_key)
end
end

View File

@@ -48,6 +48,12 @@ class UserMailerTest < ActionMailer::TestCase
mail = UserMailer.email_change_confirmation(@user)
assert_emails(1) { mail.deliver_now }
end
should "not fail for a user without an email address" do
@user = create(:user)
mail = UserMailer.email_change_confirmation(@user)
assert_emails(0) { mail.deliver_now }
end
end
context "welcome_user method" do
@@ -55,6 +61,12 @@ class UserMailerTest < ActionMailer::TestCase
mail = UserMailer.welcome_user(@user)
assert_emails(1) { mail.deliver_now }
end
should "not fail for a user without an email address" do
@user = create(:user)
mail = UserMailer.welcome_user(@user)
assert_emails(0) { mail.deliver_now }
end
end
end
end