test_remove_old_ips_command.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. from datetime import timedelta
  2. from io import StringIO
  3. import pytest
  4. from django.core.management import call_command
  5. from django.utils import timezone
  6. from ...conf.test import override_dynamic_settings
  7. from ..management.commands import removeoldips
  8. IP_STORE_TIME = 2
  9. @pytest.fixture
  10. def user_with_ip(user):
  11. user.joined_from_ip = "127.0.0.1"
  12. user.save()
  13. return user
  14. @pytest.fixture
  15. def user_with_old_ip(user_with_ip):
  16. joined_on_past = timezone.now() - timedelta(days=IP_STORE_TIME + 1)
  17. user_with_ip.joined_on = joined_on_past
  18. user_with_ip.save()
  19. return user_with_ip
  20. def test_recent_user_joined_ip_is_not_removed_by_command(user_with_ip):
  21. call_command(removeoldips.Command(), stdout=StringIO())
  22. user_with_ip.refresh_from_db()
  23. assert user_with_ip.joined_from_ip
  24. @override_dynamic_settings(ip_storage_time=IP_STORE_TIME)
  25. def test_old_user_joined_ip_is_removed_by_command(user_with_old_ip):
  26. call_command(removeoldips.Command(), stdout=StringIO())
  27. user_with_old_ip.refresh_from_db()
  28. assert user_with_old_ip.joined_from_ip is None
  29. @override_dynamic_settings(ip_storage_time=None)
  30. def test_old_user_joined_ip_is_not_removed_by_command_if_removal_is_disabled(
  31. user_with_old_ip
  32. ):
  33. call_command(removeoldips.Command(), stdout=StringIO())
  34. user_with_old_ip.refresh_from_db()
  35. assert user_with_old_ip.joined_from_ip
  36. @override_dynamic_settings(ip_storage_time=IP_STORE_TIME)
  37. def test_command_displays_message_if_old_ip_removal_is_enabled(db):
  38. stdout = StringIO()
  39. call_command(removeoldips.Command(), stdout=stdout)
  40. command_output = stdout.getvalue().splitlines()[0].strip()
  41. assert command_output == "IP addresses older than 2 days have been removed."
  42. @override_dynamic_settings(ip_storage_time=None)
  43. def test_command_displays_message_if_old_ip_removal_is_disabled(db):
  44. stdout = StringIO()
  45. call_command(removeoldips.Command(), stdout=stdout)
  46. command_output = stdout.getvalue().splitlines()[0].strip()
  47. assert command_output == "Old IP removal is disabled."