Exemple #1
0
def test_bind_organization_context_with_callback(settings, default_organization):
    configure_sdk()

    def add_context(scope, organization, **kwargs):
        scope.set_tag("organization.test", "1")

    settings.SENTRY_ORGANIZATION_CONTEXT_HELPER = add_context
    bind_organization_context(default_organization)

    assert Hub.current.scope._tags["organization.test"] == "1"
Exemple #2
0
    def test_bind_organization_context(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        org = self.create_organization()
        bind_organization_context(org)

        assert Hub.current.scope._tags["organization"] == org.id
        assert Hub.current.scope._tags["organization.slug"] == org.slug
        assert Hub.current.scope._contexts["organization"] == {"id": org.id, "slug": org.slug}
Exemple #3
0
def test_bind_organization_context(default_organization):
    configure_sdk()

    bind_organization_context(default_organization)

    assert Hub.current.scope._tags["organization"] == default_organization.id
    assert Hub.current.scope._tags["organization.slug"] == default_organization.slug
    assert Hub.current.scope._contexts["organization"] == {
        "id": default_organization.id,
        "slug": default_organization.slug,
    }
Exemple #4
0
    def test_simple(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        with self.tasks():
            event_id = raven.captureMessage("internal client test")

        event = Event.objects.get()
        assert event.project_id == settings.SENTRY_PROJECT
        assert event.event_id == event_id
        assert event.data["logentry"]["formatted"] == "internal client test"
Exemple #5
0
    def test_simple(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        with self.tasks():
            event_id = raven.captureMessage("internal client test")

        event = nodestore.get(Event.generate_node_id(settings.SENTRY_PROJECT, event_id))

        assert event["project"] == settings.SENTRY_PROJECT
        assert event["event_id"] == event_id
        assert event["logentry"]["formatted"] == "internal client test"
Exemple #6
0
    def test_simple(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        with self.tasks():
            event_id = raven.captureMessage('internal client test')

        event = Event.objects.get()
        assert event.project_id == settings.SENTRY_PROJECT
        assert event.event_id == event_id
        assert event.data['sentry.interfaces.Message']['message'] == \
            'internal client test'
Exemple #7
0
    def test_simple(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        with self.tasks():
            event_id = raven.captureMessage('internal client test')

        event = Event.objects.get()
        assert event.project_id == settings.SENTRY_PROJECT
        assert event.event_id == event_id
        assert event.data['sentry.interfaces.Message']['message'] == \
            'internal client test'
Exemple #8
0
    def test_bind_organization_context_with_callback_error(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        org = self.create_organization()

        def add_context(scope, organization, **kwargs):
            1 / 0

        with self.settings(SENTRY_ORGANIZATION_CONTEXT_HELPER=add_context):
            bind_organization_context(org)

        assert Hub.current.scope._tags["organization"] == org.id
    def test_encoding(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        class NotJSONSerializable():
            pass

        with self.tasks():
            raven.captureMessage('check the req',
                                 extra={'request': NotJSONSerializable()})

        event = Event.objects.get()
        assert event.project_id == settings.SENTRY_PROJECT
        assert event.data['logentry']['formatted'] == 'check the req'
        assert 'NotJSONSerializable' in event.data['extra']['request']
Exemple #10
0
    def test_encoding(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        class NotJSONSerializable:
            pass

        with self.tasks():
            raven.captureMessage("check the req",
                                 extra={"request": NotJSONSerializable()})

        event = Event.objects.get()
        assert event.project_id == settings.SENTRY_PROJECT
        assert event.data["logentry"]["formatted"] == "check the req"
        assert "NotJSONSerializable" in event.data["extra"]["request"]
Exemple #11
0
    def test_encoding(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        class NotJSONSerializable():
            pass

        with self.tasks():
            raven.captureMessage('check the req', extra={
                'request': NotJSONSerializable()
            })

        event = Event.objects.get()
        assert event.project_id == settings.SENTRY_PROJECT
        assert event.data['sentry.interfaces.Message']['message'] == 'check the req'
        assert 'NotJSONSerializable' in event.data['extra']['request']
Exemple #12
0
def post_event_with_sdk(settings, relay_server, wait_for_ingest_consumer):
    adjust_settings_for_relay_tests(settings)
    settings.SENTRY_ENDPOINT = relay_server["url"]

    configure_sdk()

    wait_for_ingest_consumer = wait_for_ingest_consumer(settings)

    def inner(*args, **kwargs):
        event_id = raven.captureMessage(*args, **kwargs)
        Hub.current.client.flush()

        return wait_for_ingest_consumer(lambda: eventstore.get_event_by_id(
            settings.SENTRY_PROJECT, event_id))

    return inner
Exemple #13
0
    def test_recursion_breaker(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        # If this test terminates at all then we avoided recursion.
        with self.tasks():
            with mock.patch(
                "sentry.event_manager.EventManager.save", side_effect=ValueError("oh no!")
            ) as save:
                event_id = raven.captureMessage("internal client test")

        event = nodestore.get(Event.generate_node_id(settings.SENTRY_PROJECT, event_id))
        assert event is None

        assert_mock_called_once_with_partial(
            save, settings.SENTRY_PROJECT, cache_key=u"e:{}:1".format(event_id)
        )
Exemple #14
0
    def test_encoding(self):
        configure_sdk()
        Hub.current.bind_client(Hub.main.client)

        class NotJSONSerializable:
            pass

        with self.tasks():
            event_id = raven.captureMessage(
                "check the req", extra={"request": NotJSONSerializable()}
            )

        event = nodestore.get(Event.generate_node_id(settings.SENTRY_PROJECT, event_id))

        assert event["project"] == settings.SENTRY_PROJECT
        assert event["logentry"]["formatted"] == "check the req"
        assert "NotJSONSerializable" in event["extra"]["request"]
Exemple #15
0
def initialize_app(config, skip_service_validation=False):
    settings = config["settings"]

    bootstrap_options(settings, config["options"])

    configure_structlog()

    # Commonly setups don't correctly configure themselves for production envs
    # so lets try to provide a bit more guidance
    if settings.CELERY_ALWAYS_EAGER and not settings.DEBUG:
        warnings.warn(
            "Sentry is configured to run asynchronous tasks in-process. "
            "This is not recommended within production environments. "
            "See https://docs.sentry.io/on-premise/server/queue/ for more information."
        )

    if settings.SENTRY_SINGLE_ORGANIZATION:
        settings.SENTRY_FEATURES["organizations:create"] = False

    if not hasattr(settings, "SUDO_COOKIE_SECURE"):
        settings.SUDO_COOKIE_SECURE = getattr(settings, "SESSION_COOKIE_SECURE", False)
    if not hasattr(settings, "SUDO_COOKIE_DOMAIN"):
        settings.SUDO_COOKIE_DOMAIN = getattr(settings, "SESSION_COOKIE_DOMAIN", None)
    if not hasattr(settings, "SUDO_COOKIE_PATH"):
        settings.SUDO_COOKIE_PATH = getattr(settings, "SESSION_COOKIE_PATH", "/")

    if not hasattr(settings, "CSRF_COOKIE_SECURE"):
        settings.CSRF_COOKIE_SECURE = getattr(settings, "SESSION_COOKIE_SECURE", False)
    if not hasattr(settings, "CSRF_COOKIE_DOMAIN"):
        settings.CSRF_COOKIE_DOMAIN = getattr(settings, "SESSION_COOKIE_DOMAIN", None)
    if not hasattr(settings, "CSRF_COOKIE_PATH"):
        settings.CSRF_COOKIE_PATH = getattr(settings, "SESSION_COOKIE_PATH", "/")

    settings.CACHES["default"]["VERSION"] = settings.CACHE_VERSION

    settings.ASSET_VERSION = get_asset_version(settings)
    settings.STATIC_URL = settings.STATIC_URL.format(version=settings.ASSET_VERSION)

    if getattr(settings, "SENTRY_DEBUGGER", None) is None:
        settings.SENTRY_DEBUGGER = settings.DEBUG

    monkeypatch_model_unpickle()

    import django

    django.setup()

    monkeypatch_django_migrations()

    apply_legacy_settings(settings)

    bind_cache_to_option_store()

    register_plugins(settings)

    initialize_receivers()

    validate_options(settings)

    validate_snuba()

    configure_sdk()

    setup_services(validate=not skip_service_validation)

    from django.utils import timezone
    from sentry.app import env
    from sentry.runner.settings import get_sentry_conf

    env.data["config"] = get_sentry_conf()
    env.data["start_date"] = timezone.now()
Exemple #16
0
def initialize_app(config, skip_service_validation=False):
    settings = config['settings']

    bootstrap_options(settings, config['options'])

    configure_structlog()

    if 'south' in settings.INSTALLED_APPS:
        fix_south(settings)

    apply_legacy_settings(settings)

    # Commonly setups don't correctly configure themselves for production envs
    # so lets try to provide a bit more guidance
    if settings.CELERY_ALWAYS_EAGER and not settings.DEBUG:
        warnings.warn(
            'Sentry is configured to run asynchronous tasks in-process. '
            'This is not recommended within production environments. '
            'See https://docs.sentry.io/on-premise/server/queue/ for more information.'
        )

    if settings.SENTRY_SINGLE_ORGANIZATION:
        settings.SENTRY_FEATURES['organizations:create'] = False

    if not hasattr(settings, 'SUDO_COOKIE_SECURE'):
        settings.SUDO_COOKIE_SECURE = getattr(settings, 'SESSION_COOKIE_SECURE', False)
    if not hasattr(settings, 'SUDO_COOKIE_DOMAIN'):
        settings.SUDO_COOKIE_DOMAIN = getattr(settings, 'SESSION_COOKIE_DOMAIN', None)
    if not hasattr(settings, 'SUDO_COOKIE_PATH'):
        settings.SUDO_COOKIE_PATH = getattr(settings, 'SESSION_COOKIE_PATH', '/')

    if not hasattr(settings, 'CSRF_COOKIE_SECURE'):
        settings.CSRF_COOKIE_SECURE = getattr(settings, 'SESSION_COOKIE_SECURE', False)
    if not hasattr(settings, 'CSRF_COOKIE_DOMAIN'):
        settings.CSRF_COOKIE_DOMAIN = getattr(settings, 'SESSION_COOKIE_DOMAIN', None)
    if not hasattr(settings, 'CSRF_COOKIE_PATH'):
        settings.CSRF_COOKIE_PATH = getattr(settings, 'SESSION_COOKIE_PATH', '/')

    settings.CACHES['default']['VERSION'] = settings.CACHE_VERSION

    settings.ASSET_VERSION = get_asset_version(settings)
    settings.STATIC_URL = settings.STATIC_URL.format(
        version=settings.ASSET_VERSION,
    )

    import django
    if hasattr(django, 'setup'):
        # support for Django 1.7+
        django.setup()

    bind_cache_to_option_store()

    register_plugins(settings)

    initialize_receivers()

    validate_options(settings)

    configure_sdk()

    setup_services(validate=not skip_service_validation)

    from django.utils import timezone
    from sentry.app import env
    from sentry.runner.settings import get_sentry_conf
    env.data['config'] = get_sentry_conf()
    env.data['start_date'] = timezone.now()
Exemple #17
0
def initialize_app(config, skip_service_validation=False):
    settings = config["settings"]

    if settings.DEBUG:
        # Enable line buffering for stderr, TODO(py3.9) can be removed after py3.9, see bpo-13601
        sys.stderr = os.fdopen(sys.stderr.fileno(), "w", 1)
        sys.stdout = os.fdopen(sys.stdout.fileno(), "w", 1)

    # Just reuse the integration app for Single Org / Self-Hosted as
    # it doesn't make much sense to use 2 separate apps for SSO and
    # integration.
    if settings.SENTRY_SINGLE_ORGANIZATION:
        options_mapper.update(
            {
                "github-app.client-id": "GITHUB_APP_ID",
                "github-app.client-secret": "GITHUB_API_SECRET",
            }
        )

    bootstrap_options(settings, config["options"])

    configure_structlog()

    # Commonly setups don't correctly configure themselves for production envs
    # so lets try to provide a bit more guidance
    if settings.CELERY_ALWAYS_EAGER and not settings.DEBUG:
        warnings.warn(
            "Sentry is configured to run asynchronous tasks in-process. "
            "This is not recommended within production environments. "
            "See https://docs.sentry.io/on-premise/server/queue/ for more information."
        )

    if settings.SENTRY_SINGLE_ORGANIZATION:
        settings.SENTRY_FEATURES["organizations:create"] = False

    if not hasattr(settings, "SUDO_COOKIE_SECURE"):
        settings.SUDO_COOKIE_SECURE = getattr(settings, "SESSION_COOKIE_SECURE", False)
    if not hasattr(settings, "SUDO_COOKIE_DOMAIN"):
        settings.SUDO_COOKIE_DOMAIN = getattr(settings, "SESSION_COOKIE_DOMAIN", None)
    if not hasattr(settings, "SUDO_COOKIE_PATH"):
        settings.SUDO_COOKIE_PATH = getattr(settings, "SESSION_COOKIE_PATH", "/")

    if not hasattr(settings, "CSRF_COOKIE_SECURE"):
        settings.CSRF_COOKIE_SECURE = getattr(settings, "SESSION_COOKIE_SECURE", False)
    if not hasattr(settings, "CSRF_COOKIE_DOMAIN"):
        settings.CSRF_COOKIE_DOMAIN = getattr(settings, "SESSION_COOKIE_DOMAIN", None)
    if not hasattr(settings, "CSRF_COOKIE_PATH"):
        settings.CSRF_COOKIE_PATH = getattr(settings, "SESSION_COOKIE_PATH", "/")

    for key in settings.CACHES:
        if not hasattr(settings.CACHES[key], "VERSION"):
            settings.CACHES[key]["VERSION"] = 2

    settings.ASSET_VERSION = get_asset_version(settings)
    settings.STATIC_URL = settings.STATIC_URL.format(version=settings.ASSET_VERSION)

    if getattr(settings, "SENTRY_DEBUGGER", None) is None:
        settings.SENTRY_DEBUGGER = settings.DEBUG

    monkeypatch_model_unpickle()

    import django

    django.setup()

    monkeypatch_django_migrations()

    apply_legacy_settings(settings)

    bind_cache_to_option_store()

    register_plugins(settings)

    initialize_receivers()

    validate_options(settings)

    validate_snuba()

    configure_sdk()

    setup_services(validate=not skip_service_validation)

    from django.utils import timezone
    from sentry.app import env
    from sentry.runner.settings import get_sentry_conf

    env.data["config"] = get_sentry_conf()
    env.data["start_date"] = timezone.now()
Exemple #18
0
def initialize_app(config, skip_service_validation=False):
    settings = config['settings']

    bootstrap_options(settings, config['options'])

    configure_structlog()

    if 'south' in settings.INSTALLED_APPS:
        fix_south(settings)

    apply_legacy_settings(settings)

    # Commonly setups don't correctly configure themselves for production envs
    # so lets try to provide a bit more guidance
    if settings.CELERY_ALWAYS_EAGER and not settings.DEBUG:
        warnings.warn(
            'Sentry is configured to run asynchronous tasks in-process. '
            'This is not recommended within production environments. '
            'See https://docs.sentry.io/on-premise/server/queue/ for more information.'
        )

    if settings.SENTRY_SINGLE_ORGANIZATION:
        settings.SENTRY_FEATURES['organizations:create'] = False

    if not hasattr(settings, 'SUDO_COOKIE_SECURE'):
        settings.SUDO_COOKIE_SECURE = getattr(settings, 'SESSION_COOKIE_SECURE', False)
    if not hasattr(settings, 'SUDO_COOKIE_DOMAIN'):
        settings.SUDO_COOKIE_DOMAIN = getattr(settings, 'SESSION_COOKIE_DOMAIN', None)
    if not hasattr(settings, 'SUDO_COOKIE_PATH'):
        settings.SUDO_COOKIE_PATH = getattr(settings, 'SESSION_COOKIE_PATH', '/')

    if not hasattr(settings, 'CSRF_COOKIE_SECURE'):
        settings.CSRF_COOKIE_SECURE = getattr(settings, 'SESSION_COOKIE_SECURE', False)
    if not hasattr(settings, 'CSRF_COOKIE_DOMAIN'):
        settings.CSRF_COOKIE_DOMAIN = getattr(settings, 'SESSION_COOKIE_DOMAIN', None)
    if not hasattr(settings, 'CSRF_COOKIE_PATH'):
        settings.CSRF_COOKIE_PATH = getattr(settings, 'SESSION_COOKIE_PATH', '/')

    settings.CACHES['default']['VERSION'] = settings.CACHE_VERSION

    settings.ASSET_VERSION = get_asset_version(settings)
    settings.STATIC_URL = settings.STATIC_URL.format(
        version=settings.ASSET_VERSION,
    )

    import django
    if hasattr(django, 'setup'):
        # support for Django 1.7+
        django.setup()

    bind_cache_to_option_store()

    register_plugins(settings)

    initialize_receivers()

    validate_options(settings)

    configure_sdk()

    setup_services(validate=not skip_service_validation)

    from django.utils import timezone
    from sentry.app import env
    from sentry.runner.settings import get_sentry_conf
    env.data['config'] = get_sentry_conf()
    env.data['start_date'] = timezone.now()