Skip to content

Commit 4e244d9

Browse files
committed
refactor more tests
1 parent f6b06af commit 4e244d9

File tree

3 files changed

+100
-150
lines changed

3 files changed

+100
-150
lines changed

pre_push_verifications.sh

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

12-
flake8 --ignore=D,E501 tests/catalog_test.py tests/base_test.py
12+
flake8 --ignore=D,E501 tests/catalog_test.py tests/base_test.py tests/cli_test.py
1313

1414
tox -e py35 -- tests/
1515

tests/cli_test.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
from textwrap import dedent
2626
from unittest import TestCase
2727

28-
import requests
2928
import six
3029
from mock import mock, mock_open, patch
3130

@@ -36,10 +35,10 @@
3635
from .base_test import session_mock_response as base_login_post_response
3736

3837
if six.PY2:
39-
from io import BytesIO as StringIO # noqa: F811
38+
# from io import BytesIO as StringIO # noqa: F811
4039
from test.test_support import EnvironmentVarGuard
4140
else:
42-
from io import StringIO
41+
# from io import StringIO
4342
from test.support import EnvironmentVarGuard
4443

4544
try:
@@ -151,11 +150,11 @@ def test_autocomplete_bash(self):
151150

152151
class TestCLIConfig(TestCase):
153152
def setUp(self):
154-
try:
155-
reload
156-
except NameError:
157-
# Python 3
158-
from imp import reload
153+
# try:
154+
# reload
155+
# except NameError:
156+
# # Python 3
157+
# from imp import reload
159158

160159
sys.path.insert(0, os.path.abspath("../../"))
161160

@@ -174,7 +173,7 @@ def test_configure_cli_reads_hpecp_conf_user_provided_profile(self):
174173
warn_ssl = True
175174
username = admin
176175
password = admin123
177-
176+
178177
[tenant1]
179178
api_host = tenant_mock_host
180179
username = tenant-admin

tests/user_test.py

+91-140
Original file line numberDiff line numberDiff line change
@@ -26,93 +26,96 @@
2626
from hpecp import ContainerPlatformClient
2727
from hpecp.exceptions import APIItemNotFoundException
2828

29-
from .base_test import BaseTestCase, MockResponse, mocked_login_post
30-
31-
32-
def get_client():
33-
client = ContainerPlatformClient(
34-
username="admin",
35-
password="admin123",
36-
api_host="127.0.0.1",
37-
api_port=8080,
38-
use_ssl=True,
39-
)
40-
client.create_session()
41-
return client
29+
from .base_test import BaseTestCase, MockResponse, get_client
30+
31+
BaseTestCase.registerHttpGetHandler(
32+
url="https://127.0.0.1:8080/api/v1/user/",
33+
response=MockResponse(
34+
json_data={
35+
"_embedded": {
36+
"users": [
37+
{
38+
"_links": {"self": {"href": "/api/v1/user/16"}},
39+
"label": {"name": "csnow", "description": "chris",},
40+
"is_group_added_user": False,
41+
"is_external": False,
42+
"is_service_account": False,
43+
"default_tenant": "",
44+
"is_siteadmin": False,
45+
},
46+
{
47+
"_links": {"self": {"href": "/api/v1/user/5"}},
48+
"label": {
49+
"name": "admin",
50+
"description": "BlueData Administrator",
51+
},
52+
"is_group_added_user": False,
53+
"is_external": False,
54+
"is_service_account": False,
55+
"default_tenant": "/api/v1/tenant/1",
56+
"is_siteadmin": True,
57+
},
58+
]
59+
}
60+
},
61+
status_code=200,
62+
headers={},
63+
),
64+
)
65+
66+
BaseTestCase.registerHttpGetHandler(
67+
url="https://127.0.0.1:8080/api/v1/user/16/",
68+
response=MockResponse(
69+
json_data={
70+
"_links": {"self": {"href": "/api/v1/user/16"}},
71+
"label": {"name": "csnow", "description": "chris"},
72+
"is_group_added_user": False,
73+
"is_external": False,
74+
"is_service_account": False,
75+
"default_tenant": "",
76+
"is_siteadmin": False,
77+
},
78+
status_code=200,
79+
headers={},
80+
),
81+
)
82+
83+
BaseTestCase.registerHttpGetHandler(
84+
url="https://127.0.0.1:8080/api/v1/user/123",
85+
response=MockResponse(
86+
json_data={
87+
"_links": {"self": {"href": "/api/v1/user/123"}},
88+
"purpose": "proxy",
89+
},
90+
status_code=200,
91+
headers={},
92+
),
93+
)
94+
95+
BaseTestCase.registerHttpGetHandler(
96+
url="https://127.0.0.1:8080/api/v1/user/999",
97+
response=MockResponse(
98+
text_data="Not found.",
99+
json_data={},
100+
status_code=404,
101+
raise_for_status_flag=True,
102+
headers={},
103+
),
104+
)
105+
106+
BaseTestCase.registerHttpPostHandler(
107+
url="https://127.0.0.1:8080/api/v1/user",
108+
response=MockResponse(
109+
json_data={},
110+
status_code=200,
111+
headers={"location": ("/mock/api/user/1")},
112+
),
113+
)
42114

43115

44116
class TestUsers(TestCase):
45-
def mocked_requests_get(*args, **kwargs):
46-
if args[0] == "https://127.0.0.1:8080/api/v1/user/":
47-
return MockResponse(
48-
json_data={
49-
"_embedded": {
50-
"users": [
51-
{
52-
"_links": {
53-
"self": {"href": "/api/v1/user/16"}
54-
},
55-
"label": {
56-
"name": "csnow",
57-
"description": "chris",
58-
},
59-
"is_group_added_user": False,
60-
"is_external": False,
61-
"is_service_account": False,
62-
"default_tenant": "",
63-
"is_siteadmin": False,
64-
},
65-
{
66-
"_links": {"self": {"href": "/api/v1/user/5"}},
67-
"label": {
68-
"name": "admin",
69-
"description": "BlueData Administrator",
70-
},
71-
"is_group_added_user": False,
72-
"is_external": False,
73-
"is_service_account": False,
74-
"default_tenant": "/api/v1/tenant/1",
75-
"is_siteadmin": True,
76-
},
77-
]
78-
}
79-
},
80-
status_code=200,
81-
headers={},
82-
)
83-
elif args[0] == "https://127.0.0.1:8080/api/v1/user/16/":
84-
return MockResponse(
85-
json_data={
86-
"_links": {"self": {"href": "/api/v1/user/16"}},
87-
"label": {"name": "csnow", "description": "chris"},
88-
"is_group_added_user": False,
89-
"is_external": False,
90-
"is_service_account": False,
91-
"default_tenant": "",
92-
"is_siteadmin": False,
93-
},
94-
status_code=200,
95-
headers={},
96-
)
97-
else:
98-
raise RuntimeError("Unhandle GET request: " + args[0])
99-
100-
def mocked_requests_post(*args, **kwargs):
101-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
102-
return MockResponse(
103-
json_data={},
104-
status_code=200,
105-
headers={
106-
"location": (
107-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
108-
)
109-
},
110-
)
111-
else:
112-
raise RuntimeError("Unhandle POST request: " + args[0])
113-
114-
@patch("requests.get", side_effect=mocked_requests_get)
115-
@patch("requests.post", side_effect=mocked_requests_post)
117+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
118+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
116119
def test_get_users(self, mock_get, mock_post):
117120
client = ContainerPlatformClient(
118121
username="admin",
@@ -144,27 +147,8 @@ def test_get_users(self, mock_get, mock_post):
144147

145148

146149
class TestDeleteUser(TestCase):
147-
# pylint: disable=no-method-argument
148-
def mocked_requests_get(*args, **kwargs):
149-
if args[0] == "https://127.0.0.1:8080/api/v1/user/123":
150-
return MockResponse(
151-
json_data={
152-
"_links": {"self": {"href": "/api/v1/user/123"}},
153-
"purpose": "proxy",
154-
},
155-
status_code=200,
156-
headers={},
157-
)
158-
if args[0] == "https://127.0.0.1:8080/api/v1/user/999":
159-
return MockResponse(
160-
text_data="Not found.",
161-
json_data={},
162-
status_code=404,
163-
raise_for_status_flag=True,
164-
headers={},
165-
)
166-
raise RuntimeError("Unhandle GET request: " + args[0])
167150

151+
# TODO: refactor me to BaseTestCase http handler
168152
# pylint: disable=no-method-argument
169153
def mocked_requests_delete(*args, **kwargs):
170154
if args[0] == "https://127.0.0.1:8080/api/v1/user/999":
@@ -179,23 +163,10 @@ def mocked_requests_delete(*args, **kwargs):
179163
return MockResponse(json_data={}, status_code=200, headers={},)
180164
raise RuntimeError("Unhandle GET request: " + args[0])
181165

182-
def mocked_requests_post(*args, **kwargs):
183-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
184-
return MockResponse(
185-
json_data={},
186-
status_code=200,
187-
headers={
188-
"location": (
189-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
190-
)
191-
},
192-
)
193-
raise RuntimeError("Unhandle POST request: " + args[0])
194-
195166
# delete() does a get() request to check the worker has 'purpose':'proxy'
196-
@patch("requests.get", side_effect=mocked_requests_get)
167+
@patch("requests.get", side_effect=BaseTestCase.httpGetHandlers)
197168
@patch("requests.delete", side_effect=mocked_requests_delete)
198-
@patch("requests.post", side_effect=mocked_requests_post)
169+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
199170
def test_delete_user(self, mock_get, mock_post, mock_delete):
200171
with self.assertRaisesRegexp(
201172
AssertionError, "'id' must be provided and must be a str",
@@ -213,28 +184,8 @@ def test_delete_user(self, mock_get, mock_post, mock_delete):
213184
get_client().user.delete(id="/api/v1/user/123")
214185

215186

216-
def mocked_login_post(*args, **kwargs):
217-
if args[0] == "https://127.0.0.1:8080/api/v1/login":
218-
return MockResponse(
219-
json_data={},
220-
status_code=200,
221-
headers={
222-
"location": (
223-
"/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
224-
)
225-
},
226-
)
227-
if args[0] == "https://127.0.0.1:8080/api/v1/user":
228-
return MockResponse(
229-
json_data={},
230-
status_code=200,
231-
headers={"location": ("/mock/api/user/1")},
232-
)
233-
raise RuntimeError("Unhandle POST request: " + args[0])
234-
235-
236187
class TestCLI(BaseTestCase):
237-
@patch("requests.post", side_effect=mocked_login_post)
188+
@patch("requests.post", side_effect=BaseTestCase.httpPostHandlers)
238189
def test_create(self, mock_post):
239190

240191
hpecp = self.cli.CLI()

0 commit comments

Comments
 (0)