|
| 1 | +""" |
| 2 | +Test that verifies exception capture functionality. |
| 3 | +
|
| 4 | +These tests verify that exceptions are actually captured to PostHog, not just that |
| 5 | +500 responses are returned. |
| 6 | +
|
| 7 | +Without process_exception(), view exceptions are NOT captured to PostHog (v6.7.11 and earlier). |
| 8 | +With process_exception(), Django calls this method to capture exceptions before |
| 9 | +converting them to 500 responses. |
| 10 | +""" |
| 11 | + |
| 12 | +import os |
| 13 | +import django |
| 14 | + |
| 15 | +# Setup Django before importing anything else |
| 16 | +os.environ.setdefault("DJANGO_SETTINGS_MODULE", "testdjango.settings") |
| 17 | +django.setup() |
| 18 | + |
| 19 | +import pytest # noqa: E402 |
| 20 | +from httpx import AsyncClient, ASGITransport # noqa: E402 |
| 21 | +from django.core.asgi import get_asgi_application # noqa: E402 |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="session") |
| 25 | +def asgi_app(): |
| 26 | + """Shared ASGI application for all tests.""" |
| 27 | + return get_asgi_application() |
| 28 | + |
| 29 | + |
| 30 | +@pytest.mark.asyncio |
| 31 | +async def test_async_exception_is_captured(asgi_app): |
| 32 | + """ |
| 33 | + Test that async view exceptions are captured to PostHog. |
| 34 | +
|
| 35 | + The middleware's process_exception() method ensures exceptions are captured. |
| 36 | + Without it (v6.7.11 and earlier), exceptions are NOT captured even though 500 is returned. |
| 37 | + """ |
| 38 | + from unittest.mock import patch |
| 39 | + |
| 40 | + # Track captured exceptions |
| 41 | + captured = [] |
| 42 | + |
| 43 | + def mock_capture(exception, **kwargs): |
| 44 | + """Mock capture_exception to record calls.""" |
| 45 | + captured.append( |
| 46 | + { |
| 47 | + "exception": exception, |
| 48 | + "type": type(exception).__name__, |
| 49 | + "message": str(exception), |
| 50 | + } |
| 51 | + ) |
| 52 | + |
| 53 | + # Patch at the posthog module level where middleware imports from |
| 54 | + with patch("posthog.capture_exception", side_effect=mock_capture): |
| 55 | + async with AsyncClient( |
| 56 | + transport=ASGITransport(app=asgi_app), base_url="http://testserver" |
| 57 | + ) as ac: |
| 58 | + response = await ac.get("/test/async-exception") |
| 59 | + |
| 60 | + # Django returns 500 |
| 61 | + assert response.status_code == 500 |
| 62 | + |
| 63 | + # CRITICAL: Verify PostHog captured the exception |
| 64 | + assert len(captured) > 0, "Exception was NOT captured to PostHog!" |
| 65 | + |
| 66 | + # Verify it's the right exception |
| 67 | + exception_data = captured[0] |
| 68 | + assert exception_data["type"] == "ValueError" |
| 69 | + assert "Test exception from Django 5 async view" in exception_data["message"] |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.asyncio |
| 73 | +async def test_sync_exception_is_captured(asgi_app): |
| 74 | + """ |
| 75 | + Test that sync view exceptions are captured to PostHog. |
| 76 | +
|
| 77 | + The middleware's process_exception() method ensures exceptions are captured. |
| 78 | + Without it (v6.7.11 and earlier), exceptions are NOT captured even though 500 is returned. |
| 79 | + """ |
| 80 | + from unittest.mock import patch |
| 81 | + |
| 82 | + # Track captured exceptions |
| 83 | + captured = [] |
| 84 | + |
| 85 | + def mock_capture(exception, **kwargs): |
| 86 | + """Mock capture_exception to record calls.""" |
| 87 | + captured.append( |
| 88 | + { |
| 89 | + "exception": exception, |
| 90 | + "type": type(exception).__name__, |
| 91 | + "message": str(exception), |
| 92 | + } |
| 93 | + ) |
| 94 | + |
| 95 | + # Patch at the posthog module level where middleware imports from |
| 96 | + with patch("posthog.capture_exception", side_effect=mock_capture): |
| 97 | + async with AsyncClient( |
| 98 | + transport=ASGITransport(app=asgi_app), base_url="http://testserver" |
| 99 | + ) as ac: |
| 100 | + response = await ac.get("/test/sync-exception") |
| 101 | + |
| 102 | + # Django returns 500 |
| 103 | + assert response.status_code == 500 |
| 104 | + |
| 105 | + # CRITICAL: Verify PostHog captured the exception |
| 106 | + assert len(captured) > 0, "Exception was NOT captured to PostHog!" |
| 107 | + |
| 108 | + # Verify it's the right exception |
| 109 | + exception_data = captured[0] |
| 110 | + assert exception_data["type"] == "ValueError" |
| 111 | + assert "Test exception from Django 5 sync view" in exception_data["message"] |
0 commit comments