From 7af8f4127397279d19ef7c7899e93018274e2f9b Mon Sep 17 00:00:00 2001 From: Hasan Ramezani Date: Tue, 10 Mar 2020 22:22:23 +0100 Subject: Refs #26445 -- Allowed using UserManager.create_user()/create_superuser() in migrations. Used app config to lookup user model in _create_user(). Thanks Markus Holtermann for the review and initial patch. Thanks Simon Charette for the implementation idea. --- tests/auth_tests/test_models.py | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) (limited to 'tests/auth_tests/test_models.py') diff --git a/tests/auth_tests/test_models.py b/tests/auth_tests/test_models.py index c60b66c993..2379f68b83 100644 --- a/tests/auth_tests/test_models.py +++ b/tests/auth_tests/test_models.py @@ -10,8 +10,12 @@ from django.contrib.auth.models import ( ) from django.contrib.contenttypes.models import ContentType from django.core import mail +from django.db import connection, migrations +from django.db.migrations.state import ModelState, ProjectState from django.db.models.signals import post_save -from django.test import SimpleTestCase, TestCase, override_settings +from django.test import ( + SimpleTestCase, TestCase, TransactionTestCase, override_settings, +) from .models import IntegerUsernameUser from .models.with_custom_email_field import CustomEmailField @@ -101,7 +105,12 @@ class LoadDataWithNaturalKeysAndMultipleDatabasesTestCase(TestCase): self.assertEqual(perm_other.content_type_id, other_objects[0].id) -class UserManagerTestCase(TestCase): +class UserManagerTestCase(TransactionTestCase): + available_apps = [ + 'auth_tests', + 'django.contrib.auth', + 'django.contrib.contenttypes', + ] def test_create_user(self): email_lowercase = 'normal@normal.com' @@ -156,6 +165,30 @@ class UserManagerTestCase(TestCase): for char in password: self.assertIn(char, allowed_chars) + def test_runpython_manager_methods(self): + def forwards(apps, schema_editor): + UserModel = apps.get_model('auth', 'User') + user = UserModel.objects.create_user('user1', password='secure') + self.assertIsInstance(user, UserModel) + + operation = migrations.RunPython(forwards, migrations.RunPython.noop) + project_state = ProjectState() + project_state.add_model(ModelState.from_model(User)) + project_state.add_model(ModelState.from_model(Group)) + project_state.add_model(ModelState.from_model(Permission)) + project_state.add_model(ModelState.from_model(ContentType)) + new_state = project_state.clone() + with connection.schema_editor() as editor: + operation.state_forwards('test_manager_methods', new_state) + operation.database_forwards( + 'test_manager_methods', + editor, + project_state, + new_state, + ) + user = User.objects.get(username='user1') + self.assertTrue(user.check_password('secure')) + class AbstractBaseUserTests(SimpleTestCase): -- cgit v1.2.1