0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-05 18:09:26 +08:00
discourse/spec/jobs/delete_user_posts_spec.rb
Bannon Tanner 938bf7ac25
FEATURE: Add background job and ability to delete posts from suspend user modal (#36813)
## Background
User wanted admin ability to delete all of a user's posts from the
suspend modal, to reduce the steps needed.

## Changes
Adds a background job that triggers when number of posts to be deleted
is past a certain threshold, which is determined by a new system
setting. Admins get a message in their inbox when the job is complete.

Modifies original admin "delete all posts" flow to utilize the
background job, when necessary.

Added option to the "What would you like to do with the associated post"
dropdown to delete all of a user's posts while suspending them. This
then also obeys the system setting to run the deletion as a background
or foreground job.
2026-01-07 10:25:01 -06:00

51 lines
1.8 KiB
Ruby
Vendored

# 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
end