test_sdk.py 3.9 KB

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