FIX: Don’t assume post is available in UserEmail job (#21054)

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.
This commit is contained in:
Loïc Guitaut 2023-04-12 04:34:22 +02:00 committed by GitHub
parent dfce983fc7
commit d151f4ee9d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -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

View File

@ -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