test_store_api.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. from django.core.cache import cache
  2. from django.test import override_settings
  3. from django.urls import reverse
  4. from apps.issue_events.constants import IssueEventType
  5. from apps.issue_events.models import IssueEvent
  6. from .utils import EventIngestTestCase
  7. class StoreAPITestCase(EventIngestTestCase):
  8. """
  9. These test specifically test the store API and act more of integration test
  10. Use test_process_issue_events.py for testing Event Ingest more specifically
  11. """
  12. def setUp(self):
  13. super().setUp()
  14. self.url = reverse("api:event_store", args=[self.project.id]) + self.params
  15. self.event = self.get_json_data("events/test_data/py_hi_event.json")
  16. def tearDown(self):
  17. cache.clear()
  18. def test_store_api(self):
  19. with self.assertNumQueries(16):
  20. res = self.client.post(
  21. self.url, self.event, content_type="application/json"
  22. )
  23. self.assertContains(res, self.event["event_id"])
  24. self.assertEqual(self.project.issues.count(), 1)
  25. self.assertEqual(IssueEvent.objects.count(), 1)
  26. @override_settings(MAINTENANCE_EVENT_FREEZE=True)
  27. def test_maintenance_freeze(self):
  28. res = self.client.post(self.url, self.event, content_type="application/json")
  29. self.assertEqual(res.status_code, 503)
  30. def test_store_duplicate(self):
  31. """Unlike OSS Sentry, we just accept the duplicate as a performance optimization"""
  32. for _ in range(2):
  33. self.client.post(self.url, self.event, content_type="application/json")
  34. self.assertEqual(self.project.issues.count(), 1)
  35. self.assertEqual(IssueEvent.objects.count(), 1)
  36. def test_store_invalid_key(self):
  37. params = "?sentry_key=lol"
  38. url = reverse("api:event_store", args=[self.project.id]) + params
  39. res = self.client.post(url, self.event, content_type="application/json")
  40. self.assertEqual(res.status_code, 422)
  41. def test_store_api_auth_failure(self):
  42. params = "?sentry_key=8bea9cde164a4b94b88027a3d03f7698"
  43. url = reverse("api:event_store", args=[self.project.id]) + params
  44. res = self.client.post(url, self.event, content_type="application/json")
  45. self.assertEqual(res.status_code, 401)
  46. def test_error_event(self):
  47. data = self.get_json_data("events/test_data/py_error.json")
  48. res = self.client.post(self.url, data, content_type="application/json")
  49. self.assertEqual(res.status_code, 200)
  50. self.assertEqual(
  51. self.project.issues.filter(type=IssueEventType.ERROR).count(), 1
  52. )
  53. self.assertEqual(
  54. IssueEvent.objects.filter(type=IssueEventType.ERROR).count(), 1
  55. )
  56. @override_settings(STRIPE_ENABLED=True, GLITCHTIP_THROTTLE_CHECK_INTERVAL=1)
  57. async def test_check_throttle(self):
  58. data = self.get_json_data("events/test_data/py_error.json")
  59. res = await self.async_client.post(self.url, data, content_type="application/json")
  60. self.assertEqual(res.status_code, 200)
  61. # We know the throttle was checked when this simplistic lock is set
  62. self.assertTrue(cache.get(f"org-throttle-{self.organization.id}"))
  63. @override_settings(STRIPE_ENABLED=True, GLITCHTIP_THROTTLE_CHECK_INTERVAL=100000000)
  64. async def test_check_no_throttle(self):
  65. data = self.get_json_data("events/test_data/py_error.json")
  66. res = await self.async_client.post(self.url, data, content_type="application/json")
  67. self.assertEqual(res.status_code, 200)
  68. # We know the throttle was not checked when this simplistic lock isn't set
  69. self.assertFalse(cache.get(f"org-throttle-{self.organization.id}"))