My Django site uses the LDAP backend for authentication in production, but this is not suitable for testing (impossible to create requests from dummy users). How can I disable this backend, solely for tests?
Here is the relevant settings.py section:
    AUTHENTICATION_BACKENDS = (
#'crowd.backend.CrowdBackend',
# 'django_auth_ldap.backend.LDAPBackend',
'django.contrib.auth.backends.ModelBackend',
    )
   AUTH_LDAP_SERVER_URI = "ldap://ldap.cablelabs.com"
   import ldap
   from django_auth_ldap.config import LDAPSearch
   AUTH_LDAP_BIND_DN = "CN=CableLabs  Internal,OU=cabletest,OU=Teamwork,OU=community,DC=cablelabs,DC=com"
   AUTH_LDAP_BIND_PASSWORD = "UAq,0@ki"
   AUTH_LDAP_USER_SEARCH = LDAPSearch("ou=community,dc=cablelabs,dc=com",ldap.SCOPE_SUBTREE, "(sAMAccountName=%(user)s)")
   AUTH_LDAP_USER_ATTR_MAP = {"first_name": "givenName", "last_name": "sn","username":"sAMAccountName","email":"mail","photo":"thumbnailPhoto"} 
   AUTH_LDAP_CONNECTION_OPTIONS = {
     ldap.OPT_REFERRALS: 0
   }
                If you only need/want to disable the backend for certain tests, you can also use the override_settings decorator. You can use this decorator on the test case class:
from django.test.utils import override_settings
@override_settings(AUTHENTICATION_BACKENDS=
                   ('django.contrib.auth.backends.ModelBackend',))
class FooTest(TestCase):
    def test_bar(self):
        pass
But you can also selectively use it on a test method:
from django.test.utils import override_settings
class FooTest(TestCase):
    @override_settings(AUTHENTICATION_BACKENDS=
                       ('django.contrib.auth.backends.ModelBackend',))
    def test_bar(self):
        pass
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With