Ticket #19822: 19822-1.diff

File 19822-1.diff, 2.7 KB (added by claudep, 2 years ago)
  • django/contrib/auth/tests/custom_user.py

    diff --git a/django/contrib/auth/tests/custom_user.py b/django/contrib/auth/tests/custom_user.py
    index 8cc57d4..e4ac8bc 100644
    a b class IsActiveTestUser1(AbstractBaseUser): 
    144144        app_label = 'auth'
    145145
    146146    # the is_active attr is provided by AbstractBaseUser
     147
     148
     149class CustomUserNonUniqueUsername(AbstractBaseUser):
     150    username =models.CharField(max_length=30)
     151
     152    USERNAME_FIELD = 'username'
     153
     154    class Meta:
     155        app_label = 'auth'
  • django/contrib/auth/tests/management.py

    diff --git a/django/contrib/auth/tests/management.py b/django/contrib/auth/tests/management.py
    index 42f14d6..67708fc 100644
    a b from django.contrib.auth.tests import CustomUser 
    99from django.contrib.auth.tests.utils import skipIfCustomUser
    1010from django.core.management import call_command
    1111from django.core.management.base import CommandError
     12from django.db.models.loading import get_app
    1213from django.test import TestCase
    1314from django.test.utils import override_settings
    1415from django.utils import six
    class CreatesuperuserManagementCommandTestCase(TestCase): 
    170171        self.assertEqual(CustomUser._default_manager.count(), 0)
    171172
    172173
     174class CustomUserModelValidationTestCase(TestCase):
     175    @override_settings(AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername')
     176    def test_username_non_unique(self):
     177        """
     178        A non-unique USERNAME_FIELD should raise a model validation error.
     179        """
     180        from django.core.management.validation import get_validation_errors
     181        new_io = StringIO()
     182        get_validation_errors(new_io, get_app('auth'))
     183        self.assertIn("The USERNAME_FIELD must be indexed as unique", new_io.getvalue())
     184
    173185class PermissionDuplicationTestCase(TestCase):
    174186
    175187    def setUp(self):
  • django/core/management/validation.py

    diff --git a/django/core/management/validation.py b/django/core/management/validation.py
    index f49a3c2..ab47f3b 100644
    a b def get_validation_errors(outfile, app=None): 
    5454            # Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS.
    5555            if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS:
    5656                e.add(opts, 'The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.')
     57            if not opts.get_field(cls.USERNAME_FIELD).unique:
     58                e.add(opts, 'The USERNAME_FIELD must be indexed as unique (unique=True in the field parameters).')
    5759
    5860        # Model isn't swapped; do field-specific validation.
    5961        for f in opts.local_fields:
Back to Top