test_throttling.py 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. baker.make(
  85. "projects.TransactionEventProjectHourlyStatistic",
  86. project__organization=organization,
  87. count=1,
  88. )
  89. set_organization_throttle()
  90. organization.refresh_from_db()
  91. self.assertFalse(organization.is_accepting_events)
  92. def test_organization_event_count(self):
  93. plan = baker.make("djstripe.Plan", active=True, amount=0)
  94. organization = baker.make("organizations_ext.Organization")
  95. project = baker.make("projects.Project", organization=organization)
  96. user = baker.make("users.user")
  97. organization.add_user(user)
  98. customer = baker.make(
  99. "djstripe.Customer", subscriber=organization, livemode=False
  100. )
  101. with freeze_time(timezone.datetime(2000, 1, 1)):
  102. baker.make(
  103. "djstripe.Subscription",
  104. customer=customer,
  105. livemode=False,
  106. plan=plan,
  107. status="active",
  108. current_period_end=timezone.make_aware(timezone.datetime(2000, 2, 1)),
  109. )
  110. baker.make("issue_events.IssueEvent", issue__project=project, _quantity=3)
  111. baker.make(
  112. "projects.IssueEventProjectHourlyStatistic", project=project, count=3
  113. )
  114. baker.make(
  115. "performance.TransactionEvent",
  116. group__project=project,
  117. _quantity=2,
  118. )
  119. baker.make(
  120. "projects.TransactionEventProjectHourlyStatistic",
  121. project=project,
  122. count=2,
  123. )
  124. free_org = get_free_tier_organizations_with_event_count().first()
  125. self.assertEqual(free_org.total_event_count, 5)
  126. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  127. def test_non_subscriber_throttling_performance(self):
  128. for _ in range(2):
  129. plan = baker.make("djstripe.Plan", active=True, amount=0)
  130. organization = baker.make("organizations_ext.Organization")
  131. user = baker.make("users.user")
  132. organization.add_user(user)
  133. customer = baker.make(
  134. "djstripe.Customer", subscriber=organization, livemode=False
  135. )
  136. baker.make(
  137. "djstripe.Subscription",
  138. customer=customer,
  139. livemode=False,
  140. plan=plan,
  141. status="active",
  142. )
  143. baker.make(
  144. "issue_events.IssueEvent",
  145. issue__project__organization=organization,
  146. _quantity=2,
  147. )
  148. baker.make(
  149. "projects.IssueEventProjectHourlyStatistic",
  150. project__organization=organization,
  151. count=2,
  152. )
  153. with self.assertNumQueries(4):
  154. set_organization_throttle()
  155. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  156. def test_no_plan_throttle(self):
  157. """
  158. It's possible to not sign up for a free plan, they should be limited to free tier events
  159. """
  160. organization = baker.make("organizations_ext.Organization")
  161. user = baker.make("users.user")
  162. organization.add_user(user)
  163. project = baker.make("projects.Project", organization=organization)
  164. baker.make("issue_events.IssueEvent", issue__project=project, _quantity=2)
  165. set_organization_throttle()
  166. organization.refresh_from_db()
  167. self.assertFalse(organization.is_accepting_events)
  168. # Make plan active
  169. customer = baker.make(
  170. "djstripe.Customer", subscriber=organization, livemode=False
  171. )
  172. plan = baker.make("djstripe.Plan", active=True, amount=1)
  173. subscription = baker.make(
  174. "djstripe.Subscription",
  175. customer=customer,
  176. livemode=False,
  177. plan=plan,
  178. status="active",
  179. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  180. )
  181. set_organization_throttle()
  182. organization.refresh_from_db()
  183. self.assertTrue(organization.is_accepting_events)
  184. # Cancel plan
  185. subscription.status = "canceled"
  186. subscription.save()
  187. set_organization_throttle()
  188. organization.refresh_from_db()
  189. self.assertFalse(organization.is_accepting_events)
  190. # Add new active plan (still has canceled plan)
  191. subscription = baker.make(
  192. "djstripe.Subscription",
  193. customer=customer,
  194. livemode=False,
  195. plan=plan,
  196. status="active",
  197. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  198. )
  199. set_organization_throttle()
  200. organization.refresh_from_db()
  201. self.assertTrue(organization.is_accepting_events)
  202. def test_canceled_plan(self):
  203. # Start with no plan and throttled
  204. organization = baker.make(
  205. "organizations_ext.Organization", is_accepting_events=False
  206. )
  207. user = baker.make("users.user")
  208. organization.add_user(user)
  209. organization.refresh_from_db()
  210. self.assertFalse(organization.is_accepting_events)
  211. # Add old paid plan and active free plan
  212. customer = baker.make(
  213. "djstripe.Customer", subscriber=organization, livemode=False
  214. )
  215. free_plan = baker.make("djstripe.Plan", active=True, amount=0)
  216. paid_plan = baker.make("djstripe.Plan", active=True, amount=1)
  217. baker.make(
  218. "djstripe.Subscription",
  219. customer=customer,
  220. livemode=False,
  221. plan=paid_plan,
  222. status="canceled",
  223. current_period_end=timezone.make_aware(timezone.datetime(2000, 1, 31)),
  224. )
  225. baker.make(
  226. "djstripe.Subscription",
  227. customer=customer,
  228. livemode=False,
  229. plan=free_plan,
  230. status="active",
  231. current_period_end=timezone.make_aware(timezone.datetime(2100, 1, 31)),
  232. )
  233. # Should not be throttled
  234. set_organization_throttle()
  235. organization.refresh_from_db()
  236. self.assertTrue(organization.is_accepting_events)