test_store_api.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. from django.test import override_settings
  2. from django.urls import reverse
  3. from apps.issue_events.constants import IssueEventType
  4. from apps.issue_events.models import IssueEvent
  5. from .utils import EventIngestTestCase
  6. class StoreAPITestCase(EventIngestTestCase):
  7. """
  8. These test specifically test the store API and act more of integration test
  9. Use test_process_issue_events.py for testing Event Ingest more specifically
  10. """
  11. def setUp(self):
  12. super().setUp()
  13. self.url = reverse("api:event_store", args=[self.project.id]) + self.params
  14. self.event = self.get_json_data("events/test_data/py_hi_event.json")
  15. def test_store_api(self):
  16. with self.assertNumQueries(7):
  17. res = self.client.post(
  18. self.url, self.event, content_type="application/json"
  19. )
  20. self.assertContains(res, self.event["event_id"])
  21. self.assertEqual(self.project.issues.count(), 1)
  22. self.assertEqual(IssueEvent.objects.count(), 1)
  23. @override_settings(MAINTENANCE_EVENT_FREEZE=True)
  24. def test_maintenance_freeze(self):
  25. res = self.client.post(self.url, self.event, content_type="application/json")
  26. self.assertEqual(res.status_code, 503)
  27. def test_store_duplicate(self):
  28. """Unlike OSS Sentry, we just accept the duplicate as a performance optimization"""
  29. for _ in range(2):
  30. self.client.post(self.url, self.event, content_type="application/json")
  31. self.assertEqual(self.project.issues.count(), 1)
  32. self.assertEqual(IssueEvent.objects.count(), 1)
  33. def test_store_invalid_key(self):
  34. params = "?sentry_key=lol"
  35. url = reverse("api:event_store", args=[self.project.id]) + params
  36. res = self.client.post(url, self.event, content_type="application/json")
  37. self.assertEqual(res.status_code, 422)
  38. def test_store_api_auth_failure(self):
  39. params = "?sentry_key=8bea9cde164a4b94b88027a3d03f7698"
  40. url = reverse("api:event_store", args=[self.project.id]) + params
  41. res = self.client.post(url, self.event, content_type="application/json")
  42. self.assertEqual(res.status_code, 401)
  43. def test_error_event(self):
  44. data = self.get_json_data("events/test_data/py_error.json")
  45. res = self.client.post(self.url, data, content_type="application/json")
  46. self.assertEqual(res.status_code, 200)
  47. self.assertEqual(
  48. self.project.issues.filter(type=IssueEventType.ERROR).count(), 1
  49. )
  50. self.assertEqual(
  51. IssueEvent.objects.filter(type=IssueEventType.ERROR).count(), 1
  52. )