test_sdk.py 4.0 KB

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