diff options
| author | Simon Charette <charette.s@gmail.com> | 2016-04-05 22:58:22 -0400 |
|---|---|---|
| committer | Simon Charette <charette.s@gmail.com> | 2016-04-06 22:40:43 -0400 |
| commit | a872194802c2bacb067c2b9c9cb76361e2071c0f (patch) | |
| tree | d98ce05eee98567dde63e672092daa55d0dc09ad /tests/auth_tests/test_checks.py | |
| parent | fc34be896d89374991ff7ef5a5ff205536f5143b (diff) | |
Fixed #26470 -- Converted auth permission validation to system checks.
Thanks Tim for the review.
Diffstat (limited to 'tests/auth_tests/test_checks.py')
| -rw-r--r-- | tests/auth_tests/test_checks.py | 84 |
1 files changed, 83 insertions, 1 deletions
diff --git a/tests/auth_tests/test_checks.py b/tests/auth_tests/test_checks.py index 392e72e62c..3ff78d9851 100644 --- a/tests/auth_tests/test_checks.py +++ b/tests/auth_tests/test_checks.py @@ -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', + ), + ]) |
