Skip to content

Commit

Permalink
Extend tests
Browse files Browse the repository at this point in the history
  • Loading branch information
drew2a committed Dec 30, 2021
1 parent 4f4ab66 commit a7c4915
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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',
Expand All @@ -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

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit a7c4915

Please sign in to comment.