From 7ce81ceccb2a9425fca9ce81f39db7aef57d9b22 Mon Sep 17 00:00:00 2001 From: evazion Date: Tue, 4 Jan 2022 17:57:16 -0600 Subject: [PATCH] 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. --- app/helpers/users_helper.rb | 2 +- test/unit/user_mailer_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index c4d94758f..8c1ad5074 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -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 diff --git a/test/unit/user_mailer_test.rb b/test/unit/user_mailer_test.rb index b01ae5564..25387a72a 100644 --- a/test/unit/user_mailer_test.rb +++ b/test/unit/user_mailer_test.rb @@ -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