summaryrefslogtreecommitdiff
path: root/tests/auth_tests
diff options
context:
space:
mode:
authorJeremy Lainé <jeremy.laine@m4x.org>2016-04-02 13:18:26 +0200
committerTim Graham <timograham@gmail.com>2016-04-09 14:54:18 -0400
commitc1aec0feda73ede09503192a66f973598aef901d (patch)
treef1e4c09f3e98177cfe78cc9039b300f8984e7aed /tests/auth_tests
parentc16b9dd8e0ae757616e9accbaffecc521191ee98 (diff)
Fixed #25847 -- Made User.is_(anonymous|authenticated) properties.
Diffstat (limited to 'tests/auth_tests')
-rw-r--r--tests/auth_tests/test_auth_backends.py19
-rw-r--r--tests/auth_tests/test_basic.py48
-rw-r--r--tests/auth_tests/test_middleware.py4
-rw-r--r--tests/auth_tests/test_mixins.py4
-rw-r--r--tests/auth_tests/test_remote_user.py14
5 files changed, 66 insertions, 23 deletions
diff --git a/tests/auth_tests/test_auth_backends.py b/tests/auth_tests/test_auth_backends.py
index e3c0109c96..91f92397a2 100644
--- a/tests/auth_tests/test_auth_backends.py
+++ b/tests/auth_tests/test_auth_backends.py
@@ -12,7 +12,7 @@ from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.http import HttpRequest
from django.test import (
- SimpleTestCase, TestCase, modify_settings, override_settings,
+ SimpleTestCase, TestCase, mock, modify_settings, override_settings,
)
from .models import (
@@ -142,11 +142,10 @@ class BaseModelBackendTest(object):
self.assertEqual(backend.get_user_permissions(user), {'auth.test_user', 'auth.test_group'})
self.assertEqual(backend.get_group_permissions(user), {'auth.test_group'})
- user.is_anonymous = lambda: True
-
- self.assertEqual(backend.get_all_permissions(user), set())
- self.assertEqual(backend.get_user_permissions(user), set())
- self.assertEqual(backend.get_group_permissions(user), set())
+ with mock.patch.object(self.UserModel, 'is_anonymous', True):
+ self.assertEqual(backend.get_all_permissions(user), set())
+ self.assertEqual(backend.get_user_permissions(user), set())
+ self.assertEqual(backend.get_group_permissions(user), set())
def test_inactive_has_no_permissions(self):
"""
@@ -334,14 +333,14 @@ class SimpleRowlevelBackend(object):
if isinstance(obj, TestObj):
if user.username == 'test2':
return True
- elif user.is_anonymous() and perm == 'anon':
+ elif user.is_anonymous and perm == 'anon':
return True
elif not user.is_active and perm == 'inactive':
return True
return False
def has_module_perms(self, user, app_label):
- if not user.is_anonymous() and not user.is_active:
+ if not user.is_anonymous and not user.is_active:
return False
return app_label == "app1"
@@ -352,7 +351,7 @@ class SimpleRowlevelBackend(object):
if not isinstance(obj, TestObj):
return ['none']
- if user.is_anonymous():
+ if user.is_anonymous:
return ['anon']
if user.username == 'test2':
return ['simple', 'advanced']
@@ -578,7 +577,7 @@ class ChangedBackendSettingsTest(TestCase):
# Assert that the user retrieval is successful and the user is
# anonymous as the backend is not longer available.
self.assertIsNotNone(user)
- self.assertTrue(user.is_anonymous())
+ self.assertTrue(user.is_anonymous)
class TypeErrorBackend(object):
diff --git a/tests/auth_tests/test_basic.py b/tests/auth_tests/test_basic.py
index 81b7ac9c56..818f6a6d53 100644
--- a/tests/auth_tests/test_basic.py
+++ b/tests/auth_tests/test_basic.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+import warnings
+
from django.apps import apps
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AnonymousUser, User
@@ -44,7 +46,8 @@ class BasicTestCase(TestCase):
self.assertEqual(u.get_username(), 'testuser')
# Check authentication/permissions
- self.assertTrue(u.is_authenticated())
+ self.assertFalse(u.is_anonymous)
+ self.assertTrue(u.is_authenticated)
self.assertFalse(u.is_staff)
self.assertTrue(u.is_active)
self.assertFalse(u.is_superuser)
@@ -53,6 +56,26 @@ class BasicTestCase(TestCase):
u2 = User.objects.create_user('testuser2', 'test2@example.com')
self.assertFalse(u2.has_usable_password())
+ def test_is_anonymous_authenticated_method_deprecation(self):
+ deprecation_message = (
+ 'Using user.is_authenticated() and user.is_anonymous() as a '
+ 'method is deprecated. Remove the parentheses to use it as an '
+ 'attribute.'
+ )
+ u = User.objects.create_user('testuser', 'test@example.com', 'testpw')
+ # Backwards-compatibility callables
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.simplefilter('always')
+ self.assertFalse(u.is_anonymous())
+ self.assertEqual(len(warns), 1)
+ self.assertEqual(str(warns[0].message), deprecation_message)
+
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.simplefilter('always')
+ self.assertTrue(u.is_authenticated())
+ self.assertEqual(len(warns), 1)
+ self.assertEqual(str(warns[0].message), deprecation_message)
+
def test_user_no_email(self):
"Check that users can be created without an email"
u = User.objects.create_user('testuser1')
@@ -70,13 +93,34 @@ class BasicTestCase(TestCase):
self.assertEqual(a.pk, None)
self.assertEqual(a.username, '')
self.assertEqual(a.get_username(), '')
- self.assertFalse(a.is_authenticated())
+ self.assertTrue(a.is_anonymous)
+ self.assertFalse(a.is_authenticated)
self.assertFalse(a.is_staff)
self.assertFalse(a.is_active)
self.assertFalse(a.is_superuser)
self.assertEqual(a.groups.all().count(), 0)
self.assertEqual(a.user_permissions.all().count(), 0)
+ def test_anonymous_user_is_anonymous_authenticated_method_deprecation(self):
+ a = AnonymousUser()
+ deprecation_message = (
+ 'Using user.is_authenticated() and user.is_anonymous() as a '
+ 'method is deprecated. Remove the parentheses to use it as an '
+ 'attribute.'
+ )
+ # Backwards-compatibility callables
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.simplefilter('always') # prevent warnings from appearing as errors
+ self.assertTrue(a.is_anonymous())
+ self.assertEqual(len(warns), 1)
+ self.assertEqual(str(warns[0].message), deprecation_message)
+
+ with warnings.catch_warnings(record=True) as warns:
+ warnings.simplefilter('always') # prevent warnings from appearing as errors
+ self.assertFalse(a.is_authenticated())
+ self.assertEqual(len(warns), 1)
+ self.assertEqual(str(warns[0].message), deprecation_message)
+
def test_superuser(self):
"Check the creation and properties of a superuser"
super = User.objects.create_superuser('super', 'super@example.com', 'super')
diff --git a/tests/auth_tests/test_middleware.py b/tests/auth_tests/test_middleware.py
index 9ebb1e46ee..635c43beb0 100644
--- a/tests/auth_tests/test_middleware.py
+++ b/tests/auth_tests/test_middleware.py
@@ -16,7 +16,7 @@ class TestAuthenticationMiddleware(TestCase):
self.request.session = self.client.session
self.middleware.process_request(self.request)
self.assertIsNotNone(self.request.user)
- self.assertFalse(self.request.user.is_anonymous())
+ self.assertFalse(self.request.user.is_anonymous)
def test_changed_password_invalidates_session(self):
# After password change, user should be anonymous
@@ -24,6 +24,6 @@ class TestAuthenticationMiddleware(TestCase):
self.user.save()
self.middleware.process_request(self.request)
self.assertIsNotNone(self.request.user)
- self.assertTrue(self.request.user.is_anonymous())
+ self.assertTrue(self.request.user.is_anonymous)
# session should be flushed
self.assertIsNone(self.request.session.session_key)
diff --git a/tests/auth_tests/test_mixins.py b/tests/auth_tests/test_mixins.py
index 22e5709bc3..5df5ee3c4e 100644
--- a/tests/auth_tests/test_mixins.py
+++ b/tests/auth_tests/test_mixins.py
@@ -5,7 +5,7 @@ from django.contrib.auth.mixins import (
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
-from django.test import RequestFactory, TestCase
+from django.test import RequestFactory, TestCase, mock
from django.views.generic import View
@@ -78,9 +78,9 @@ class AccessMixinTests(TestCase):
with self.assertRaises(PermissionDenied):
view(request)
+ @mock.patch.object(models.User, 'is_authenticated', False)
def test_stacked_mixins_not_logged_in(self):
user = models.User.objects.create(username='joe', password='qwerty')
- user.is_authenticated = lambda: False
perms = models.Permission.objects.filter(codename__in=('add_customuser', 'change_customuser'))
user.user_permissions.add(*perms)
request = self.factory.get('/rand')
diff --git a/tests/auth_tests/test_remote_user.py b/tests/auth_tests/test_remote_user.py
index 4e916d80ec..3d77ea3b89 100644
--- a/tests/auth_tests/test_remote_user.py
+++ b/tests/auth_tests/test_remote_user.py
@@ -38,15 +38,15 @@ class RemoteUserTest(TestCase):
num_users = User.objects.count()
response = self.client.get('/remote_user/')
- self.assertTrue(response.context['user'].is_anonymous())
+ self.assertTrue(response.context['user'].is_anonymous)
self.assertEqual(User.objects.count(), num_users)
response = self.client.get('/remote_user/', **{self.header: None})
- self.assertTrue(response.context['user'].is_anonymous())
+ self.assertTrue(response.context['user'].is_anonymous)
self.assertEqual(User.objects.count(), num_users)
response = self.client.get('/remote_user/', **{self.header: ''})
- self.assertTrue(response.context['user'].is_anonymous())
+ self.assertTrue(response.context['user'].is_anonymous)
self.assertEqual(User.objects.count(), num_users)
def test_unknown_user(self):
@@ -118,7 +118,7 @@ class RemoteUserTest(TestCase):
self.assertEqual(response.context['user'].username, 'knownuser')
# During the session, the REMOTE_USER header disappears. Should trigger logout.
response = self.client.get('/remote_user/')
- self.assertEqual(response.context['user'].is_anonymous(), True)
+ self.assertTrue(response.context['user'].is_anonymous)
# verify the remoteuser middleware will not remove a user
# authenticated via another backend
User.objects.create_user(username='modeluser', password='foo')
@@ -148,7 +148,7 @@ class RemoteUserTest(TestCase):
def test_inactive_user(self):
User.objects.create(username='knownuser', is_active=False)
response = self.client.get('/remote_user/', **{self.header: 'knownuser'})
- self.assertTrue(response.context['user'].is_anonymous())
+ self.assertTrue(response.context['user'].is_anonymous)
class RemoteUserNoCreateBackend(RemoteUserBackend):
@@ -167,7 +167,7 @@ class RemoteUserNoCreateTest(RemoteUserTest):
def test_unknown_user(self):
num_users = User.objects.count()
response = self.client.get('/remote_user/', **{self.header: 'newuser'})
- self.assertTrue(response.context['user'].is_anonymous())
+ self.assertTrue(response.context['user'].is_anonymous)
self.assertEqual(User.objects.count(), num_users)
@@ -268,5 +268,5 @@ class PersistentRemoteUserTest(RemoteUserTest):
self.assertEqual(response.context['user'].username, 'knownuser')
# Should stay logged in if the REMOTE_USER header disappears.
response = self.client.get('/remote_user/')
- self.assertEqual(response.context['user'].is_anonymous(), False)
+ self.assertFalse(response.context['user'].is_anonymous)
self.assertEqual(response.context['user'].username, 'knownuser')