-
-
Notifications
You must be signed in to change notification settings - Fork 7k
Ensure request.user
is available to response middleware.
#2155
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tomchristie
merged 3 commits into
encode:master
from
martinmaillard:set-user-on-wrapped-request
Dec 17, 2014
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
|
||
from django.conf.urls import patterns, url | ||
from django.contrib.auth.models import User | ||
from rest_framework.authentication import TokenAuthentication | ||
from rest_framework.authtoken.models import Token | ||
from rest_framework.test import APITestCase | ||
from rest_framework.views import APIView | ||
|
||
|
||
urlpatterns = patterns( | ||
'', | ||
url(r'^$', APIView.as_view(authentication_classes=(TokenAuthentication,))), | ||
) | ||
|
||
|
||
class MyMiddleware(object): | ||
|
||
def process_response(self, request, response): | ||
assert hasattr(request, 'user'), '`user` is not set on request' | ||
assert request.user.is_authenticated(), '`user` is not authenticated' | ||
return response | ||
|
||
|
||
class TestMiddleware(APITestCase): | ||
|
||
urls = 'tests.test_middleware' | ||
|
||
def test_middleware_can_access_user_when_processing_response(self): | ||
user = User.objects.create_user('john', '[email protected]', 'password') | ||
key = 'abcd1234' | ||
Token.objects.create(key=key, user=user) | ||
|
||
with self.settings( | ||
MIDDLEWARE_CLASSES=('tests.test_middleware.MyMiddleware',) | ||
): | ||
auth = 'Token ' + key | ||
self.client.get('/', HTTP_AUTHORIZATION=auth) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -224,7 +224,8 @@ class TestUserSetter(TestCase): | |
def setUp(self): | ||
# Pass request object through session middleware so session is | ||
# available to login and logout functions | ||
self.request = Request(factory.get('/')) | ||
self.wrapped_request = factory.get('/') | ||
self.request = Request(self.wrapped_request) | ||
SessionMiddleware().process_request(self.request) | ||
|
||
User.objects.create_user('ringo', '[email protected]', 'yellow') | ||
|
@@ -244,6 +245,10 @@ def test_user_can_logout(self): | |
logout(self.request) | ||
self.assertTrue(self.request.user.is_anonymous()) | ||
|
||
def test_logged_in_user_is_set_on_wrapped_request(self): | ||
login(self.request, self.user) | ||
self.assertEqual(self.wrapped_request.user, self.user) | ||
|
||
|
||
class TestAuthSetter(TestCase): | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was actually hoping for a test demonstrating the change this has against middleware.
The test as it is here demonstrates the implementation, but doesn't make the motivation clear.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, perhaps write a simple Middleware that you can unittest
process_response()
and demonstrate being able to access the expected user there?