Skip to content

Commit 7054d0c

Browse files
committed
tests: update mocks to work with wrapper= for CloudifyClient
Prepare the mock client to work with cloudify-cosmo/cloudify-common#1204
1 parent fb775c2 commit 7054d0c

File tree

1 file changed

+21
-25
lines changed

1 file changed

+21
-25
lines changed

rest-service/manager_rest/test/mocks.py

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import json
12
import numbers
23
import types
34
from datetime import datetime
@@ -9,6 +10,7 @@
910

1011
from cloudify_rest_client.client import HTTPClient
1112
from cloudify_rest_client.executions import Execution
13+
from cloudify_rest_client.exceptions import CloudifyClientError
1214

1315
from manager_rest import utils
1416
from manager_rest.storage import get_storage_manager, models
@@ -59,7 +61,7 @@ def __init__(self, app, headers=None, root_path=None):
5961
self._root_path = root_path
6062

6163
def do_request(self,
62-
requests_method,
64+
method,
6365
uri,
6466
data=None,
6567
params=None,
@@ -69,6 +71,8 @@ def do_request(self,
6971
expected_status_code=200,
7072
stream=False,
7173
versioned_url=True,
74+
verify=False,
75+
wrapper=None,
7276
timeout=None):
7377
# hack: we have app-ctx everywhere in tests, but we'd like to still
7478
# load the user again on every request, in case this client uses
@@ -77,30 +81,18 @@ def do_request(self,
7781
if '_login_user' in g:
7882
delattr(g, '_login_user')
7983

80-
if CLIENT_API_VERSION == 'v1':
81-
# in v1, HTTPClient won't append the version part of the URL
82-
# on its own, so it's done here instead
83-
uri = '/api/{0}{1}'.format(CLIENT_API_VERSION, uri)
84-
85-
return super(MockHTTPClient, self).do_request(
86-
requests_method=requests_method,
87-
uri=uri,
88-
data=data,
89-
params=params,
90-
headers=headers,
91-
expected_status_code=expected_status_code,
92-
stream=stream
93-
)
94-
95-
def _do_request(self, requests_method, request_url, body, params, headers,
96-
expected_status_code, stream, verify, timeout=None):
97-
if 'get' in requests_method.__name__:
84+
request_url = f'/api/{CLIENT_API_VERSION}{uri}'
85+
if isinstance(data, dict):
86+
body = json.dumps(data)
87+
else:
88+
body = data
89+
if method == 'GET':
9890
response = self.app.get(request_url,
9991
headers=headers,
10092
data=body,
10193
query_string=build_query_string(params))
10294

103-
elif 'put' in requests_method.__name__:
95+
elif method == 'PUT':
10496
if isinstance(body, types.GeneratorType):
10597
body = b''.join(body)
10698
if hasattr(body, 'fields'):
@@ -128,19 +120,19 @@ def _do_request(self, requests_method, request_url, body, params, headers,
128120
headers=headers,
129121
data=body,
130122
query_string=build_query_string(params))
131-
elif 'post' in requests_method.__name__:
123+
elif method == 'POST':
132124
if isinstance(body, types.GeneratorType):
133125
body = b''.join(body)
134126
response = self.app.post(request_url,
135127
headers=headers,
136128
data=body,
137129
query_string=build_query_string(params))
138-
elif 'patch' in requests_method.__name__:
130+
elif method == 'PATCH':
139131
response = self.app.patch(request_url,
140132
headers=headers,
141133
data=body,
142134
query_string=build_query_string(params))
143-
elif 'delete' in requests_method.__name__:
135+
elif method == 'DELETE':
144136
response = self.app.delete(request_url,
145137
headers=headers,
146138
data=body,
@@ -152,14 +144,18 @@ def _do_request(self, requests_method, request_url, body, params, headers,
152144
expected_status_code = [expected_status_code]
153145
if response.status_code not in expected_status_code:
154146
response.content = response.data
155-
self._raise_client_error(MockClientResponse(response), request_url)
147+
raise CloudifyClientError.from_response(
148+
response, response.status_code, response.content)
156149

157150
if stream:
158151
return MockStreamedResponse(response, self._root_path)
159152

160153
if response.status_code == 204:
161154
return None
162-
return response.get_json()
155+
response_json = response.get_json()
156+
if wrapper:
157+
return wrapper(response_json)
158+
return response_json
163159

164160

165161
class MockStreamedResponse(object):

0 commit comments

Comments
 (0)