test_tasks.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 ..tasks import warn_organization_throttle
  8. class OrganizationWarnThrottlingTestCase(TestCase):
  9. def test_warn_organization_throttle(self):
  10. user = baker.make("users.User")
  11. project = baker.make(
  12. "projects.Project",
  13. organization__owner__organization_user__user=user,
  14. )
  15. plan = baker.make(
  16. "djstripe.Plan",
  17. active=True,
  18. amount=0,
  19. product__metadata={"events": "10"},
  20. )
  21. baker.make(
  22. "projects.Project",
  23. organization__owner__organization_user__user=user,
  24. organization__djstripe_customers__subscriptions__plan=plan,
  25. )
  26. with freeze_time(timezone.datetime(2000, 1, 1)):
  27. subscription = baker.make(
  28. "djstripe.Subscription",
  29. customer__subscriber=project.organization,
  30. livemode=False,
  31. plan=plan,
  32. status="active",
  33. )
  34. subscription.current_period_end = (
  35. subscription.current_period_start + timedelta(days=30)
  36. )
  37. subscription.save()
  38. baker.make("issue_events.IssueEvent", issue__project=project, _quantity=9)
  39. baker.make(
  40. "projects.IssueEventProjectHourlyStatistic",
  41. project=project,
  42. count=9,
  43. )
  44. warn_organization_throttle()
  45. self.assertEqual(len(mail.outbox), 1)
  46. warn_organization_throttle()
  47. self.assertEqual(len(mail.outbox), 1)
  48. with freeze_time(timezone.datetime(2000, 2, 2)):
  49. subscription.current_period_start = timezone.make_aware(
  50. timezone.datetime(2000, 2, 1)
  51. )
  52. subscription.current_period_end = (
  53. subscription.current_period_start + timedelta(days=30)
  54. )
  55. subscription.save()
  56. warn_organization_throttle()
  57. self.assertEqual(len(mail.outbox), 1)
  58. baker.make("issue_events.IssueEvent", issue__project=project, _quantity=9)
  59. baker.make(
  60. "projects.IssueEventProjectHourlyStatistic",
  61. project=project,
  62. count=9,
  63. )
  64. warn_organization_throttle()
  65. self.assertEqual(len(mail.outbox), 2)