test_throttling.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.test_utils import generators # 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()
  115. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  116. def test_no_plan_throttle(self):
  117. """
  118. It's possible to not sign up for a free plan, they should be limited to free tier events
  119. """
  120. organization = baker.make("organizations_ext.Organization")
  121. user = baker.make("users.user")
  122. organization.add_user(user)
  123. project = baker.make("projects.Project", organization=organization)
  124. baker.make("events.Event", issue__project=project, _quantity=2)
  125. set_organization_throttle()
  126. organization.refresh_from_db()
  127. self.assertFalse(organization.is_accepting_events)
  128. # Make plan active
  129. customer = baker.make(
  130. "djstripe.Customer", subscriber=organization, livemode=False
  131. )
  132. plan = baker.make("djstripe.Plan", active=True, amount=1)
  133. subscription = baker.make(
  134. "djstripe.Subscription",
  135. customer=customer,
  136. livemode=False,
  137. plan=plan,
  138. status="active",
  139. current_period_end="2000-01-31",
  140. )
  141. set_organization_throttle()
  142. organization.refresh_from_db()
  143. self.assertTrue(organization.is_accepting_events)
  144. # Cancel plan
  145. subscription.status = "canceled"
  146. subscription.save()
  147. set_organization_throttle()
  148. organization.refresh_from_db()
  149. self.assertFalse(organization.is_accepting_events)
  150. # Add new active plan (still has canceled plan)
  151. subscription = baker.make(
  152. "djstripe.Subscription",
  153. customer=customer,
  154. livemode=False,
  155. plan=plan,
  156. status="active",
  157. current_period_end="2000-01-31",
  158. )
  159. set_organization_throttle()
  160. organization.refresh_from_db()
  161. self.assertTrue(organization.is_accepting_events)
  162. def test_canceled_plan(self):
  163. # Start with no plan and throttled
  164. organization = baker.make(
  165. "organizations_ext.Organization", is_accepting_events=False
  166. )
  167. user = baker.make("users.user")
  168. organization.add_user(user)
  169. organization.refresh_from_db()
  170. self.assertFalse(organization.is_accepting_events)
  171. # Add old paid plan and active free plan
  172. customer = baker.make(
  173. "djstripe.Customer", subscriber=organization, livemode=False
  174. )
  175. free_plan = baker.make("djstripe.Plan", active=True, amount=0)
  176. paid_plan = baker.make("djstripe.Plan", active=True, amount=1)
  177. baker.make(
  178. "djstripe.Subscription",
  179. customer=customer,
  180. livemode=False,
  181. plan=paid_plan,
  182. status="canceled",
  183. current_period_end="2000-01-31",
  184. )
  185. baker.make(
  186. "djstripe.Subscription",
  187. customer=customer,
  188. livemode=False,
  189. plan=free_plan,
  190. status="active",
  191. current_period_end="2100-01-31",
  192. )
  193. # Should not be throttled
  194. set_organization_throttle()
  195. organization.refresh_from_db()
  196. self.assertTrue(organization.is_accepting_events)