From 7aa842573d99b6be1ffdb8d12e1817b51e3e5ec8 Mon Sep 17 00:00:00 2001 From: Aryan Curiel Date: Mon, 29 Jul 2024 21:34:06 +0300 Subject: [PATCH 1/2] Using django HttpRequest as spec when mocking request in test client --- ninja/testing/client.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ninja/testing/client.py b/ninja/testing/client.py index 0be8ce0a..f657814b 100644 --- a/ninja/testing/client.py +++ b/ninja/testing/client.py @@ -5,7 +5,7 @@ from urllib.parse import urljoin from django.http import QueryDict, StreamingHttpResponse -from django.http.request import HttpHeaders +from django.http.request import HttpHeaders, HttpRequest from ninja import NinjaAPI, Router from ninja.responses import NinjaJSONEncoder @@ -111,7 +111,7 @@ def _resolve( def _build_request( self, method: str, path: str, data: Dict, request_params: Any ) -> Mock: - request = Mock() + request = Mock(spec=HttpRequest) request.method = method request.path = path request.body = "" From 818505e19b18f2e9984195d8338156af4acd2266 Mon Sep 17 00:00:00 2001 From: Aryan Curiel Date: Mon, 29 Jul 2024 21:35:38 +0300 Subject: [PATCH 2/2] Mocking request.user field --- ninja/testing/client.py | 1 + 1 file changed, 1 insertion(+) diff --git a/ninja/testing/client.py b/ninja/testing/client.py index f657814b..37c4f890 100644 --- a/ninja/testing/client.py +++ b/ninja/testing/client.py @@ -121,6 +121,7 @@ def _build_request( request.build_absolute_uri = build_absolute_uri request.auth = None + request.user = Mock() if "user" not in request_params: request.user.is_authenticated = False