Extracted auth checks tests into their own module.
This commit is contained in:
parent
7d6e6e8367
commit
fc34be896d
|
@ -0,0 +1,82 @@
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.contrib.auth.checks import check_user_model
|
||||||
|
from django.contrib.auth.models import AbstractBaseUser
|
||||||
|
from django.core import checks
|
||||||
|
from django.db import models
|
||||||
|
from django.test import (
|
||||||
|
SimpleTestCase, override_settings, override_system_checks,
|
||||||
|
)
|
||||||
|
from django.test.utils import isolate_apps
|
||||||
|
|
||||||
|
from .models import CustomUserNonUniqueUsername
|
||||||
|
|
||||||
|
|
||||||
|
@isolate_apps('auth_tests', attr_name='apps')
|
||||||
|
@override_system_checks([check_user_model])
|
||||||
|
class UserModelChecksTests(SimpleTestCase):
|
||||||
|
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonListRequiredFields')
|
||||||
|
def test_required_fields_is_list(self):
|
||||||
|
"""REQUIRED_FIELDS should be a list."""
|
||||||
|
class CustomUserNonListRequiredFields(AbstractBaseUser):
|
||||||
|
username = models.CharField(max_length=30, unique=True)
|
||||||
|
date_of_birth = models.DateField()
|
||||||
|
|
||||||
|
USERNAME_FIELD = 'username'
|
||||||
|
REQUIRED_FIELDS = 'date_of_birth'
|
||||||
|
|
||||||
|
errors = checks.run_checks(app_configs=self.apps.get_app_configs())
|
||||||
|
self.assertEqual(errors, [
|
||||||
|
checks.Error(
|
||||||
|
"'REQUIRED_FIELDS' must be a list or tuple.",
|
||||||
|
obj=CustomUserNonListRequiredFields,
|
||||||
|
id='auth.E001',
|
||||||
|
),
|
||||||
|
])
|
||||||
|
|
||||||
|
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserBadRequiredFields')
|
||||||
|
def test_username_not_in_required_fields(self):
|
||||||
|
"""USERNAME_FIELD should not appear in REQUIRED_FIELDS."""
|
||||||
|
class CustomUserBadRequiredFields(AbstractBaseUser):
|
||||||
|
username = models.CharField(max_length=30, unique=True)
|
||||||
|
date_of_birth = models.DateField()
|
||||||
|
|
||||||
|
USERNAME_FIELD = 'username'
|
||||||
|
REQUIRED_FIELDS = ['username', 'date_of_birth']
|
||||||
|
|
||||||
|
errors = checks.run_checks(self.apps.get_app_configs())
|
||||||
|
self.assertEqual(errors, [
|
||||||
|
checks.Error(
|
||||||
|
"The field named as the 'USERNAME_FIELD' for a custom user model "
|
||||||
|
"must not be included in 'REQUIRED_FIELDS'.",
|
||||||
|
obj=CustomUserBadRequiredFields,
|
||||||
|
id='auth.E002',
|
||||||
|
),
|
||||||
|
])
|
||||||
|
|
||||||
|
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonUniqueUsername')
|
||||||
|
def test_username_non_unique(self):
|
||||||
|
"""
|
||||||
|
A non-unique USERNAME_FIELD should raise an error only if we use the
|
||||||
|
default authentication backend. Otherwise, an warning should be raised.
|
||||||
|
"""
|
||||||
|
errors = checks.run_checks()
|
||||||
|
self.assertEqual(errors, [
|
||||||
|
checks.Error(
|
||||||
|
"'CustomUserNonUniqueUsername.username' must be "
|
||||||
|
"unique because it is named as the 'USERNAME_FIELD'.",
|
||||||
|
obj=CustomUserNonUniqueUsername,
|
||||||
|
id='auth.E003',
|
||||||
|
),
|
||||||
|
])
|
||||||
|
with self.settings(AUTHENTICATION_BACKENDS=['my.custom.backend']):
|
||||||
|
errors = checks.run_checks()
|
||||||
|
self.assertEqual(errors, [
|
||||||
|
checks.Warning(
|
||||||
|
"'CustomUserNonUniqueUsername.username' is named as "
|
||||||
|
"the 'USERNAME_FIELD', but it is not unique.",
|
||||||
|
hint='Ensure that your authentication backend(s) can handle non-unique usernames.',
|
||||||
|
obj=CustomUserNonUniqueUsername,
|
||||||
|
id='auth.W004',
|
||||||
|
),
|
||||||
|
])
|
|
@ -5,23 +5,16 @@ from datetime import date
|
||||||
|
|
||||||
from django.apps import apps
|
from django.apps import apps
|
||||||
from django.contrib.auth import management
|
from django.contrib.auth import management
|
||||||
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 (
|
from django.contrib.auth.models import Group, Permission, User
|
||||||
AbstractBaseUser, Group, Permission, User,
|
|
||||||
)
|
|
||||||
from django.contrib.contenttypes.models import ContentType
|
from django.contrib.contenttypes.models import ContentType
|
||||||
from django.core import checks, exceptions
|
from django.core import exceptions
|
||||||
from django.core.management import call_command
|
from django.core.management import call_command
|
||||||
from django.core.management.base import CommandError
|
from django.core.management.base import CommandError
|
||||||
from django.db import models
|
from django.test import TestCase, mock, override_settings
|
||||||
from django.test import (
|
|
||||||
SimpleTestCase, TestCase, mock, override_settings, override_system_checks,
|
|
||||||
)
|
|
||||||
from django.test.utils import isolate_apps
|
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
from django.utils.encoding import force_str
|
from django.utils.encoding import force_str
|
||||||
from django.utils.translation import ugettext_lazy as _
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
@ -569,83 +562,6 @@ class MultiDBCreatesuperuserTestCase(TestCase):
|
||||||
self.assertEqual(user.email, 'joe@somewhere.org')
|
self.assertEqual(user.email, 'joe@somewhere.org')
|
||||||
|
|
||||||
|
|
||||||
class CustomUserModelValidationTestCase(SimpleTestCase):
|
|
||||||
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonListRequiredFields')
|
|
||||||
@override_system_checks([check_user_model])
|
|
||||||
@isolate_apps('auth_tests', kwarg_name='apps')
|
|
||||||
def test_required_fields_is_list(self, apps):
|
|
||||||
"""REQUIRED_FIELDS should be a list."""
|
|
||||||
class CustomUserNonListRequiredFields(AbstractBaseUser):
|
|
||||||
username = models.CharField(max_length=30, unique=True)
|
|
||||||
date_of_birth = models.DateField()
|
|
||||||
|
|
||||||
USERNAME_FIELD = 'username'
|
|
||||||
REQUIRED_FIELDS = 'date_of_birth'
|
|
||||||
|
|
||||||
errors = checks.run_checks(app_configs=apps.get_app_configs())
|
|
||||||
expected = [
|
|
||||||
checks.Error(
|
|
||||||
"'REQUIRED_FIELDS' must be a list or tuple.",
|
|
||||||
obj=CustomUserNonListRequiredFields,
|
|
||||||
id='auth.E001',
|
|
||||||
),
|
|
||||||
]
|
|
||||||
self.assertEqual(errors, expected)
|
|
||||||
|
|
||||||
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserBadRequiredFields')
|
|
||||||
@override_system_checks([check_user_model])
|
|
||||||
@isolate_apps('auth_tests', kwarg_name='apps')
|
|
||||||
def test_username_not_in_required_fields(self, apps):
|
|
||||||
"""USERNAME_FIELD should not appear in REQUIRED_FIELDS."""
|
|
||||||
class CustomUserBadRequiredFields(AbstractBaseUser):
|
|
||||||
username = models.CharField(max_length=30, unique=True)
|
|
||||||
date_of_birth = models.DateField()
|
|
||||||
|
|
||||||
USERNAME_FIELD = 'username'
|
|
||||||
REQUIRED_FIELDS = ['username', 'date_of_birth']
|
|
||||||
|
|
||||||
errors = checks.run_checks(apps.get_app_configs())
|
|
||||||
expected = [
|
|
||||||
checks.Error(
|
|
||||||
"The field named as the 'USERNAME_FIELD' for a custom user model "
|
|
||||||
"must not be included in 'REQUIRED_FIELDS'.",
|
|
||||||
obj=CustomUserBadRequiredFields,
|
|
||||||
id='auth.E002',
|
|
||||||
),
|
|
||||||
]
|
|
||||||
self.assertEqual(errors, expected)
|
|
||||||
|
|
||||||
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserNonUniqueUsername')
|
|
||||||
@override_system_checks([check_user_model])
|
|
||||||
def test_username_non_unique(self):
|
|
||||||
"""
|
|
||||||
A non-unique USERNAME_FIELD should raise an error only if we use the
|
|
||||||
default authentication backend. Otherwise, an warning should be raised.
|
|
||||||
"""
|
|
||||||
errors = checks.run_checks()
|
|
||||||
expected = [
|
|
||||||
checks.Error(
|
|
||||||
"'CustomUserNonUniqueUsername.username' must be "
|
|
||||||
"unique because it is named as the 'USERNAME_FIELD'.",
|
|
||||||
obj=CustomUserNonUniqueUsername,
|
|
||||||
id='auth.E003',
|
|
||||||
),
|
|
||||||
]
|
|
||||||
self.assertEqual(errors, expected)
|
|
||||||
with self.settings(AUTHENTICATION_BACKENDS=['my.custom.backend']):
|
|
||||||
errors = checks.run_checks()
|
|
||||||
expected = [
|
|
||||||
checks.Warning(
|
|
||||||
"'CustomUserNonUniqueUsername.username' is named as "
|
|
||||||
"the 'USERNAME_FIELD', but it is not unique.",
|
|
||||||
hint='Ensure that your authentication backend(s) can handle non-unique usernames.',
|
|
||||||
obj=CustomUserNonUniqueUsername,
|
|
||||||
id='auth.W004',
|
|
||||||
)
|
|
||||||
]
|
|
||||||
self.assertEqual(errors, expected)
|
|
||||||
|
|
||||||
|
|
||||||
class PermissionTestCase(TestCase):
|
class PermissionTestCase(TestCase):
|
||||||
|
|
||||||
def setUp(self):
|
def setUp(self):
|
||||||
|
|
Loading…
Reference in New Issue