|
4 | 4 | import time |
5 | 5 | import json |
6 | 6 | import os.path |
| 7 | +import mock |
7 | 8 |
|
8 | 9 | from jwcrypto import jwk, jws as cryptoJWS |
9 | 10 | from jwcrypto.common import json_encode |
@@ -177,6 +178,42 @@ def __getJwkKeySet(self, location): |
177 | 178 | else: |
178 | 179 | raise HyperwalletException('Wrong JWK key set location path = ' + location) |
179 | 180 |
|
| 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 | + |
180 | 217 | def __findJwkKeyByAlgorithm(self, jwkKeySet, algorithm): |
181 | 218 | ''' |
182 | 219 | Finds JWK key by given algorithm. |
|
0 commit comments