[deploy] Move banishing spam users to async job (#6745)

* Move banish_user to async

* Fix up specs

* Fix up format
This commit is contained in:
Ben Halpern 2020-03-20 11:21:49 -04:00 committed by GitHub
parent 14c13cb33b
commit 325389467e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 69 additions and 66 deletions

View file

@ -45,13 +45,9 @@ class Internal::UsersController < Internal::ApplicationController
end
def banish
@user = User.find(params[:id])
begin
Moderator::BanishUser.call(admin: current_user, user: @user)
rescue StandardError => e
flash[:danger] = e.message
end
redirect_to "/internal/users/#{@user.id}/edit"
Moderator::BanishUserWorker.perform_async(current_user.id, params[:id].to_i)
flash[:success] = "This user is being banished in the background. The job will complete soon."
redirect_to "/internal/users/#{params[:id]}/edit"
end
def full_delete

View file

@ -0,0 +1,16 @@
module Moderator
class BanishUserWorker
include Sidekiq::Worker
sidekiq_options queue: :high_priority, retry: 10
def perform(admin_id, abuser_id)
abuser = User.find(abuser_id)
admin = User.find(admin_id)
Moderator::BanishUser.call(admin: admin, user: abuser)
rescue StandardError => e
DatadogStatsClient.count("moderators.banishuser", 1, tags: ["action:failed", "user_id:#{abuser.id}"])
Honeybadger.notify(e)
end
end
end

View file

@ -197,64 +197,6 @@ RSpec.describe "Internal::Users", type: :request do
end
end
context "when banishing user" do
def banish_user
post "/internal/users/#{user.id}/banish"
user.reload
end
it "reassigns username and removes profile info" do
user.currently_hacking_on = "currently hackin on !!!!!!!!!!!!"
user.save
banish_user
expect(user.currently_hacking_on).to eq("")
expect(user.username).to include("spam_")
end
it "adds banned role" do
banish_user
expect(user.roles.last.name).to eq("banned")
expect(Note.count).to eq(1)
end
it "deletes user content" do
banish_user
expect(user.reactions.count).to eq(0)
expect(user.comments.count).to eq(0)
expect(user.articles.count).to eq(0)
end
it "removes a user's direct chat channels" do
ChatChannel.create_with_users([user, user2])
expect { banish_user }.to change(user.chat_channels, :count).from(1).to(0)
end
it "removes all follow relationships" do
user.follow(user2)
banish_user
expect(user.follows.count).to eq(0)
end
it "removes a user's classified listings" do
create(:classified_listing, user: user)
banish_user
expect(user.classified_listings.count).to eq(0)
end
it "creates an entry in the BanishedUsers table" do
expect do
banish_user
end.to change(BanishedUser, :count).by(1)
end
it "records who banished a user" do
banish_user
admin = BanishedUser.last
expect(admin.banished_by).to eq super_admin
end
end
context "when handling credits" do
before do
create(:organization_membership, user: super_admin, organization: organization, type_of_user: "admin")

View file

@ -41,8 +41,10 @@ RSpec.describe "internal/users", type: :request do
describe "POST /internal/users/:id/banish" do
it "bans user for spam" do
allow(Moderator::BanishUserWorker).to receive(:perform_async)
post "/internal/users/#{user.id}/banish"
expect(user.reload.username).to include("spam")
expect(Moderator::BanishUserWorker).to have_received(:perform_async).with(admin.id, user.id)
expect(request.flash[:success]).to include("This user is being banished in the background")
end
end

View file

@ -0,0 +1,47 @@
require "rails_helper"
RSpec.describe Moderator::BanishUserWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "high_priority", 1
describe "#perform" do
let(:user) { create(:user, currently_hacking_on: "text is here") }
let(:user2) { create(:user) }
let(:admin) { create(:user, :super_admin) }
before do
create(:article, user_id: user.id)
create(:article, user_id: user.id)
create(:classified_listing, user: user)
ChatChannel.create_with_users([user, user2])
user.follow(user2)
described_class.new.perform(admin.id, user.id)
user.reload
end
it "makes user banned and username spam" do
expect(user.username).to include("spam")
expect(user.has_role?(:banned)).to be true
end
it "deletes user content" do
expect(user.reactions.count).to eq(0)
expect(user.comments.count).to eq(0)
expect(user.articles.count).to eq(0)
expect(user.chat_channels.count).to eq(0)
expect(user.follows.count).to eq(0)
expect(user.classified_listings.count).to eq(0)
end
it "reassigns profile info" do
expect(user.currently_hacking_on).to eq("")
end
it "creates an entry in the BanishedUsers table" do
expect(BanishedUser.all.size).to be 1
end
it "records who banished a user" do
expect(BanishedUser.last.banished_by).to eq admin
end
end
end