2013-04-11 16:04:20 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
require_dependency 'user_destroyer'
|
|
|
|
|
|
|
|
describe UserDestroyer do
|
2013-04-15 14:52:07 -04:00
|
|
|
|
|
|
|
before do
|
|
|
|
RestClient.stubs(:delete).returns( {success: 'OK'}.to_json )
|
|
|
|
end
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
describe 'new' do
|
|
|
|
it 'raises an error when user is nil' do
|
|
|
|
expect { UserDestroyer.new(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is not a User' do
|
|
|
|
expect { UserDestroyer.new(5) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is a regular user' do
|
|
|
|
expect { UserDestroyer.new( Fabricate(:user) ) }.to raise_error(Discourse::InvalidAccess)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is a moderator' do
|
|
|
|
expect { UserDestroyer.new( Fabricate(:moderator) ) }.to raise_error(Discourse::InvalidAccess)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns an instance of UserDestroyer when user is an admin' do
|
|
|
|
UserDestroyer.new( Fabricate(:admin) ).should be_a(UserDestroyer)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'destroy' do
|
|
|
|
before do
|
|
|
|
@admin = Fabricate(:admin)
|
|
|
|
@user = Fabricate(:user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is nil' do
|
|
|
|
expect { UserDestroyer.new(@admin).destroy(nil) }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error when user is not a User' do
|
|
|
|
expect { UserDestroyer.new(@admin).destroy('nothing') }.to raise_error(Discourse::InvalidParameters)
|
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
shared_examples "successfully destroy a user" do
|
|
|
|
it 'should delete the user' do
|
|
|
|
expect { destroy }.to change { User.count }.by(-1)
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it 'should return the deleted user record' do
|
|
|
|
return_value = destroy
|
|
|
|
return_value.should == @user
|
|
|
|
return_value.should be_destroyed
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it 'should log the action' do
|
|
|
|
StaffActionLogger.any_instance.expects(:log_user_deletion).with(@user).once
|
|
|
|
destroy
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
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
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
2013-04-15 14:52:07 -04:00
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
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)
|
2013-04-15 14:52:07 -04:00
|
|
|
DiscourseHub.expects(:unregister_nickname).never
|
2013-07-24 13:48:55 -04:00
|
|
|
destroy
|
2013-04-15 14:52:07 -04:00
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-25 15:30:03 -04:00
|
|
|
shared_examples "email block list" do
|
|
|
|
it "doesn't add email to block list by default" do
|
|
|
|
BlockedEmail.expects(:block).never
|
|
|
|
destroy
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds email to block list if block_email is true" do
|
|
|
|
b = Fabricate.build(:blocked_email, email: @user.email)
|
|
|
|
BlockedEmail.expects(:block).with(@user.email).returns(b)
|
|
|
|
b.expects(:record_match!).once.returns(true)
|
|
|
|
UserDestroyer.new(@admin).destroy(@user, destroy_opts.merge({block_email: true}))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
context 'user has posts' do
|
|
|
|
let!(:post) { Fabricate(:post, user: @user) }
|
2013-04-11 16:04:20 -04:00
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
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 }
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it 'should raise an error' do
|
|
|
|
expect { destroy }.to raise_error( UserDestroyer::PostsExistError )
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
2013-04-15 14:52:07 -04:00
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it 'should not log the action' do
|
|
|
|
StaffActionLogger.any_instance.expects(:log_user_deletion).never
|
|
|
|
destroy rescue nil
|
2013-04-15 14:52:07 -04:00
|
|
|
end
|
2013-04-29 11:38:43 -04:00
|
|
|
|
2013-07-24 13:48:55 -04:00
|
|
|
it 'should not unregister the user at the discourse hub' do
|
2013-04-29 11:38:43 -04:00
|
|
|
DiscourseHub.expects(:unregister_nickname).never
|
2013-07-24 13:48:55 -04:00
|
|
|
destroy rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "delete_posts is true" do
|
2013-07-25 15:30:03 -04:00
|
|
|
let(:destroy_opts) { {delete_posts: true} }
|
|
|
|
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user, destroy_opts) }
|
2013-07-24 13:48:55 -04:00
|
|
|
|
|
|
|
include_examples "successfully destroy a user"
|
2013-07-25 15:30:03 -04:00
|
|
|
include_examples "email block list"
|
2013-07-24 13:48:55 -04:00
|
|
|
|
|
|
|
it "deletes the posts" do
|
2013-04-29 11:38:43 -04:00
|
|
|
destroy
|
2013-07-24 13:48:55 -04:00
|
|
|
post.reload.deleted_at.should_not be_nil
|
|
|
|
post.nuked_user.should be_true
|
2013-04-29 11:38:43 -04:00
|
|
|
end
|
2013-07-24 13:48:55 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'user has no posts' do
|
|
|
|
context 'and destroy succeeds' do
|
|
|
|
|
2013-07-25 15:30:03 -04:00
|
|
|
let(:destroy_opts) { {} }
|
2013-07-24 13:48:55 -04:00
|
|
|
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
|
|
|
|
|
|
|
|
include_examples "successfully destroy a user"
|
2013-07-25 15:30:03 -04:00
|
|
|
include_examples "email block list"
|
2013-07-10 16:52:34 -04:00
|
|
|
|
|
|
|
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)
|
|
|
|
expect { destroy }.to change { User.count }.by(-1)
|
|
|
|
post.reload.nuked_user.should be_true
|
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'and destroy fails' do
|
2013-07-24 13:48:55 -04:00
|
|
|
subject(:destroy) { UserDestroyer.new(@admin).destroy(@user) }
|
|
|
|
|
2013-04-11 16:04:20 -04:00
|
|
|
before do
|
|
|
|
@user.stubs(:destroy).returns(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should return false' do
|
|
|
|
destroy.should == false
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should not log the action' do
|
2013-07-23 17:58:26 -04:00
|
|
|
StaffActionLogger.any_instance.expects(:log_user_deletion).never
|
2013-04-11 16:04:20 -04:00
|
|
|
destroy
|
|
|
|
end
|
2013-04-15 14:52:07 -04:00
|
|
|
|
|
|
|
it 'should not unregister the user at the discourse hub' do
|
|
|
|
DiscourseHub.expects(:unregister_nickname).never
|
|
|
|
destroy rescue nil
|
|
|
|
end
|
2013-04-11 16:04:20 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|