Skip to content

Commit

Permalink
Add test for async capability in middleware
Browse files Browse the repository at this point in the history
A new test has been added to verify the async capability of the EasyAuditMiddleware. This ensures that the Django logger does not emit a debug message for asynchronous handler adaptation for the middleware. This is aligned with the Django documentation recommendations on async views.
  • Loading branch information
Kamil Niski committed Apr 25, 2024
1 parent 83e965f commit 6b5f9b4
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import logging

import pytest
from asgiref.sync import sync_to_async
Expand Down Expand Up @@ -338,6 +339,23 @@ async def test_remote_addr_another(self, async_client):
event = await RequestEvent.objects.aget(url=reverse("test_app:index"))
assert event.remote_ip == "10.0.0.1"

async def test_middleware_is_async_capable(self, async_client, caplog, settings):
"""Test for async capability of EasyAuditMiddleware.
If the EasyAuditMiddleware is async capable Django `django.request` logger
will not emit debug message 'Asynchronous handler adapted for middleware …'
See: https://docs.djangoproject.com/en/5.0/topics/async/#async-views
"""
unwanted_log_message = (
"Asynchronous handler adapted for middleware "
"easyaudit.middleware.easyaudit.EasyAuditMiddleware"
)
settings.DEBUG = True
with caplog.at_level(logging.DEBUG, "django.request"):
await async_client.get(reverse("test_app:index"))
assert unwanted_log_message not in caplog.text


@pytest.mark.django_db
class TestWSGIRequestEvent:
Expand Down

0 comments on commit 6b5f9b4

Please sign in to comment.