test_throttling.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 set_organization_throttle
  8. class OrganizationThrottlingTestCase(TestCase):
  9. @override_settings(BILLING_FREE_TIER_EVENTS=10)
  10. def test_non_subscriber_throttling(self):
  11. plan = baker.make("djstripe.Plan", active=True, amount=0)
  12. with freeze_time(timezone.datetime(2000, 1, 1)):
  13. organization = baker.make("organizations_ext.Organization")
  14. user = baker.make("users.user")
  15. organization.add_user(user)
  16. customer = baker.make(
  17. "djstripe.Customer", subscriber=organization, livemode=False
  18. )
  19. subscription = baker.make(
  20. "djstripe.Subscription",
  21. customer=customer,
  22. livemode=False,
  23. plan=plan,
  24. status="active",
  25. )
  26. baker.make(
  27. "events.Event", issue__project__organization=organization, _quantity=3
  28. )
  29. set_organization_throttle()
  30. organization.refresh_from_db()
  31. self.assertTrue(organization.is_accepting_events)
  32. baker.make(
  33. "events.Event", issue__project__organization=organization, _quantity=8
  34. )
  35. set_organization_throttle()
  36. organization.refresh_from_db()
  37. self.assertFalse(organization.is_accepting_events)
  38. self.assertTrue(mail.outbox[0])
  39. with freeze_time(timezone.datetime(2000, 2, 1)):
  40. # Month should reset throttle
  41. subscription.current_period_start = timezone.make_aware(
  42. timezone.datetime(2000, 2, 1)
  43. )
  44. subscription.save()
  45. set_organization_throttle()
  46. organization.refresh_from_db()
  47. self.assertTrue(organization.is_accepting_events)
  48. # Throttle again
  49. baker.make(
  50. "events.Event", issue__project__organization=organization, _quantity=10
  51. )
  52. baker.make(
  53. "performance.TransactionEvent",
  54. project__organization=organization,
  55. _quantity=1,
  56. )
  57. set_organization_throttle()
  58. organization.refresh_from_db()
  59. self.assertFalse(organization.is_accepting_events)
  60. @override_settings(BILLING_FREE_TIER_EVENTS=1)
  61. def test_non_subscriber_throttling_performance(self):
  62. """ Task should take no more than 4 + (1 * orgs) queries """
  63. for _ in range(2):
  64. plan = baker.make("djstripe.Plan", active=True, amount=0)
  65. organization = baker.make("organizations_ext.Organization")
  66. user = baker.make("users.user")
  67. organization.add_user(user)
  68. customer = baker.make(
  69. "djstripe.Customer", subscriber=organization, livemode=False
  70. )
  71. baker.make(
  72. "djstripe.Subscription",
  73. customer=customer,
  74. livemode=False,
  75. plan=plan,
  76. status="active",
  77. )
  78. baker.make(
  79. "events.Event", issue__project__organization=organization, _quantity=2
  80. )
  81. with self.assertNumQueries(6):
  82. set_organization_throttle()