Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Django Authentication Backend for Testing

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
   }
like image 332
Shaun Singh Avatar asked Jul 05 '13 21:07

Shaun Singh


1 Answers

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
like image 136
Mark van Lent Avatar answered Nov 05 '22 09:11

Mark van Lent