diff --git a/hyperwallet/tests/resources/private-jwkset1-invalid b/hyperwallet/tests/resources/private-jwkset1-invalid new file mode 100644 index 0000000..c545551 --- /dev/null +++ b/hyperwallet/tests/resources/private-jwkset1-invalid @@ -0,0 +1 @@ +invalid jwkset \ No newline at end of file diff --git a/hyperwallet/tests/test_encryption.py b/hyperwallet/tests/test_encryption.py index 0265a68..f87e50b 100644 --- a/hyperwallet/tests/test_encryption.py +++ b/hyperwallet/tests/test_encryption.py @@ -4,6 +4,7 @@ import time import json import os.path +import mock from jwcrypto import jwk, jws as cryptoJWS from jwcrypto.common import json_encode @@ -177,6 +178,42 @@ def __getJwkKeySet(self, location): else: raise HyperwalletException('Wrong JWK key set location path = ' + location) + def test_should_throw_exception_when_jwk_set_file_has_invalid_json_format(self): + + localDir = os.path.abspath(os.path.dirname(__file__)) + clientPath = os.path.join(localDir, 'resources', 'private-jwkset1-invalid') + hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1') + encryption = Encryption(clientPath, hyperwalletPath) + + with self.assertRaises(HyperwalletException) as exc: + encryption.encrypt('testMessage') + + self.assertEqual(exc.exception.message, 'Wrong JWK key set invalid jwkset') + + @mock.patch('requests.Session.request') + def test_should_throw_exception_when_jwk_set_file_retrieved_from_url_is_invalid(self, session_mock): + + data = { + 'key': 'value' + } + + session_mock.return_value = mock.MagicMock( + status_code=200, + content=data, + headers={ + "Content-Type": "application/json" + } + ) + + localDir = os.path.abspath(os.path.dirname(__file__)) + hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1') + encryption = Encryption('https://api.sandbox.hyperwallet.com/', hyperwalletPath) + + with self.assertRaises(TypeError) as exc: + encryption.encrypt('testMessage') + + self.assertEqual(exc.exception.message, 'expected string or buffer') + def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm): ''' Finds JWK key by given algorithm. diff --git a/hyperwallet/utils/apiclient.py b/hyperwallet/utils/apiclient.py index 4fcfcad..af0e9e9 100644 --- a/hyperwallet/utils/apiclient.py +++ b/hyperwallet/utils/apiclient.py @@ -203,8 +203,6 @@ def __checkResponseHeaderContentType(self, response): Response to be checked. **REQUIRED** ''' - if response is None: - return contentType = response.headers['Content-Type'] if (not self.encrypted and contentType != 'application/json') or (self.encrypted and contentType != 'application/jose+json'): raise HyperwalletAPIException('Invalid Content-Type specified in Response Header') diff --git a/hyperwallet/utils/encryption.py b/hyperwallet/utils/encryption.py index bb8515d..221203a 100644 --- a/hyperwallet/utils/encryption.py +++ b/hyperwallet/utils/encryption.py @@ -145,7 +145,7 @@ def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm): try: keySet = json.loads(jwkKeySet) except ValueError: - raise HyperwalletException('Wrong JWK key set' + jwkKeySet) + raise HyperwalletException('Wrong JWK key set ' + jwkKeySet) for key in keySet['keys']: if key['alg'] == algorithm: