From a19c4622452353fd7f3a76c04062779c0587b9b5 Mon Sep 17 00:00:00 2001 From: Sebastian Jambor Date: Wed, 2 Aug 2023 10:38:33 +0200 Subject: [PATCH] only remove accounts containing underscores --- .../old_account_cleanup_scheduler.rb | 10 ++---- .../old_account_cleanup_scheduler_spec.rb | 33 +++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 spec/workers/scheduler/old_account_cleanup_scheduler_spec.rb diff --git a/app/workers/scheduler/old_account_cleanup_scheduler.rb b/app/workers/scheduler/old_account_cleanup_scheduler.rb index aaf54ebaf..b9a6deb92 100644 --- a/app/workers/scheduler/old_account_cleanup_scheduler.rb +++ b/app/workers/scheduler/old_account_cleanup_scheduler.rb @@ -28,17 +28,13 @@ class Scheduler::OldAccountCleanupScheduler .where("domain IS NULL") # id -99 is the instance actor .where("id <> -99") - # don't delete admin - .where("username <> 'admin'") - # don't delete crepels - .where("username <> 'crepels'") - # don't delete alice - .where("username <> 'alice'") + # only delete accounts whose username contains underscores (those are auto-generated) + .where("username LIKE '%\\_%'") .where("created_at < ?", 1.day.ago) .order(created_at: :asc) .limit(MAX_DELETIONS_PER_JOB) .each do |account| - AccountDeletionWorker.perform_async(account.id, { :reserve_username => false }) + AccountDeletionWorker.perform_async(account.id, { 'reserve_username' => false }) end end end diff --git a/spec/workers/scheduler/old_account_cleanup_scheduler_spec.rb b/spec/workers/scheduler/old_account_cleanup_scheduler_spec.rb new file mode 100644 index 000000000..949214761 --- /dev/null +++ b/spec/workers/scheduler/old_account_cleanup_scheduler_spec.rb @@ -0,0 +1,33 @@ +require 'json' +require 'rails_helper' + +RSpec.describe Scheduler::OldAccountCleanupScheduler do + subject { described_class.new } + let!(:generated_user) { Fabricate(:account, username: 'containing_underscore', created_at: 25.hours.ago) } + let!(:alice) { Fabricate(:account, username: 'alice', created_at: 25.hours.ago) } + let!(:generated_user_other_instance) { Fabricate(:account, username: 'containing_underscore', domain: 'example.com', created_at: 25.hours.ago) } + let!(:instance_actor) { Fabricate(:account, id: 99, created_at: 25.hours.ago) } + + describe '#perform' do + it 'removes auto-generated user-accounts that are older than one day' do + expect { subject.perform }.to change { Account.exists?(generated_user.id) }.from(true).to(false) + end + + it 'does not remove auto-generated user-accounts that are younger than one day' do + generated_user.update!(created_at: 23.hours.ago) + expect { subject.perform }.not_to change { Account.exists?(generated_user.id) }.from(true) + end + + it 'does not remove accounts with underscores from other instances' do + expect { subject.perform }.not_to change { Account.exists?(generated_user_other_instance.id) }.from(true) + end + + it 'does not remove accounts without underscores' do + expect { subject.perform }.not_to change { Account.exists?(alice.id) }.from(true) + end + + it 'does not remove instance actor' do + expect { subject.perform }.not_to change { Account.exists?(instance_actor.id) }.from(true) + end + end +end