andrea_crotti andrea_crotti - 8 months ago 56
Python Question

Mocking python HTTP request with an Oauth1 certificate

I'm using the python response library to mock a call with requests, but I get this error:

File "/lib/python3.5/site-packages/requests/", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/lib/python3.5/site-packages/requests/", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/lib/python3.5/site-packages/requests/", line 474, in request
prep = self.prepare_request(req)
File "/lib/python3.5/site-packages/requests/", line 407, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/lib/python3.5/site-packages/requests/", line 306, in prepare
self.prepare_auth(auth, url)
File "/lib/python3.5/site-packages/requests/", line 518, in prepare_auth
r = auth(self)
File "/lib/python3.5/site-packages/requests_oauthlib/", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/", line 150, in get_oauth_signature
sig = self.SIGNATURE_METHODS[self.signature_method](base_string, self)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/", line 505, in sign_rsa_sha1_with_client
return sign_rsa_sha1(base_string, client.rsa_key)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/", line 497, in sign_rsa_sha1
key = _prepare_key_plus(alg, rsa_private_key)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/", line 574, in _prepare_key_plus
return alg.prepare_key(keystr)
File "/lib/python3.5/site-packages/jwt/", line 169, in prepare_key
key = load_pem_public_key(key, backend=default_backend())
File "/lib/python3.5/site-packages/cryptography/hazmat/primitives/", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/", line 312, in load_pem_public_key
return b.load_pem_public_key(data)
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/", line 1074, in load_pem_public_key
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/", line 1289, in _handle_key_loading_error
raise ValueError("Could not unserialize key data.")
ValueError: Could not unserialize key data

The code I try to mock is the following:

def function_to_test():
oauth = OAuth1(
return"", auth=oauth, cert="/path/to/certificate")

And the testing code:

def test_token_expired(self):
responses.add(responses.POST, url='',
response = function_to_test()
self.assertEqual(response.content, 'my_expected_result)

The test is failing with the error displayed above, does it mean that the responses mocking has not worked? Is it related to auth/cert parameters in my Am I missing something?

Answer Source

I think that your issue is not related to responses library, but to OAuth1 initialisation.

Before even trying to mock the request, OAuth1 fails because he is trying to access the key, which is not probably correctly initialised in your test.

You can either give a fake value for this key, on even mock the OAuth1 object, responses will then be able to mock your call.

We could have expected responses to completely mock the requests call, but it seems that some initialisation still happens before that.