mirror of
https://github.com/django/django.git
synced 2025-08-04 19:08:28 +00:00
Fixed #26470 -- Converted auth permission validation to system checks.
Thanks Tim for the review.
This commit is contained in:
parent
fc34be896d
commit
a872194802
6 changed files with 170 additions and 116 deletions
|
@ -1,6 +1,8 @@
|
|||
from __future__ import unicode_literals
|
||||
|
||||
from django.contrib.auth.checks import check_user_model
|
||||
from django.contrib.auth.checks import (
|
||||
check_models_permissions, check_user_model,
|
||||
)
|
||||
from django.contrib.auth.models import AbstractBaseUser
|
||||
from django.core import checks
|
||||
from django.db import models
|
||||
|
@ -80,3 +82,83 @@ class UserModelChecksTests(SimpleTestCase):
|
|||
id='auth.W004',
|
||||
),
|
||||
])
|
||||
|
||||
|
||||
@isolate_apps('auth_tests', attr_name='apps')
|
||||
@override_system_checks([check_models_permissions])
|
||||
class ModelsPermissionsChecksTests(SimpleTestCase):
|
||||
def test_clashing_default_permissions(self):
|
||||
class Checked(models.Model):
|
||||
class Meta:
|
||||
permissions = [
|
||||
('change_checked', 'Can edit permission (duplicate)')
|
||||
]
|
||||
errors = checks.run_checks(self.apps.get_app_configs())
|
||||
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',
|
||||
),
|
||||
])
|
||||
|
||||
def test_non_clashing_custom_permissions(self):
|
||||
class Checked(models.Model):
|
||||
class Meta:
|
||||
permissions = [
|
||||
('my_custom_permission', 'Some permission'),
|
||||
('other_one', 'Some other permission'),
|
||||
]
|
||||
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 = [
|
||||
('my_custom_permission', 'Some permission'),
|
||||
('other_one', 'Some other permission'),
|
||||
('my_custom_permission', 'Some permission with duplicate permission code'),
|
||||
]
|
||||
errors = checks.run_checks(self.apps.get_app_configs())
|
||||
self.assertEqual(errors, [
|
||||
checks.Error(
|
||||
"The permission codenamed 'my_custom_permission' is duplicated for "
|
||||
"model 'auth_tests.Checked'.",
|
||||
obj=Checked,
|
||||
id='auth.E006',
|
||||
),
|
||||
])
|
||||
|
||||
def test_verbose_name_max_length(self):
|
||||
class Checked(models.Model):
|
||||
class Meta:
|
||||
verbose_name = 'some ridiculously long verbose name that is out of control' * 5
|
||||
errors = checks.run_checks(self.apps.get_app_configs())
|
||||
self.assertEqual(errors, [
|
||||
checks.Error(
|
||||
"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.",
|
||||
obj=Checked,
|
||||
id='auth.E007',
|
||||
),
|
||||
])
|
||||
|
||||
def test_custom_permission_name_max_length(self):
|
||||
custom_permission_name = 'some ridiculously long verbose name that is out of control' * 5
|
||||
|
||||
class Checked(models.Model):
|
||||
class Meta:
|
||||
permissions = [
|
||||
('my_custom_permission', custom_permission_name),
|
||||
]
|
||||
errors = checks.run_checks(self.apps.get_app_configs())
|
||||
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',
|
||||
),
|
||||
])
|
||||
|
|
|
@ -11,7 +11,6 @@ from django.contrib.auth.management.commands import (
|
|||
)
|
||||
from django.contrib.auth.models import Group, Permission, User
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
from django.core import exceptions
|
||||
from django.core.management import call_command
|
||||
from django.core.management.base import CommandError
|
||||
from django.test import TestCase, mock, override_settings
|
||||
|
@ -567,48 +566,12 @@ class PermissionTestCase(TestCase):
|
|||
def setUp(self):
|
||||
self._original_permissions = Permission._meta.permissions[:]
|
||||
self._original_default_permissions = Permission._meta.default_permissions
|
||||
self._original_verbose_name = Permission._meta.verbose_name
|
||||
|
||||
def tearDown(self):
|
||||
Permission._meta.permissions = self._original_permissions
|
||||
Permission._meta.default_permissions = self._original_default_permissions
|
||||
Permission._meta.verbose_name = self._original_verbose_name
|
||||
ContentType.objects.clear_cache()
|
||||
|
||||
def test_duplicated_permissions(self):
|
||||
"""
|
||||
Test that we show proper error message if we are trying to create
|
||||
duplicate permissions.
|
||||
"""
|
||||
auth_app_config = apps.get_app_config('auth')
|
||||
|
||||
# check duplicated default permission
|
||||
Permission._meta.permissions = [
|
||||
('change_permission', 'Can edit permission (duplicate)')]
|
||||
msg = (
|
||||
"The permission codename 'change_permission' clashes with a "
|
||||
"builtin permission for model 'auth.Permission'."
|
||||
)
|
||||
with self.assertRaisesMessage(CommandError, msg):
|
||||
create_permissions(auth_app_config, verbosity=0)
|
||||
|
||||
# check duplicated custom permissions
|
||||
Permission._meta.permissions = [
|
||||
('my_custom_permission', 'Some permission'),
|
||||
('other_one', 'Some other permission'),
|
||||
('my_custom_permission', 'Some permission with duplicate permission code'),
|
||||
]
|
||||
msg = "The permission codename 'my_custom_permission' is duplicated for model 'auth.Permission'."
|
||||
with self.assertRaisesMessage(CommandError, msg):
|
||||
create_permissions(auth_app_config, verbosity=0)
|
||||
|
||||
# should not raise anything
|
||||
Permission._meta.permissions = [
|
||||
('my_custom_permission', 'Some permission'),
|
||||
('other_one', 'Some other permission'),
|
||||
]
|
||||
create_permissions(auth_app_config, verbosity=0)
|
||||
|
||||
def test_default_permissions(self):
|
||||
auth_app_config = apps.get_app_config('auth')
|
||||
|
||||
|
@ -631,32 +594,3 @@ class PermissionTestCase(TestCase):
|
|||
self.assertEqual(Permission.objects.filter(
|
||||
content_type=permission_content_type,
|
||||
).count(), 1)
|
||||
|
||||
def test_verbose_name_length(self):
|
||||
auth_app_config = apps.get_app_config('auth')
|
||||
|
||||
permission_content_type = ContentType.objects.get_by_natural_key('auth', 'permission')
|
||||
Permission.objects.filter(content_type=permission_content_type).delete()
|
||||
Permission._meta.verbose_name = "some ridiculously long verbose name that is out of control" * 5
|
||||
|
||||
msg = "The verbose_name of auth.permission is longer than 244 characters"
|
||||
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
||||
create_permissions(auth_app_config, verbosity=0)
|
||||
|
||||
def test_custom_permission_name_length(self):
|
||||
auth_app_config = apps.get_app_config('auth')
|
||||
|
||||
ContentType.objects.get_by_natural_key('auth', 'permission')
|
||||
custom_perm_name = 'a' * 256
|
||||
Permission._meta.permissions = [
|
||||
('my_custom_permission', custom_perm_name),
|
||||
]
|
||||
try:
|
||||
msg = (
|
||||
"The permission name %s of auth.permission is longer than "
|
||||
"255 characters" % custom_perm_name
|
||||
)
|
||||
with self.assertRaisesMessage(exceptions.ValidationError, msg):
|
||||
create_permissions(auth_app_config, verbosity=0)
|
||||
finally:
|
||||
Permission._meta.permissions = []
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue