test_deleteoldnotifications.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.contrib.auth import get_user_model
  4. from django.test import TestCase
  5. from django.utils import timezone
  6. from django.utils.six import StringIO
  7. from misago.notifications import notify_user
  8. from misago.notifications.management.commands import deleteoldnotifications
  9. class DeleteOldNotificatinsTests(TestCase):
  10. def test_regen_blank_avatar(self):
  11. """command deletes old notifications"""
  12. # create user
  13. User = get_user_model()
  14. user = User.objects.create_user("Bob", "bob@boberson.com", "Pass.123")
  15. # notify him
  16. notify_user(user, "Hello Bob!", "/")
  17. # run command
  18. command = deleteoldnotifications.Command()
  19. out = StringIO()
  20. command.execute(stdout=out)
  21. command_output = out.getvalue().splitlines()[0].strip()
  22. self.assertEqual(command_output, 'Old notifications have been deleted')
  23. self.assertEqual(user.misago_notifications.count(), 1)
  24. # outdate notifications
  25. cutoff = timedelta(days=settings.MISAGO_NOTIFICATIONS_MAX_AGE * 2)
  26. cutoff_date = timezone.now() - cutoff
  27. user.misago_notifications.update(date=cutoff_date)
  28. # run command again
  29. out = StringIO()
  30. command.execute(stdout=out)
  31. command_output = out.getvalue().splitlines()[0].strip()
  32. self.assertEqual(command_output, 'Old notifications have been deleted')
  33. self.assertEqual(user.misago_notifications.count(), 0)