32
32
from hpecp .gateway import Gateway
33
33
34
34
from .base_test import BaseTestCase , MockResponse
35
- from .base_test import session_mock_response as base_login_post_response
35
+ from .cli_mock_api_responses import mockApiSetup
36
+
37
+ # setup the mock data
38
+ mockApiSetup ()
39
+
36
40
37
41
if six .PY2 :
38
42
# from io import BytesIO as StringIO # noqa: F811
@@ -204,7 +208,7 @@ def test_configure_cli_reads_hpecp_conf_user_provided_profile(self):
204
208
205
209
206
210
class TestBaseProxy (BaseTestCase ):
207
- @patch ("requests.post" , side_effect = base_login_post_response )
211
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
208
212
def test_list_with_invalid_column (self , mock_post ):
209
213
210
214
with self .assertRaises (SystemExit ) as cm :
@@ -223,7 +227,7 @@ def test_list_with_invalid_column(self, mock_post):
223
227
224
228
self .assertEqual (cm .exception .code , 1 )
225
229
226
- @patch ("requests.post" , side_effect = base_login_post_response )
230
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
227
231
def test_list_with_invalid_columns_list (self , mock_post ):
228
232
229
233
with self .assertRaises (SystemExit ) as cm :
@@ -242,7 +246,7 @@ def test_list_with_invalid_columns_list(self, mock_post):
242
246
243
247
self .assertEqual (cm .exception .code , 1 )
244
248
245
- @patch ("requests.post" , side_effect = base_login_post_response )
249
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
246
250
def test_list_with_invalid_output_param (self , mock_post ):
247
251
248
252
with self .assertRaises (SystemExit ) as cm :
@@ -289,21 +293,6 @@ def test_hpe_config_file_var(self):
289
293
290
294
291
295
class TestCLIHttpClient (BaseTestCase ):
292
- def mocked_requests_post (* args , ** kwargs ):
293
- if args [0 ] == "https://127.0.0.1:8080/api/v1/login" :
294
- return base_login_post_response ()
295
- raise RuntimeError ("Unhandle POST request: " + args [0 ])
296
-
297
- def mocked_requests_get (* args , ** kwargs ):
298
- if args [0 ] == "https://127.0.0.1:8080/some/url" :
299
- return MockResponse (
300
- json_data = {"foo" : "bar" },
301
- text_data = '{"foo":"bar"}' ,
302
- status_code = 200 ,
303
- headers = dict (),
304
- )
305
- raise RuntimeError ("Unhandle GET request: " + args [0 ])
306
-
307
296
def mocked_requests_failed_login (* args , ** kwargs ):
308
297
if args [0 ] == "https://127.0.0.1:8080/api/v1/login" :
309
298
return MockResponse (
@@ -315,7 +304,7 @@ def mocked_requests_failed_login(*args, **kwargs):
315
304
)
316
305
raise RuntimeError ("Unhandle POST request: " + args [0 ])
317
306
318
- @patch ("requests.get" , side_effect = mocked_requests_get )
307
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
319
308
@patch ("requests.post" , side_effect = mocked_requests_failed_login )
320
309
def test_get_failed_login (self , mock_get , mock_post ):
321
310
@@ -338,8 +327,8 @@ def test_get_failed_login(self, mock_get, mock_post):
338
327
"Expected: `{}` Actual: `{}`" .format (expected_err , actual_err ),
339
328
)
340
329
341
- @patch ("requests.get" , side_effect = mocked_requests_get )
342
- @patch ("requests.post" , side_effect = mocked_requests_post )
330
+ @patch ("requests.get" , side_effect = BaseTestCase . httpGetHandlers )
331
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
343
332
def test_get (self , mock_get , mock_post ):
344
333
345
334
hpecp = self .cli .CLI ()
@@ -358,7 +347,7 @@ def mocked_requests_delete(*args, **kwargs):
358
347
raise RuntimeError ("Unhandle DELETE request: " + args [0 ])
359
348
360
349
@patch ("requests.delete" , side_effect = mocked_requests_delete )
361
- @patch ("requests.post" , side_effect = mocked_requests_post )
350
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
362
351
def test_delete (self , mock_delete , mock_post ):
363
352
364
353
hpecp = self .cli .CLI ()
@@ -367,20 +356,9 @@ def test_delete(self, mock_delete, mock_post):
367
356
self .assertEqual (self .out .getvalue (), "" )
368
357
369
358
def test_post (self ):
370
- def mocked_requests_post (* args , ** kwargs ):
371
- if args [0 ] == "https://127.0.0.1:8080/api/v1/login" :
372
- return base_login_post_response ()
373
- if args [0 ] == "https://127.0.0.1:8080/some/url" :
374
- return MockResponse (
375
- text_data = {"mock_data" : True },
376
- json_data = {},
377
- status_code = 200 ,
378
- headers = {},
379
- )
380
- raise RuntimeError ("Unhandle POST request: " + args [0 ])
381
359
382
360
with patch ("requests.post" ) as mock_requests :
383
- mock_requests .side_effect = mocked_requests_post
361
+ mock_requests .side_effect = BaseTestCase . httpPostHandlers
384
362
385
363
with tempfile .NamedTemporaryFile () as json_file :
386
364
json_file .write (json .dumps ({"abc" : "def" }).encode ("utf-8" ))
@@ -415,20 +393,11 @@ def mocked_requests_post(*args, **kwargs):
415
393
if six .PY2 :
416
394
self .assertEqual (stderr , expected_stderr )
417
395
418
- @patch ("requests.post" , side_effect = mocked_requests_post )
396
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
419
397
def test_put (self , mock_post ):
420
- def mocked_requests_put (* args , ** kwargs ):
421
- if args [0 ] == "https://127.0.0.1:8080/some/url" :
422
- return MockResponse (
423
- text_data = {"mock_data" : True },
424
- json_data = {},
425
- status_code = 200 ,
426
- headers = {},
427
- )
428
- raise RuntimeError ("Unhandle PUT request: " + args [0 ])
429
398
430
399
with patch ("requests.put" ) as mock_requests :
431
- mock_requests .side_effect = mocked_requests_put
400
+ mock_requests .side_effect = BaseTestCase . httpPutHandlers
432
401
433
402
with tempfile .NamedTemporaryFile () as json_file :
434
403
json_file .write (json .dumps ({"abc" : "def" }).encode ("utf-8" ))
0 commit comments