tests.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import json
  2. from django.shortcuts import reverse
  3. from rest_framework.test import APITestCase
  4. from model_bakery import baker
  5. from glitchtip import test_utils # pylint: disable=unused-import
  6. from issues.models import Issue, Event, EventStatus
  7. from .test_data.csp import mdn_sample_csp
  8. class EventStoreTestCase(APITestCase):
  9. def setUp(self):
  10. self.project = baker.make("projects.Project")
  11. self.projectkey = self.project.projectkey_set.first()
  12. self.params = f"?sentry_key={self.projectkey.public_key}"
  13. self.url = reverse("event_store", args=[self.project.id]) + self.params
  14. def test_store_api(self):
  15. with open("event_store/test_data/py_hi_event.json") as json_file:
  16. data = json.load(json_file)
  17. res = self.client.post(self.url, data, format="json")
  18. self.assertEqual(res.status_code, 200)
  19. def test_store_api_auth_failure(self):
  20. url = "/api/1/store/"
  21. with open("event_store/test_data/py_hi_event.json") as json_file:
  22. data = json.load(json_file)
  23. res = self.client.post(url, data, format="json")
  24. self.assertEqual(res.status_code, 403)
  25. def test_error_event(self):
  26. with open("event_store/test_data/py_error.json") as json_file:
  27. data = json.load(json_file)
  28. res = self.client.post(self.url, data, format="json")
  29. self.assertEqual(res.status_code, 200)
  30. def test_csp_event(self):
  31. url = reverse("csp_store", args=[self.project.id]) + self.params
  32. data = mdn_sample_csp
  33. res = self.client.post(url, data, format="json")
  34. self.assertEqual(res.status_code, 200)
  35. expected_title = "Blocked 'style' from 'example.com'"
  36. issue = Issue.objects.get(title=expected_title)
  37. event = Event.objects.get()
  38. self.assertEqual(event.data["csp"]["effective_directive"], "style-src")
  39. self.assertTrue(issue)
  40. def test_reopen_resolved_issue(self):
  41. with open("event_store/test_data/py_hi_event.json") as json_file:
  42. data = json.load(json_file)
  43. self.client.post(self.url, data, format="json")
  44. issue = Issue.objects.all().first()
  45. issue.status = EventStatus.RESOLVED
  46. issue.save()
  47. data["event_id"] = "6600a066e64b4caf8ed7ec5af64ac4ba"
  48. self.client.post(self.url, data, format="json")
  49. issue.refresh_from_db()
  50. self.assertEqual(issue.status, EventStatus.UNRESOLVED)
  51. def test_performance(self):
  52. with open("event_store/test_data/py_hi_event.json") as json_file:
  53. data = json.load(json_file)
  54. with self.assertNumQueries(8):
  55. res = self.client.post(self.url, data, format="json")
  56. self.assertEqual(res.status_code, 200)
  57. def test_throttle_organization(self):
  58. organization = self.project.organization
  59. organization.is_accepting_events = False
  60. organization.save()
  61. with open("event_store/test_data/py_hi_event.json") as json_file:
  62. data = json.load(json_file)
  63. res = self.client.post(self.url, data, format="json")
  64. self.assertEqual(res.status_code, 429)