1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #33178 -- Added createsuperuser tests for validation of foreign keys.

This commit is contained in:
Christophe Henry 2021-10-12 07:44:43 +02:00 committed by Mariusz Felisiak
parent 4ff500f294
commit 57273e1569

View File

@ -531,6 +531,43 @@ class CreatesuperuserManagementCommandTestCase(TestCase):
test(self)
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithFK')
def test_validate_fk(self):
email = Email.objects.create(email='mymail@gmail.com')
Group.objects.all().delete()
nonexistent_group_id = 1
msg = f'group instance with id {nonexistent_group_id} does not exist.'
with self.assertRaisesMessage(CommandError, msg):
call_command(
'createsuperuser',
interactive=False,
username=email.pk,
email=email.email,
group=nonexistent_group_id,
verbosity=0,
)
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithFK')
def test_validate_fk_environment_variable(self):
email = Email.objects.create(email='mymail@gmail.com')
Group.objects.all().delete()
nonexistent_group_id = 1
msg = f'group instance with id {nonexistent_group_id} does not exist.'
with mock.patch.dict(
os.environ,
{'DJANGO_SUPERUSER_GROUP': str(nonexistent_group_id)},
):
with self.assertRaisesMessage(CommandError, msg):
call_command(
'createsuperuser',
interactive=False,
username=email.pk,
email=email.email,
verbosity=0,
)
@override_settings(AUTH_USER_MODEL='auth_tests.CustomUserWithM2m')
def test_fields_with_m2m(self):
new_io = StringIO()