ip_cleanup_scheduler_spec.rb 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Scheduler::IpCleanupScheduler do
  4. let(:worker) { described_class.new }
  5. describe '#perform' do
  6. context 'with IP-related data past retention times' do
  7. let!(:future_ip_block) { Fabricate :ip_block, expires_at: 1.week.from_now }
  8. let!(:old_ip_block) { Fabricate :ip_block, expires_at: 1.week.ago }
  9. let!(:session_past_retention) { Fabricate :session_activation, ip: '10.0.0.0', updated_at: 18.months.ago }
  10. let!(:inactive_user) { Fabricate :user, current_sign_in_at: 18.months.ago, sign_up_ip: '10.0.0.0' }
  11. let!(:old_login_activity) { Fabricate :login_activity, created_at: 18.months.ago }
  12. let!(:old_token) { Fabricate :access_token, last_used_at: 18.months.ago, last_used_ip: '10.0.0.0' }
  13. before { stub_const 'Scheduler::IpCleanupScheduler::SESSION_RETENTION_PERIOD', 10.years.to_i.seconds }
  14. it 'deletes the expired block' do
  15. expect { worker.perform }
  16. .to_not raise_error
  17. expect { old_ip_block.reload }
  18. .to raise_error(ActiveRecord::RecordNotFound)
  19. expect { old_login_activity.reload }
  20. .to raise_error(ActiveRecord::RecordNotFound)
  21. expect(session_past_retention.reload.ip)
  22. .to be_nil
  23. expect(inactive_user.reload.sign_up_ip)
  24. .to be_nil
  25. expect(old_token.reload.last_used_ip)
  26. .to be_nil
  27. expect(future_ip_block.reload)
  28. .to be_present
  29. end
  30. end
  31. context 'with old session data' do
  32. let!(:new_activation) { Fabricate :session_activation, updated_at: 1.week.ago }
  33. let!(:old_activation) { Fabricate :session_activation, updated_at: 1.month.ago }
  34. before { stub_const 'Scheduler::IpCleanupScheduler::SESSION_RETENTION_PERIOD', 10.days.to_i.seconds }
  35. it 'clears old sessions' do
  36. expect { worker.perform }
  37. .to_not raise_error
  38. expect { old_activation.reload }
  39. .to raise_error(ActiveRecord::RecordNotFound)
  40. expect(new_activation.reload)
  41. .to be_present
  42. end
  43. end
  44. end
  45. end