summaryrefslogtreecommitdiff
path: root/tests/async
diff options
context:
space:
mode:
authorJaap Roes <jroes@leukeleu.nl>2024-11-28 14:42:59 +0100
committerSarah Boyce <42296566+sarahboyce@users.noreply.github.com>2024-11-28 17:43:46 +0100
commitceecd518b19044181a3598c55ebed7c2545963cc (patch)
tree2d50bac2f6898f1c84e9c6ea0276a5d5ac3fa890 /tests/async
parent28b9b8d6d900feea731d0724b996959a73ff33b5 (diff)
Fixed #35530 -- Deprecated request.user fallback in auth.login and auth.alogin.
Diffstat (limited to 'tests/async')
-rw-r--r--tests/async/test_async_auth.py54
1 files changed, 52 insertions, 2 deletions
diff --git a/tests/async/test_async_auth.py b/tests/async/test_async_auth.py
index 37884d13a6..3d5a6b678d 100644
--- a/tests/async/test_async_auth.py
+++ b/tests/async/test_async_auth.py
@@ -8,6 +8,7 @@ from django.contrib.auth import (
from django.contrib.auth.models import AnonymousUser, User
from django.http import HttpRequest
from django.test import TestCase, override_settings
+from django.utils.deprecation import RemovedInDjango61Warning
class AsyncAuthTest(TestCase):
@@ -60,7 +61,52 @@ class AsyncAuthTest(TestCase):
self.assertIsInstance(user, User)
self.assertEqual(user.username, second_user.username)
- async def test_alogin_without_user(self):
+ # RemovedInDjango61Warning: When the deprecation ends, replace with:
+ # async def test_alogin_without_user(self):
+ async def test_alogin_without_user_no_request_user(self):
+ request = HttpRequest()
+ request.session = await self.client.asession()
+ # RemovedInDjango61Warning: When the deprecation ends, replace with:
+ # with self.assertRaisesMessage(
+ # AttributeError,
+ # "'NoneType' object has no attribute 'get_session_auth_hash'",
+ # ):
+ # await alogin(request, None)
+ with (
+ self.assertRaisesMessage(
+ AttributeError,
+ "'HttpRequest' object has no attribute 'auser'",
+ ),
+ self.assertWarnsMessage(
+ RemovedInDjango61Warning,
+ "Fallback to request.user when user is None will be removed.",
+ ),
+ ):
+ await alogin(request, None)
+
+ # RemovedInDjango61Warning: When the deprecation ends, remove completely.
+ async def test_alogin_without_user_anonymous_request(self):
+ async def auser():
+ return AnonymousUser()
+
+ request = HttpRequest()
+ request.user = AnonymousUser()
+ request.auser = auser
+ request.session = await self.client.asession()
+ with (
+ self.assertRaisesMessage(
+ AttributeError,
+ "'AnonymousUser' object has no attribute '_meta'",
+ ),
+ self.assertWarnsMessage(
+ RemovedInDjango61Warning,
+ "Fallback to request.user when user is None will be removed.",
+ ),
+ ):
+ await alogin(request, None)
+
+ # RemovedInDjango61Warning: When the deprecation ends, remove completely.
+ async def test_alogin_without_user_authenticated_request(self):
async def auser():
return self.test_user
@@ -68,7 +114,11 @@ class AsyncAuthTest(TestCase):
request.user = self.test_user
request.auser = auser
request.session = await self.client.asession()
- await alogin(request, None)
+ with self.assertWarnsMessage(
+ RemovedInDjango61Warning,
+ "Fallback to request.user when user is None will be removed.",
+ ):
+ await alogin(request, None)
user = await aget_user(request)
self.assertIsInstance(user, User)
self.assertEqual(user.username, self.test_user.username)