test_envelope_api.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import json
  2. import uuid
  3. from django.shortcuts import reverse
  4. from django.test import override_settings
  5. from model_bakery import baker
  6. from rest_framework.test import APITestCase
  7. from glitchtip import test_utils # pylint: disable=unused-import
  8. from performance.models import TransactionEvent, TransactionGroup
  9. from ..models import Event
  10. class EnvelopeStoreTestCase(APITestCase):
  11. def setUp(self):
  12. self.project = baker.make("projects.Project")
  13. self.projectkey = self.project.projectkey_set.first()
  14. self.params = f"?sentry_key={self.projectkey.public_key}"
  15. self.url = reverse("envelope_store", args=[self.project.id]) + self.params
  16. def get_payload(self, path, replace_id=False, set_release=None):
  17. """Convert JSON file into envelope format string"""
  18. with open(path) as json_file:
  19. json_data = json.load(json_file)
  20. if replace_id:
  21. new_id = uuid.uuid4().hex
  22. json_data[0]["event_id"] = new_id
  23. json_data[2]["event_id"] = new_id
  24. if set_release:
  25. json_data[0]["trace"]["release"] = set_release
  26. json_data[2]["release"] = set_release
  27. data = "\n".join([json.dumps(line) for line in json_data])
  28. return data
  29. def test_accept(self):
  30. data = self.get_payload("events/test_data/transactions/django_simple.json")
  31. res = self.client.generic("POST", self.url, data)
  32. self.assertEqual(res.status_code, 200)
  33. self.assertTrue(TransactionEvent.objects.exists())
  34. def test_maintenance_freeze(self):
  35. data = self.get_payload("events/test_data/transactions/django_simple.json")
  36. with override_settings(MAINTENANCE_EVENT_FREEZE=True):
  37. res = self.client.generic("POST", self.url, data)
  38. self.assertEqual(res.status_code, 503)
  39. def test_accept_js_transaction(self):
  40. data = self.get_payload("events/test_data/transactions/js_simple.json")
  41. res = self.client.generic("POST", self.url, data)
  42. self.assertEqual(res.status_code, 200)
  43. self.assertTrue(TransactionEvent.objects.exists())
  44. def test_accept_dsn_event(self):
  45. # Ensure JS tunnel works
  46. # https://gitlab.com/glitchtip/glitchtip-backend/-/issues/181
  47. data = [
  48. {
  49. "event_id": "37f658fddae1465ab1ed7569ca653177",
  50. "dsn": f"http://{self.projectkey.public_key}@172.17.0.1:8000/18",
  51. },
  52. {"type": "event"},
  53. {"exception": {"values": []}},
  54. ]
  55. data = "\n".join([json.dumps(line) for line in data])
  56. res = self.client.generic(
  57. "POST", reverse("envelope_store", args=[self.project.id]), data
  58. )
  59. self.assertEqual(res.status_code, 200)
  60. self.assertTrue(Event.objects.exists())
  61. def test_android_sdk_event(self):
  62. data = self.get_payload(
  63. "events/test_data/incoming_events/android_sdk_envelope.json"
  64. )
  65. res = self.client.generic("POST", self.url, data)
  66. self.assertEqual(res.status_code, 200)
  67. self.assertTrue(Event.objects.exists())
  68. def test_js_angular(self):
  69. data = self.get_payload("events/test_data/transactions/js_angular.json")
  70. res = self.client.generic("POST", self.url, data)
  71. self.assertEqual(res.status_code, 200)
  72. def test_environment_release(self):
  73. data = self.get_payload(
  74. "events/test_data/transactions/environment_release.json"
  75. )
  76. res = self.client.generic("POST", self.url, data)
  77. event_id = res.data["id"]
  78. self.assertEqual(res.status_code, 200)
  79. self.assertTrue(
  80. TransactionEvent.objects.filter(
  81. pk=event_id, tags__release="1.0", tags__environment="dev"
  82. ).exists()
  83. )
  84. self.assertTrue(
  85. TransactionGroup.objects.filter(
  86. transactionevent__pk=event_id,
  87. tags__release__contains="1.0",
  88. tags__environment__contains="dev",
  89. ).exists()
  90. )
  91. data = self.get_payload(
  92. "events/test_data/transactions/environment_release.json",
  93. replace_id=True,
  94. set_release="1.1",
  95. )
  96. res = self.client.generic("POST", self.url, data)
  97. self.assertTrue(
  98. TransactionGroup.objects.filter(
  99. transactionevent__pk=event_id,
  100. tags__release__contains="1.1",
  101. tags__environment__contains="dev",
  102. ).exists()
  103. )
  104. def test_duplicate_id(self):
  105. data = self.get_payload("events/test_data/transactions/django_simple.json")
  106. self.client.generic("POST", self.url, data)
  107. res = self.client.generic("POST", self.url, data)
  108. self.assertEqual(res.status_code, 400)