Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Ensurerequest.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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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', 'john@example.com', '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) |
| 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.wrapped_request = factory.get('/') | ||
| self.request = Request(self.wrapped_request) | ||
| SessionMiddleware().process_request(self.request) | ||
| User.objects.create_user('ringo', 'starr@thebeatles.com', '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) | ||
Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. Contributor There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Right, perhaps write a simple Middleware that you can unittest | ||
| class TestAuthSetter(TestCase): | ||