test_throttling.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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 ..tasks import (
  7. get_free_tier_organizations_with_event_count,
  8. set_organization_throttle,
  9. )
  10. class OrganizationThrottlingTestCase(TestCase):
  11. @override_settings(BILLING_FREE_TIER_EVENTS=10)
  12. def test_non_subscriber_throttling(self):
  13. plan = baker.make("djstripe.Plan", active=True, amount=0)
  14. organization = baker.make("organizations_ext.Organization")
  15. user = baker.make("users.user")
  16. organization.add_user(user)
  17. customer = baker.make(
  18. "djstripe.Customer", subscriber=organization, livemode=False
  19. )
  20. with freeze_time(timezone.datetime(2000, 1, 1)):
  21. subscription = baker.make(
  22. "djstripe.Subscription",
  23. customer=customer,
  24. livemode=False,
  25. plan=plan,
  26. status="active",
  27. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  28. )
  29. baker.make(
  30. "issue_events.IssueEvent",
  31. issue__project__organization=organization,
  32. _quantity=3,
  33. )
  34. baker.make(
  35. "projects.IssueEventProjectHourlyStatistic",
  36. project__organization=organization,
  37. count=3,
  38. )
  39. set_organization_throttle()
  40. organization.refresh_from_db()
  41. self.assertTrue(organization.is_accepting_events)
  42. baker.make(
  43. "issue_events.IssueEvent",
  44. issue__project__organization=organization,
  45. _quantity=8,
  46. )
  47. baker.make(
  48. "projects.IssueEventProjectHourlyStatistic",
  49. project__organization=organization,
  50. count=8,
  51. )
  52. set_organization_throttle()
  53. organization.refresh_from_db()
  54. self.assertFalse(organization.is_accepting_events)
  55. self.assertTrue(mail.outbox[0])
  56. with freeze_time(timezone.datetime(2000, 2, 1)):
  57. # Month should reset throttle
  58. subscription.current_period_start = timezone.make_aware(
  59. timezone.datetime(2000, 2, 1)
  60. )
  61. subscription.current_period_end = timezone.make_aware(
  62. timezone.datetime(2000, 2, 28)
  63. )
  64. subscription.save()
  65. set_organization_throttle()
  66. organization.refresh_from_db()
  67. self.assertTrue(organization.is_accepting_events)
  68. # Throttle again
  69. baker.make(
  70. "issue_events.IssueEvent",
  71. issue__project__organization=organization,
  72. _quantity=10,
  73. )
  74. baker.make(
  75. "projects.IssueEventProjectHourlyStatistic",
  76. project__organization=organization,
  77. count=10,
  78. )
  79. baker.make(
  80. "performance.TransactionEvent",
  81. group__project__organization=organization,
  82. _quantity=1,
  83. )
  84. set_organization_throttle()
  85. organization.refresh_from_db()
  86. self.assertFalse(organization.is_accepting_events)
  87. def test_organization_event_count(self):
  88. plan = baker.make("djstripe.Plan", active=True, amount=0)
  89. organization = baker.make("organizations_ext.Organization")
  90. project = baker.make("projects.Project", organization=organization)
  91. user = baker.make("users.user")
  92. organization.add_user(user)
  93. customer = baker.make(
  94. "djstripe.Customer", subscriber=organization, livemode=False
  95. )
  96. with freeze_time(timezone.datetime(2000, 1, 1)):
  97. baker.make(
  98. "djstripe.Subscription",
  99. customer=customer,
  100. livemode=False,
  101. plan=plan,
  102. status="active",
  103. current_period_end=timezone.make_aware(timezone.datetime(2000, 2, 1)),
  104. )
  105. baker.make("issue_events.IssueEvent", issue__project=project, _quantity=3)
  106. baker.make(
  107. "projects.IssueEventProjectHourlyStatistic", project=project, count=3
  108. )
  109. baker.make(
  110. "performance.TransactionEvent",
  111. group__project=project,
  112. _quantity=2,
  113. )
  114. free_org = get_free_tier_organizations_with_event_count().first()
  115. self.assertEqual(free_org.total_event_count, 5)
  116. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  117. def test_non_subscriber_throttling_performance(self):
  118. for _ in range(2):
  119. plan = baker.make("djstripe.Plan", active=True, amount=0)
  120. organization = baker.make("organizations_ext.Organization")
  121. user = baker.make("users.user")
  122. organization.add_user(user)
  123. customer = baker.make(
  124. "djstripe.Customer", subscriber=organization, livemode=False
  125. )
  126. baker.make(
  127. "djstripe.Subscription",
  128. customer=customer,
  129. livemode=False,
  130. plan=plan,
  131. status="active",
  132. )
  133. baker.make(
  134. "issue_events.IssueEvent",
  135. issue__project__organization=organization,
  136. _quantity=2,
  137. )
  138. baker.make(
  139. "projects.IssueEventProjectHourlyStatistic",
  140. project__organization=organization,
  141. count=2,
  142. )
  143. with self.assertNumQueries(4):
  144. set_organization_throttle()
  145. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  146. def test_no_plan_throttle(self):
  147. """
  148. It's possible to not sign up for a free plan, they should be limited to free tier events
  149. """
  150. organization = baker.make("organizations_ext.Organization")
  151. user = baker.make("users.user")
  152. organization.add_user(user)
  153. project = baker.make("projects.Project", organization=organization)
  154. baker.make("issue_events.IssueEvent", issue__project=project, _quantity=2)
  155. set_organization_throttle()
  156. organization.refresh_from_db()
  157. self.assertFalse(organization.is_accepting_events)
  158. # Make plan active
  159. customer = baker.make(
  160. "djstripe.Customer", subscriber=organization, livemode=False
  161. )
  162. plan = baker.make("djstripe.Plan", active=True, amount=1)
  163. subscription = baker.make(
  164. "djstripe.Subscription",
  165. customer=customer,
  166. livemode=False,
  167. plan=plan,
  168. status="active",
  169. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  170. )
  171. set_organization_throttle()
  172. organization.refresh_from_db()
  173. self.assertTrue(organization.is_accepting_events)
  174. # Cancel plan
  175. subscription.status = "canceled"
  176. subscription.save()
  177. set_organization_throttle()
  178. organization.refresh_from_db()
  179. self.assertFalse(organization.is_accepting_events)
  180. # Add new active plan (still has canceled plan)
  181. subscription = baker.make(
  182. "djstripe.Subscription",
  183. customer=customer,
  184. livemode=False,
  185. plan=plan,
  186. status="active",
  187. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  188. )
  189. set_organization_throttle()
  190. organization.refresh_from_db()
  191. self.assertTrue(organization.is_accepting_events)
  192. def test_canceled_plan(self):
  193. # Start with no plan and throttled
  194. organization = baker.make(
  195. "organizations_ext.Organization", is_accepting_events=False
  196. )
  197. user = baker.make("users.user")
  198. organization.add_user(user)
  199. organization.refresh_from_db()
  200. self.assertFalse(organization.is_accepting_events)
  201. # Add old paid plan and active free plan
  202. customer = baker.make(
  203. "djstripe.Customer", subscriber=organization, livemode=False
  204. )
  205. free_plan = baker.make("djstripe.Plan", active=True, amount=0)
  206. paid_plan = baker.make("djstripe.Plan", active=True, amount=1)
  207. baker.make(
  208. "djstripe.Subscription",
  209. customer=customer,
  210. livemode=False,
  211. plan=paid_plan,
  212. status="canceled",
  213. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  214. )
  215. baker.make(
  216. "djstripe.Subscription",
  217. customer=customer,
  218. livemode=False,
  219. plan=free_plan,
  220. status="active",
  221. current_period_end=timezone.make_aware(timezone.datetime(2100, 1, 31)),
  222. )
  223. # Should not be throttled
  224. set_organization_throttle()
  225. organization.refresh_from_db()
  226. self.assertTrue(organization.is_accepting_events)