Merge pull request #894 from ae-utbm/sentry

Test sentry-debug endpoint
This commit is contained in:
thomas girod 2024-10-15 20:48:01 +02:00 committed by GitHub
commit 150d08dc45
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 6 deletions

View File

@ -724,7 +724,8 @@ if DEBUG:
"debug_toolbar.panels.signals.SignalsPanel",
"debug_toolbar.panels.redirects.RedirectsPanel",
]
SENTRY_ENV = "development"
if not TESTING:
SENTRY_ENV = "development" # We can't test if it gets overridden in settings
if TESTING:
CAPTCHA_TEST_MODE = True

32
sith/tests.py Normal file
View File

@ -0,0 +1,32 @@
from contextlib import nullcontext as does_not_raise
import pytest
from _pytest.python_api import RaisesContext
from django.test import Client
from django.test.utils import override_settings
from django.urls import reverse
@pytest.mark.django_db
@pytest.mark.parametrize(
("sentry_dsn", "sentry_env", "expected_error", "expected_return_code"),
[
# Working case
("something", "development", pytest.raises(ZeroDivisionError), None),
# View is disabled when DSN isn't defined or environment isn't development
("something", "production", does_not_raise(), 404),
("", "development", does_not_raise(), 404),
("", "production", does_not_raise(), 404),
],
)
def test_sentry_debug_endpoint(
client: Client,
sentry_dsn: str,
sentry_env: str,
expected_error: RaisesContext[ZeroDivisionError] | does_not_raise[None],
expected_return_code: int | None,
):
with expected_error, override_settings(
SENTRY_DSN=sentry_dsn, SENTRY_ENV=sentry_env
):
assert client.get(reverse("sentry-debug")).status_code == expected_return_code

View File

@ -17,6 +17,7 @@ from ajax_select import urls as ajax_select_urls
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.http import Http404
from django.urls import include, path
from django.views.i18n import JavaScriptCatalog
from ninja_extra import NinjaExtraAPI
@ -71,7 +72,8 @@ if settings.DEBUG:
urlpatterns += [path("__debug__/", include(debug_toolbar.urls))]
if settings.SENTRY_ENV == "development" and settings.SENTRY_DSN:
def sentry_debug(request):
"""Sentry debug endpoint
This function always crash and allows us to test
@ -83,8 +85,9 @@ if settings.SENTRY_ENV == "development" and settings.SENTRY_DSN:
NOTE : you need to specify the SENTRY_DSN setting in settings_custom.py
"""
def raise_exception(request):
if settings.SENTRY_ENV != "development" or not settings.SENTRY_DSN:
raise Http404
_division_by_zero = 1 / 0
urlpatterns += [path("sentry-debug/", raise_exception)]
urlpatterns += [path("sentry-debug/", sentry_debug, name="sentry-debug")]