Commit 69373f34 authored by Chris Streeter's avatar Chris Streeter Committed by Tim Graham
Browse files

Fixed #19925 - Added validation for REQUIRED_FIELDS being a list

Thanks Roman Alexander for the suggestion.
parent 357d62d9
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -550,6 +550,7 @@ answer newbie questions, and generally made Django that much better:
    Thomas Steinacher <http://www.eggdrop.ch/>
    Emil Stenström <em@kth.se>
    Johan C. Stöver <johan@nilling.nl>
    Chris Streeter <chris@chrisstreeter.com>
    Nowell Strite <http://nowell.strite.org/>
    Thomas Stromberg <tstromberg@google.com>
    Hannes Struß <x@hannesstruss.de>
+12 −0
Original line number Diff line number Diff line
@@ -156,6 +156,18 @@ class CustomUserNonUniqueUsername(AbstractBaseUser):
        app_label = 'auth'


class CustomUserNonListRequiredFields(AbstractBaseUser):
    "A user with a non-list REQUIRED_FIELDS"
    username = models.CharField(max_length=30, unique=True)
    date_of_birth = models.DateField()

    USERNAME_FIELD = 'username'
    REQUIRED_FIELDS = 'date_of_birth'

    class Meta:
        app_label = 'auth'


class CustomUserBadRequiredFields(AbstractBaseUser):
    "A user with a non-unique username"
    username = models.CharField(max_length=30, unique=True)
+7 −0
Original line number Diff line number Diff line
@@ -174,6 +174,13 @@ class CreatesuperuserManagementCommandTestCase(TestCase):


class CustomUserModelValidationTestCase(TestCase):
    @override_settings(AUTH_USER_MODEL='auth.CustomUserNonListRequiredFields')
    def test_required_fields_is_list(self):
        "REQUIRED_FIELDS should be a list."
        new_io = StringIO()
        get_validation_errors(new_io, get_app('auth'))
        self.assertIn("The REQUIRED_FIELDS must be a list or tuple.", new_io.getvalue())

    @override_settings(AUTH_USER_MODEL='auth.CustomUserBadRequiredFields')
    def test_username_not_in_required_fields(self):
        "USERNAME_FIELD should not appear in REQUIRED_FIELDS."
+4 −0
Original line number Diff line number Diff line
@@ -51,6 +51,10 @@ def get_validation_errors(outfile, app=None):

        # If this is the current User model, check known validation problems with User models
        if settings.AUTH_USER_MODEL == '%s.%s' % (opts.app_label, opts.object_name):
            # Check that REQUIRED_FIELDS is a list
            if not isinstance(cls.REQUIRED_FIELDS, (list, tuple)):
                e.add(opts, 'The REQUIRED_FIELDS must be a list or tuple.')

            # Check that the USERNAME FIELD isn't included in REQUIRED_FIELDS.
            if cls.USERNAME_FIELD in cls.REQUIRED_FIELDS:
                e.add(opts, 'The field named as the USERNAME_FIELD should not be included in REQUIRED_FIELDS on a swappable User model.')