test_clearreadtracker.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. from datetime import timedelta
  2. from io import StringIO
  3. from django.contrib.auth import get_user_model
  4. from django.core.management import call_command
  5. from django.test import TestCase
  6. from django.utils import timezone
  7. from misago.categories.models import Category
  8. from misago.conf import settings
  9. from misago.readtracker.management.commands import clearreadtracker
  10. from misago.readtracker.models import PostRead
  11. from misago.threads import testutils
  12. UserModel = get_user_model()
  13. class ClearReadTrackerTests(TestCase):
  14. def setUp(self):
  15. self.user_a = UserModel.objects.create_user("UserA", "testa@user.com", 'Pass.123')
  16. self.user_b = UserModel.objects.create_user("UserB", "testb@user.com", 'Pass.123')
  17. self.category = Category.objects.get(slug='first-category')
  18. def test_no_deleted(self):
  19. """command works when there are no attachments"""
  20. command = clearreadtracker.Command()
  21. out = StringIO()
  22. call_command(command, stdout=out)
  23. command_output = out.getvalue().strip()
  24. self.assertEqual(command_output, "No expired entries were found")
  25. def test_delete_expired_entries(self):
  26. """test deletes one expired tracker entry, but spares the other"""
  27. thread = testutils.post_thread(self.category)
  28. existing = PostRead.objects.create(
  29. user=self.user_a,
  30. category=self.category,
  31. thread=thread,
  32. post=thread.first_post,
  33. last_read_on=timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF / 4)
  34. )
  35. deleted = PostRead.objects.create(
  36. user=self.user_b,
  37. category=self.category,
  38. thread=thread,
  39. post=thread.first_post,
  40. last_read_on=timezone.now() - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF * 2)
  41. )
  42. command = clearreadtracker.Command()
  43. out = StringIO()
  44. call_command(command, stdout=out)
  45. command_output = out.getvalue().strip()
  46. self.assertEqual(command_output, "Deleted 1 expired entries")
  47. PostRead.objects.get(pk=existing.pk)
  48. with self.assertRaises(PostRead.DoesNotExist):
  49. PostRead.objects.get(pk=deleted.pk)