BanishUser also destroy organization (#19911)

This commit is contained in:
Mac Siri 2023-08-10 08:26:45 -04:00 committed by GitHub
parent 7db08e34b8
commit 1c351bf22c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 17 additions and 3 deletions

View file

@ -18,6 +18,7 @@ module Moderator
user.remove_from_mailchimp_newsletters if user.email?
remove_profile_info
handle_user_status("Suspended", I18n.t("services.moderator.banish_user.spam_account"))
delete_organization
delete_user_activity
delete_comments
delete_articles
@ -48,5 +49,11 @@ module Moderator
def delete_vomit_reactions
Reaction.where(reactable: user, category: "vomit").delete_all
end
def delete_organization
user.organizations.each do |organization|
organization.destroy! if organization.users.count == 1
end
end
end
end

View file

@ -1,7 +1,7 @@
FactoryBot.define do
factory :organization_membership do
association :user, factory: :user, strategy: :create
association :organization, factory: :organization, strategy: :create
user
organization
type_of_user { "member" }
after(:build) do |organization_membership|

View file

@ -22,13 +22,17 @@ RSpec.describe Moderator::BanishUser, type: :service do
expect(BanishedUser.exists?(username: original_username)).to be true
end
it "removes all their articles, comments, podcasts, abuse_reports, and vomit reactions", :aggregate_failures do
it "removes everything", :aggregate_failures do
# should remove:
# articles, comments, podcasts, organizations, abuse_reports, and vomit reactions
article = create(:article, user: user, published: true)
podcast = create(:podcast, creator: user)
create(:podcast_ownership, owner: user, podcast: podcast)
create(:comment, user: user, commentable: article)
create(:reaction, category: "vomit", reactable: user, user: moderator)
create(:feedback_message, :abuse_report, reporter_id: moderator.id, offender_id: user.id)
create(:organization_membership, user: user, type_of_user: "admin")
expect do
sidekiq_perform_enqueued_jobs do
@ -37,7 +41,9 @@ RSpec.describe Moderator::BanishUser, type: :service do
end.to change { user.comments.count }.by(-1)
.and change { user.articles.count }.by(-1)
.and change { user.created_podcasts.count }.by(-1)
.and change { user.organizations.count }.by(-1)
.and change { Reaction.where(reactable: user).count }.by(-1)
.and change { user.offender_feedback_messages.first.status }.from("Open").to("Resolved")
.and change(Organization, :count).by(-1)
end
end

View file

@ -3,6 +3,7 @@ require "rails_helper"
RSpec.describe Moderator::BanishUserWorker, type: :worker do
include_examples "#enqueues_on_correct_queue", "high_priority", 1
# TODO: Remove this test because BanihedUser already covers it
describe "#perform" do
let(:user) { create(:user) }
let(:user2) { create(:user) }