test_envelope_api.py 3.7 KB

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