only remove accounts containing underscores

This commit is contained in:
Sebastian Jambor 2023-08-02 10:38:33 +02:00
parent 97bde32a9c
commit a19c462245
2 changed files with 36 additions and 7 deletions

View file

@ -28,17 +28,13 @@ class Scheduler::OldAccountCleanupScheduler
.where("domain IS NULL") .where("domain IS NULL")
# id -99 is the instance actor # id -99 is the instance actor
.where("id <> -99") .where("id <> -99")
# don't delete admin # only delete accounts whose username contains underscores (those are auto-generated)
.where("username <> 'admin'") .where("username LIKE '%\\_%'")
# don't delete crepels
.where("username <> 'crepels'")
# don't delete alice
.where("username <> 'alice'")
.where("created_at < ?", 1.day.ago) .where("created_at < ?", 1.day.ago)
.order(created_at: :asc) .order(created_at: :asc)
.limit(MAX_DELETIONS_PER_JOB) .limit(MAX_DELETIONS_PER_JOB)
.each do |account| .each do |account|
AccountDeletionWorker.perform_async(account.id, { :reserve_username => false }) AccountDeletionWorker.perform_async(account.id, { 'reserve_username' => false })
end end
end end
end end

View file

@ -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