Skip to content

Commit 979ef94

Browse files
author
Peter Joseph Olamit
authored
Merge pull request #13 from akreisman-epam/fix-tests-coverage-encryption
Fix tests coverage
2 parents ace079b + 44f99cd commit 979ef94

4 files changed

Lines changed: 39 additions & 3 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
invalid jwkset

hyperwallet/tests/test_encryption.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import time
55
import json
66
import os.path
7+
import mock
78

89
from jwcrypto import jwk, jws as cryptoJWS
910
from jwcrypto.common import json_encode
@@ -177,6 +178,42 @@ def __getJwkKeySet(self, location):
177178
else:
178179
raise HyperwalletException('Wrong JWK key set location path = ' + location)
179180

181+
def test_should_throw_exception_when_jwk_set_file_has_invalid_json_format(self):
182+
183+
localDir = os.path.abspath(os.path.dirname(__file__))
184+
clientPath = os.path.join(localDir, 'resources', 'private-jwkset1-invalid')
185+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
186+
encryption = Encryption(clientPath, hyperwalletPath)
187+
188+
with self.assertRaises(HyperwalletException) as exc:
189+
encryption.encrypt('testMessage')
190+
191+
self.assertEqual(exc.exception.message, 'Wrong JWK key set invalid jwkset')
192+
193+
@mock.patch('requests.Session.request')
194+
def test_should_throw_exception_when_jwk_set_file_retrieved_from_url_is_invalid(self, session_mock):
195+
196+
data = {
197+
'key': 'value'
198+
}
199+
200+
session_mock.return_value = mock.MagicMock(
201+
status_code=200,
202+
content=data,
203+
headers={
204+
"Content-Type": "application/json"
205+
}
206+
)
207+
208+
localDir = os.path.abspath(os.path.dirname(__file__))
209+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
210+
encryption = Encryption('https://api.sandbox.hyperwallet.com/', hyperwalletPath)
211+
212+
with self.assertRaises(TypeError) as exc:
213+
encryption.encrypt('testMessage')
214+
215+
self.assertEqual(exc.exception.message, 'expected string or buffer')
216+
180217
def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm):
181218
'''
182219
Finds JWK key by given algorithm.

hyperwallet/utils/apiclient.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ def __checkResponseHeaderContentType(self, response):
203203
Response to be checked. **REQUIRED**
204204
'''
205205

206-
if response is None:
207-
return
208206
contentType = response.headers['Content-Type']
209207
if (not self.encrypted and contentType != 'application/json') or (self.encrypted and contentType != 'application/jose+json'):
210208
raise HyperwalletAPIException('Invalid Content-Type specified in Response Header')

hyperwallet/utils/encryption.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm):
145145
try:
146146
keySet = json.loads(jwkKeySet)
147147
except ValueError:
148-
raise HyperwalletException('Wrong JWK key set' + jwkKeySet)
148+
raise HyperwalletException('Wrong JWK key set ' + jwkKeySet)
149149

150150
for key in keySet['keys']:
151151
if key['alg'] == algorithm:

0 commit comments

Comments
 (0)