Commit 76f2f58a authored by Jürno Ader's avatar Jürno Ader Committed by Tim Graham
Browse files

Fixed #22956 -- Made PermissionManager.get_by_natural_key() use the correct...

Fixed #22956 -- Made PermissionManager.get_by_natural_key() use the correct database for content type lookup.
parent da599022
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -32,8 +32,7 @@ class PermissionManager(models.Manager):
    def get_by_natural_key(self, codename, app_label, model):
        return self.get(
            codename=codename,
            content_type=ContentType.objects.get_by_natural_key(app_label,
                                                                model),
            content_type=ContentType.objects.db_manager(self.db).get_by_natural_key(app_label, model),
        )


+60 −1
Original line number Diff line number Diff line
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractUser, Group, User, UserManager
from django.contrib.auth.models import AbstractUser, Group, Permission, User, UserManager
from django.contrib.auth.tests.utils import skipIfCustomUser
from django.contrib.contenttypes.models import ContentType
from django.core import mail
from django.db.models.signals import post_save
from django.test import TestCase, override_settings
@@ -43,6 +44,64 @@ class LoadDataWithNaturalKeysTestCase(TestCase):
        self.assertEqual(group, user.groups.get())


class LoadDataWithNaturalKeysAndMultipleDatabasesTestCase(TestCase):
    multi_db = True

    def test_load_data_with_user_permissions(self):
        # Create test contenttypes for both databases
        default_objects = [
            ContentType.objects.db_manager('default').create(
                model='examplemodela',
                name='example model a',
                app_label='app_a',
            ),
            ContentType.objects.db_manager('default').create(
                model='examplemodelb',
                name='example model b',
                app_label='app_b',
            ),
        ]
        other_objects = [
            ContentType.objects.db_manager('other').create(
                model='examplemodelb',
                name='example model b',
                app_label='app_b',
            ),
            ContentType.objects.db_manager('other').create(
                model='examplemodela',
                name='example model a',
                app_label='app_a',
            ),
        ]

        # Now we create the test UserPermission
        Permission.objects.db_manager("default").create(
            name="Can delete example model b",
            codename="delete_examplemodelb",
            content_type=default_objects[1],
        )
        Permission.objects.db_manager("other").create(
            name="Can delete example model b",
            codename="delete_examplemodelb",
            content_type=other_objects[0],
        )

        perm_default = Permission.objects.get_by_natural_key(
            'delete_examplemodelb',
            'app_b',
            'examplemodelb',
        )

        perm_other = Permission.objects.db_manager('other').get_by_natural_key(
            'delete_examplemodelb',
            'app_b',
            'examplemodelb',
        )

        self.assertEqual(perm_default.content_type_id, default_objects[1].id)
        self.assertEqual(perm_other.content_type_id, other_objects[0].id)


@skipIfCustomUser
class UserManagerTestCase(TestCase):