diff options
| author | Alasdair Nicol <alasdair@thenicols.net> | 2015-06-05 14:33:04 +0100 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2015-06-06 09:33:02 -0400 |
| commit | 1ea87c8c7974acb5fa795362253c61b38f3cb137 (patch) | |
| tree | 2192bdc6b85457eb4352563a1d9976f37c3a80cc /tests/auth_tests | |
| parent | a391b17ad24bc5f255a3928c23c158c79004c656 (diff) | |
Fixed #24910 -- Added createsuperuser support for non-unique USERNAME_FIELDs
Clarified docs to say that a non-unique USERNAME_FIELD is permissable
as long as the custom auth backend can support it.
Diffstat (limited to 'tests/auth_tests')
| -rw-r--r-- | tests/auth_tests/models/invalid_models.py | 15 | ||||
| -rw-r--r-- | tests/auth_tests/test_management.py | 27 |
2 files changed, 40 insertions, 2 deletions
diff --git a/tests/auth_tests/models/invalid_models.py b/tests/auth_tests/models/invalid_models.py index 46b4819fa4..eaaff2aa15 100644 --- a/tests/auth_tests/models/invalid_models.py +++ b/tests/auth_tests/models/invalid_models.py @@ -1,12 +1,23 @@ -from django.contrib.auth.models import AbstractBaseUser +from django.contrib.auth.models import AbstractBaseUser, UserManager from django.db import models class CustomUserNonUniqueUsername(AbstractBaseUser): - "A user with a non-unique username" + """ + A user with a non-unique username. + + This model is not invalid if it is used with a custom authentication + backend which supports non-unique usernames. + """ username = models.CharField(max_length=30) + email = models.EmailField(blank=True) + is_staff = models.BooleanField(default=False) + is_superuser = models.BooleanField(default=False) USERNAME_FIELD = 'username' + REQUIRED_FIELDS = ['email'] + + objects = UserManager() class Meta: app_label = 'auth' diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py index e9c6105496..f45703fcfd 100644 --- a/tests/auth_tests/test_management.py +++ b/tests/auth_tests/test_management.py @@ -305,6 +305,33 @@ class CreatesuperuserManagementCommandTestCase(TestCase): self.assertEqual(CustomUser._default_manager.count(), 0) + @override_settings( + AUTH_USER_MODEL='auth.CustomUserNonUniqueUsername', + AUTHENTICATION_BACKENDS=['my.custom.backend'], + ) + def test_swappable_user_username_non_unique(self): + @mock_inputs({ + 'username': 'joe', + 'password': 'nopasswd', + }) + def createsuperuser(): + new_io = six.StringIO() + call_command( + "createsuperuser", + interactive=True, + email="joe@somewhere.org", + stdout=new_io, + stdin=MockTTY(), + ) + command_output = new_io.getvalue().strip() + self.assertEqual(command_output, 'Superuser created successfully.') + + for i in range(2): + createsuperuser() + + users = CustomUserNonUniqueUsername.objects.filter(username="joe") + self.assertEqual(users.count(), 2) + def test_skip_if_not_in_TTY(self): """ If the command is not called from a TTY, it should be skipped and a |
