2019-01-16 15:07:28 +00:00
|
|
|
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
|
2019-11-28 10:46:08 +00:00
|
|
|
from django.db import connection, connections
|
|
|
|
from django.test import TransactionTestCase
|
2019-04-26 06:51:58 +00:00
|
|
|
from django.test.utils import captured_stdout
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
from .models import Proxy, UserProxy
|
|
|
|
|
2022-02-03 19:24:19 +00:00
|
|
|
update_proxy_permissions = import_module(
|
|
|
|
"django.contrib.auth.migrations.0011_update_proxy_permissions"
|
|
|
|
)
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
|
2019-11-28 10:46:08 +00:00
|
|
|
class ProxyModelWithDifferentAppLabelTests(TransactionTestCase):
|
2019-01-16 15:07:28 +00:00
|
|
|
available_apps = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"auth_tests",
|
|
|
|
"django.contrib.auth",
|
|
|
|
"django.contrib.contenttypes",
|
2019-01-16 15:07:28 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create proxy permissions with content_type to the concrete model
|
|
|
|
rather than the proxy model (as they were before Django 2.2 and
|
|
|
|
migration 11).
|
|
|
|
"""
|
|
|
|
Permission.objects.all().delete()
|
|
|
|
self.concrete_content_type = ContentType.objects.get_for_model(UserProxy)
|
|
|
|
self.default_permission = Permission.objects.create(
|
|
|
|
content_type=self.concrete_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="add_userproxy",
|
|
|
|
name="Can add userproxy",
|
2019-01-16 15:07:28 +00:00
|
|
|
)
|
|
|
|
self.custom_permission = Permission.objects.create(
|
|
|
|
content_type=self.concrete_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="use_different_app_label",
|
|
|
|
name="May use a different app label",
|
2019-01-16 15:07:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_proxy_model_permissions_contenttype(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
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
|
|
|
|
)
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
self.default_permission.refresh_from_db()
|
|
|
|
self.assertEqual(self.default_permission.content_type, proxy_model_content_type)
|
|
|
|
self.custom_permission.refresh_from_db()
|
|
|
|
self.assertEqual(self.custom_permission.content_type, proxy_model_content_type)
|
|
|
|
|
|
|
|
def test_user_has_now_proxy_model_permissions(self):
|
|
|
|
user = User.objects.create()
|
|
|
|
user.user_permissions.add(self.default_permission)
|
|
|
|
user.user_permissions.add(self.custom_permission)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth." + permission.codename))
|
|
|
|
self.assertFalse(user.has_perm("auth_tests." + permission.codename))
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
# Reload user to purge the _perm_cache.
|
|
|
|
user = User._default_manager.get(pk=user.pk)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertFalse(user.has_perm("auth." + permission.codename))
|
|
|
|
self.assertTrue(user.has_perm("auth_tests." + permission.codename))
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
def test_migrate_backwards(self):
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
|
|
|
update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
self.default_permission.refresh_from_db()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.default_permission.content_type, self.concrete_content_type
|
|
|
|
)
|
2019-01-16 15:07:28 +00:00
|
|
|
self.custom_permission.refresh_from_db()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.custom_permission.content_type, self.concrete_content_type
|
|
|
|
)
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
def test_user_keeps_same_permissions_after_migrating_backward(self):
|
|
|
|
user = User.objects.create()
|
|
|
|
user.user_permissions.add(self.default_permission)
|
|
|
|
user.user_permissions.add(self.custom_permission)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth." + permission.codename))
|
|
|
|
self.assertFalse(user.has_perm("auth_tests." + permission.codename))
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
|
|
|
update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
# Reload user to purge the _perm_cache.
|
|
|
|
user = User._default_manager.get(pk=user.pk)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth." + permission.codename))
|
|
|
|
self.assertFalse(user.has_perm("auth_tests." + permission.codename))
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
|
2019-11-28 10:46:08 +00:00
|
|
|
class ProxyModelWithSameAppLabelTests(TransactionTestCase):
|
2019-01-16 15:07:28 +00:00
|
|
|
available_apps = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"auth_tests",
|
|
|
|
"django.contrib.auth",
|
|
|
|
"django.contrib.contenttypes",
|
2019-01-16 15:07:28 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""
|
|
|
|
Create proxy permissions with content_type to the concrete model
|
|
|
|
rather than the proxy model (as they were before Django 2.2 and
|
|
|
|
migration 11).
|
|
|
|
"""
|
|
|
|
Permission.objects.all().delete()
|
|
|
|
self.concrete_content_type = ContentType.objects.get_for_model(Proxy)
|
|
|
|
self.default_permission = Permission.objects.create(
|
|
|
|
content_type=self.concrete_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="add_proxy",
|
|
|
|
name="Can add proxy",
|
2019-01-16 15:07:28 +00:00
|
|
|
)
|
|
|
|
self.custom_permission = Permission.objects.create(
|
|
|
|
content_type=self.concrete_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="display_proxys",
|
|
|
|
name="May display proxys information",
|
2019-01-16 15:07:28 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_proxy_model_permissions_contenttype(self):
|
2022-02-03 19:24:19 +00:00
|
|
|
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
|
|
|
|
)
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
self.default_permission.refresh_from_db()
|
|
|
|
self.custom_permission.refresh_from_db()
|
|
|
|
self.assertEqual(self.default_permission.content_type, proxy_model_content_type)
|
|
|
|
self.assertEqual(self.custom_permission.content_type, proxy_model_content_type)
|
|
|
|
|
|
|
|
def test_user_still_has_proxy_model_permissions(self):
|
|
|
|
user = User.objects.create()
|
|
|
|
user.user_permissions.add(self.default_permission)
|
|
|
|
user.user_permissions.add(self.custom_permission)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth_tests." + permission.codename))
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
# Reload user to purge the _perm_cache.
|
|
|
|
user = User._default_manager.get(pk=user.pk)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth_tests." + permission.codename))
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
def test_migrate_backwards(self):
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
|
|
|
update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
self.default_permission.refresh_from_db()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.default_permission.content_type, self.concrete_content_type
|
|
|
|
)
|
2019-01-16 15:07:28 +00:00
|
|
|
self.custom_permission.refresh_from_db()
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertEqual(
|
|
|
|
self.custom_permission.content_type, self.concrete_content_type
|
|
|
|
)
|
2019-01-16 15:07:28 +00:00
|
|
|
|
|
|
|
def test_user_keeps_same_permissions_after_migrating_backward(self):
|
|
|
|
user = User.objects.create()
|
|
|
|
user.user_permissions.add(self.default_permission)
|
|
|
|
user.user_permissions.add(self.custom_permission)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth_tests." + permission.codename))
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
|
|
|
update_proxy_permissions.revert_proxy_model_permissions(apps, editor)
|
2019-01-16 15:07:28 +00:00
|
|
|
# Reload user to purge the _perm_cache.
|
|
|
|
user = User._default_manager.get(pk=user.pk)
|
|
|
|
for permission in [self.default_permission, self.custom_permission]:
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertTrue(user.has_perm("auth_tests." + permission.codename))
|
2019-04-26 06:51:58 +00:00
|
|
|
|
|
|
|
def test_migrate_with_existing_target_permission(self):
|
|
|
|
"""
|
|
|
|
Permissions may already exist:
|
|
|
|
|
|
|
|
- Old workaround was to manually create permissions for proxy models.
|
|
|
|
- Model may have been concrete and then converted to proxy.
|
|
|
|
|
|
|
|
Output a reminder to audit relevant permissions.
|
|
|
|
"""
|
2022-02-03 19:24:19 +00:00
|
|
|
proxy_model_content_type = ContentType.objects.get_for_model(
|
|
|
|
Proxy, for_concrete_model=False
|
|
|
|
)
|
2019-04-26 06:51:58 +00:00
|
|
|
Permission.objects.create(
|
|
|
|
content_type=proxy_model_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="add_proxy",
|
|
|
|
name="Can add proxy",
|
2019-04-26 06:51:58 +00:00
|
|
|
)
|
|
|
|
Permission.objects.create(
|
|
|
|
content_type=proxy_model_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="display_proxys",
|
|
|
|
name="May display proxys information",
|
2019-04-26 06:51:58 +00:00
|
|
|
)
|
|
|
|
with captured_stdout() as stdout:
|
2019-11-28 10:46:08 +00:00
|
|
|
with connection.schema_editor() as editor:
|
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
2022-02-03 19:24:19 +00:00
|
|
|
self.assertIn(
|
|
|
|
"A problem arose migrating proxy model permissions", stdout.getvalue()
|
|
|
|
)
|
2019-11-28 10:46:08 +00:00
|
|
|
|
|
|
|
|
|
|
|
class MultiDBProxyModelAppLabelTests(TransactionTestCase):
|
2022-02-03 19:24:19 +00:00
|
|
|
databases = {"default", "other"}
|
2019-11-28 10:46:08 +00:00
|
|
|
available_apps = [
|
2022-02-03 19:24:19 +00:00
|
|
|
"auth_tests",
|
|
|
|
"django.contrib.auth",
|
|
|
|
"django.contrib.contenttypes",
|
2019-11-28 10:46:08 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
ContentType.objects.all().delete()
|
2022-02-03 19:24:19 +00:00
|
|
|
Permission.objects.using("other").delete()
|
|
|
|
concrete_content_type = ContentType.objects.db_manager("other").get_for_model(
|
|
|
|
Proxy
|
|
|
|
)
|
|
|
|
self.permission = Permission.objects.using("other").create(
|
2019-11-28 10:46:08 +00:00
|
|
|
content_type=concrete_content_type,
|
2022-02-03 19:24:19 +00:00
|
|
|
codename="add_proxy",
|
|
|
|
name="Can add proxy",
|
2019-11-28 10:46:08 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
def test_migrate_other_database(self):
|
|
|
|
proxy_model_content_type = ContentType.objects.db_manager(
|
2022-02-03 19:24:19 +00:00
|
|
|
"other"
|
2019-11-28 10:46:08 +00:00
|
|
|
).get_for_model(Proxy, for_concrete_model=False)
|
2022-02-03 19:24:19 +00:00
|
|
|
with connections["other"].schema_editor() as editor:
|
2019-11-28 10:46:08 +00:00
|
|
|
update_proxy_permissions.update_proxy_model_permissions(apps, editor)
|
|
|
|
self.permission.refresh_from_db()
|
|
|
|
self.assertEqual(self.permission.content_type, proxy_model_content_type)
|