Skip to content

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

FIX: return nil correctly for nil check (#37460)

Keegan Georgefe36d9ae0d3f
diff --git a/app/jobs/regular/delete_user_posts.rb b/app/jobs/regular/delete_user_posts.rb
index 19a6e06bc65..a6187bc0941 100644
--- a/app/jobs/regular/delete_user_posts.rb
+++ b/app/jobs/regular/delete_user_posts.rb
@@ -5,10 +5,10 @@ module Jobs
sidekiq_options queue: "critical"
def execute(args)
- user = User.find(args[:user_id])
+ user = User.find_by(id: args[:user_id])
return if user.nil?
- acting_user = User.find(args[:acting_user_id]) if args[:acting_user_id]
+ acting_user = User.find_by(id: args[:acting_user_id])
return if acting_user.nil?
guardian = Guardian.new(acting_user)
diff --git a/spec/jobs/delete_user_posts_spec.rb b/spec/jobs/delete_user_posts_spec.rb
index 36a6b867aea..8db849e5b30 100644
--- a/spec/jobs/delete_user_posts_spec.rb
+++ b/spec/jobs/delete_user_posts_spec.rb
@@ -48,4 +48,16 @@ RSpec.describe Jobs::DeleteUserPosts do
user.reload
expect(user.posts.count).to eq(0)
end
+
+ it "does nothing if user does not exist" do
+ expect {
+ described_class.new.execute(user_id: -999, acting_user_id: admin.id)
+ }.not_to raise_error
+ end
+
+ it "does nothing if acting_user does not exist" do
+ expect {
+ described_class.new.execute(user_id: user.id, acting_user_id: -999)
+ }.not_to raise_error
+ end
end