Skip to content

Commit 7fbf5b0

Browse files
committed
Merge pull request #2155 from martinmaillard/set-user-on-wrapped-request
Set authenticated user on wrapped request
2 parents d872c8e + a68e78b commit 7fbf5b0

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

rest_framework/request.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ def user(self, value):
277277
Sets the user on the current request. This is necessary to maintain
278278
compatibility with django.contrib.auth where the user property is
279279
set in the login and logout functions.
280+
281+
Sets the user on the wrapped original request as well.
280282
"""
281283
self._user = value
284+
self._request.user = value
282285

283286
@property
284287
def auth(self):
@@ -456,7 +459,7 @@ def _authenticate(self):
456459

457460
if user_auth_tuple is not None:
458461
self._authenticator = authenticator
459-
self._user, self._auth = user_auth_tuple
462+
self.user, self._auth = user_auth_tuple
460463
return
461464

462465
self._not_authenticated()
@@ -471,9 +474,9 @@ def _not_authenticated(self):
471474
self._authenticator = None
472475

473476
if api_settings.UNAUTHENTICATED_USER:
474-
self._user = api_settings.UNAUTHENTICATED_USER()
477+
self.user = api_settings.UNAUTHENTICATED_USER()
475478
else:
476-
self._user = None
479+
self.user = None
477480

478481
if api_settings.UNAUTHENTICATED_TOKEN:
479482
self._auth = api_settings.UNAUTHENTICATED_TOKEN()

tests/test_middleware.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
from django.conf.urls import patterns, url
3+
from django.contrib.auth.models import User
4+
from rest_framework.authentication import TokenAuthentication
5+
from rest_framework.authtoken.models import Token
6+
from rest_framework.test import APITestCase
7+
from rest_framework.views import APIView
8+
9+
10+
urlpatterns = patterns(
11+
'',
12+
url(r'^$', APIView.as_view(authentication_classes=(TokenAuthentication,))),
13+
)
14+
15+
16+
class MyMiddleware(object):
17+
18+
def process_response(self, request, response):
19+
assert hasattr(request, 'user'), '`user` is not set on request'
20+
assert request.user.is_authenticated(), '`user` is not authenticated'
21+
return response
22+
23+
24+
class TestMiddleware(APITestCase):
25+
26+
urls = 'tests.test_middleware'
27+
28+
def test_middleware_can_access_user_when_processing_response(self):
29+
user = User.objects.create_user('john', '[email protected]', 'password')
30+
key = 'abcd1234'
31+
Token.objects.create(key=key, user=user)
32+
33+
with self.settings(
34+
MIDDLEWARE_CLASSES=('tests.test_middleware.MyMiddleware',)
35+
):
36+
auth = 'Token ' + key
37+
self.client.get('/', HTTP_AUTHORIZATION=auth)

tests/test_request.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ class TestUserSetter(TestCase):
224224
def setUp(self):
225225
# Pass request object through session middleware so session is
226226
# available to login and logout functions
227-
self.request = Request(factory.get('/'))
227+
self.wrapped_request = factory.get('/')
228+
self.request = Request(self.wrapped_request)
228229
SessionMiddleware().process_request(self.request)
229230

230231
User.objects.create_user('ringo', '[email protected]', 'yellow')
@@ -244,6 +245,10 @@ def test_user_can_logout(self):
244245
logout(self.request)
245246
self.assertTrue(self.request.user.is_anonymous())
246247

248+
def test_logged_in_user_is_set_on_wrapped_request(self):
249+
login(self.request, self.user)
250+
self.assertEqual(self.wrapped_request.user, self.user)
251+
247252

248253
class TestAuthSetter(TestCase):
249254

0 commit comments

Comments
 (0)