test_throttling.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. from django.core import mail
  2. from django.test import TestCase, override_settings
  3. from django.utils import timezone
  4. from model_bakery import baker
  5. from freezegun import freeze_time
  6. from glitchtip import test_utils # pylint: disable=unused-import
  7. from ..tasks import (
  8. set_organization_throttle,
  9. get_free_tier_organizations_with_event_count,
  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. )
  29. baker.make(
  30. "events.Event", issue__project__organization=organization, _quantity=3
  31. )
  32. set_organization_throttle()
  33. organization.refresh_from_db()
  34. self.assertTrue(organization.is_accepting_events)
  35. baker.make(
  36. "events.Event", issue__project__organization=organization, _quantity=8
  37. )
  38. set_organization_throttle()
  39. organization.refresh_from_db()
  40. self.assertFalse(organization.is_accepting_events)
  41. self.assertTrue(mail.outbox[0])
  42. with freeze_time(timezone.datetime(2000, 2, 1)):
  43. # Month should reset throttle
  44. subscription.current_period_start = timezone.make_aware(
  45. timezone.datetime(2000, 2, 1)
  46. )
  47. subscription.save()
  48. set_organization_throttle()
  49. organization.refresh_from_db()
  50. self.assertTrue(organization.is_accepting_events)
  51. # Throttle again
  52. baker.make(
  53. "events.Event", issue__project__organization=organization, _quantity=10
  54. )
  55. baker.make(
  56. "performance.TransactionEvent",
  57. project__organization=organization,
  58. _quantity=1,
  59. )
  60. set_organization_throttle()
  61. organization.refresh_from_db()
  62. self.assertFalse(organization.is_accepting_events)
  63. def test_organization_event_count(self):
  64. plan = baker.make("djstripe.Plan", active=True, amount=0)
  65. organization = baker.make("organizations_ext.Organization")
  66. project = baker.make("projects.Project", organization=organization)
  67. user = baker.make("users.user")
  68. organization.add_user(user)
  69. customer = baker.make(
  70. "djstripe.Customer", subscriber=organization, livemode=False
  71. )
  72. with freeze_time(timezone.datetime(2000, 1, 1)):
  73. baker.make(
  74. "djstripe.Subscription",
  75. customer=customer,
  76. livemode=False,
  77. plan=plan,
  78. status="active",
  79. )
  80. baker.make("events.Event", issue__project=project, _quantity=3)
  81. baker.make(
  82. "performance.TransactionEvent", project=project, _quantity=2,
  83. )
  84. free_org = get_free_tier_organizations_with_event_count().first()
  85. self.assertEqual(free_org.event_count, 5)
  86. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  87. def test_non_subscriber_throttling_performance(self):
  88. for _ in range(2):
  89. plan = baker.make("djstripe.Plan", active=True, amount=0)
  90. organization = baker.make("organizations_ext.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. baker.make(
  97. "djstripe.Subscription",
  98. customer=customer,
  99. livemode=False,
  100. plan=plan,
  101. status="active",
  102. )
  103. baker.make(
  104. "events.Event", issue__project__organization=organization, _quantity=2
  105. )
  106. with self.assertNumQueries(12):
  107. set_organization_throttle()