Skip to content

Commit b032b58

Browse files
committed
refactor tests
1 parent 47abd82 commit b032b58

File tree

3 files changed

+45
-50
lines changed

3 files changed

+45
-50
lines changed

pre_push_verifications.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ black bin/ tests/ hpecp/
1010
#flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
1111
flake8 --docstring-convention numpy bin/ hpecp/
1212

13-
flake8 --ignore=D,E501 tests/*.py # don't verify documentation in tests
13+
flake8 --ignore=D,E501 tests/cli_test.py # don't verify documentation in tests
1414

1515
tox -e py35 -- tests/
1616

tests/config_mock_api.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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.registerHttpPostHandler(
28+
url="https://127.0.0.1:8080/api/v2/config/auth",
29+
response=MockResponse(
30+
json_data={},
31+
status_code=200,
32+
headers={
33+
"location": (
34+
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
35+
)
36+
},
37+
),
38+
)

tests/config_test.py

+6-49
Original file line numberDiff line numberDiff line change
@@ -25,58 +25,15 @@
2525

2626
from hpecp import ContainerPlatformClient
2727

28+
from .base_test import BaseTestCase, MockResponse
29+
from .config_mock_api import mockApiSetup
2830

29-
class MockResponse:
30-
def __init__(
31-
self,
32-
json_data,
33-
status_code,
34-
headers,
35-
raise_for_status_flag=False,
36-
text_data="",
37-
):
38-
self.json_data = json_data
39-
self.text = text_data
40-
self.status_code = status_code
41-
self.raise_for_status_flag = raise_for_status_flag
42-
self.headers = headers
31+
# setup the mock data
32+
mockApiSetup()
4333

44-
def raise_for_status(self):
45-
if self.raise_for_status_flag:
46-
self.text = "some error occurred"
47-
raise requests.exceptions.HTTPError()
48-
else:
49-
return
5034

51-
def json(self):
52-
return self.json_data
53-
54-
55-
class TestTentants(TestCase):
56-
def mocked_requests_post(*args, **kwargs):
57-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
58-
return MockResponse(
59-
json_data={},
60-
status_code=200,
61-
headers={
62-
"location": (
63-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
64-
)
65-
},
66-
)
67-
elif args[0] == "https://127.0.0.1:8080/api/v2/config/auth":
68-
return MockResponse(
69-
json_data={},
70-
status_code=200,
71-
headers={
72-
"location": (
73-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
74-
)
75-
},
76-
)
77-
raise RuntimeError("Unhandle POST request: " + args[0])
78-
79-
@patch("requests.post", side_effect=mocked_requests_post)
35+
class TestTentants(BaseTestCase):
36+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
8037
def test_epic_tenant_list(self, mock_post):
8138

8239
client = ContainerPlatformClient(

0 commit comments

Comments
 (0)