Skip to content

Commit 60db519

Browse files
committed
Added apiclient tests with encryption, updated changelog version
1 parent bdf5f2a commit 60db519

2 files changed

Lines changed: 137 additions & 1 deletion

File tree

CHANGELOG.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
Changelog
22
=========
33

4-
1.2.1 (current)
4+
1.2.2 (current)
55
------------------
66

7+
1.2.1 (2019-01-17)
8+
------------------
9+
10+
- FIX: Resolved issue with restricted "Accept" & "Content-Type" headers to support only "application/json" or "application/jose+json"
711

812
1.2.0 (2018-12-18)
913
------------------

hyperwallet/tests/test_client.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import mock
44
import json
55
import unittest
6+
import os.path
67

78
from hyperwallet.utils import ApiClient
89
from hyperwallet.config import SERVER
910
from hyperwallet.exceptions import HyperwalletAPIException
11+
from hyperwallet.utils.encryption import Encryption
1012

1113

1214
class ApiClientTest(unittest.TestCase):
@@ -19,6 +21,17 @@ def setUp(self):
1921
SERVER
2022
)
2123

24+
localDir = os.path.abspath(os.path.dirname(__file__))
25+
clientPath = os.path.join(localDir, 'resources', 'private-jwkset1')
26+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
27+
28+
self.clientWithEncryption = ApiClient(
29+
'test-user',
30+
'test-pass',
31+
SERVER,
32+
{'clientPrivateKeySetLocation': clientPath, 'hyperwalletKeySetLocation': hyperwalletPath}
33+
)
34+
2235
def test_failed_connection(self):
2336

2437
with self.assertRaises(HyperwalletAPIException) as exc:
@@ -120,6 +133,125 @@ def test_receive_valid_json_response(self, session_mock):
120133
json.loads(encoded)
121134
)
122135

136+
@mock.patch('requests.Session.request')
137+
def test_request_with_encryption_successful(self, session_mock):
138+
139+
data = {
140+
'key': 'value'
141+
}
142+
143+
localDir = os.path.abspath(os.path.dirname(__file__))
144+
clientPath = os.path.join(localDir, 'resources', 'private-jwkset1')
145+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
146+
encryption = Encryption(clientPath, hyperwalletPath)
147+
encryptedMessage = encryption.encrypt(json.dumps(data))
148+
149+
session_mock.return_value = mock.MagicMock(
150+
status_code=200,
151+
content=encryptedMessage,
152+
headers={
153+
"Content-Type": "application/jose+json"
154+
}
155+
)
156+
157+
encoded = json.dumps(data)
158+
if hasattr(encoded, 'decode'): # Python 2
159+
encoded = encoded.decode('utf-8')
160+
161+
self.assertEqual(
162+
self.clientWithEncryption._makeRequest(),
163+
json.loads(encoded)
164+
)
165+
166+
@mock.patch('requests.Session.request')
167+
def test_request_with_encryption_when_content_type_contains_charset(self, session_mock):
168+
169+
data = {
170+
'key': 'value'
171+
}
172+
173+
localDir = os.path.abspath(os.path.dirname(__file__))
174+
clientPath = os.path.join(localDir, 'resources', 'private-jwkset1')
175+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
176+
encryption = Encryption(clientPath, hyperwalletPath)
177+
encryptedMessage = encryption.encrypt(json.dumps(data))
178+
179+
session_mock.return_value = mock.MagicMock(
180+
status_code=200,
181+
content=encryptedMessage,
182+
headers={
183+
"Content-Type": "application/jose+json;charset=utf-8"
184+
}
185+
)
186+
187+
encoded = json.dumps(data)
188+
if hasattr(encoded, 'decode'): # Python 2
189+
encoded = encoded.decode('utf-8')
190+
191+
self.assertEqual(
192+
self.clientWithEncryption._makeRequest(),
193+
json.loads(encoded)
194+
)
195+
196+
@mock.patch('requests.Session.request')
197+
def test_request_with_encryption_when_content_type_contains_charset_ahead(self, session_mock):
198+
199+
data = {
200+
'key': 'value'
201+
}
202+
203+
localDir = os.path.abspath(os.path.dirname(__file__))
204+
clientPath = os.path.join(localDir, 'resources', 'private-jwkset1')
205+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
206+
encryption = Encryption(clientPath, hyperwalletPath)
207+
encryptedMessage = encryption.encrypt(json.dumps(data))
208+
209+
session_mock.return_value = mock.MagicMock(
210+
status_code=200,
211+
content=encryptedMessage,
212+
headers={
213+
"Content-Type": "charset=utf-8;application/jose+json"
214+
}
215+
)
216+
217+
encoded = json.dumps(data)
218+
if hasattr(encoded, 'decode'): # Python 2
219+
encoded = encoded.decode('utf-8')
220+
221+
self.assertEqual(
222+
self.clientWithEncryption._makeRequest(),
223+
json.loads(encoded)
224+
)
225+
226+
@mock.patch('requests.Session.request')
227+
def test_request_with_encryption_when_response_content_type_is_not_valid(self, session_mock):
228+
229+
data = {
230+
'key': 'value'
231+
}
232+
233+
localDir = os.path.abspath(os.path.dirname(__file__))
234+
clientPath = os.path.join(localDir, 'resources', 'private-jwkset1')
235+
hyperwalletPath = os.path.join(localDir, 'resources', 'public-jwkset1')
236+
encryption = Encryption(clientPath, hyperwalletPath)
237+
encryptedMessage = encryption.encrypt(json.dumps(data))
238+
239+
session_mock.return_value = mock.MagicMock(
240+
status_code=200,
241+
content=encryptedMessage,
242+
headers={
243+
"Content-Type": "wrongContentType"
244+
}
245+
)
246+
247+
with self.assertRaises(HyperwalletAPIException) as exc:
248+
self.clientWithEncryption._makeRequest()
249+
250+
self.assertEqual(
251+
exc.exception.message,
252+
'Invalid Content-Type specified in Response Header'
253+
)
254+
123255
@mock.patch('requests.Session.request')
124256
def test_receive_valid_json_response_when_content_type_contains_charset(self, session_mock):
125257

0 commit comments

Comments
 (0)