mirror of
https://github.com/discourse/discourse.git
synced 2026-03-03 23:54:20 +08:00
## 🔍 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.
63 lines
2.1 KiB
Ruby
63 lines
2.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe Jobs::DeleteUserPosts do
|
|
fab!(:admin)
|
|
fab!(:user)
|
|
fab!(:topic) { Fabricate(:topic, user: user) }
|
|
fab!(:posts) { Fabricate.times(3, :post, user: user, topic: topic) }
|
|
|
|
it "deletes all posts for the user in batches" do
|
|
expect(user.posts.count).to eq(3)
|
|
described_class.new.execute(user_id: user.id, acting_user_id: admin.id)
|
|
user.reload
|
|
expect(user.post_count).to eq(0)
|
|
expect(topic.reload.posts.count).to eq(0)
|
|
end
|
|
|
|
it "sends a system message with deletion count and invites admins" do
|
|
described_class.new.execute(user_id: user.id, acting_user_id: admin.id)
|
|
system_message = Post.where(user: Discourse.system_user).last
|
|
expect(system_message).to be_present
|
|
expect(system_message.topic.allowed_groups).to include(Group[:admins])
|
|
end
|
|
|
|
it "does nothing if not authorized to delete posts" do
|
|
non_admin = Fabricate(:user)
|
|
expect {
|
|
described_class.new.execute(user_id: user.id, acting_user_id: non_admin.id)
|
|
}.not_to change { user.posts.count }
|
|
|
|
allow_any_instance_of(Guardian).to receive(:can_delete_all_posts?).and_return(false)
|
|
expect {
|
|
described_class.new.execute(user_id: user.id, acting_user_id: admin.id)
|
|
}.not_to change { user.posts.count }
|
|
end
|
|
|
|
it "does nothing if user has no posts" do
|
|
user.posts.destroy_all
|
|
user.reload
|
|
expect {
|
|
described_class.new.execute(user_id: user.id, acting_user_id: admin.id)
|
|
}.not_to change { user.posts.count }
|
|
end
|
|
|
|
it "handles large post counts by batching" do
|
|
Fabricate.times(7, :post, user: user, topic: topic)
|
|
expect(user.posts.count).to eq(10)
|
|
described_class.new.execute(user_id: user.id, acting_user_id: admin.id)
|
|
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
|