-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use new handle_exception hook, refs #1
Also refs simonw/datasette#1770
- Loading branch information
Showing
3 changed files
with
78 additions
and
20 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,61 @@ | ||
from datasette_sentry import asgi_wrapper | ||
from datasette.app import Datasette | ||
from datasette import hookimpl | ||
from datasette.plugins import pm | ||
import sentry_sdk | ||
import functools | ||
|
||
|
||
class FakeDatasette: | ||
def __init__(self, config): | ||
self.config = config | ||
|
||
def plugin_config(self, name): | ||
assert "datasette-sentry" == name | ||
return self.config | ||
import pytest | ||
|
||
|
||
def test_asgi_wrapper(): | ||
app = object() | ||
wrapper = asgi_wrapper(FakeDatasette({"dsn": "https://demo@sentry.io/1234"})) | ||
wrapped = wrapper(app) | ||
assert app == wrapped.app | ||
ds = Datasette( | ||
metadata={ | ||
"plugins": {"datasette-sentry": {"dsn": "https://demo@sentry.io/1234"}} | ||
} | ||
) | ||
wrapper = asgi_wrapper(ds) | ||
wrapped = wrapper(ds) | ||
assert ds == wrapped.app | ||
assert isinstance(wrapped, sentry_sdk.integrations.asgi.SentryAsgiMiddleware) | ||
|
||
|
||
def test_not_wrapped_if_no_configuration(): | ||
app = object() | ||
wrapper = asgi_wrapper(FakeDatasette({})) | ||
assert app == wrapper(app) | ||
ds = Datasette() | ||
wrapper = asgi_wrapper(ds) | ||
assert ds == wrapper(ds) | ||
|
||
|
||
@pytest.mark.asyncio | ||
@pytest.mark.parametrize("configured", (True, False)) | ||
async def test_logs_errors_to_sentry(configured): | ||
class ErrorPlugin: | ||
__name__ = "ErrorPlugin" | ||
|
||
@hookimpl | ||
def register_routes(self): | ||
return (("/error", lambda: 1 / 0),) | ||
|
||
pm.register(ErrorPlugin(), name="undo") | ||
try: | ||
# Rest of test goes here | ||
if configured: | ||
ds = Datasette( | ||
metadata={ | ||
"plugins": { | ||
"datasette-sentry": { | ||
"dsn": "https://demo@sentry.io/1234", | ||
"capture_events": True, | ||
} | ||
} | ||
} | ||
) | ||
else: | ||
ds = Datasette() | ||
response = await ds.client.get("/error") | ||
assert response.status_code == 500 | ||
if configured: | ||
assert len(ds._datasette_sentry_events) == 1 | ||
else: | ||
assert not hasattr(ds, "_datasette_sentry_events") | ||
assert not hasattr(ds, "_datasette_sentry_envelopes") | ||
finally: | ||
pm.unregister(name="undo") |