I mocked a method inside my login function so i don't use my lpdap connection. Now i can't set the mock to false, he always returns true. Even if i insert invalid credentials it always returns me a token, while i want to get the return message "invalid credentials" Any help?
Login function:
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
The test case:
#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)
Now if i changed this line to False:
bind_user.verify.return_value = False
It should do the work, but it doesn't, it returns true and therefore accepts any credentials.
test output:
E AssertionError: b'Invalid Credentials' not found in b'{\n "access_token": "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpYXQiOjE1Njk1MDU5OTQsIm5iZiI6MTU2OTUwNTk5NCwianRpIjoiOTg2YjQwNTYtODQyNC00ZDhjLTg2N2YtM2E3ODc4OWE4MTZjIiwiZXhwIjoxNTY5NTA2ODk0LCJpZGVudGl0eSI6Ik5vdXNlciIsImZyZXNoIjpmYWxzZSwidHlwZSI6ImFjY2VzcyJ9.RFKW2mSmqHbLtX8jBlxj9w1waug1wAe0uExPtAMkhkg"\n}\n'
I solved it by adding this line to the @patch: return_value=None With this line the mock it's override to return value none. Then with this line bind_user.verify.return_value = False
I can set my return to False.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.