26
26
from hpecp import ContainerPlatformClient
27
27
from hpecp .exceptions import APIItemNotFoundException
28
28
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
+ )
42
114
43
115
44
116
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 )
116
119
def test_get_users (self , mock_get , mock_post ):
117
120
client = ContainerPlatformClient (
118
121
username = "admin" ,
@@ -144,27 +147,8 @@ def test_get_users(self, mock_get, mock_post):
144
147
145
148
146
149
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 ])
167
150
151
+ # TODO: refactor me to BaseTestCase http handler
168
152
# pylint: disable=no-method-argument
169
153
def mocked_requests_delete (* args , ** kwargs ):
170
154
if args [0 ] == "https://127.0.0.1:8080/api/v1/user/999" :
@@ -179,23 +163,10 @@ def mocked_requests_delete(*args, **kwargs):
179
163
return MockResponse (json_data = {}, status_code = 200 , headers = {},)
180
164
raise RuntimeError ("Unhandle GET request: " + args [0 ])
181
165
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
-
195
166
# 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 )
197
168
@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 )
199
170
def test_delete_user (self , mock_get , mock_post , mock_delete ):
200
171
with self .assertRaisesRegexp (
201
172
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):
213
184
get_client ().user .delete (id = "/api/v1/user/123" )
214
185
215
186
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
-
236
187
class TestCLI (BaseTestCase ):
237
- @patch ("requests.post" , side_effect = mocked_login_post )
188
+ @patch ("requests.post" , side_effect = BaseTestCase . httpPostHandlers )
238
189
def test_create (self , mock_post ):
239
190
240
191
hpecp = self .cli .CLI ()
0 commit comments