From d151f4ee9d8a3139c3ad2c2c4bf15269e8c67ce8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Guitaut?= Date: Wed, 12 Apr 2023 04:34:22 +0200 Subject: [PATCH] =?UTF-8?q?FIX:=20Don=E2=80=99t=20assume=20post=20is=20ava?= =?UTF-8?q?ilable=20in=20UserEmail=20job=20(#21054)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Currently, we’re performing a check when a user is suspended in the `UserEmail` job and we’re assuming a `post` is always available, which is not the case. The code indeed breaks when the job is called with the `account_suspended` type option. This patch fixes this issue by making the check use the safe navigation operator, thus making it working when `post` is not provided. --- app/jobs/regular/user_email.rb | 2 +- spec/jobs/user_email_spec.rb | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/app/jobs/regular/user_email.rb b/app/jobs/regular/user_email.rb index 43ca70d6d46..f12165e2c94 100644 --- a/app/jobs/regular/user_email.rb +++ b/app/jobs/regular/user_email.rb @@ -113,7 +113,7 @@ module Jobs if user.suspended? if !type.in?(%w[user_private_message account_suspended]) return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended_not_pm]) - elsif post.topic.group_pm? + elsif post&.topic&.group_pm? return skip_message(SkippedEmailLog.reason_types[:user_email_user_suspended]) end end diff --git a/spec/jobs/user_email_spec.rb b/spec/jobs/user_email_spec.rb index 4d45bed6f85..749b9ad2601 100644 --- a/spec/jobs/user_email_spec.rb +++ b/spec/jobs/user_email_spec.rb @@ -930,5 +930,24 @@ RSpec.describe Jobs::UserEmail do end end end + + context "without post" do + context "when user is suspended" do + subject(:send_email) do + described_class.new.execute( + type: :account_suspended, + user_id: suspended.id, + user_history_id: user_history.id, + ) + end + + let(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:suspend_user]) } + + it "does send an email" do + send_email + expect(ActionMailer::Base.deliveries.first.to).to contain_exactly(suspended.email) + end + end + end end end