From 185744225a0afa5d34c8d71fc517a84dd3f1f1eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Freitag?= Date: Thu, 10 Oct 2024 10:30:56 +0200 Subject: [PATCH] Allow importing the backend without loading apps Make it possible to monkey-patch the backend from the settings. --- django_auth_ldap/backend.py | 5 ++++- tests/import_test_without_django.py | 16 ++++++++++++++++ tox.ini | 4 +++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 tests/import_test_without_django.py diff --git a/django_auth_ldap/backend.py b/django_auth_ldap/backend.py index c63b6cec..4356d79c 100644 --- a/django_auth_ldap/backend.py +++ b/django_auth_ldap/backend.py @@ -51,7 +51,6 @@ import django.dispatch import ldap from django.contrib.auth import get_user_model -from django.contrib.auth.models import Group, Permission from django.core.cache import cache from django.core.exceptions import ImproperlyConfigured, ObjectDoesNotExist @@ -777,6 +776,8 @@ def _mirror_groups(self): Mirrors the user's LDAP groups in the Django database and updates the user's membership. """ + from django.contrib.auth.models import Group + try: target_group_names = frozenset(self._get_groups().get_group_names()) except ldap.LDAPError as e: @@ -833,6 +834,8 @@ def _load_group_permissions(self): Populates self._group_permissions based on LDAP group membership and Django group permissions. """ + from django.contrib.auth.models import Permission + group_names = self._get_groups().get_group_names() perms = Permission.objects.filter(group__name__in=group_names) diff --git a/tests/import_test_without_django.py b/tests/import_test_without_django.py new file mode 100644 index 00000000..0fbdc9b4 --- /dev/null +++ b/tests/import_test_without_django.py @@ -0,0 +1,16 @@ +import os +from unittest import TestCase + + +class TestLoading(TestCase): + def test_django_not_ready(self): + orig_env = os.environ.copy() + + def reset_env(): + os.environ = orig_env + + self.addCleanup(reset_env) + + os.environ["DJANGO_SETTINGS_MODULE"] = "tests.settings" + + import django_auth_ldap.backend # noqa: F401 diff --git a/tox.ini b/tox.ini index 87ffa51a..f7226111 100644 --- a/tox.ini +++ b/tox.ini @@ -18,7 +18,9 @@ python = 3.13 = django{42,50,51,main} [testenv] -commands = {envpython} -Wa -b -m django test --settings tests.settings +commands = + {envpython} -Wa -b -m django test --settings tests.settings + {envpython} -Wa -b -m unittest discover --pattern *_test_without_django.py deps = django42: Django>=4.2,<4.3 django50: Django>=5.0,<5.1 pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy