From 6b5ba5c3bebf55f5bdfa5f3f5b4916127026285a Mon Sep 17 00:00:00 2001 From: Julian Gallimore Date: Tue, 19 Dec 2023 10:01:50 +0100 Subject: [PATCH] Sends recover email to main email address & fallback Issue on production with user not recieving email on their main email address, only getting sent to the fallback email. Decided to just send one email with both address (doubt why, should just be the email used in recovery). --- app/mailers/user_mailer.rb | 4 +--- spec/mailers/user_mailer_spec.rb | 13 +++++++++++-- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 7a87baf3..6e9e9cfc 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -65,9 +65,7 @@ def account_recovery_instructions user_id @user = User.find(user_id) emails = [@user.email_string, @user.email_fallback].reject(&:blank?) - emails.each do |email| - mail(to: email, from: "FabLabs.io ", subject: "Account Recovery Instructions") - end + mail(to: emails, from: "FabLabs.io ", subject: "Account Recovery Instructions") rescue ActiveRecord::RecordNotFound end end diff --git a/spec/mailers/user_mailer_spec.rb b/spec/mailers/user_mailer_spec.rb index df1b9f62..d8235458 100644 --- a/spec/mailers/user_mailer_spec.rb +++ b/spec/mailers/user_mailer_spec.rb @@ -2,8 +2,10 @@ describe UserMailer, type: :mailer do + let(:user_emails) { ['email@bitsushi.com', 'fallback@example.com'] } + let(:lab) { FactoryBot.create(:lab) } - let(:user) { FactoryBot.create(:user) } + let(:user) { FactoryBot.create(:user, email: user_emails[0], email_fallback: user_emails[1]) } let(:employee) { FactoryBot.create(:employee, user: user, lab: lab) } %w( @@ -57,9 +59,16 @@ recovery = FactoryBot.create(:recovery, user: user, email_or_username: [user.email, user.username].sample) mail = UserMailer.account_recovery_instructions(user.id) expect(mail.subject).to match("Account Recovery Instructions") - expect(mail.to).to eq([user.email]) + expect(mail.to).to eq(['email@bitsushi.com', 'fallback@example.com']) expect(mail.from).to eq(["support@fablabs.io"]) expect(mail.body.encoded).to match( recovery_url(user.recovery_key) ) end + it "account_recovery_instructions_single" do + simpleuser = FactoryBot.create(:user, email: 'one@example.com') + recovery = FactoryBot.create(:recovery, user: simpleuser, email_or_username: [simpleuser.email, simpleuser.username].sample) + mail = UserMailer.account_recovery_instructions(simpleuser.id) + expect(mail.to).to eq(['one@example.com']) + end + end