This repository has been archived by the owner on Sep 12, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(sentry): configure sentry before app instantiation (#258)
* 🐛 fix(sentry): call configure before starlite init * chore: changes for rebase on 0.29 Co-authored-by: Peter Schutt <peter.github@proton.me>
- Loading branch information
1 parent
efe26d6
commit 45982b4
Showing
10 changed files
with
146 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions
3
tests/unit/no_extras/test_init_plugin.py → ..._extras/test_init_plugin_for_no_extras.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Tests for init_plugin.py.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from unittest.mock import MagicMock | ||
|
||
from starlite import Starlite | ||
|
||
from starlite_saqlalchemy import init_plugin, worker | ||
|
||
if TYPE_CHECKING: | ||
|
||
from pytest import MonkeyPatch | ||
|
||
|
||
def test_do_worker_but_not_logging(monkeypatch: MonkeyPatch) -> None: | ||
"""Tests branch where we can have the worker enabled, but logging | ||
disabled.""" | ||
mock = MagicMock() | ||
monkeypatch.setattr(worker, "create_worker_instance", mock) | ||
config = init_plugin.PluginConfig(do_logging=False) | ||
Starlite(route_handlers=[], on_app_init=[init_plugin.ConfigureApp(config=config)]) | ||
mock.assert_called_once() | ||
call = mock.mock_calls[0] | ||
assert "before_process" not in call.kwargs | ||
assert "after_process" not in call.kwargs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"""Tests for init_plugin.py.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING | ||
from unittest.mock import MagicMock | ||
|
||
import pytest | ||
from sentry_sdk.integrations.starlite import SentryStarliteASGIMiddleware | ||
from sentry_sdk.integrations.starlite import ( | ||
exception_handler as sentry_after_exception_handler, | ||
) | ||
from starlite import Starlite | ||
from starlite.handlers import BaseRouteHandler | ||
from starlite.routes.http import HTTPRoute | ||
|
||
from starlite_saqlalchemy import init_plugin, sentry | ||
|
||
if TYPE_CHECKING: | ||
|
||
from pytest import MonkeyPatch | ||
|
||
|
||
@pytest.mark.parametrize( | ||
("env", "exp"), [("dev", True), ("prod", True), ("local", False), ("test", False)] | ||
) | ||
def test_sentry_environment_gate(env: str, exp: bool, monkeypatch: MonkeyPatch) -> None: | ||
"""Test that the sentry integration is configured under different | ||
environment names.""" | ||
monkeypatch.setattr(init_plugin, "IS_LOCAL_ENVIRONMENT", env == "local") | ||
monkeypatch.setattr(init_plugin, "IS_TEST_ENVIRONMENT", env == "test") | ||
mock = MagicMock() | ||
monkeypatch.setattr(sentry, "configure", mock) | ||
Starlite(route_handlers=[], on_app_init=[init_plugin.ConfigureApp()]) | ||
assert mock.call_count == int(exp) | ||
|
||
|
||
def test_do_sentry() -> None: | ||
"""Test that do_sentry flag correctly patch Starlite.""" | ||
old_init = Starlite.__init__ | ||
old_route_handle = HTTPRoute.handle | ||
old_resolve_middleware = BaseRouteHandler.resolve_middleware | ||
|
||
app = Starlite( | ||
route_handlers=[], | ||
on_app_init=[init_plugin.ConfigureApp(config=init_plugin.PluginConfig(do_sentry=True))], | ||
) | ||
after_exception_handlers = [] | ||
for handler in app.after_exception: | ||
handler_fn = handler.ref.value | ||
# If wrapped with `starlite.utils.async_partial` | ||
if hasattr(handler_fn, "func"): | ||
handler_fn = handler_fn.func | ||
after_exception_handlers.append(handler_fn) | ||
assert SentryStarliteASGIMiddleware in app.middleware # type: ignore | ||
assert sentry_after_exception_handler in after_exception_handlers # type: ignore | ||
|
||
Starlite.__init__ = old_init # type: ignore | ||
HTTPRoute.handle = old_route_handle # type: ignore | ||
BaseRouteHandler.resolve_middleware = old_resolve_middleware # type: ignore |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters