Skip to content

Commit 5dcf5d4

Browse files
authored
Merge pull request #232 from hpe-container-platform-community/refactor_tests
refactor user test
2 parents 8aa0610 + 8ce85cd commit 5dcf5d4

File tree

4 files changed

+137
-187
lines changed

4 files changed

+137
-187
lines changed

tests/role_test.py

Lines changed: 6 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,55 +27,12 @@
2727

2828
from hpecp import ContainerPlatformClient
2929

30-
from .base_test import BaseTestCase
31-
32-
33-
class MockResponse:
34-
def __init__(
35-
self,
36-
json_data,
37-
status_code,
38-
headers,
39-
raise_for_status_flag=False,
40-
text_data="",
41-
):
42-
self.json_data = json_data
43-
self.text = text_data
44-
self.status_code = status_code
45-
self.raise_for_status_flag = raise_for_status_flag
46-
self.headers = headers
47-
48-
def raise_for_status(self):
49-
if self.raise_for_status_flag:
50-
self.text = "some error occurred"
51-
raise requests.exceptions.HTTPError()
52-
else:
53-
return
54-
55-
def json(self):
56-
return self.json_data
57-
58-
59-
def get_client():
60-
client = ContainerPlatformClient(
61-
username="admin",
62-
password="admin123",
63-
api_host="127.0.0.1",
64-
api_port=8080,
65-
use_ssl=True,
66-
)
67-
client.create_session()
68-
return client
69-
70-
71-
def session_mock_response():
72-
return MockResponse(
73-
json_data={},
74-
status_code=200,
75-
headers={
76-
"location": "/api/v1/session/df1bfacb-xxxx-xxxx-xxxx-c8f57d8f3c71"
77-
},
78-
)
30+
from .base_test import (
31+
BaseTestCase,
32+
MockResponse,
33+
get_client,
34+
session_mock_response,
35+
)
7936

8037

8138
# pylint: disable=no-method-argument

tests/tenant_test.py

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -26,43 +26,7 @@
2626
from hpecp import ContainerPlatformClient
2727
from hpecp.exceptions import APIItemNotFoundException
2828

29-
30-
class MockResponse:
31-
def __init__(
32-
self,
33-
json_data,
34-
status_code,
35-
headers,
36-
raise_for_status_flag=False,
37-
text_data="",
38-
):
39-
self.json_data = json_data
40-
self.text = text_data
41-
self.status_code = status_code
42-
self.raise_for_status_flag = raise_for_status_flag
43-
self.headers = headers
44-
45-
def raise_for_status(self):
46-
if self.raise_for_status_flag:
47-
self.text = "some error occurred"
48-
raise requests.exceptions.HTTPError()
49-
else:
50-
return
51-
52-
def json(self):
53-
return self.json_data
54-
55-
56-
def get_client():
57-
client = ContainerPlatformClient(
58-
username="admin",
59-
password="admin123",
60-
api_host="127.0.0.1",
61-
api_port=8080,
62-
use_ssl=True,
63-
)
64-
client.create_session()
65-
return client
29+
from .base_test import MockResponse, get_client
6630

6731

6832
class TestTentants(TestCase):

tests/user_mock_api_responses.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
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/user/",
29+
response=MockResponse(
30+
json_data={
31+
"_embedded": {
32+
"users": [
33+
{
34+
"_links": {"self": {"href": "/api/v1/user/16"}},
35+
"label": {"name": "csnow", "description": "chris"},
36+
"is_group_added_user": False,
37+
"is_external": False,
38+
"is_service_account": False,
39+
"default_tenant": "",
40+
"is_siteadmin": False,
41+
},
42+
{
43+
"_links": {"self": {"href": "/api/v1/user/5"}},
44+
"label": {
45+
"name": "admin",
46+
"description": "BlueData Administrator",
47+
},
48+
"is_group_added_user": False,
49+
"is_external": False,
50+
"is_service_account": False,
51+
"default_tenant": "/api/v1/tenant/1",
52+
"is_siteadmin": True,
53+
},
54+
]
55+
}
56+
},
57+
status_code=200,
58+
headers={},
59+
),
60+
)
61+
62+
BaseTestCase.registerHttpGetHandler(
63+
url="https://127.0.0.1:8080/api/v1/user/16/",
64+
response=MockResponse(
65+
json_data={
66+
"_links": {"self": {"href": "/api/v1/user/16"}},
67+
"label": {"name": "csnow", "description": "chris"},
68+
"is_group_added_user": False,
69+
"is_external": False,
70+
"is_service_account": False,
71+
"default_tenant": "",
72+
"is_siteadmin": False,
73+
},
74+
status_code=200,
75+
headers={},
76+
),
77+
)
78+
79+
BaseTestCase.registerHttpGetHandler(
80+
url="https://127.0.0.1:8080/api/v1/user/123",
81+
response=MockResponse(
82+
json_data={
83+
"_links": {"self": {"href": "/api/v1/user/123"}},
84+
"purpose": "proxy",
85+
},
86+
status_code=200,
87+
headers={},
88+
),
89+
)
90+
91+
BaseTestCase.registerHttpGetHandler(
92+
url="https://127.0.0.1:8080/api/v1/user/999",
93+
response=MockResponse(
94+
text_data="Not found.",
95+
json_data={},
96+
status_code=404,
97+
raise_for_status_flag=True,
98+
headers={},
99+
),
100+
)
101+
102+
BaseTestCase.registerHttpPostHandler(
103+
url="https://127.0.0.1:8080/api/v1/user",
104+
response=MockResponse(
105+
json_data={},
106+
status_code=200,
107+
headers={"location": ("/mock/api/user/1")},
108+
),
109+
)
110+
111+
BaseTestCase.registerHttpDeleteHandler(
112+
url="https://127.0.0.1:8080/api/v1/user/999",
113+
response=MockResponse(
114+
text_data="Not found.",
115+
json_data={},
116+
status_code=404,
117+
raise_for_status_flag=True,
118+
headers={},
119+
),
120+
)
121+
122+
BaseTestCase.registerHttpDeleteHandler(
123+
url="https://127.0.0.1:8080/api/v1/user/123",
124+
response=MockResponse(
125+
text_data="", json_data={}, status_code=200, headers={},
126+
),
127+
)

tests/user_test.py

Lines changed: 3 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -27,108 +27,10 @@
2727
from hpecp.exceptions import APIItemNotFoundException
2828

2929
from .base_test import BaseTestCase, MockResponse, get_client
30+
from .user_mock_api_responses import mockApiSetup
3031

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-
)
114-
115-
BaseTestCase.registerHttpDeleteHandler(
116-
url="https://127.0.0.1:8080/api/v1/user/999",
117-
response=MockResponse(
118-
text_data="Not found.",
119-
json_data={},
120-
status_code=404,
121-
raise_for_status_flag=True,
122-
headers={},
123-
),
124-
)
125-
126-
BaseTestCase.registerHttpDeleteHandler(
127-
url="https://127.0.0.1:8080/api/v1/user/123",
128-
response=MockResponse(
129-
text_data="", json_data={}, status_code=200, headers={},
130-
),
131-
)
32+
# setup the mock data
33+
mockApiSetup()
13234

13335

13436
class TestUsers(TestCase):

0 commit comments

Comments
 (0)