Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions hyperwallet/tests/resources/private-jwkset1-invalid
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invalid jwkset
37 changes: 37 additions & 0 deletions hyperwallet/tests/test_encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions hyperwallet/utils/apiclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
2 changes: 1 addition & 1 deletion hyperwallet/utils/encryption.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down