2022-02-03 19:24:19 +00:00
|
|
|
from django.contrib.auth.checks import check_models_permissions, check_user_model
|
2016-04-05 22:39:59 +00:00
|
|
|
from django.contrib.auth.models import AbstractBaseUser
|
|
|
|
from django.core import checks
|
|
|
|
from django.db import models
|
2020-10-19 14:15:17 +00:00
|
|
|
from django.db.models import Q, UniqueConstraint
|
2022-02-03 19:24:19 +00:00
|
|
|
from django.test import SimpleTestCase, override_settings, override_system_checks
|
2016-04-05 22:39:59 +00:00
|
|
|
from django.test.utils import isolate_apps
|
|
|
|
|
|
|
|
from .models import CustomUserNonUniqueUsername
|
|
|
|
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@isolate_apps("auth_tests", attr_name="apps")
|
2016-04-05 22:39:59 +00:00
|
|
|
@override_system_checks([check_user_model])
|
|
|
|
class UserModelChecksTests(SimpleTestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserNonListRequiredFields")
|
2016-04-05 22:39:59 +00:00
|
|
|
def test_required_fields_is_list(self):
|
|
|
|
"""REQUIRED_FIELDS should be a list."""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-04-05 22:39:59 +00:00
|
|
|
class CustomUserNonListRequiredFields(AbstractBaseUser):
|
|
|
|
username = models.CharField(max_length=30, unique=True)
|
|
|
|
date_of_birth = models.DateField()
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
USERNAME_FIELD = "username"
|
|
|
|
REQUIRED_FIELDS = "date_of_birth"
|
2016-04-05 22:39:59 +00:00
|
|
|
|
|
|
|
errors = checks.run_checks(app_configs=self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
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")
|
2016-04-05 22:39:59 +00:00
|
|
|
def test_username_not_in_required_fields(self):
|
|
|
|
"""USERNAME_FIELD should not appear in REQUIRED_FIELDS."""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-04-05 22:39:59 +00:00
|
|
|
class CustomUserBadRequiredFields(AbstractBaseUser):
|
|
|
|
username = models.CharField(max_length=30, unique=True)
|
|
|
|
date_of_birth = models.DateField()
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
USERNAME_FIELD = "username"
|
|
|
|
REQUIRED_FIELDS = ["username", "date_of_birth"]
|
2016-04-05 22:39:59 +00:00
|
|
|
|
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"The field named as the 'USERNAME_FIELD' for a custom user model "
|
|
|
|
"must not be included in 'REQUIRED_FIELDS'.",
|
|
|
|
hint=(
|
|
|
|
"The 'USERNAME_FIELD' is currently set to 'username', you "
|
|
|
|
"should remove 'username' from the 'REQUIRED_FIELDS'."
|
|
|
|
),
|
|
|
|
obj=CustomUserBadRequiredFields,
|
|
|
|
id="auth.E002",
|
2020-02-20 18:16:59 +00:00
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
|
|
|
)
|
2016-04-05 22:39:59 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserNonUniqueUsername")
|
2016-04-05 22:39:59 +00:00
|
|
|
def test_username_non_unique(self):
|
|
|
|
"""
|
2017-11-07 03:11:39 +00:00
|
|
|
A non-unique USERNAME_FIELD raises an error only if the default
|
|
|
|
authentication backend is used. Otherwise, a warning is raised.
|
2016-04-05 22:39:59 +00:00
|
|
|
"""
|
|
|
|
errors = checks.run_checks()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"'CustomUserNonUniqueUsername.username' must be "
|
|
|
|
"unique because it is named as the 'USERNAME_FIELD'.",
|
2016-04-05 22:39:59 +00:00
|
|
|
obj=CustomUserNonUniqueUsername,
|
2022-02-03 19:24:19 +00:00
|
|
|
id="auth.E003",
|
2016-04-05 22:39:59 +00:00
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
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.",
|
2022-02-04 07:08:27 +00:00
|
|
|
hint=(
|
|
|
|
"Ensure that your authentication backend(s) can handle "
|
|
|
|
"non-unique usernames."
|
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
obj=CustomUserNonUniqueUsername,
|
|
|
|
id="auth.W004",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2016-04-06 02:58:22 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserPartiallyUnique")
|
2020-10-19 14:15:17 +00:00
|
|
|
def test_username_partially_unique(self):
|
|
|
|
class CustomUserPartiallyUnique(AbstractBaseUser):
|
|
|
|
username = models.CharField(max_length=30)
|
2022-02-03 19:24:19 +00:00
|
|
|
USERNAME_FIELD = "username"
|
2020-10-19 14:15:17 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
|
|
|
UniqueConstraint(
|
2022-02-03 19:24:19 +00:00
|
|
|
fields=["username"],
|
|
|
|
name="partial_username_unique",
|
2020-10-19 14:15:17 +00:00
|
|
|
condition=Q(password__isnull=False),
|
|
|
|
),
|
|
|
|
]
|
|
|
|
|
|
|
|
errors = checks.run_checks(app_configs=self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"'CustomUserPartiallyUnique.username' must be unique because "
|
|
|
|
"it is named as the 'USERNAME_FIELD'.",
|
2020-10-19 14:15:17 +00:00
|
|
|
obj=CustomUserPartiallyUnique,
|
2022-02-03 19:24:19 +00:00
|
|
|
id="auth.E003",
|
2020-10-19 14:15:17 +00:00
|
|
|
),
|
2022-02-03 19:24:19 +00:00
|
|
|
],
|
|
|
|
)
|
|
|
|
with self.settings(AUTHENTICATION_BACKENDS=["my.custom.backend"]):
|
|
|
|
errors = checks.run_checks(app_configs=self.apps.get_app_configs())
|
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Warning(
|
|
|
|
"'CustomUserPartiallyUnique.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=CustomUserPartiallyUnique,
|
|
|
|
id="auth.W004",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-10-19 14:15:17 +00:00
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(AUTH_USER_MODEL="auth_tests.CustomUserUniqueConstraint")
|
2020-10-19 14:15:17 +00:00
|
|
|
def test_username_unique_with_model_constraint(self):
|
|
|
|
class CustomUserUniqueConstraint(AbstractBaseUser):
|
|
|
|
username = models.CharField(max_length=30)
|
2022-02-03 19:24:19 +00:00
|
|
|
USERNAME_FIELD = "username"
|
2020-10-19 14:15:17 +00:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
constraints = [
|
2022-02-03 19:24:19 +00:00
|
|
|
UniqueConstraint(fields=["username"], name="username_unique"),
|
2020-10-19 14:15:17 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
self.assertEqual(checks.run_checks(app_configs=self.apps.get_app_configs()), [])
|
2022-02-03 19:24:19 +00:00
|
|
|
with self.settings(AUTHENTICATION_BACKENDS=["my.custom.backend"]):
|
2020-10-19 14:15:17 +00:00
|
|
|
errors = checks.run_checks(app_configs=self.apps.get_app_configs())
|
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
@override_settings(AUTH_USER_MODEL="auth_tests.BadUser")
|
2016-05-05 16:42:19 +00:00
|
|
|
def test_is_anonymous_authenticated_methods(self):
|
|
|
|
"""
|
|
|
|
<User Model>.is_anonymous/is_authenticated must not be methods.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-05-05 16:42:19 +00:00
|
|
|
class BadUser(AbstractBaseUser):
|
|
|
|
username = models.CharField(max_length=30, unique=True)
|
2022-02-03 19:24:19 +00:00
|
|
|
USERNAME_FIELD = "username"
|
2016-05-05 16:42:19 +00:00
|
|
|
|
|
|
|
def is_anonymous(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
def is_authenticated(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
errors = checks.run_checks(app_configs=self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Critical(
|
|
|
|
"%s.is_anonymous must be an attribute or property rather than "
|
|
|
|
"a method. Ignoring this is a security issue as anonymous "
|
|
|
|
"users will be treated as authenticated!" % BadUser,
|
|
|
|
obj=BadUser,
|
|
|
|
id="auth.C009",
|
|
|
|
),
|
|
|
|
checks.Critical(
|
|
|
|
"%s.is_authenticated must be an attribute or property rather "
|
|
|
|
"than a method. Ignoring this is a security issue as anonymous "
|
|
|
|
"users will be treated as authenticated!" % BadUser,
|
|
|
|
obj=BadUser,
|
|
|
|
id="auth.C010",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@isolate_apps("auth_tests", attr_name="apps")
|
2016-04-06 02:58:22 +00:00
|
|
|
@override_system_checks([check_models_permissions])
|
|
|
|
class ModelsPermissionsChecksTests(SimpleTestCase):
|
|
|
|
def test_clashing_default_permissions(self):
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
2022-02-03 19:24:19 +00:00
|
|
|
permissions = [("change_checked", "Can edit permission (duplicate)")]
|
|
|
|
|
2016-04-06 02:58:22 +00:00
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"The permission codenamed 'change_checked' clashes with a builtin "
|
|
|
|
"permission for model 'auth_tests.Checked'.",
|
|
|
|
obj=Checked,
|
|
|
|
id="auth.E005",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2016-04-06 02:58:22 +00:00
|
|
|
|
|
|
|
def test_non_clashing_custom_permissions(self):
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
|
|
|
permissions = [
|
2022-02-03 19:24:19 +00:00
|
|
|
("my_custom_permission", "Some permission"),
|
|
|
|
("other_one", "Some other permission"),
|
2016-04-06 02:58:22 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-04-06 02:58:22 +00:00
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
|
|
|
self.assertEqual(errors, [])
|
|
|
|
|
|
|
|
def test_clashing_custom_permissions(self):
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
|
|
|
permissions = [
|
2022-02-03 19:24:19 +00:00
|
|
|
("my_custom_permission", "Some permission"),
|
|
|
|
("other_one", "Some other permission"),
|
|
|
|
(
|
|
|
|
"my_custom_permission",
|
|
|
|
"Some permission with duplicate permission code",
|
|
|
|
),
|
2016-04-06 02:58:22 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-04-06 02:58:22 +00:00
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"The permission codenamed 'my_custom_permission' is duplicated for "
|
|
|
|
"model 'auth_tests.Checked'.",
|
|
|
|
obj=Checked,
|
|
|
|
id="auth.E006",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2016-04-06 02:58:22 +00:00
|
|
|
|
|
|
|
def test_verbose_name_max_length(self):
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
2022-02-03 19:24:19 +00:00
|
|
|
verbose_name = (
|
|
|
|
"some ridiculously long verbose name that is out of control" * 5
|
|
|
|
)
|
|
|
|
|
2016-04-06 02:58:22 +00:00
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
2022-02-04 07:08:27 +00:00
|
|
|
"The verbose_name of model 'auth_tests.Checked' must be at most "
|
|
|
|
"244 characters for its builtin permission names to be at most 255 "
|
|
|
|
"characters.",
|
2022-02-03 19:24:19 +00:00
|
|
|
obj=Checked,
|
|
|
|
id="auth.E007",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2016-04-06 02:58:22 +00:00
|
|
|
|
2020-01-23 02:14:24 +00:00
|
|
|
def test_model_name_max_length(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
model_name = "X" * 94
|
|
|
|
model = type(model_name, (models.Model,), {"__module__": self.__module__})
|
2020-01-23 02:14:24 +00:00
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"The name of model 'auth_tests.%s' must be at most 93 "
|
|
|
|
"characters for its builtin permission codenames to be at "
|
|
|
|
"most 100 characters." % model_name,
|
|
|
|
obj=model,
|
|
|
|
id="auth.E011",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-01-23 02:14:24 +00:00
|
|
|
|
2016-04-06 02:58:22 +00:00
|
|
|
def test_custom_permission_name_max_length(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
custom_permission_name = (
|
|
|
|
"some ridiculously long verbose name that is out of control" * 5
|
|
|
|
)
|
2016-04-06 02:58:22 +00:00
|
|
|
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
|
|
|
permissions = [
|
2022-02-03 19:24:19 +00:00
|
|
|
("my_custom_permission", custom_permission_name),
|
2016-04-06 02:58:22 +00:00
|
|
|
]
|
2022-02-03 19:24:19 +00:00
|
|
|
|
2016-04-06 02:58:22 +00:00
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"The permission named '%s' of model 'auth_tests.Checked' is longer "
|
|
|
|
"than 255 characters." % custom_permission_name,
|
|
|
|
obj=Checked,
|
|
|
|
id="auth.E008",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2016-08-02 19:19:01 +00:00
|
|
|
|
2020-01-23 02:14:24 +00:00
|
|
|
def test_custom_permission_codename_max_length(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
custom_permission_codename = "x" * 101
|
2020-01-23 02:14:24 +00:00
|
|
|
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
|
|
|
permissions = [
|
2022-02-03 19:24:19 +00:00
|
|
|
(custom_permission_codename, "Custom permission"),
|
2020-01-23 02:14:24 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
errors = checks.run_checks(self.apps.get_app_configs())
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
errors,
|
|
|
|
[
|
|
|
|
checks.Error(
|
|
|
|
"The permission codenamed '%s' of model 'auth_tests.Checked' "
|
|
|
|
"is longer than 100 characters." % custom_permission_codename,
|
|
|
|
obj=Checked,
|
|
|
|
id="auth.E012",
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2020-01-23 02:14:24 +00:00
|
|
|
|
2016-08-02 19:19:01 +00:00
|
|
|
def test_empty_default_permissions(self):
|
|
|
|
class Checked(models.Model):
|
|
|
|
class Meta:
|
|
|
|
default_permissions = ()
|
|
|
|
|
|
|
|
self.assertEqual(checks.run_checks(self.apps.get_app_configs()), [])
|