test_sdk.py 3.5 KB

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