Permalink
Browse files

Added setter to user property

  • Loading branch information...
Mark Hughes
Mark Hughes committed Nov 24, 2012
1 parent fd89bca commit 85a921c7efcea50a5d594082f0e4ddeefd95402f
Showing with 37 additions and 0 deletions.
  1. +9 −0 rest_framework/request.py
  2. +28 −0 rest_framework/tests/request.py
@@ -169,6 +169,15 @@ def user(self):
self._user, self._auth = self._authenticate()
return self._user
+ @user.setter
+ def user(self, value):
+ """
+ Sets the user on the current request. This is necessary to maintain
+ compatilbility with django.contrib.auth where the user proprety is
+ set in the login and logout functions.
+ """
+ self._user = value
+
@property
def auth(self):
"""
@@ -3,6 +3,8 @@
"""
from django.conf.urls.defaults import patterns
from django.contrib.auth.models import User
+from django.contrib.auth import authenticate, login, logout
+from django.contrib.sessions.middleware import SessionMiddleware
from django.test import TestCase, Client
from django.utils import simplejson as json
@@ -276,3 +278,29 @@ def test_user_logged_in_authentication_has_POST_when_not_logged_in(self):
# response = self.csrf_client.post('/', content)
# self.assertEqual(status.OK, response.status_code, "POST data is malformed")
+
+
+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('/'))
+ SessionMiddleware().process_request(self.request)
+
+ User.objects.create_user('ringo', 'starr@thebeatles.com', 'yellow')
+ self.user = authenticate(username='ringo', password='yellow')
+
+ def test_user_can_be_set(self):
+ self.request.user = self.user
+ self.assertEqual(self.request.user, self.user)
+
+ def test_user_can_login(self):
+ login(self.request, self.user)
+ self.assertEqual(self.request.user, self.user)
+
+ def test_user_can_logout(self):
+ self.request.user = self.user
+ self.assertFalse(self.request.user.is_anonymous())
+ logout(self.request)
+ self.assertTrue(self.request.user.is_anonymous())

0 comments on commit 85a921c

Please sign in to comment.