test_subscriptions.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from datetime import timedelta
  2. from django.test import TestCase
  3. from django.utils import timezone
  4. from .. import test
  5. from ...categories.models import Category
  6. from ...users.models import AnonymousUser
  7. from ...users.test import create_test_user
  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. self.anon = AnonymousUser()
  14. self.user = create_test_user("User", "user@example.com")
  15. def post_thread(self, datetime):
  16. return test.post_thread(category=self.category, started_on=datetime)
  17. def test_anon_subscription(self):
  18. """make single thread sub aware for anon"""
  19. make_subscription_aware(self.anon, self.thread)
  20. self.assertIsNone(self.thread.subscription)
  21. def test_anon_threads_subscription(self):
  22. """make multiple threads list sub aware for anon"""
  23. threads = []
  24. for _ in range(10):
  25. threads.append(self.post_thread(timezone.now() - timedelta(days=10)))
  26. make_subscription_aware(self.anon, threads)
  27. for thread in threads:
  28. self.assertIsNone(thread.subscription)
  29. def test_no_subscription(self):
  30. """make thread sub aware for authenticated"""
  31. make_subscription_aware(self.user, self.thread)
  32. self.assertIsNone(self.thread.subscription)
  33. def test_subscribed_thread(self):
  34. """make thread sub aware for authenticated"""
  35. self.user.subscription_set.create(
  36. thread=self.thread,
  37. category=self.category,
  38. last_read_on=timezone.now(),
  39. send_email=True,
  40. )
  41. make_subscription_aware(self.user, self.thread)
  42. self.assertTrue(self.thread.subscription.send_email)
  43. def test_threads_no_subscription(self):
  44. """make mulitple threads sub aware for authenticated"""
  45. threads = []
  46. for i in range(10):
  47. threads.append(self.post_thread(timezone.now() - timedelta(days=10)))
  48. if i % 3 == 0:
  49. self.user.subscription_set.create(
  50. thread=threads[-1],
  51. category=self.category,
  52. last_read_on=timezone.now(),
  53. send_email=False,
  54. )
  55. elif i % 2 == 0:
  56. self.user.subscription_set.create(
  57. thread=threads[-1],
  58. category=self.category,
  59. last_read_on=timezone.now(),
  60. send_email=True,
  61. )
  62. make_subscription_aware(self.user, threads)
  63. for i in range(10):
  64. if i % 3 == 0:
  65. self.assertFalse(threads[i].subscription.send_email)
  66. elif i % 2 == 0:
  67. self.assertTrue(threads[i].subscription.send_email)
  68. else:
  69. self.assertIsNone(threads[i].subscription)