Skip to content

A platform for community discussion. Free, open, simple.

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

Loïc Guitautd151f4ee9d8a
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