Skip to content

Commit 354ae73

Browse files
authored
Make APIClient.force_authenticate() work with user=None (#8212)
* Fix testing with token * Add unit test * Split unit test into 3 * Fix linting error
1 parent acf6582 commit 354ae73

File tree

2 files changed

+46
-10
lines changed

2 files changed

+46
-10
lines changed

rest_framework/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ def force_authenticate(self, user=None, token=None):
277277
"""
278278
self.handler._force_user = user
279279
self.handler._force_token = token
280-
if user is None:
280+
if user is None and token is None:
281281
self.logout() # Also clear any possible session info if required
282282

283283
def request(self, **kwargs):

tests/test_testing.py

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from django.urls import path
1111

1212
from rest_framework import fields, serializers
13+
from rest_framework.authtoken.models import Token
1314
from rest_framework.decorators import api_view
1415
from rest_framework.response import Response
1516
from rest_framework.test import (
@@ -19,10 +20,12 @@
1920

2021
@api_view(['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'OPTIONS'])
2122
def view(request):
22-
return Response({
23-
'auth': request.META.get('HTTP_AUTHORIZATION', b''),
24-
'user': request.user.username
25-
})
23+
data = {'auth': request.META.get('HTTP_AUTHORIZATION', b'')}
24+
if request.user:
25+
data['user'] = request.user.username
26+
if request.auth:
27+
data['token'] = request.auth.key
28+
return Response(data)
2629

2730

2831
@api_view(['GET', 'POST'])
@@ -78,14 +81,46 @@ def test_credentials(self):
7881
response = self.client.get('/view/')
7982
assert response.data['auth'] == 'example'
8083

81-
def test_force_authenticate(self):
84+
def test_force_authenticate_with_user(self):
8285
"""
83-
Setting `.force_authenticate()` forcibly authenticates each request.
86+
Setting `.force_authenticate()` with a user forcibly authenticates each
87+
request with that user.
8488
"""
8589
user = User.objects.create_user('example', '[email protected]')
86-
self.client.force_authenticate(user)
90+
91+
self.client.force_authenticate(user=user)
92+
response = self.client.get('/view/')
93+
94+
assert response.data['user'] == 'example'
95+
assert 'token' not in response.data
96+
97+
def test_force_authenticate_with_token(self):
98+
"""
99+
Setting `.force_authenticate()` with a token forcibly authenticates each
100+
request with that token.
101+
"""
102+
user = User.objects.create_user('example', '[email protected]')
103+
token = Token.objects.create(key='xyz', user=user)
104+
105+
self.client.force_authenticate(token=token)
87106
response = self.client.get('/view/')
107+
108+
assert response.data['token'] == 'xyz'
109+
assert 'user' not in response.data
110+
111+
def test_force_authenticate_with_user_and_token(self):
112+
"""
113+
Setting `.force_authenticate()` with a user and token forcibly
114+
authenticates each request with that user and token.
115+
"""
116+
user = User.objects.create_user('example', '[email protected]')
117+
token = Token.objects.create(key='xyz', user=user)
118+
119+
self.client.force_authenticate(user=user, token=token)
120+
response = self.client.get('/view/')
121+
88122
assert response.data['user'] == 'example'
123+
assert response.data['token'] == 'xyz'
89124

90125
def test_force_authenticate_with_sessions(self):
91126
"""
@@ -102,8 +137,9 @@ def test_force_authenticate_with_sessions(self):
102137
response = self.client.get('/session-view/')
103138
assert response.data['active_session'] is True
104139

105-
# Force authenticating as `None` should also logout the user session.
106-
self.client.force_authenticate(None)
140+
# Force authenticating with `None` user and token should also logout
141+
# the user session.
142+
self.client.force_authenticate(user=None, token=None)
107143
response = self.client.get('/session-view/')
108144
assert response.data['active_session'] is False
109145

0 commit comments

Comments
 (0)