summaryrefslogtreecommitdiff
path: root/tests/auth_tests
diff options
context:
space:
mode:
authorChristophe Henry <contact2@c-henry.fr>2021-10-12 07:44:43 +0200
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-10-12 07:54:50 +0200
commit57273e15699e2f87eda4d7cc1e3014c7d7c8f89a (patch)
tree9f7b4bbc1bfe9ed4c74cc8c1104a9b030740d29b /tests/auth_tests
parent4ff500f2948bfc332b3f4159021cad06e91943d3 (diff)
downloaddjango-57273e15699e2f87eda4d7cc1e3014c7d7c8f89a.tar.gz
Refs #33178 -- Added createsuperuser tests for validation of foreign keys.
Diffstat (limited to 'tests/auth_tests')
-rw-r--r--tests/auth_tests/test_management.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/auth_tests/test_management.py b/tests/auth_tests/test_management.py
index 8cd7b378c0..3405e8588a 100644
--- a/tests/auth_tests/test_management.py
+++ b/tests/auth_tests/test_management.py
@@ -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()