test_store_api.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. )