Skip to content

Commit cc0db1c

Browse files
committed
add tests for async security middleware
1 parent ef407c8 commit cc0db1c

File tree

2 files changed

+408
-0
lines changed

2 files changed

+408
-0
lines changed

tests/test_middlewares/test_middleware_mixin.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.http.response import HttpResponse
77

88
from django_async_extensions.middleware.base import AsyncMiddlewareMixin
9+
from django_async_extensions.middleware.security import AsyncSecurityMiddleware
910

1011
req = HttpResponse()
1112
resp = HttpResponse()
@@ -30,6 +31,10 @@ async def process_request(self, request):
3031

3132

3233
class TestMiddlewareMixin:
34+
middlewares = [
35+
AsyncSecurityMiddleware,
36+
]
37+
3338
def test_repr(self):
3439
class GetResponse:
3540
async def __call__(self):
@@ -48,6 +53,28 @@ async def get_response():
4853
"TestMiddlewareMixin.test_repr.<locals>.get_response>"
4954
)
5055

56+
def test_passing_explicit_none(self, subtests):
57+
msg = "get_response must be provided"
58+
for middleware in self.middlewares:
59+
with subtests.test(middleware=middleware):
60+
with pytest.raises(ValueError, match=msg):
61+
middleware(None)
62+
63+
def test_coroutine(self, subtests):
64+
async def async_get_response(request):
65+
return HttpResponse()
66+
67+
def sync_get_response(request):
68+
return HttpResponse()
69+
70+
for middleware in self.middlewares:
71+
with subtests.test(middleware=middleware.__qualname__):
72+
middleware_instance = middleware(async_get_response)
73+
assert iscoroutinefunction(middleware_instance)
74+
75+
with pytest.raises(ImproperlyConfigured):
76+
middleware(sync_get_response)
77+
5178
def test_call_is_async(self):
5279
assert iscoroutinefunction(AsyncMiddlewareMixin.__call__)
5380

0 commit comments

Comments
 (0)