| 1 |
from unittest import mock |
| 2 |
import warnings |
| 3 |
|
| 4 |
from django.contrib.auth import get_user_model |
| 5 |
from django.contrib.auth.models import Group, Permission |
| 6 |
from django.contrib.contenttypes.models import ContentType |
| 7 |
from django.test import TestCase |
| 8 |
|
| 9 |
from guardian.managers import ( |
| 10 |
GroupObjectPermissionManager, |
| 11 |
UserObjectPermissionManager, |
| 12 |
_is_using_default_content_type, |
| 13 |
) |
| 14 |
from guardian.models import ( |
| 15 |
GroupObjectPermission as DefaultGroupObjectPermission, |
| 16 |
) |
| 17 |
from guardian.models import ( |
| 18 |
UserObjectPermission as DefaultUserObjectPermission, |
| 19 |
) |
| 20 |
from guardian.testapp.models import ChildTestModel, ParentTestModel |
| 21 |
from guardian.utils import get_group_obj_perms_model, get_user_obj_perms_model |
| 22 |
|
| 23 |
User = get_user_model() |
| 24 |
|
| 25 |
|
| 26 |
def get_parent_content_type(obj): |
| 27 |
""" |
| 28 |
Custom GET_CONTENT_TYPE function simulating polymorphic model libraries |
| 29 |
(e.g. django-polymorphic). Returns ParentTestModel's ContentType for |
| 30 |
ChildTestModel instances instead of the concrete model's ContentType. |
| 31 |
Used as the GUARDIAN_GET_CONTENT_TYPE target in regression tests. |
| 32 |
""" |
| 33 |
if isinstance(obj, ChildTestModel): |
| 34 |
return ContentType.objects.get_for_model(ParentTestModel) |
| 35 |
return ContentType.objects.get_for_model(obj) |
| 36 |
|
| 37 |
|
| 38 |
class TestManagers(TestCase): |
| 39 |
def test_user_manager_assign(self): |
| 40 |
manager = UserObjectPermissionManager() |
| 41 |
manager.assign_perm = mock.Mock() |
| 42 |
|
| 43 |
with warnings.catch_warnings(record=True) as w: |
| 44 |
warnings.simplefilter("always") |
| 45 |
|
| 46 |
manager.assign("perm", "user", "object") |
| 47 |
|
| 48 |
manager.assign_perm.assert_called_once_with("perm", "user", "object") |
| 49 |
|
| 50 |
self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
| 51 |
self.assertIn( |
| 52 |
"UserObjectPermissionManager method 'assign' is being renamed to 'assign_perm'.", str(w[0].message) |
| 53 |
) |
| 54 |
|
| 55 |
def test_group_manager_assign(self): |
| 56 |
manager = GroupObjectPermissionManager() |
| 57 |
manager.assign_perm = mock.Mock() |
| 58 |
|
| 59 |
with warnings.catch_warnings(record=True) as w: |
| 60 |
warnings.simplefilter("always") |
| 61 |
|
| 62 |
manager.assign("perm", "group", "object") |
| 63 |
|
| 64 |
manager.assign_perm.assert_called_once_with("perm", "group", "object") |
| 65 |
|
| 66 |
self.assertTrue(issubclass(w[0].category, DeprecationWarning)) |
| 67 |
self.assertIn( |
| 68 |
"UserObjectPermissionManager method 'assign' is being renamed to 'assign_perm'.", str(w[0].message) |
| 69 |
) |
| 70 |
|
| 71 |
|
| 72 |
class TestManagerAssignPerm(TestCase): |
| 73 |
def setUp(self): |
| 74 |
self.user = User.objects.create_user(username="testuser", email="[email protected]") |
| 75 |
self.group = Group.objects.create(name="testgroup") |
| 76 |
self.obj = ContentType.objects.create(model="foo", app_label="guardian-tests") |
| 77 |
self.content_type = ContentType.objects.get_for_model(self.obj) |
| 78 |
self.permission = Permission.objects.get(content_type=self.content_type, codename="change_contenttype") |
| 79 |
|
| 80 |
def test_user_assign_perm(self): |
| 81 |
UserObjectPermission = get_user_obj_perms_model(self.obj) |
| 82 |
with self.assertNumQueries(5): |
| 83 |
assigned = UserObjectPermission.objects.assign_perm(self.permission, self.user, self.obj) |
| 84 |
self.assertEqual(assigned, UserObjectPermission.objects.get(user=self.user, permission=self.permission)) |
| 85 |
|
| 86 |
def test_user_bulk_assign_perm(self): |
| 87 |
UserObjectPermission = get_user_obj_perms_model(self.obj) |
| 88 |
with self.assertNumQueries(3): |
| 89 |
assigned = UserObjectPermission.objects.bulk_assign_perm(self.permission, self.user, [self.obj]) |
| 90 |
self.assertEqual(assigned, [UserObjectPermission.objects.get(user=self.user, permission=self.permission)]) |
| 91 |
|
| 92 |
def test_user_bulk_assign_perm_empty_list(self): |
| 93 |
UserObjectPermission = get_user_obj_perms_model(self.obj) |
| 94 |
with self.assertNumQueries(0): |
| 95 |
assigned = UserObjectPermission.objects.bulk_assign_perm(self.permission, self.user, []) |
| 96 |
self.assertEqual(assigned, []) |
| 97 |
|
| 98 |
def test_user_assign_perm_to_many(self): |
| 99 |
UserObjectPermission = get_user_obj_perms_model(self.obj) |
| 100 |
with self.assertNumQueries(1): |
| 101 |
assigned = UserObjectPermission.objects.assign_perm_to_many(self.permission, [self.user], self.obj) |
| 102 |
self.assertEqual(assigned, [UserObjectPermission.objects.get(user=self.user, permission=self.permission)]) |
| 103 |
|
| 104 |
def test_group_assign_perm(self): |
| 105 |
GroupObjectPermission = get_group_obj_perms_model(self.obj) |
| 106 |
with self.assertNumQueries(5): |
| 107 |
assigned = GroupObjectPermission.objects.assign_perm(self.permission, self.group, self.obj) |
| 108 |
self.assertEqual(assigned, GroupObjectPermission.objects.get(group=self.group, permission=self.permission)) |
| 109 |
|
| 110 |
def test_group_bulk_assign_perm(self): |
| 111 |
GroupObjectPermission = get_group_obj_perms_model(self.obj) |
| 112 |
with self.assertNumQueries(2): |
| 113 |
assigned = GroupObjectPermission.objects.bulk_assign_perm(self.permission, self.group, [self.obj]) |
| 114 |
self.assertEqual(assigned, [GroupObjectPermission.objects.get(group=self.group, permission=self.permission)]) |
| 115 |
|
| 116 |
def test_group_bulk_assign_perm_empty_list(self): |
| 117 |
GroupObjectPermission = get_group_obj_perms_model(self.obj) |
| 118 |
with self.assertNumQueries(0): |
| 119 |
assigned = GroupObjectPermission.objects.bulk_assign_perm(self.permission, self.group, []) |
| 120 |
self.assertEqual(assigned, []) |
| 121 |
|
| 122 |
def test_group_assign_perm_to_many(self): |
| 123 |
GroupObjectPermission = get_group_obj_perms_model(self.obj) |
| 124 |
with self.assertNumQueries(1): |
| 125 |
assigned = GroupObjectPermission.objects.assign_perm_to_many(self.permission, [self.group], self.obj) |
| 126 |
self.assertEqual(assigned, [GroupObjectPermission.objects.get(group=self.group, permission=self.permission)]) |
| 127 |
|
| 128 |
|
| 129 |
class TestAssignPermCustomContentType(TestCase): |
| 130 |
""" |
| 131 |
Regression tests for the bug introduced in 3.3.0 where adding |
| 132 |
``defaults={"content_object": obj}`` to the ``get_or_create`` call in |
| 133 |
``assign_perm`` caused ``GenericForeignKey.__set__`` to silently overwrite |
| 134 |
the ``content_type`` that was set by a custom ``GUARDIAN_GET_CONTENT_TYPE`` |
| 135 |
function with the object's own concrete ``ContentType``. |
| 136 |
|
| 137 |
The ``GUARDIAN_GET_CONTENT_TYPE`` setting is authoritative; whatever |
| 138 |
content type it returns must be the one persisted on the permission row. |
| 139 |
This matters for polymorphic model setups where the custom function |
| 140 |
deliberately returns a base/proxy content type instead of the concrete one. |
| 141 |
""" |
| 142 |
|
| 143 |
def setUp(self): |
| 144 |
self.user = User.objects.create_user(username="testuser_ctype") |
| 145 |
self.group = Group.objects.create(name="testgroup_ctype") |
| 146 |
self.child = ChildTestModel.objects.create(name="polymorphic child") |
| 147 |
self.parent_ctype = ContentType.objects.get_for_model(ParentTestModel) |
| 148 |
self.child_ctype = ContentType.objects.get_for_model(ChildTestModel) |
| 149 |
# Sanity: the two content types must differ for these tests to be meaningful. |
| 150 |
self.assertNotEqual(self.parent_ctype, self.child_ctype) |
| 151 |
|
| 152 |
def test_assign_perm_user_uses_custom_content_type(self): |
| 153 |
""" |
| 154 |
UserObjectPermission.assign_perm must store the content_type returned |
| 155 |
by GUARDIAN_GET_CONTENT_TYPE, not the content_type derived from the |
| 156 |
GenericForeignKey assignment introduced by defaults={"content_object": obj}. |
| 157 |
""" |
| 158 |
with mock.patch( |
| 159 |
"guardian.conf.settings.GET_CONTENT_TYPE", |
| 160 |
"guardian.testapp.tests.test_managers.get_parent_content_type", |
| 161 |
): |
| 162 |
obj_perm = DefaultUserObjectPermission.objects.assign_perm("change_parenttestmodel", self.user, self.child) |
| 163 |
|
| 164 |
self.assertEqual( |
| 165 |
obj_perm.content_type, |
| 166 |
self.parent_ctype, |
| 167 |
"assign_perm ignored GUARDIAN_GET_CONTENT_TYPE: content_type was overwritten " |
| 168 |
"by GenericForeignKey.__set__ via defaults={'content_object': obj}", |
| 169 |
) |
| 170 |
self.assertNotEqual(obj_perm.content_type, self.child_ctype) |
| 171 |
|
| 172 |
def test_assign_perm_group_uses_custom_content_type(self): |
| 173 |
""" |
| 174 |
GroupObjectPermission.assign_perm must store the content_type returned |
| 175 |
by GUARDIAN_GET_CONTENT_TYPE, not the content_type derived from the |
| 176 |
GenericForeignKey assignment introduced by defaults={"content_object": obj}. |
| 177 |
""" |
| 178 |
with mock.patch( |
| 179 |
"guardian.conf.settings.GET_CONTENT_TYPE", |
| 180 |
"guardian.testapp.tests.test_managers.get_parent_content_type", |
| 181 |
): |
| 182 |
obj_perm = DefaultGroupObjectPermission.objects.assign_perm( |
| 183 |
"change_parenttestmodel", self.group, self.child |
| 184 |
) |
| 185 |
|
| 186 |
self.assertEqual( |
| 187 |
obj_perm.content_type, |
| 188 |
self.parent_ctype, |
| 189 |
"assign_perm ignored GUARDIAN_GET_CONTENT_TYPE: content_type was overwritten " |
| 190 |
"by GenericForeignKey.__set__ via defaults={'content_object': obj}", |
| 191 |
) |
| 192 |
self.assertNotEqual(obj_perm.content_type, self.child_ctype) |
| 193 |
|
| 194 |
|
| 195 |
class TestIsUsingDefaultContentType(TestCase): |
| 196 |
""" |
| 197 |
Unit tests for the _is_using_default_content_type helper function. |
| 198 |
|
| 199 |
This function determines whether the GUARDIAN_GET_CONTENT_TYPE setting |
| 200 |
points to the default get_default_content_type function or a custom one. |
| 201 |
This is critical for avoiding the GenericForeignKey content_type overwrite bug. |
| 202 |
""" |
| 203 |
|
| 204 |
def test_returns_true_for_default_content_type(self): |
| 205 |
"""Should return True when using the default content type function.""" |
| 206 |
# Default setting should point to guardian.ctypes.get_default_content_type |
| 207 |
with mock.patch("guardian.conf.settings.GET_CONTENT_TYPE", "guardian.ctypes.get_default_content_type"): |
| 208 |
self.assertTrue(_is_using_default_content_type()) |
| 209 |
|
| 210 |
def test_returns_false_for_custom_content_type(self): |
| 211 |
"""Should return False when using a custom content type function.""" |
| 212 |
with mock.patch( |
| 213 |
"guardian.conf.settings.GET_CONTENT_TYPE", |
| 214 |
"guardian.testapp.tests.test_managers.get_parent_content_type", |
| 215 |
): |
| 216 |
self.assertFalse(_is_using_default_content_type()) |
| 217 |
|
| 218 |
def test_returns_false_for_any_non_default_path(self): |
| 219 |
"""Should return False for any setting value that doesn't equal the default path.""" |
| 220 |
for path in [ |
| 221 |
"nonexistent.module.function", |
| 222 |
"fake_module.fake_function", |
| 223 |
"guardian.ctypes.nonexistent_function", |
| 224 |
]: |
| 225 |
with mock.patch("guardian.conf.settings.GET_CONTENT_TYPE", path): |
| 226 |
self.assertFalse(_is_using_default_content_type()) |
| 227 |
|
| 228 |
def test_string_comparison(self): |
| 229 |
"""Should compare the setting string directly against the default dotted path.""" |
| 230 |
with mock.patch("guardian.conf.settings.GET_CONTENT_TYPE", "guardian.ctypes.get_default_content_type"): |
| 231 |
result = _is_using_default_content_type() |
| 232 |
self.assertTrue(result, "Default path string should be recognized") |
| 233 |
|
| 234 |
def test_not_cached_allows_dynamic_changes(self): |
| 235 |
""" |
| 236 |
The function should not cache results to allow mock.patch during tests. |
| 237 |
Each call should re-evaluate the setting. |
| 238 |
""" |
| 239 |
# First call with default |
| 240 |
with mock.patch("guardian.conf.settings.GET_CONTENT_TYPE", "guardian.ctypes.get_default_content_type"): |
| 241 |
first_result = _is_using_default_content_type() |
| 242 |
self.assertTrue(first_result) |
| 243 |
|
| 244 |
# Second call with custom - should return different result |
| 245 |
with mock.patch( |
| 246 |
"guardian.conf.settings.GET_CONTENT_TYPE", |
| 247 |
"guardian.testapp.tests.test_managers.get_parent_content_type", |
| 248 |
): |
| 249 |
second_result = _is_using_default_content_type() |
| 250 |
self.assertFalse(second_result) |
| 251 |
|
| 252 |
# Third call with default again - should work without caching issues |
| 253 |
with mock.patch("guardian.conf.settings.GET_CONTENT_TYPE", "guardian.ctypes.get_default_content_type"): |
| 254 |
third_result = _is_using_default_content_type() |
| 255 |
self.assertTrue(third_result) |