test_tasks.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. from datetime import timedelta
  2. from django.core import mail
  3. from django.test import TestCase
  4. from django.utils import timezone
  5. from model_bakery import baker
  6. from freezegun import freeze_time
  7. from glitchtip import test_utils # pylint: disable=unused-import
  8. from ..tasks import warn_organization_throttle
  9. class OrganizationWarnThrottlingTestCase(TestCase):
  10. def test_warn_organization_throttle(self):
  11. user = baker.make("users.User")
  12. project = baker.make(
  13. "projects.Project", organization__owner__organization_user__user=user,
  14. )
  15. plan = baker.make(
  16. "djstripe.Plan", active=True, amount=0, product__metadata={"events": "10"},
  17. )
  18. project2 = baker.make(
  19. "projects.Project",
  20. organization__owner__organization_user__user=user,
  21. organization__djstripe_customers__subscriptions__plan=plan,
  22. )
  23. with freeze_time(timezone.datetime(2000, 1, 1)):
  24. subscription = baker.make(
  25. "djstripe.Subscription",
  26. customer__subscriber=project.organization,
  27. livemode=False,
  28. plan=plan,
  29. status="active",
  30. )
  31. subscription.current_period_end = (
  32. subscription.current_period_start + timedelta(days=30)
  33. )
  34. subscription.save()
  35. baker.make("events.Event", issue__project=project, _quantity=9)
  36. warn_organization_throttle()
  37. self.assertEqual(len(mail.outbox), 1)
  38. warn_organization_throttle()
  39. self.assertEqual(len(mail.outbox), 1)
  40. with freeze_time(timezone.datetime(2000, 2, 2)):
  41. subscription.current_period_start = timezone.make_aware(
  42. timezone.datetime(2000, 2, 1)
  43. )
  44. subscription.current_period_end = (
  45. subscription.current_period_start + timedelta(days=30)
  46. )
  47. subscription.save()
  48. warn_organization_throttle()
  49. self.assertEqual(len(mail.outbox), 1)
  50. baker.make("events.Event", issue__project=project, _quantity=9)
  51. warn_organization_throttle()
  52. self.assertEqual(len(mail.outbox), 2)