Skip to content

Commit 9022dfc

Browse files
committed
refactor more tests
Signed-off-by: Chris Snow <[email protected]>
1 parent 92d5e4a commit 9022dfc

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed
File renamed without changes.

tests/role_mock_api_responses.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# (C) Copyright [2020] Hewlett Packard Enterprise Development LP
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a
4+
# copy of this software and associated documentation files (the "Software"),
5+
# to deal in the Software without restriction, including without limitation
6+
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
7+
# and/or sell copies of the Software, and to permit persons to whom the
8+
# Software is furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included
11+
# in all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
17+
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
18+
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
19+
# OTHER DEALINGS IN THE SOFTWARE.
20+
21+
22+
from .base_test import BaseTestCase, MockResponse
23+
24+
25+
def mockApiSetup():
26+
27+
BaseTestCase.registerHttpGetHandler(
28+
url="https://127.0.0.1:8080/api/v1/role/1",
29+
response=MockResponse(
30+
json_data={
31+
"_links": {
32+
"self": {"href": "/api/v1/role/1"},
33+
"all_roles": {"href": "/api/v1/role"},
34+
},
35+
"label": {
36+
"name": "Site Admin",
37+
"description": "Role for Site Admin",
38+
},
39+
},
40+
status_code=200,
41+
headers={},
42+
),
43+
)

tests/role_test.py

Lines changed: 11 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -33,37 +33,15 @@
3333
get_client,
3434
session_mock_response,
3535
)
36+
from .role_mock_api_responses import mockApiSetup
3637

37-
38-
# pylint: disable=no-method-argument
39-
def mocked_requests_get(*args, **kwargs):
40-
if args[0] == "https://127.0.0.1:8080/api/v1/role/1":
41-
return MockResponse(
42-
json_data={
43-
"_links": {
44-
"self": {"href": "/api/v1/role/1"},
45-
"all_roles": {"href": "/api/v1/role"},
46-
},
47-
"label": {
48-
"name": "Site Admin",
49-
"description": "Role for Site Admin",
50-
},
51-
},
52-
status_code=200,
53-
headers={},
54-
)
55-
raise RuntimeError("Unhandle GET request: " + args[0])
56-
57-
58-
def mocked_requests_post(*args, **kwargs):
59-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
60-
return session_mock_response()
61-
raise RuntimeError("Unhandle POST request: " + args[0])
38+
# setup the mock data
39+
mockApiSetup()
6240

6341

6442
class TestRoleGet(TestCase):
65-
@patch("requests.get", side_effect=mocked_requests_get)
66-
@patch("requests.post", side_effect=mocked_requests_post)
43+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
44+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
6745
def test_get_role_assertions(self, mock_get, mock_post):
6846

6947
with self.assertRaisesRegexp(
@@ -77,8 +55,8 @@ def test_get_role_assertions(self, mock_get, mock_post):
7755
):
7856
get_client().role.get("garbage")
7957

80-
@patch("requests.get", side_effect=mocked_requests_get)
81-
@patch("requests.post", side_effect=mocked_requests_post)
58+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
59+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
8260
def test_get_role(self, mock_get, mock_post):
8361

8462
role = get_client().role.get("/api/v1/role/1")
@@ -89,8 +67,8 @@ def test_get_role(self, mock_get, mock_post):
8967

9068

9169
class TestCLI(BaseTestCase):
92-
@patch("requests.get", side_effect=mocked_requests_get)
93-
@patch("requests.post", side_effect=mocked_requests_post)
70+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
71+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
9472
def test_get(self, mock_post, mock_delete):
9573

9674
try:
@@ -121,8 +99,8 @@ def test_get(self, mock_post, mock_delete):
12199
if six.PY2:
122100
self.assertEqual(stderr, expected_stderr)
123101

124-
@patch("requests.get", side_effect=mocked_requests_get)
125-
@patch("requests.post", side_effect=mocked_requests_post)
102+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
103+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
126104
def test_get_json(self, mock_post, mock_delete):
127105

128106
try:

0 commit comments

Comments
 (0)