| 1 |
from unittest import skipUnless |
| 2 |
from django.contrib.auth import get_user_model |
| 3 |
from django.contrib.contenttypes.models import ContentType |
| 4 |
from django.test import TestCase |
| 5 |
|
| 6 |
try: |
| 7 |
import guardian # noqa: F401 |
| 8 |
|
| 9 |
GUARDIAN_INSTALLED = True |
| 10 |
except ImportError: |
| 11 |
GUARDIAN_INSTALLED = False |
| 12 |
|
| 13 |
from ..models import Article, BlogPost, NewsArticle |
| 14 |
from ....models import PlainD |
| 15 |
from polymorphic.contrib.guardian import get_polymorphic_base_content_type |
| 16 |
|
| 17 |
|
| 18 |
User = get_user_model() |
| 19 |
|
| 20 |
|
| 21 |
@skipUnless(GUARDIAN_INSTALLED, "django-guardian is not installed") |
| 22 |
class GuardianIntegrationTests(TestCase): |
| 23 |
""" |
| 24 |
Tests for django-guardian integration with polymorphic models. |
| 25 |
|
| 26 |
These tests verify that get_polymorphic_base_content_type returns the correct |
| 27 |
base content type for polymorphic models, which is essential for django-guardian |
| 28 |
to work correctly with polymorphic inheritance. |
| 29 |
|
| 30 |
When configured with GUARDIAN_GET_CONTENT_TYPE pointing to this function, |
| 31 |
django-guardian will assign permissions to the base polymorphic model rather |
| 32 |
than the specific child model, ensuring consistent permission handling across |
| 33 |
the polymorphic hierarchy. |
| 34 |
""" |
| 35 |
|
| 36 |
def setUp(self): |
| 37 |
"""Create test objects.""" |
| 38 |
self.user = User.objects.create_user( |
| 39 |
username="testuser", password="testpass" |
| 40 |
) |
| 41 |
self.blog_post = BlogPost.objects.create( |
| 42 |
title="Test Blog", content="Blog content", author="Blog Author" |
| 43 |
) |
| 44 |
self.news_article = NewsArticle.objects.create( |
| 45 |
title="Test News", content="News content", source="News Source" |
| 46 |
) |
| 47 |
|
| 48 |
def test_non_polymorphic_model(self): |
| 49 |
"""Test that non-polymorphic models return their own content type.""" |
| 50 |
plain_d = PlainD.objects.create(field1="Plain D", field2="Plain D") |
| 51 |
ctype = get_polymorphic_base_content_type(plain_d) |
| 52 |
|
| 53 |
expected_ctype = ContentType.objects.get_for_model(PlainD) |
| 54 |
self.assertEqual(ctype, expected_ctype) |
| 55 |
self.assertEqual(ctype.model, "plaind") |
| 56 |
|
| 57 |
def test_get_polymorphic_base_content_type_for_child_model(self): |
| 58 |
"""Test that get_polymorphic_base_content_type returns base type for child models.""" |
| 59 |
# Get content type for BlogPost instance |
| 60 |
blog_ctype = get_polymorphic_base_content_type(self.blog_post) |
| 61 |
|
| 62 |
# Should return Article content type, not BlogPost |
| 63 |
article_ctype = ContentType.objects.get_for_model( |
| 64 |
Article, for_concrete_model=False |
| 65 |
) |
| 66 |
self.assertEqual(blog_ctype, article_ctype) |
| 67 |
self.assertNotEqual(blog_ctype.model, "blogpost") |
| 68 |
self.assertEqual(blog_ctype.model, "article") |
| 69 |
|
| 70 |
def test_get_polymorphic_base_content_type_for_different_child_models( |
| 71 |
self, |
| 72 |
): |
| 73 |
"""Test that different child models return the same base content type.""" |
| 74 |
blog_ctype = get_polymorphic_base_content_type(self.blog_post) |
| 75 |
news_ctype = get_polymorphic_base_content_type(self.news_article) |
| 76 |
|
| 77 |
# Both should return the same Article content type |
| 78 |
self.assertEqual(blog_ctype, news_ctype) |
| 79 |
self.assertEqual(blog_ctype.model, "article") |
| 80 |
|
| 81 |
def test_get_polymorphic_base_content_type_for_base_model(self): |
| 82 |
"""Test that base polymorphic models return their own content type.""" |
| 83 |
article = Article.objects.create( |
| 84 |
title="Plain Article", content="Plain content" |
| 85 |
) |
| 86 |
article_ctype = get_polymorphic_base_content_type(article) |
| 87 |
|
| 88 |
expected_ctype = ContentType.objects.get_for_model( |
| 89 |
Article, for_concrete_model=False |
| 90 |
) |
| 91 |
self.assertEqual(article_ctype, expected_ctype) |
| 92 |
self.assertEqual(article_ctype.model, "article") |
| 93 |
|
| 94 |
def test_get_polymorphic_base_content_type_for_non_polymorphic_model( |
| 95 |
self, |
| 96 |
): |
| 97 |
"""Test that non-polymorphic models return their own content type.""" |
| 98 |
user_ctype = get_polymorphic_base_content_type(self.user) |
| 99 |
|
| 100 |
expected_ctype = ContentType.objects.get_for_model(User) |
| 101 |
self.assertEqual(user_ctype, expected_ctype) |
| 102 |
|
| 103 |
def test_get_polymorphic_base_content_type_with_model_class(self): |
| 104 |
"""Test that the function works with model classes, not just instances.""" |
| 105 |
# Test with a model class instead of instance |
| 106 |
blog_class_ctype = get_polymorphic_base_content_type(BlogPost) |
| 107 |
|
| 108 |
article_ctype = ContentType.objects.get_for_model( |
| 109 |
Article, for_concrete_model=False |
| 110 |
) |
| 111 |
self.assertEqual(blog_class_ctype, article_ctype) |
| 112 |
|
| 113 |
def test_content_type_consistency_across_inheritance_chain(self): |
| 114 |
"""Test that content type is consistent across the inheritance chain.""" |
| 115 |
# Create multiple levels if they exist |
| 116 |
blog_ctype = get_polymorphic_base_content_type(self.blog_post) |
| 117 |
news_ctype = get_polymorphic_base_content_type(self.news_article) |
| 118 |
|
| 119 |
# All should point to the same base Article type |
| 120 |
article_ctype = ContentType.objects.get_for_model( |
| 121 |
Article, for_concrete_model=False |
| 122 |
) |
| 123 |
self.assertEqual(blog_ctype, article_ctype) |
| 124 |
self.assertEqual(news_ctype, article_ctype) |
| 125 |
|
| 126 |
def test_get_polymorphic_base_content_type_with_instance_and_class( |
| 127 |
self, |
| 128 |
): |
| 129 |
"""Test that the function works consistently with instances and classes.""" |
| 130 |
# Get content type from instance |
| 131 |
instance_ctype = get_polymorphic_base_content_type(self.blog_post) |
| 132 |
|
| 133 |
# Get content type from class |
| 134 |
class_ctype = get_polymorphic_base_content_type(BlogPost) |
| 135 |
|
| 136 |
# Both should return the same Article content type |
| 137 |
self.assertEqual(instance_ctype, class_ctype) |
| 138 |
self.assertEqual(instance_ctype.model, "article") |
| 139 |
|
| 140 |
def test_get_polymorphic_base_content_type_returns_content_type_object( |
| 141 |
self, |
| 142 |
): |
| 143 |
"""Test that the function returns a ContentType instance.""" |
| 144 |
self.assertIsInstance( |
| 145 |
get_polymorphic_base_content_type(self.blog_post), ContentType |
| 146 |
) |
| 147 |
|
| 148 |
def test_guardian_permissions_use_base_model_namespace(self): |
| 149 |
""" |
| 150 |
Test that guardian permissions are assigned to base model when configured. |
| 151 |
|
| 152 |
This test verifies that when GUARDIAN_GET_CONTENT_TYPE is set to use |
| 153 |
get_polymorphic_base_content_type, permissions assigned to child model |
| 154 |
instances (BlogPost, NewsArticle) are stored against the base Article |
| 155 |
content type, creating a single permission namespace for the entire |
| 156 |
polymorphic model hierarchy. |
| 157 |
""" |
| 158 |
from guardian.shortcuts import assign_perm, remove_perm |
| 159 |
from guardian.models import UserObjectPermission |
| 160 |
|
| 161 |
# The setting is configured in settings.py when guardian is installed |
| 162 |
# GUARDIAN_GET_CONTENT_TYPE = "polymorphic.contrib.guardian..." |
| 163 |
|
| 164 |
# Assign permission to a BlogPost instance |
| 165 |
assign_perm("add_article", self.user, self.blog_post) |
| 166 |
|
| 167 |
# Get the stored permission object |
| 168 |
perm = UserObjectPermission.objects.filter( |
| 169 |
user=self.user, object_pk=str(self.blog_post.pk) |
| 170 |
).first() |
| 171 |
|
| 172 |
# Verify permission was created |
| 173 |
self.assertIsNotNone(perm, "Permission should be created") |
| 174 |
|
| 175 |
# The critical assertion: permission should use Article content type, |
| 176 |
# NOT BlogPost content type |
| 177 |
article_ctype = ContentType.objects.get_for_model( |
| 178 |
Article, for_concrete_model=False |
| 179 |
) |
| 180 |
self.assertEqual( |
| 181 |
perm.content_type, |
| 182 |
article_ctype, |
| 183 |
"Permission should be stored against Article, not BlogPost", |
| 184 |
) |
| 185 |
|
| 186 |
# Verify the permission codename is correct |
| 187 |
self.assertEqual(perm.permission.codename, "add_article") |
| 188 |
|
| 189 |
# Now assign permission to a NewsArticle instance |
| 190 |
assign_perm("change_article", self.user, self.news_article) |
| 191 |
|
| 192 |
news_perm = UserObjectPermission.objects.filter( |
| 193 |
user=self.user, object_pk=str(self.news_article.pk) |
| 194 |
).first() |
| 195 |
|
| 196 |
# NewsArticle permission should ALSO use Article content type |
| 197 |
self.assertEqual( |
| 198 |
news_perm.content_type, |
| 199 |
article_ctype, |
| 200 |
"NewsArticle permission should also use Article content type", |
| 201 |
) |
| 202 |
|
| 203 |
# All permissions for this polymorphic tree should share the same |
| 204 |
# content type, creating a unified permission namespace |
| 205 |
all_perms = UserObjectPermission.objects.filter(user=self.user) |
| 206 |
content_types = set(p.content_type for p in all_perms) |
| 207 |
|
| 208 |
self.assertEqual( |
| 209 |
len(content_types), |
| 210 |
1, |
| 211 |
"All permissions should use the same Article content type", |
| 212 |
) |
| 213 |
self.assertEqual( |
| 214 |
content_types.pop(), |
| 215 |
article_ctype, |
| 216 |
"The shared content type should be Article", |
| 217 |
) |
| 218 |
|
| 219 |
# Clean up |
| 220 |
remove_perm("add_article", self.user, self.blog_post) |
| 221 |
remove_perm("change_article", self.user, self.news_article) |