From a7c491596a0781e1a77ec282329b0a4b7d1f7b1e Mon Sep 17 00:00:00 2001 From: drew2a Date: Thu, 30 Dec 2021 13:03:09 +0100 Subject: [PATCH] Extend tests --- .../sentry_reporter/sentry_reporter.py | 5 +- .../tests/test_sentry_reporter.py | 95 ++++++++++++++++--- 2 files changed, 84 insertions(+), 16 deletions(-) diff --git a/src/tribler-common/tribler_common/sentry_reporter/sentry_reporter.py b/src/tribler-common/tribler_common/sentry_reporter/sentry_reporter.py index b4e55ee9071..659fe937ef5 100644 --- a/src/tribler-common/tribler_common/sentry_reporter/sentry_reporter.py +++ b/src/tribler-common/tribler_common/sentry_reporter/sentry_reporter.py @@ -7,8 +7,9 @@ from hashlib import md5 from typing import Dict, List, Optional -import sentry_sdk from faker import Faker + +import sentry_sdk from sentry_sdk.integrations.logging import LoggingIntegration, ignore_logger from sentry_sdk.integrations.threading import ThreadingIntegration @@ -335,7 +336,7 @@ def get_test_sentry_url(): def is_in_test_mode(): return bool(SentryReporter.get_test_sentry_url()) - def _before_send(self, event: Dict, hint: Dict) -> Optional[Dict]: + def _before_send(self, event: Optional[Dict], hint: Optional[Dict]) -> Optional[Dict]: """The method that is called before each send. Both allowed and disallowed. diff --git a/src/tribler-common/tribler_common/sentry_reporter/tests/test_sentry_reporter.py b/src/tribler-common/tribler_common/sentry_reporter/tests/test_sentry_reporter.py index b2f6dfe52df..765a651f92a 100644 --- a/src/tribler-common/tribler_common/sentry_reporter/tests/test_sentry_reporter.py +++ b/src/tribler-common/tribler_common/sentry_reporter/tests/test_sentry_reporter.py @@ -4,15 +4,16 @@ from tribler_common.patch_import import patch_import from tribler_common.sentry_reporter.sentry_reporter import ( - EXCEPTION, OS_ENVIRON, + EXCEPTION, + OS_ENVIRON, PLATFORM_DETAILS, SentryReporter, SentryStrategy, - VALUES, this_sentry_strategy, + VALUES, + this_sentry_strategy, ) from tribler_common.sentry_reporter.sentry_scrubber import SentryScrubber - # fmt: off # pylint: disable=redefined-outer-name, protected-access @@ -77,7 +78,7 @@ def test_event_from_exception(mocked_capture_exception: Mock, sentry_reporter: S def capture_exception(_): # this behaviour normally is way more complicated, but at the end, `capture_exception` should transform - # an exception to a sentry event and this event should be stored in `sentry_reporter.last_event` + # the exception to a sentry event and this event should be stored in `sentry_reporter.last_event` sentry_reporter.last_event = {'sentry': 'event'} mocked_capture_exception.side_effect = capture_exception @@ -90,6 +91,7 @@ def capture_exception(_): def test_set_user(sentry_reporter): + # test that sentry_reporter transforms `user_id` to a fake identity assert sentry_reporter.set_user(b'some_id') == { 'id': 'db69fe66ec6b6b013c2f7d271ce17cae', 'username': 'Wanda Brown', @@ -101,6 +103,81 @@ def test_set_user(sentry_reporter): } +def test_get_actual_strategy(sentry_reporter): + # test that sentry_reporter use `thread_strategy` in case it has been set, and `global_strategy` otherwise + sentry_reporter.thread_strategy.set(None) + sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION + assert sentry_reporter.get_actual_strategy() == SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION + + sentry_reporter.thread_strategy.set(SentryStrategy.SEND_ALLOWED) + assert sentry_reporter.get_actual_strategy() == SentryStrategy.SEND_ALLOWED + + sentry_reporter.thread_strategy.set(None) + assert sentry_reporter.get_actual_strategy() == SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION + + +@patch('os.environ', {}) +def test_is_not_in_test_mode(): + assert SentryReporter.get_test_sentry_url() is None + assert not SentryReporter.is_in_test_mode() + + +@patch('os.environ', {'TRIBLER_TEST_SENTRY_URL': 'url'}) +def test_is_in_test_mode(): + assert SentryReporter.get_test_sentry_url() == 'url' + assert SentryReporter.is_in_test_mode() + + +def test_before_send_no_event(sentry_reporter: SentryReporter): + # test that in case of a None event, `_before_send` will never fail + assert not sentry_reporter._before_send(None, None) + + +def test_before_send_ignored_exceptions(sentry_reporter: SentryReporter): + # test that in case of an ignored exception, `_before_send` will return None + assert not sentry_reporter._before_send({'some': 'event'}, {'exc_info': [KeyboardInterrupt]}) + + +def test_before_send_suppressed(sentry_reporter: SentryReporter): + # test that in case of strategy==SentryStrategy.SEND_SUPPRESSED, the event will be stored in `self.last_event` + sentry_reporter.global_strategy = SentryStrategy.SEND_SUPPRESSED + assert not sentry_reporter._before_send({'some': 'event'}, None) + assert sentry_reporter.last_event == {'some': 'event'} + + +@patch.object(SentryReporter, 'get_confirmation', lambda _, __: True) +def test_before_send_allowed_with_confiration(sentry_reporter: SentryReporter): + # test that in case of strategy==SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION, the event will be + # sent after the positive confirmation + sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION + assert sentry_reporter._before_send({'some': 'event'}, None) + + +@patch.object(SentryReporter, 'get_confirmation', lambda _, __: False) +def test_before_send_allowed_with_confiration(sentry_reporter: SentryReporter): + # test that in case of strategy==SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION, the event will not be + # sent after the negative confirmation + sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION + assert not sentry_reporter._before_send({'some': 'event'}, None) + + +def test_before_send_scrubber_exists(sentry_reporter: SentryReporter): + # test that in case of a set scrubber, it will be called for scrubbing an event + event = {'some': 'event'} + + sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED + sentry_reporter.scrubber = Mock() + assert sentry_reporter._before_send(event, None) + sentry_reporter.scrubber.scrub_event.assert_called_with(event) + + +def test_before_send_scrubber_doesnt_exists(sentry_reporter: SentryReporter): + # test that in case of a missed scrubber, it will not be called + sentry_reporter.scrubber = None + sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED + assert sentry_reporter._before_send({'some': 'event'}, None) + + def test_send_defaults(sentry_reporter): assert sentry_reporter.send_event(None, None, None) is None @@ -253,16 +330,6 @@ def test_sentry_strategy(sentry_reporter): assert sentry_reporter.global_strategy == SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION -def test_get_actual_strategy(sentry_reporter): - sentry_reporter.thread_strategy.set(None) # default - sentry_reporter.global_strategy = SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION - - assert sentry_reporter.get_actual_strategy() == SentryStrategy.SEND_ALLOWED_WITH_CONFIRMATION - - sentry_reporter.thread_strategy.set(SentryStrategy.SEND_ALLOWED) - assert sentry_reporter.get_actual_strategy() == SentryStrategy.SEND_ALLOWED - - def test_retrieve_error_message_from_stacktrace(sentry_reporter): post_data = {"stack": '--LONG TEXT--Type: Text'} event = sentry_reporter.send_event({EXCEPTION: {VALUES: []}}, post_data, None, None, True)