test_tasks.py 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 freezegun import freeze_time
  6. from model_bakery import baker
  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",
  14. organization__owner__organization_user__user=user,
  15. )
  16. plan = baker.make(
  17. "djstripe.Plan",
  18. active=True,
  19. amount=0,
  20. product__metadata={"events": "10"},
  21. )
  22. project2 = baker.make(
  23. "projects.Project",
  24. organization__owner__organization_user__user=user,
  25. organization__djstripe_customers__subscriptions__plan=plan,
  26. )
  27. with freeze_time(timezone.datetime(2000, 1, 1)):
  28. subscription = baker.make(
  29. "djstripe.Subscription",
  30. customer__subscriber=project.organization,
  31. livemode=False,
  32. plan=plan,
  33. status="active",
  34. )
  35. subscription.current_period_end = (
  36. subscription.current_period_start + timedelta(days=30)
  37. )
  38. subscription.save()
  39. baker.make("events.Event", issue__project=project, _quantity=9)
  40. warn_organization_throttle()
  41. self.assertEqual(len(mail.outbox), 1)
  42. warn_organization_throttle()
  43. self.assertEqual(len(mail.outbox), 1)
  44. with freeze_time(timezone.datetime(2000, 2, 2)):
  45. subscription.current_period_start = timezone.make_aware(
  46. timezone.datetime(2000, 2, 1)
  47. )
  48. subscription.current_period_end = (
  49. subscription.current_period_start + timedelta(days=30)
  50. )
  51. subscription.save()
  52. warn_organization_throttle()
  53. self.assertEqual(len(mail.outbox), 1)
  54. baker.make("events.Event", issue__project=project, _quantity=9)
  55. warn_organization_throttle()
  56. self.assertEqual(len(mail.outbox), 2)