FIX: before nuking a user, do a real count of posts instead of relying on user_stat record

This commit is contained in:
Neil Lalonde 2014-08-18 12:07:21 -04:00
parent 5b3a758ba9
commit d273374f1a
3 changed files with 14 additions and 3 deletions

View file

@ -14,7 +14,7 @@ class UserDestroyer
def destroy(user, opts={})
raise Discourse::InvalidParameters.new('user is nil') unless user and user.is_a?(User)
@guardian.ensure_can_delete_user!(user)
raise PostsExistError if !opts[:delete_posts] && user.post_count != 0
raise PostsExistError if !opts[:delete_posts] && user.posts.count != 0
User.transaction do
if opts[:delete_posts]
user.posts.each do |post|

View file

@ -311,8 +311,9 @@ describe Admin::UsersController do
context "user has post" do
before do
@user = build(:user)
@user.stubs(:post_count).returns(1)
@user = Fabricate(:user)
topic = create_topic(user: @user)
post = create_post(topic: topic, user: @user)
@user.stubs(:first_post_created_at).returns(Time.zone.now)
User.expects(:find_by).with(id: @delete_me.id).returns(@user)
end

View file

@ -145,6 +145,16 @@ describe UserDestroyer do
end
end
context 'user has no posts, but user_stats table has post_count > 0' do
before do
# out of sync user_stat data shouldn't break UserDestroyer
@user.user_stat.update_attribute(:post_count, 1)
end
subject(:destroy) { UserDestroyer.new(@user).destroy(@user, {delete_posts: false}) }
include_examples "successfully destroy a user"
end
context 'user has deleted posts' do
let!(:deleted_post) { Fabricate(:post, user: @user, deleted_at: 1.hour.ago) }
it "should mark the user's deleted posts as belonging to a nuked user" do