Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions tests/integrations/django/asgi/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
import django
import pytest
from channels.testing import HttpCommunicator

from sentry_sdk import capture_message
from sentry_sdk.integrations.django import DjangoIntegration
from sentry_sdk.integrations.django.asgi import _asgi_middleware_mixin_factory
from tests.integrations.django.myapp.asgi import channels_application
from tests.integrations.django.utils import pytest_mark_django_db_decorator

try:
from django.urls import reverse
Expand Down Expand Up @@ -737,3 +739,49 @@ async def test_transaction_http_method_custom(sentry_init, capture_events, appli
(event1, event2) = events
assert event1["request"]["method"] == "OPTIONS"
assert event2["request"]["method"] == "HEAD"


@pytest.mark.asyncio
@pytest.mark.forked
@pytest_mark_django_db_decorator()
@pytest.mark.skipif(
django.VERSION < (3, 0), reason="Django ASGI support shipped in 3.0"
)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test skipif condition requires newer Django version

Medium Severity

The test test_user_pii_in_asgi_with_auth has a skipif condition checking for django.VERSION < (3, 0), but the test view async_mylogin uses User.objects.acreate_user() (added in Django 4.1) and alogin (added in Django 5.1). This mismatch means the test will fail with an ImportError when run on Django versions 3.0 through 5.0, since the skipif condition won't exclude those versions but the required APIs don't exist.

🔬 Verification Test

Why verification test was not possible: This test failure would manifest only when running the test suite against specific Django versions (3.x, 4.x, 5.0). The test would fail with an ImportError: cannot import name 'alogin' from 'django.contrib.auth' when Django < 5.1 is used. Verifying this would require setting up multiple Django version environments which is outside the scope of this review.

Additional Locations (1)

Fix in Cursor Fix in Web

async def test_user_pii_in_asgi_with_auth(sentry_init, capture_events, settings):
settings.MIDDLEWARE = [
"django.contrib.sessions.middleware.SessionMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
]

asgi_application.load_middleware(is_async=True)

sentry_init(
integrations=[DjangoIntegration()],
send_default_pii=True,
)

events = capture_events()

comm = HttpCommunicator(asgi_application, "GET", "/async_mylogin")
response = await comm.get_response()
await comm.wait()

assert response["status"] == 200

# Get session cookie from login response
set_cookie = next(v for k, v in response["headers"] if k.lower() == b"set-cookie")
headers = [(b"cookie", set_cookie)]

comm = HttpCommunicator(asgi_application, "GET", "/async_message", headers=headers)
response = await comm.get_response()
await comm.wait()

assert response["status"] == 200

(event,) = events
assert event["message"] == "hi"
assert event["user"] == {
"email": "lennon@thebeatles.com",
"username": "john_async",
"id": "1",
}
3 changes: 3 additions & 0 deletions tests/integrations/django/myapp/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ def path(path, *args, **kwargs):
]

# async views
if views.async_mylogin is not None:
urlpatterns.append(path("async_mylogin", views.async_mylogin, name="async_mylogin"))

if views.async_message is not None:
urlpatterns.append(path("async_message", views.async_message, name="async_message"))

Expand Down
13 changes: 13 additions & 0 deletions tests/integrations/django/myapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,19 @@ def mylogin(request):
return HttpResponse("ok")


@csrf_exempt
async def async_mylogin(request):
user = await User.objects.acreate_user(
"john_async", "lennon@thebeatles.com", "johnpassword"
)
user.backend = "django.contrib.auth.backends.ModelBackend"

from django.contrib.auth import alogin

await alogin(request, user)
return HttpResponse("ok")


@csrf_exempt
def handler500(request):
return HttpResponseServerError("Sentry error.")
Expand Down
Loading