I have the following function that I am unit testing:
class CustomDailyDownload():
....
def authenticate(self):
response = requests.post(self.finfo.extra['auth_url'], data=res, headers=headers)
token = response.json()['access_token']
return token
I would simply like to to mock out request.post
so that it returns a json dictionary with a key access_token
. So that in the next like token can be assigned properly.
I found this article semi helpful but couldn't get all the way: How can I mock requests and the response?.
Here is what I tried:
@mock.patch("<path_to_above_funcion>.requests")
def test_authenticate(mock_request):
responses.add(responses.POST, 'https://auth.cmauth2', json={'access_token': 'eyJhbGciOiJKV0duTHRYby1kWlo', 'token_type': 'Bearer', 'expires_in': 1799}, status=200)
finfo = MagicMock()
mock_request.post = requests.post('https://auth.cmauth2')
cme_ref_api = CustomDailyDownload(finfo)
assert cme_ref_api.authenticate()== 'eyJhbGciOiRYby1kWlo'
When I run the above, I get the error TypeError: 'Response' object is not callable
on the line: response = requests.post(self.finfo.extra['auth_url'], data=res, headers=headers)