Use DeleteUserWorker for removing users via spam logs

Before deleting a user via a SpamLog would just call `user.destroy`,
which may omit other things that need to be cleaned up.
This commit is contained in:
Stan Hu 2017-04-16 14:54:41 -07:00
parent dbcfbd68b6
commit fd60561936
3 changed files with 12 additions and 5 deletions

View File

@ -7,7 +7,7 @@ class Admin::SpamLogsController < Admin::ApplicationController
spam_log = SpamLog.find(params[:id])
if params[:remove_user]
spam_log.remove_user
spam_log.remove_user(deleted_by: current_user)
redirect_to admin_spam_logs_path, notice: "User #{spam_log.user.username} was successfully removed."
else
spam_log.destroy

View File

@ -3,9 +3,9 @@ class SpamLog < ActiveRecord::Base
validates :user, presence: true
def remove_user
def remove_user(deleted_by:)
user.block
user.destroy
DeleteUserWorker.perform_async(deleted_by.id, user.id, delete_solo_owned_groups: true, hard_delete: true)
end
def text

View File

@ -1,6 +1,8 @@
require 'spec_helper'
describe SpamLog, models: true do
let(:admin) { create(:admin) }
describe 'associations' do
it { is_expected.to belong_to(:user) }
end
@ -13,13 +15,18 @@ describe SpamLog, models: true do
it 'blocks the user' do
spam_log = build(:spam_log)
expect { spam_log.remove_user }.to change { spam_log.user.blocked? }.to(true)
expect { spam_log.remove_user(deleted_by: admin) }.to change { spam_log.user.blocked? }.to(true)
end
it 'removes the user' do
spam_log = build(:spam_log)
user = spam_log.user
expect { spam_log.remove_user }.to change { User.count }.by(-1)
Sidekiq::Testing.inline! do
spam_log.remove_user(deleted_by: admin)
end
expect { User.find(user.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end