diff --git a/app/services/moderator/banish_user.rb b/app/services/moderator/banish_user.rb index 17f43a5cb..384764e49 100644 --- a/app/services/moderator/banish_user.rb +++ b/app/services/moderator/banish_user.rb @@ -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 diff --git a/spec/factories/organization_memberships.rb b/spec/factories/organization_memberships.rb index 5c756fcfb..34bcd4fa1 100644 --- a/spec/factories/organization_memberships.rb +++ b/spec/factories/organization_memberships.rb @@ -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| diff --git a/spec/services/moderator/banish_user_spec.rb b/spec/services/moderator/banish_user_spec.rb index 9177ca132..96e0f54aa 100644 --- a/spec/services/moderator/banish_user_spec.rb +++ b/spec/services/moderator/banish_user_spec.rb @@ -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 diff --git a/spec/workers/moderator/banish_user_worker_spec.rb b/spec/workers/moderator/banish_user_worker_spec.rb index 02976f9d3..c13b34e34 100644 --- a/spec/workers/moderator/banish_user_worker_spec.rb +++ b/spec/workers/moderator/banish_user_worker_spec.rb @@ -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) }