简体   繁体   English

更改Django身份验证后端进行测试

[英]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). 我的Django站点使用LDAP后端在生产环境中进行身份验证,但这不适用于测试(无法创建来自虚拟用户的请求)。 How can I disable this backend, solely for tests? 如何仅针对测试禁用此后端?

Here is the relevant settings.py section: 这是相关的settings.py部分:

    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. 如果您只需要/想为某些测试禁用后端,则也可以使用override_settings装饰器。 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

Create an alternative settings file, for example myproj/test_settings.py , and specify that settings file when running unit tests. 创建一个备用设置文件,例如myproj/test_settings.py ,并在运行单元测试时指定该设置文件。

Write the alternative settings file like this: 像这样编写备用设置文件:

from myproj.settings import *

AUTHENTICATION_BACKENDS = (
        #'your.ldap.backend',
        'django.contrib.auth.backends.ModelBackend',
        )

That is, the settings inherits everything from your regular settings, but overrides the AUTHENTICATION_BACKENDS definition, with your LDAP backend commented out. 也就是说,这些设置会继承您常规设置的所有内容,但会覆盖AUTHENTICATION_BACKENDS定义,并注释掉LDAP后端。

Then, run your tests like this: 然后,像这样运行测试:

python manage.py test --settings=myproj.test_settings

For future reference, another option to look into for testing is to change the is_authenticated property of the User object to a lambda. 供将来参考,研究的另一种选择是将User对象的is_authenticated属性更改为lambda。 For example: 例如:

user = User(...)
user.is_authenticated = lambda: True

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM