andrea_crotti andrea_crotti - 12 days ago 9
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/api.py", line 110, in post
return request('post', url, data=data, json=json, **kwargs)
File "/lib/python3.5/site-packages/requests/api.py", line 56, in request
return session.request(method=method, url=url, **kwargs)
File "/lib/python3.5/site-packages/requests/sessions.py", line 474, in request
prep = self.prepare_request(req)
File "/lib/python3.5/site-packages/requests/sessions.py", line 407, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/lib/python3.5/site-packages/requests/models.py", line 306, in prepare
self.prepare_auth(auth, url)
File "/lib/python3.5/site-packages/requests/models.py", line 518, in prepare_auth
r = auth(self)
File "/lib/python3.5/site-packages/requests_oauthlib/oauth1_auth.py", line 88, in __call__
unicode(r.url), unicode(r.method), None, r.headers)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/__init__.py", line 313, in sign
('oauth_signature', self.get_oauth_signature(request)))
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/__init__.py", 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/signature.py", 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/signature.py", line 497, in sign_rsa_sha1
key = _prepare_key_plus(alg, rsa_private_key)
File "/lib/python3.5/site-packages/oauthlib/oauth1/rfc5849/signature.py", line 574, in _prepare_key_plus
return alg.prepare_key(keystr)
File "/lib/python3.5/site-packages/jwt/algorithms.py", line 169, in prepare_key
key = load_pem_public_key(key, backend=default_backend())
File "/lib/python3.5/site-packages/cryptography/hazmat/primitives/serialization.py", line 24, in load_pem_public_key
return backend.load_pem_public_key(data)
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/multibackend.py", line 312, in load_pem_public_key
return b.load_pem_public_key(data)
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", line 1074, in load_pem_public_key
self._handle_key_loading_error()
File "/lib/python3.5/site-packages/cryptography/hazmat/backends/openssl/backend.py", 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(
self.consumer_key,
client_secret=self.consumer_secret,
resource_owner_key=self.oauth_token,
resource_owner_secret=self.oauth_token_secret,
rsa_key=self.rsa_key,
signature_method=self._signature_method
)
return requests.post(url="https://example.com", auth=oauth, cert="/path/to/certificate")


And the testing code:

@responses.activate
def test_token_expired(self):
responses.add(responses.POST, url='https://example.com/',
body='my_expected_result',
status=200)
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 requests.post? Am I missing something?

Answer

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.