test_throttling.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. from django.core import mail
  2. from django.test import TestCase, override_settings
  3. from django.utils import timezone
  4. from freezegun import freeze_time
  5. from model_bakery import baker
  6. from glitchtip import test_utils # pylint: disable=unused-import
  7. from ..tasks import (
  8. get_free_tier_organizations_with_event_count,
  9. set_organization_throttle,
  10. )
  11. class OrganizationThrottlingTestCase(TestCase):
  12. @override_settings(BILLING_FREE_TIER_EVENTS=10)
  13. def test_non_subscriber_throttling(self):
  14. plan = baker.make("djstripe.Plan", active=True, amount=0)
  15. organization = baker.make("organizations_ext.Organization")
  16. user = baker.make("users.user")
  17. organization.add_user(user)
  18. customer = baker.make(
  19. "djstripe.Customer", subscriber=organization, livemode=False
  20. )
  21. with freeze_time(timezone.datetime(2000, 1, 1)):
  22. subscription = baker.make(
  23. "djstripe.Subscription",
  24. customer=customer,
  25. livemode=False,
  26. plan=plan,
  27. status="active",
  28. current_period_end="2000-01-31",
  29. )
  30. baker.make(
  31. "events.Event", issue__project__organization=organization, _quantity=3
  32. )
  33. set_organization_throttle()
  34. organization.refresh_from_db()
  35. self.assertTrue(organization.is_accepting_events)
  36. baker.make(
  37. "events.Event", issue__project__organization=organization, _quantity=8
  38. )
  39. set_organization_throttle()
  40. organization.refresh_from_db()
  41. self.assertFalse(organization.is_accepting_events)
  42. self.assertTrue(mail.outbox[0])
  43. with freeze_time(timezone.datetime(2000, 2, 1)):
  44. # Month should reset throttle
  45. subscription.current_period_start = timezone.make_aware(
  46. timezone.datetime(2000, 2, 1)
  47. )
  48. subscription.current_period_end = timezone.make_aware(
  49. timezone.datetime(2000, 2, 28)
  50. )
  51. subscription.save()
  52. set_organization_throttle()
  53. organization.refresh_from_db()
  54. self.assertTrue(organization.is_accepting_events)
  55. # Throttle again
  56. baker.make(
  57. "events.Event", issue__project__organization=organization, _quantity=10
  58. )
  59. baker.make(
  60. "performance.TransactionEvent",
  61. group__project__organization=organization,
  62. _quantity=1,
  63. )
  64. set_organization_throttle()
  65. organization.refresh_from_db()
  66. self.assertFalse(organization.is_accepting_events)
  67. def test_organization_event_count(self):
  68. plan = baker.make("djstripe.Plan", active=True, amount=0)
  69. organization = baker.make("organizations_ext.Organization")
  70. project = baker.make("projects.Project", organization=organization)
  71. user = baker.make("users.user")
  72. organization.add_user(user)
  73. customer = baker.make(
  74. "djstripe.Customer", subscriber=organization, livemode=False
  75. )
  76. with freeze_time(timezone.datetime(2000, 1, 1)):
  77. baker.make(
  78. "djstripe.Subscription",
  79. customer=customer,
  80. livemode=False,
  81. plan=plan,
  82. status="active",
  83. current_period_end="2000-02-01",
  84. )
  85. baker.make("events.Event", issue__project=project, _quantity=3)
  86. baker.make(
  87. "performance.TransactionEvent",
  88. group__project=project,
  89. _quantity=2,
  90. )
  91. free_org = get_free_tier_organizations_with_event_count().first()
  92. self.assertEqual(free_org.total_event_count, 5)
  93. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  94. def test_non_subscriber_throttling_performance(self):
  95. for _ in range(2):
  96. plan = baker.make("djstripe.Plan", active=True, amount=0)
  97. organization = baker.make("organizations_ext.Organization")
  98. user = baker.make("users.user")
  99. organization.add_user(user)
  100. customer = baker.make(
  101. "djstripe.Customer", subscriber=organization, livemode=False
  102. )
  103. baker.make(
  104. "djstripe.Subscription",
  105. customer=customer,
  106. livemode=False,
  107. plan=plan,
  108. status="active",
  109. )
  110. baker.make(
  111. "events.Event", issue__project__organization=organization, _quantity=2
  112. )
  113. with self.assertNumQueries(4):
  114. set_organization_throttle()