| 1 |
""" |
| 2 |
Integration tests to verify that the new backend methods work correctly |
| 3 |
with Django's authentication system. |
| 4 |
""" |
| 5 |
|
| 6 |
from django.contrib.auth import get_user_model |
| 7 |
from django.contrib.auth.models import Group |
| 8 |
from django.test import TestCase, override_settings |
| 9 |
|
| 10 |
from guardian.shortcuts import assign_perm |
| 11 |
from guardian.testapp.models import Project |
| 12 |
|
| 13 |
User = get_user_model() |
| 14 |
|
| 15 |
|
| 16 |
@override_settings( |
| 17 |
AUTHENTICATION_BACKENDS=[ |
| 18 |
"guardian.backends.ObjectPermissionBackend", |
| 19 |
"django.contrib.auth.backends.ModelBackend", |
| 20 |
] |
| 21 |
) |
| 22 |
class BackendIntegrationTest(TestCase): |
| 23 |
"""Test that the backend integrates correctly with Django's auth system""" |
| 24 |
|
| 25 |
def setUp(self): |
| 26 |
self.user = User.objects.create_user(username="testuser", password="testpass") |
| 27 |
self.superuser = User.objects.create_superuser(username="superuser", password="superpass") |
| 28 |
self.group = Group.objects.create(name="testgroup") |
| 29 |
self.user.groups.add(self.group) |
| 30 |
self.project = Project.objects.create(name="Test Project") |
| 31 |
|
| 32 |
def test_user_get_group_permissions_integration(self): |
| 33 |
"""Test that user.get_group_permissions() calls our backend correctly""" |
| 34 |
# Assign permission to group |
| 35 |
assign_perm("change_project", self.group, self.project) |
| 36 |
|
| 37 |
# Django's User.get_group_permissions() should now include our backend |
| 38 |
group_perms = self.user.get_group_permissions(self.project) |
| 39 |
self.assertIn("change_project", group_perms) |
| 40 |
|
| 41 |
def test_user_get_group_permissions_without_object(self): |
| 42 |
"""Test that user.get_group_permissions() without object works""" |
| 43 |
# When no object is provided, our backend should return empty set |
| 44 |
group_perms = self.user.get_group_permissions() |
| 45 |
# The result should not include our object-specific permissions |
| 46 |
# but may include model-level permissions from ModelBackend |
| 47 |
self.assertIsInstance(group_perms, set) |
| 48 |
|
| 49 |
def test_user_get_all_permissions_integration(self): |
| 50 |
"""Test that user.get_all_permissions() includes our object permissions""" |
| 51 |
# Assign both user and group permissions |
| 52 |
assign_perm("change_project", self.user, self.project) |
| 53 |
assign_perm("delete_project", self.group, self.project) |
| 54 |
|
| 55 |
# Get all permissions for the object |
| 56 |
all_perms = self.user.get_all_permissions(self.project) |
| 57 |
|
| 58 |
# Should include both user and group permissions |
| 59 |
self.assertIn("change_project", all_perms) |
| 60 |
self.assertIn("delete_project", all_perms) |
| 61 |
|
| 62 |
def test_backend_priority_with_model_backend(self): |
| 63 |
"""Test that both backends work together correctly""" |
| 64 |
# Our backend should handle object-level permissions |
| 65 |
assign_perm("change_project", self.user, self.project) |
| 66 |
|
| 67 |
# Check that the permission is detected |
| 68 |
self.assertTrue(self.user.has_perm("testapp.change_project", self.project)) |
| 69 |
|
| 70 |
# Verify it comes from our backend (not model-level permission) |
| 71 |
all_perms = self.user.get_all_permissions(self.project) |
| 72 |
self.assertIn("change_project", all_perms) |
| 73 |
|
| 74 |
def test_anonymous_user_support(self): |
| 75 |
"""Test that anonymous user support works correctly""" |
| 76 |
from django.contrib.auth.models import AnonymousUser |
| 77 |
|
| 78 |
anonymous = AnonymousUser() |
| 79 |
|
| 80 |
# Should not have group permissions |
| 81 |
group_perms = anonymous.get_group_permissions(self.project) |
| 82 |
self.assertEqual(set(), group_perms) |