summaryrefslogtreecommitdiff
path: root/tests/auth_tests/test_migrations.py
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2019-11-28 11:46:08 +0100
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2019-11-29 11:10:59 +0100
commit9a17ae50c61a3a0ea6c552ce4e3eab27f796d094 (patch)
tree868005d98c068d630158c536f0b049bee0a81a43 /tests/auth_tests/test_migrations.py
parent019a1b9274c645db82593bc5898a279d134b464b (diff)
[2.2.x] Fixed #31021 -- Fixed proxy model permissions data migration crash with a multiple databases setup.
Regression in 98296f86b340c8c9c968375d59f1d3a3479e60c2. Backport of e8fcdaad5c428878d0a5d6ba820d957013f75595 from master
Diffstat (limited to 'tests/auth_tests/test_migrations.py')
-rw-r--r--tests/auth_tests/test_migrations.py72
1 files changed, 56 insertions, 16 deletions
diff --git a/tests/auth_tests/test_migrations.py b/tests/auth_tests/test_migrations.py
index 98c5b5964a..a91b9a9146 100644
--- a/tests/auth_tests/test_migrations.py
+++ b/tests/auth_tests/test_migrations.py
@@ -3,7 +3,8 @@ from importlib import import_module
from django.apps import apps
from django.contrib.auth.models import Permission, User
from django.contrib.contenttypes.models import ContentType
-from django.test import TestCase
+from django.db import connection, connections
+from django.test import TransactionTestCase
from django.test.utils import captured_stdout
from .models import Proxy, UserProxy
@@ -11,7 +12,7 @@ from .models import Proxy, UserProxy
update_proxy_permissions = import_module('django.contrib.auth.migrations.0011_update_proxy_permissions')
-class ProxyModelWithDifferentAppLabelTests(TestCase):
+class ProxyModelWithDifferentAppLabelTests(TransactionTestCase):
available_apps = [
'auth_tests',
'django.contrib.auth',
@@ -41,7 +42,8 @@ class ProxyModelWithDifferentAppLabelTests(TestCase):
proxy_model_content_type = ContentType.objects.get_for_model(UserProxy, for_concrete_model=False)
self.assertEqual(self.default_permission.content_type, self.concrete_content_type)
self.assertEqual(self.custom_permission.content_type, self.concrete_content_type)
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
self.default_permission.refresh_from_db()
self.assertEqual(self.default_permission.content_type, proxy_model_content_type)
self.custom_permission.refresh_from_db()
@@ -54,7 +56,8 @@ class ProxyModelWithDifferentAppLabelTests(TestCase):
for permission in [self.default_permission, self.custom_permission]:
self.assertTrue(user.has_perm('auth.' + permission.codename))
self.assertFalse(user.has_perm('auth_tests.' + permission.codename))
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
# Reload user to purge the _perm_cache.
user = User._default_manager.get(pk=user.pk)
for permission in [self.default_permission, self.custom_permission]:
@@ -62,8 +65,9 @@ class ProxyModelWithDifferentAppLabelTests(TestCase):
self.assertTrue(user.has_perm('auth_tests.' + permission.codename))
def test_migrate_backwards(self):
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
- update_proxy_permissions.revert_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
+ update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
self.default_permission.refresh_from_db()
self.assertEqual(self.default_permission.content_type, self.concrete_content_type)
self.custom_permission.refresh_from_db()
@@ -76,8 +80,9 @@ class ProxyModelWithDifferentAppLabelTests(TestCase):
for permission in [self.default_permission, self.custom_permission]:
self.assertTrue(user.has_perm('auth.' + permission.codename))
self.assertFalse(user.has_perm('auth_tests.' + permission.codename))
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
- update_proxy_permissions.revert_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
+ update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
# Reload user to purge the _perm_cache.
user = User._default_manager.get(pk=user.pk)
for permission in [self.default_permission, self.custom_permission]:
@@ -85,7 +90,7 @@ class ProxyModelWithDifferentAppLabelTests(TestCase):
self.assertFalse(user.has_perm('auth_tests.' + permission.codename))
-class ProxyModelWithSameAppLabelTests(TestCase):
+class ProxyModelWithSameAppLabelTests(TransactionTestCase):
available_apps = [
'auth_tests',
'django.contrib.auth',
@@ -115,7 +120,8 @@ class ProxyModelWithSameAppLabelTests(TestCase):
proxy_model_content_type = ContentType.objects.get_for_model(Proxy, for_concrete_model=False)
self.assertEqual(self.default_permission.content_type, self.concrete_content_type)
self.assertEqual(self.custom_permission.content_type, self.concrete_content_type)
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
self.default_permission.refresh_from_db()
self.custom_permission.refresh_from_db()
self.assertEqual(self.default_permission.content_type, proxy_model_content_type)
@@ -127,15 +133,17 @@ class ProxyModelWithSameAppLabelTests(TestCase):
user.user_permissions.add(self.custom_permission)
for permission in [self.default_permission, self.custom_permission]:
self.assertTrue(user.has_perm('auth_tests.' + permission.codename))
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
# Reload user to purge the _perm_cache.
user = User._default_manager.get(pk=user.pk)
for permission in [self.default_permission, self.custom_permission]:
self.assertTrue(user.has_perm('auth_tests.' + permission.codename))
def test_migrate_backwards(self):
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
- update_proxy_permissions.revert_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
+ update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
self.default_permission.refresh_from_db()
self.assertEqual(self.default_permission.content_type, self.concrete_content_type)
self.custom_permission.refresh_from_db()
@@ -147,8 +155,9 @@ class ProxyModelWithSameAppLabelTests(TestCase):
user.user_permissions.add(self.custom_permission)
for permission in [self.default_permission, self.custom_permission]:
self.assertTrue(user.has_perm('auth_tests.' + permission.codename))
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
- update_proxy_permissions.revert_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
+ update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
# Reload user to purge the _perm_cache.
user = User._default_manager.get(pk=user.pk)
for permission in [self.default_permission, self.custom_permission]:
@@ -175,5 +184,36 @@ class ProxyModelWithSameAppLabelTests(TestCase):
name='May display proxys information',
)
with captured_stdout() as stdout:
- update_proxy_permissions.update_proxy_model_permissions(apps, None)
+ with connection.schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
self.assertIn('A problem arose migrating proxy model permissions', stdout.getvalue())
+
+
+class MultiDBProxyModelAppLabelTests(TransactionTestCase):
+ databases = {'default', 'other'}
+ available_apps = [
+ 'auth_tests',
+ 'django.contrib.auth',
+ 'django.contrib.contenttypes',
+ ]
+
+ def setUp(self):
+ ContentType.objects.all().delete()
+ Permission.objects.using('other').delete()
+ concrete_content_type = ContentType.objects.db_manager(
+ 'other'
+ ).get_for_model(Proxy)
+ self.permission = Permission.objects.using('other').create(
+ content_type=concrete_content_type,
+ codename='add_proxy',
+ name='Can add proxy',
+ )
+
+ def test_migrate_other_database(self):
+ proxy_model_content_type = ContentType.objects.db_manager(
+ 'other'
+ ).get_for_model(Proxy, for_concrete_model=False)
+ with connections['other'].schema_editor() as editor:
+ update_proxy_permissions.update_proxy_model_permissions(apps, editor)
+ self.permission.refresh_from_db()
+ self.assertEqual(self.permission.content_type, proxy_model_content_type)