user_cleanup_scheduler_spec.rb 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Scheduler::UserCleanupScheduler do
  4. subject { described_class.new }
  5. let!(:new_unconfirmed_user) { Fabricate(:user) }
  6. let!(:old_unconfirmed_user) { Fabricate(:user) }
  7. let!(:confirmed_user) { Fabricate(:user) }
  8. let!(:moderation_note) { Fabricate(:account_moderation_note, account: Fabricate(:account), target_account: old_unconfirmed_user.account) }
  9. describe '#perform' do
  10. before do
  11. # Update already-existing users because initialization overrides `confirmation_sent_at`
  12. new_unconfirmed_user.update!(confirmed_at: nil, confirmation_sent_at: Time.now.utc)
  13. old_unconfirmed_user.update!(confirmed_at: nil, confirmation_sent_at: 10.days.ago)
  14. confirmed_user.update!(confirmed_at: 1.day.ago)
  15. end
  16. it 'deletes the old unconfirmed user and metadata while preserving confirmed user and newer unconfirmed user' do
  17. expect { subject.perform }
  18. .to change { User.exists?(old_unconfirmed_user.id) }
  19. .from(true).to(false)
  20. .and change { Account.exists?(old_unconfirmed_user.account_id) }
  21. .from(true).to(false)
  22. expect { moderation_note.reload }
  23. .to raise_error(ActiveRecord::RecordNotFound)
  24. expect_preservation_of(new_unconfirmed_user)
  25. expect_preservation_of(confirmed_user)
  26. end
  27. private
  28. def expect_preservation_of(user)
  29. expect(User.exists?(user.id))
  30. .to be true
  31. expect(Account.exists?(user.account_id))
  32. .to be true
  33. end
  34. end
  35. end