Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit7fbf5b0

Browse files
Merge pull request#2155 from martinmaillard/set-user-on-wrapped-request
Set authenticated user on wrapped request
2 parentsd872c8e +a68e78b commit7fbf5b0

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
defauth(self):
@@ -456,7 +459,7 @@ def _authenticate(self):
456459

457460
ifuser_auth_tupleisnotNone:
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
ifapi_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
ifapi_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+
fromdjango.conf.urlsimportpatterns,url
3+
fromdjango.contrib.auth.modelsimportUser
4+
fromrest_framework.authenticationimportTokenAuthentication
5+
fromrest_framework.authtoken.modelsimportToken
6+
fromrest_framework.testimportAPITestCase
7+
fromrest_framework.viewsimportAPIView
8+
9+
10+
urlpatterns=patterns(
11+
'',
12+
url(r'^$',APIView.as_view(authentication_classes=(TokenAuthentication,))),
13+
)
14+
15+
16+
classMyMiddleware(object):
17+
18+
defprocess_response(self,request,response):
19+
asserthasattr(request,'user'),'`user` is not set on request'
20+
assertrequest.user.is_authenticated(),'`user` is not authenticated'
21+
returnresponse
22+
23+
24+
classTestMiddleware(APITestCase):
25+
26+
urls='tests.test_middleware'
27+
28+
deftest_middleware_can_access_user_when_processing_response(self):
29+
user=User.objects.create_user('john','john@example.com','password')
30+
key='abcd1234'
31+
Token.objects.create(key=key,user=user)
32+
33+
withself.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
defsetUp(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','starr@thebeatles.com','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+
deftest_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
classTestAuthSetter(TestCase):
249254

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp