test_subscriptions.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.test import TestCase
  4. from django.utils import timezone
  5. from misago.categories.models import Category
  6. from misago.users.models import AnonymousUser
  7. from .. import testutils
  8. from ..subscriptions import make_subscription_aware
  9. class SubscriptionsTests(TestCase):
  10. def setUp(self):
  11. self.category = list(Category.objects.all_categories()[:1])[0]
  12. self.thread = self.post_thread(timezone.now() - timedelta(days=10))
  13. User = get_user_model()
  14. self.user = User.objects.create_user("Bob", "bob@test.com", "Pass.123")
  15. self.anon = AnonymousUser()
  16. def post_thread(self, datetime):
  17. return testutils.post_thread(
  18. category=self.category,
  19. started_on=datetime
  20. )
  21. def test_anon_subscription(self):
  22. """make single thread sub aware for anon"""
  23. make_subscription_aware(self.anon, self.thread)
  24. self.assertIsNone(self.thread.subscription)
  25. def test_anon_threads_subscription(self):
  26. """make multiple threads list sub aware for anon"""
  27. threads = []
  28. for i in xrange(10):
  29. threads.append(
  30. self.post_thread(timezone.now() - timedelta(days=10)))
  31. make_subscription_aware(self.anon, threads)
  32. for thread in threads:
  33. self.assertIsNone(thread.subscription)
  34. def test_no_subscription(self):
  35. """make thread sub aware for authenticated"""
  36. make_subscription_aware(self.user, self.thread)
  37. self.assertIsNone(self.thread.subscription)
  38. def test_threads_no_subscription(self):
  39. """make mulitple threads sub aware for authenticated"""
  40. threads = []
  41. for i in xrange(10):
  42. threads.append(
  43. self.post_thread(timezone.now() - timedelta(days=10)))
  44. make_subscription_aware(self.user, threads)
  45. for thread in threads:
  46. self.assertIsNone(thread.subscription)
  47. def test_subscribed_thread(self):
  48. """make thread sub aware for authenticated"""
  49. self.user.subscription_set.create(
  50. thread=self.thread,
  51. category=self.category,
  52. last_read_on=timezone.now(),
  53. send_email=True,
  54. )
  55. make_subscription_aware(self.user, self.thread)
  56. self.assertTrue(self.thread.subscription.send_email)
  57. def test_threads_no_subscription(self):
  58. """make mulitple threads sub aware for authenticated"""
  59. threads = []
  60. for i in xrange(10):
  61. threads.append(
  62. self.post_thread(timezone.now() - timedelta(days=10)))
  63. if i % 3 == 0:
  64. self.user.subscription_set.create(
  65. thread=threads[-1],
  66. category=self.category,
  67. last_read_on=timezone.now(),
  68. send_email=False,
  69. )
  70. elif i % 2 == 0:
  71. self.user.subscription_set.create(
  72. thread=threads[-1],
  73. category=self.category,
  74. last_read_on=timezone.now(),
  75. send_email=True,
  76. )
  77. make_subscription_aware(self.user, threads)
  78. for i in xrange(10):
  79. if i % 3 == 0:
  80. self.assertFalse(threads[i].subscription.send_email)
  81. elif i % 2 == 0:
  82. self.assertTrue(threads[i].subscription.send_email)
  83. else:
  84. self.assertIsNone(threads[i].subscription)