Python 模拟ldap调用始终返回True

Python 模拟ldap调用始终返回True,python,mocking,python-unittest,Python,Mocking,Python Unittest,我在登录函数中模拟了一个方法,所以我不使用lpdap连接。 现在我不能把mock设为false,他总是返回true。即使我插入了无效的凭据,它也总是返回一个令牌,而我希望得到返回消息“invalid credentials” 有什么帮助吗 登录功能: def login(): user = request.form['user'] passwd = request.form['passwd'] test = ldap.bind_user(user, passwd)

我在登录函数中模拟了一个方法,所以我不使用lpdap连接。 现在我不能把mock设为false,他总是返回true。即使我插入了无效的凭据,它也总是返回一个令牌,而我希望得到返回消息“invalid credentials” 有什么帮助吗

登录功能:

def login():

    user = request.form['user']
    passwd = request.form['passwd']

    test = ldap.bind_user(user, passwd)
    if test is None or passwd == '':
        response = jsonify(message='Invalid Credentials')
        return response ,401

    access_token = create_access_token(identity=user)
    return jsonify(access_token=access_token), 200
测试用例:


    #Test to check if we get a token when the user logs in
    @patch('dev_maintenance.active_directory.ldap.bind_user')
    def test_token_return(self, bind_user):

        bind_user.verify.return_value = True
        tester = app.test_client(self)
        with tester as client:

            response = client.post(
                '/login',
                data =dict(user="hermes", passwd ="hermes"),
                follow_redirects=True)
            self.assertIn(b'Invalid Credentials, response.data)
现在,如果我将这行更改为False:

bind_user.verify.return_value = False

它应该做这项工作,但它没有,它返回true,因此接受任何凭据

测试输出:

E           AssertionError: b'Invalid Credentials' not found in b'{\n  "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1Njk1MDU5OTQsIm5iZiI6MTU2OTUwNTk5NCwianRpIjoiOTg2YjQwNTYtODQyNC00ZDhjLTg2N2YtM2E3ODc4OWE4MTZjIiwiZXhwIjoxNTY5NTA2ODk0LCJpZGVudGl0eSI6Ik5vdXNlciIsImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyJ9.RFKW2mSmqHbLtX8jBlxj9w1waug1wAe0uExPtAMkhkg"\n}\n'


我通过将这一行添加到@patch:return\u value=None解决了这个问题 在这一行中,mock将重写为返回值none。 然后使用此行
bind\u user.verify.return\u value=False
我可以将返回设置为False