如何正确测试基于pyramid_ldap3的应用程序?

时间:2018-02-21 15:31:10

标签: python pyramid

假设我有这个简化的应用程序,名为“Alambic”。我遗漏了auth策略的东西,此时我很好奇在pyramid_ldap3 auth的上下文中解决测试的最佳方法是什么。如果以下内容不够,我可以尝试使用最小的工作示例设置一个轻量级存储库(在这种情况下我将修改此帖子)。

__ INIT __ PY:

import ldap3
    from pyramid.config import Configurator

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.include('pyramid_ldap3')

    config.ldap_setup(
        'ldap://localhost',
        bind='uid=dummy,ou=people,dc=domain,dc=fr',
        passwd='passwd'
    )

    config.ldap_set_login_query(
        base_dn='ou=people,dc=domain,dc=fr',
        filter_tmpl='(uid=%(login)s)',
        scope=ldap3.LEVEL,
    )

    config.ldap_set_groups_query(
        base_dn='ou=people,dc=domain,dc=fr',
        filter_tmpl='(uid=%(userdn)s)',
        scope=ldap3.LEVEL,
        attributes=ldap3.ALL_ATTRIBUTES,
    )
    config.add_route('home', '/')  # No auth
    config.add_route('menu', '/menu')  # Needs LDAP auth
    config.scan()
    return config.make_wsgi_app()

views.py:

from pyramid.view import view_config
@view_config(route_name='menu', permission='auth')
def menu(request):
    ...some code...

现在我知道了pyramid-ldap-test,很高兴有一个本地服务器可以连接,但是我一直试图让我的unittest设置。获得db测试是好的(所以我把它留在这里),但由于某种原因,我无法在我的测试中注册ldap设置。我使用testing.DummyRequest,但由于某种原因,即使正确处理它们,所有LDAP数据/设置也都无法保持。我最终得到一个ConfigurationError:pyramid.exceptions.ConfigurationError:您必须在安装过程中调用Configurator.ldap_setup以使用LDAP连接器。

这就是我的tests.py看起来的样子:

import unittest
import transaction
from pyramid import testing
import ldap3
from ldap_test import LdapServer    
server = LdapServer(java_delay=1)


class BaseTest(unittest.TestCase):
    def setUp(self):
        self.config = testing.setUp(settings={})
        self.config.include('pyramid_ldap3')
        self.config.add_route('home', '/')  # No auth
        self.config.add_route('menu', '/menu')  # Needs LDAP auth

        self.config.ldap_setup(
            'ldap://localhost',
            bind='uid=dummy,ou=people,dc=domain,dc=fr',
            passwd='passwd'
        )

        self.config.ldap_set_login_query(
            base_dn='ou=people,dc=domain,dc=fr',
            filter_tmpl='(uid=%(login)s)',
            scope=ldap3.LEVEL,
        )

        self.config.ldap_set_groups_query(
            base_dn='ou=people,dc=domain,dc=fr',
            filter_tmpl='(uid=%(userdn)s)',
            scope=ldap3.LEVEL,
            attributes=ldap3.ALL_ATTRIBUTES,
        )
        settings = self.config.get_settings()
        server.start()

    def tearDown(self):

        server.stop()
        testing.tearDown()
        transaction.abort()


class TestHomeSuccessCondition(BaseTest):

    def setUp(self):
        super(TestHomeSuccessCondition, self).setUp()

    def test_home_view(self):
        from .views import home
        info = home(testing.DummyRequest())
        self.assertEqual(info['project'], 'Alambic')

    def test_menu_view(self):
        from .views import menu
        info = menu(testing.DummyRequest())
        self.assertEqual(info['project'], 'Alambic')

0 个答案:

没有答案