add a way to delete posts and topics when deleting a user with UserDestroyer

This commit is contained in:
Neil Lalonde
2013-07-24 13:48:55 -04:00
parent a8df9778b5
commit e25638dab0
6 changed files with 87 additions and 49 deletions

View File

@ -1008,9 +1008,9 @@ describe Guardian do
end
context "for admins" do
it "is false if user has posts" do
Fabricate(:post, user: user)
Guardian.new(admin).can_delete_user?(user).should be_false
it "is true if user has posts" do # UserDestroyer is responsible for checking for posts
user.stubs(:post_count).returns(1)
Guardian.new(admin).can_delete_user?(user).should be_true
end
it "is true if user has no posts" do

View File

@ -35,8 +35,6 @@ describe UserDestroyer do
@user = Fabricate(:user)
end
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
it 'raises an error when user is nil' do
expect { UserDestroyer.new(@admin).destroy(nil) }.to raise_error(Discourse::InvalidParameters)
end
@ -45,58 +43,79 @@ describe UserDestroyer do
expect { UserDestroyer.new(@admin).destroy('nothing') }.to raise_error(Discourse::InvalidParameters)
end
context 'user has posts' do
before do
Fabricate(:post, user: @user)
shared_examples "successfully destroy a user" do
it 'should delete the user' do
expect { destroy }.to change { User.count }.by(-1)
end
it 'should not delete the user' do
expect { destroy rescue nil }.to_not change { User.count }
it 'should return the deleted user record' do
return_value = destroy
return_value.should == @user
return_value.should be_destroyed
end
it 'should raise an error' do
expect { destroy }.to raise_error( UserDestroyer::PostsExistError )
it 'should log the action' do
StaffActionLogger.any_instance.expects(:log_user_deletion).with(@user).once
destroy
end
it 'should not log the action' do
StaffActionLogger.any_instance.expects(:log_user_deletion).never
destroy rescue nil
it 'should unregister the nickname as the discourse hub if hub integration is enabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.expects(:unregister_nickname).with(@user.username)
destroy
end
it 'should not unregister the user at the discourse hub' do
it 'should not try to unregister the nickname as the discourse hub if hub integration is disabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:unregister_nickname).never
destroy rescue nil
destroy
end
end
context 'user has posts' do
let!(:post) { Fabricate(:post, user: @user) }
context "delete_posts is false" do
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
it 'should not delete the user' do
expect { destroy rescue nil }.to_not change { User.count }
end
it 'should raise an error' do
expect { destroy }.to raise_error( UserDestroyer::PostsExistError )
end
it 'should not log the action' do
StaffActionLogger.any_instance.expects(:log_user_deletion).never
destroy rescue nil
end
it 'should not unregister the user at the discourse hub' do
DiscourseHub.expects(:unregister_nickname).never
destroy rescue nil
end
end
context "delete_posts is true" do
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user, delete_posts: true) }
include_examples "successfully destroy a user"
it "deletes the posts" do
destroy
post.reload.deleted_at.should_not be_nil
post.nuked_user.should be_true
end
end
end
context 'user has no posts' do
context 'and destroy succeeds' do
it 'should delete the user' do
expect { destroy }.to change { User.count }.by(-1)
end
it 'should return the deleted user record' do
return_value = destroy
return_value.should == @user
return_value.should be_destroyed
end
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
it 'should log the action' do
StaffActionLogger.any_instance.expects(:log_user_deletion).with(@user).once
destroy
end
it 'should unregister the nickname as the discourse hub if hub integration is enabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.expects(:unregister_nickname).with(@user.username)
destroy
end
it 'should not try to unregister the nickname as the discourse hub if hub integration is disabled' do
SiteSetting.stubs(:call_discourse_hub?).returns(false)
DiscourseHub.expects(:unregister_nickname).never
destroy
end
include_examples "successfully destroy a user"
it "should mark the user's deleted posts as belonging to a nuked user" do
post = Fabricate(:post, user: @user, deleted_at: 1.hour.ago)
@ -106,6 +125,8 @@ describe UserDestroyer do
end
context 'and destroy fails' do
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
before do
@user.stubs(:destroy).returns(false)
end