Imported required models directly in auth management tests.
This commit is contained in:
parent
7bb373e309
commit
3096f4b082
|
@ -5,13 +5,13 @@ import sys
|
||||||
from datetime import date
|
from datetime import date
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.contrib.auth import management, models
|
from django.contrib.auth import management
|
||||||
from django.contrib.auth.checks import check_user_model
|
from django.contrib.auth.checks import check_user_model
|
||||||
from django.contrib.auth.management import create_permissions
|
from django.contrib.auth.management import create_permissions
|
||||||
from django.contrib.auth.management.commands import (
|
from django.contrib.auth.management.commands import (
|
||||||
changepassword, createsuperuser,
|
changepassword, createsuperuser,
|
||||||
)
|
)
|
||||||
from django.contrib.auth.models import Group, User
|
from django.contrib.auth.models import Group, Permission, User
|
||||||
from django.contrib.auth.tests.custom_user import CustomUser
|
from django.contrib.auth.tests.custom_user import CustomUser
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core import checks, exceptions
|
from django.core import checks, exceptions
|
||||||
|
@ -97,7 +97,7 @@ class GetDefaultUsernameTestCase(TestCase):
|
||||||
self.assertEqual(management.get_default_username(), 'joe')
|
self.assertEqual(management.get_default_username(), 'joe')
|
||||||
|
|
||||||
def test_existing(self):
|
def test_existing(self):
|
||||||
models.User.objects.create(username='joe')
|
User.objects.create(username='joe')
|
||||||
management.get_system_username = lambda: 'joe'
|
management.get_system_username = lambda: 'joe'
|
||||||
self.assertEqual(management.get_default_username(), '')
|
self.assertEqual(management.get_default_username(), '')
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
|
@ -115,7 +115,7 @@ class GetDefaultUsernameTestCase(TestCase):
|
||||||
class ChangepasswordManagementCommandTestCase(TestCase):
|
class ChangepasswordManagementCommandTestCase(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self.user = models.User.objects.create_user(username='joe', password='qwerty')
|
self.user = User.objects.create_user(username='joe', password='qwerty')
|
||||||
self.stdout = six.StringIO()
|
self.stdout = six.StringIO()
|
||||||
self.stderr = six.StringIO()
|
self.stderr = six.StringIO()
|
||||||
|
|
||||||
|
@ -136,7 +136,7 @@ class ChangepasswordManagementCommandTestCase(TestCase):
|
||||||
command_output,
|
command_output,
|
||||||
"Changing password for user 'joe'\nPassword changed successfully for user 'joe'"
|
"Changing password for user 'joe'\nPassword changed successfully for user 'joe'"
|
||||||
)
|
)
|
||||||
self.assertTrue(models.User.objects.get(username="joe").check_password("not qwerty"))
|
self.assertTrue(User.objects.get(username="joe").check_password("not qwerty"))
|
||||||
|
|
||||||
def test_that_max_tries_exits_1(self):
|
def test_that_max_tries_exits_1(self):
|
||||||
"""
|
"""
|
||||||
|
@ -168,7 +168,7 @@ class ChangepasswordManagementCommandTestCase(TestCase):
|
||||||
non-ASCII characters from the User object representation.
|
non-ASCII characters from the User object representation.
|
||||||
"""
|
"""
|
||||||
# 'Julia' with accented 'u':
|
# 'Julia' with accented 'u':
|
||||||
models.User.objects.create_user(username='J\xfalia', password='qwerty')
|
User.objects.create_user(username='J\xfalia', password='qwerty')
|
||||||
|
|
||||||
command = changepassword.Command()
|
command = changepassword.Command()
|
||||||
command._get_pass = lambda *args: 'not qwerty'
|
command._get_pass = lambda *args: 'not qwerty'
|
||||||
|
@ -639,14 +639,14 @@ class CustomUserModelValidationTestCase(SimpleTestCase):
|
||||||
class PermissionTestCase(TestCase):
|
class PermissionTestCase(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
self._original_permissions = models.Permission._meta.permissions[:]
|
self._original_permissions = Permission._meta.permissions[:]
|
||||||
self._original_default_permissions = models.Permission._meta.default_permissions
|
self._original_default_permissions = Permission._meta.default_permissions
|
||||||
self._original_verbose_name = models.Permission._meta.verbose_name
|
self._original_verbose_name = Permission._meta.verbose_name
|
||||||
|
|
||||||
def tearDown(self):
|
def tearDown(self):
|
||||||
models.Permission._meta.permissions = self._original_permissions
|
Permission._meta.permissions = self._original_permissions
|
||||||
models.Permission._meta.default_permissions = self._original_default_permissions
|
Permission._meta.default_permissions = self._original_default_permissions
|
||||||
models.Permission._meta.verbose_name = self._original_verbose_name
|
Permission._meta.verbose_name = self._original_verbose_name
|
||||||
ContentType.objects.clear_cache()
|
ContentType.objects.clear_cache()
|
||||||
|
|
||||||
def test_duplicated_permissions(self):
|
def test_duplicated_permissions(self):
|
||||||
|
@ -657,7 +657,7 @@ class PermissionTestCase(TestCase):
|
||||||
auth_app_config = apps.get_app_config('auth')
|
auth_app_config = apps.get_app_config('auth')
|
||||||
|
|
||||||
# check duplicated default permission
|
# check duplicated default permission
|
||||||
models.Permission._meta.permissions = [
|
Permission._meta.permissions = [
|
||||||
('change_permission', 'Can edit permission (duplicate)')]
|
('change_permission', 'Can edit permission (duplicate)')]
|
||||||
six.assertRaisesRegex(self, CommandError,
|
six.assertRaisesRegex(self, CommandError,
|
||||||
"The permission codename 'change_permission' clashes with a "
|
"The permission codename 'change_permission' clashes with a "
|
||||||
|
@ -665,7 +665,7 @@ class PermissionTestCase(TestCase):
|
||||||
create_permissions, auth_app_config, verbosity=0)
|
create_permissions, auth_app_config, verbosity=0)
|
||||||
|
|
||||||
# check duplicated custom permissions
|
# check duplicated custom permissions
|
||||||
models.Permission._meta.permissions = [
|
Permission._meta.permissions = [
|
||||||
('my_custom_permission', 'Some permission'),
|
('my_custom_permission', 'Some permission'),
|
||||||
('other_one', 'Some other permission'),
|
('other_one', 'Some other permission'),
|
||||||
('my_custom_permission', 'Some permission with duplicate permission code'),
|
('my_custom_permission', 'Some permission with duplicate permission code'),
|
||||||
|
@ -676,7 +676,7 @@ class PermissionTestCase(TestCase):
|
||||||
create_permissions, auth_app_config, verbosity=0)
|
create_permissions, auth_app_config, verbosity=0)
|
||||||
|
|
||||||
# should not raise anything
|
# should not raise anything
|
||||||
models.Permission._meta.permissions = [
|
Permission._meta.permissions = [
|
||||||
('my_custom_permission', 'Some permission'),
|
('my_custom_permission', 'Some permission'),
|
||||||
('other_one', 'Some other permission'),
|
('other_one', 'Some other permission'),
|
||||||
]
|
]
|
||||||
|
@ -686,22 +686,22 @@ class PermissionTestCase(TestCase):
|
||||||
auth_app_config = apps.get_app_config('auth')
|
auth_app_config = apps.get_app_config('auth')
|
||||||
|
|
||||||
permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission')
|
permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission')
|
||||||
models.Permission._meta.permissions = [
|
Permission._meta.permissions = [
|
||||||
('my_custom_permission', 'Some permission'),
|
('my_custom_permission', 'Some permission'),
|
||||||
]
|
]
|
||||||
create_permissions(auth_app_config, verbosity=0)
|
create_permissions(auth_app_config, verbosity=0)
|
||||||
|
|
||||||
# add/change/delete permission by default + custom permission
|
# add/change/delete permission by default + custom permission
|
||||||
self.assertEqual(models.Permission.objects.filter(
|
self.assertEqual(Permission.objects.filter(
|
||||||
content_type=permission_content_type,
|
content_type=permission_content_type,
|
||||||
).count(), 4)
|
).count(), 4)
|
||||||
|
|
||||||
models.Permission.objects.filter(content_type=permission_content_type).delete()
|
Permission.objects.filter(content_type=permission_content_type).delete()
|
||||||
models.Permission._meta.default_permissions = []
|
Permission._meta.default_permissions = []
|
||||||
create_permissions(auth_app_config, verbosity=0)
|
create_permissions(auth_app_config, verbosity=0)
|
||||||
|
|
||||||
# custom permission only since default permissions is empty
|
# custom permission only since default permissions is empty
|
||||||
self.assertEqual(models.Permission.objects.filter(
|
self.assertEqual(Permission.objects.filter(
|
||||||
content_type=permission_content_type,
|
content_type=permission_content_type,
|
||||||
).count(), 1)
|
).count(), 1)
|
||||||
|
|
||||||
|
@ -709,8 +709,8 @@ class PermissionTestCase(TestCase):
|
||||||
auth_app_config = apps.get_app_config('auth')
|
auth_app_config = apps.get_app_config('auth')
|
||||||
|
|
||||||
permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission')
|
permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission')
|
||||||
models.Permission.objects.filter(content_type=permission_content_type).delete()
|
Permission.objects.filter(content_type=permission_content_type).delete()
|
||||||
models.Permission._meta.verbose_name = "some ridiculously long verbose name that is out of control" * 5
|
Permission._meta.verbose_name = "some ridiculously long verbose name that is out of control" * 5
|
||||||
|
|
||||||
six.assertRaisesRegex(self, exceptions.ValidationError,
|
six.assertRaisesRegex(self, exceptions.ValidationError,
|
||||||
"The verbose_name of auth.permission is longer than 244 characters",
|
"The verbose_name of auth.permission is longer than 244 characters",
|
||||||
|
@ -721,7 +721,7 @@ class PermissionTestCase(TestCase):
|
||||||
|
|
||||||
ContentType.objects.get_by_natural_key('auth', 'permission')
|
ContentType.objects.get_by_natural_key('auth', 'permission')
|
||||||
custom_perm_name = 'a' * 256
|
custom_perm_name = 'a' * 256
|
||||||
models.Permission._meta.permissions = [
|
Permission._meta.permissions = [
|
||||||
('my_custom_permission', custom_perm_name),
|
('my_custom_permission', custom_perm_name),
|
||||||
]
|
]
|
||||||
try:
|
try:
|
||||||
|
@ -732,4 +732,4 @@ class PermissionTestCase(TestCase):
|
||||||
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
||||||
create_permissions(auth_app_config, verbosity=0)
|
create_permissions(auth_app_config, verbosity=0)
|
||||||
finally:
|
finally:
|
||||||
models.Permission._meta.permissions = []
|
Permission._meta.permissions = []
|
||||||
|
|
Loading…
Reference in New Issue