test_sdk.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. import uuid
  2. from unittest import mock
  3. import pytest
  4. import sentry_sdk
  5. from django.conf import settings
  6. from sentry_sdk import Hub, push_scope
  7. from sentry import eventstore
  8. from sentry.eventstore.models import Event
  9. from sentry.testutils import assert_mock_called_once_with_partial
  10. from sentry.utils.pytest.relay import adjust_settings_for_relay_tests
  11. from sentry.utils.sdk import bind_organization_context, configure_sdk
  12. @pytest.fixture
  13. def post_event_with_sdk(settings, relay_server, wait_for_ingest_consumer):
  14. adjust_settings_for_relay_tests(settings)
  15. settings.SENTRY_ENDPOINT = relay_server["url"]
  16. configure_sdk()
  17. wait_for_ingest_consumer = wait_for_ingest_consumer(settings)
  18. def inner(*args, **kwargs):
  19. event_id = sentry_sdk.capture_event(*args, **kwargs)
  20. Hub.current.client.flush()
  21. with push_scope():
  22. return wait_for_ingest_consumer(
  23. lambda: eventstore.get_event_by_id(settings.SENTRY_PROJECT, event_id)
  24. )
  25. return inner
  26. @pytest.mark.django_db
  27. def test_simple(post_event_with_sdk):
  28. event = post_event_with_sdk({"message": "internal client test"})
  29. assert event
  30. assert event.data["project"] == settings.SENTRY_PROJECT
  31. assert event.data["logentry"]["formatted"] == "internal client test"
  32. @pytest.mark.django_db
  33. def test_recursion_breaker(settings, post_event_with_sdk):
  34. # If this test terminates at all then we avoided recursion.
  35. settings.SENTRY_INGEST_CONSUMER_APM_SAMPLING = 1.0
  36. event_id = uuid.uuid4().hex
  37. with mock.patch(
  38. "sentry.event_manager.EventManager.save", spec=Event, side_effect=ValueError("oh no!")
  39. ) as save:
  40. with pytest.raises(ValueError):
  41. post_event_with_sdk({"message": "internal client test", "event_id": event_id})
  42. assert_mock_called_once_with_partial(save, settings.SENTRY_PROJECT, cache_key=f"e:{event_id}:1")
  43. @pytest.mark.django_db
  44. def test_encoding(post_event_with_sdk):
  45. class NotJSONSerializable:
  46. pass
  47. with push_scope() as scope:
  48. scope.set_extra("request", NotJSONSerializable())
  49. event = post_event_with_sdk({"message": "check the req"})
  50. assert event.data["project"] == settings.SENTRY_PROJECT
  51. assert event.data["logentry"]["formatted"] == "check the req"
  52. assert "NotJSONSerializable" in event.data["extra"]["request"]
  53. @pytest.mark.django_db
  54. def test_bind_organization_context(default_organization):
  55. configure_sdk()
  56. bind_organization_context(default_organization)
  57. assert Hub.current.scope._tags["organization"] == default_organization.id
  58. assert Hub.current.scope._tags["organization.slug"] == default_organization.slug
  59. assert Hub.current.scope._contexts["organization"] == {
  60. "id": default_organization.id,
  61. "slug": default_organization.slug,
  62. }
  63. @pytest.mark.django_db
  64. def test_bind_organization_context_with_callback(settings, default_organization):
  65. configure_sdk()
  66. def add_context(scope, organization, **kwargs):
  67. scope.set_tag("organization.test", "1")
  68. settings.SENTRY_ORGANIZATION_CONTEXT_HELPER = add_context
  69. bind_organization_context(default_organization)
  70. assert Hub.current.scope._tags["organization.test"] == "1"
  71. @pytest.mark.django_db
  72. def test_bind_organization_context_with_callback_error(settings, default_organization):
  73. configure_sdk()
  74. def add_context(scope, organization, **kwargs):
  75. 1 / 0
  76. settings.SENTRY_ORGANIZATION_CONTEXT_HELPER = add_context
  77. bind_organization_context(default_organization)
  78. assert Hub.current.scope._tags["organization"] == default_organization.id