mirror of
https://github.com/discourse/discourse.git
synced 2026-08-02 09:29:37 -05:00
FIX: return nil correctly for nil check (#37460)
## 🔍 Overview This update is a follow-up to: https://github.com/discourse/discourse/pull/36813. The PR had a follow-up adding `nil` checks to the `DeleteUserPosts` job, but since the look-ups were using `find` instead of `find_by` a `RecordNotFound` error would be returned instead of `nil`. This update ensures that we use `find_by` instead so that the `nil` checks work correctly. Additionally, we add some tests to ensure the job isn't triggered when the params are missing.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user