简体   繁体   English

Python 模拟补丁 ldap3 搜索响应

[英]Python Mock patch ldap3 search response

I am trying to mock the below function but I'm not sure how to mock the Connection response:我正在尝试模拟以下 function 但我不确定如何模拟连接响应:

def get_user_res(user, pass):
    res = None
    server = Server('my_server')
    connnection = Connection(server, user, pass, strategy=SAFE_SYNC, auto_bind=True)
    if connection.bind():
        connection.search(search_base, search_filter, SUBTREE)
        res = connection.response
        connection.unbind()
    return res
@mock.patch("ldap3.Server")
@mock.patch("ldap3.Connection.response")
def test_get_user_res(mock_connection, mock_server):
    mock_connection.return_value = ""
    retrived_res = get_user_res("fake_user","fake_password")
    expected_res = ""
    assert retrived_res == expected_res

The root problem is that you're mocking the wrong things.根本问题是你mocking搞错了。 If you have a file named ldapclient.py that contains your get_user_rest method, like this (note that I've rewritten things a bit to make our lives easier when writing tests):如果您有一个名为ldapclient.py的文件,其中包含您的get_user_rest方法,如下所示(请注意,我已经重写了一些内容以使我们在编写测试时更轻松):

import ldap3

server = ldap3.Server('my_server')
search_base = 'dc=example, dc=com'


def get_user_res(user, password, search_filter=None):
    res = None

    connection = ldap3.Connection(
        server, user, password,
        client_strategy=ldap3.SAFE_SYNC, auto_bind=True)

    if connection.bind():
        res = connection.search(search_base, search_filter, ldap3.SUBTREE)
        connection.unbind()

    return res

Then what you need to mock is the ldap3.Connection class.那么你需要模拟的是ldap3.Connection class。 But since your test is in a different module, you'll need to call @mock.patch('ldapclient.ldap3.Connection) , assuming that your test is defined like this:但是由于您的测试位于不同的模块中,因此您需要调用@mock.patch('ldapclient.ldap3.Connection) ,假设您的测试定义如下:

import ldap3

from unittest import mock

import ldapclient


@mock.patch("ldapclient.ldap3.Connection")
def test_get_user_res(mock_connection_class):
    mock_connection = mock.Mock()
    mock_connection.search.return_value = 'fake_return'
    mock_connection_class.return_value = mock_connection

    retrived_res = ldapclient.get_user_res("fake_user", "fake_password")
    expected_res = "fake_return"
    assert retrived_res == expected_res

There are a few things to note here:这里有几点需要注意:

  1. As mentioned earlier, because we have import ldapclient , we need to mock ldapclient.ldap3.Connection .如前所述,因为我们有import ldapclient ,我们需要模拟ldapclient.ldap3.Connection
  2. We make the ldap3.Connection class return a new mock.Mock object, since we want to be able to mock methods on the object returned when calling connection = ldap3.Connection(...)我们让ldap3.Connection class 返回一个新的mock.Mock object,因为我们希望能够在调用connection = ldap3.Connection(...)
  3. We make the search method return a fake value so that we can ensure it gets called as expected.我们使search方法返回一个假值,以便我们可以确保它按预期被调用。

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

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