Skip to content

Commit

Permalink
tests: update mocks to work with wrapper= for CloudifyClient
Browse files Browse the repository at this point in the history
Prepare the mock client to work with cloudify-cosmo/cloudify-common#1204
  • Loading branch information
tehasdf committed Dec 13, 2022
1 parent fb775c2 commit 7054d0c
Showing 1 changed file with 21 additions and 25 deletions.
46 changes: 21 additions & 25 deletions rest-service/manager_rest/test/mocks.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import numbers
import types
from datetime import datetime
Expand All @@ -9,6 +10,7 @@

from cloudify_rest_client.client import HTTPClient
from cloudify_rest_client.executions import Execution
from cloudify_rest_client.exceptions import CloudifyClientError

from manager_rest import utils
from manager_rest.storage import get_storage_manager, models
Expand Down Expand Up @@ -59,7 +61,7 @@ def __init__(self, app, headers=None, root_path=None):
self._root_path = root_path

def do_request(self,
requests_method,
method,
uri,
data=None,
params=None,
Expand All @@ -69,6 +71,8 @@ def do_request(self,
expected_status_code=200,
stream=False,
versioned_url=True,
verify=False,
wrapper=None,
timeout=None):
# hack: we have app-ctx everywhere in tests, but we'd like to still
# load the user again on every request, in case this client uses
Expand All @@ -77,30 +81,18 @@ def do_request(self,
if '_login_user' in g:
delattr(g, '_login_user')

if CLIENT_API_VERSION == 'v1':
# in v1, HTTPClient won't append the version part of the URL
# on its own, so it's done here instead
uri = '/api/{0}{1}'.format(CLIENT_API_VERSION, uri)

return super(MockHTTPClient, self).do_request(
requests_method=requests_method,
uri=uri,
data=data,
params=params,
headers=headers,
expected_status_code=expected_status_code,
stream=stream
)

def _do_request(self, requests_method, request_url, body, params, headers,
expected_status_code, stream, verify, timeout=None):
if 'get' in requests_method.__name__:
request_url = f'/api/{CLIENT_API_VERSION}{uri}'
if isinstance(data, dict):
body = json.dumps(data)
else:
body = data
if method == 'GET':
response = self.app.get(request_url,
headers=headers,
data=body,
query_string=build_query_string(params))

elif 'put' in requests_method.__name__:
elif method == 'PUT':
if isinstance(body, types.GeneratorType):
body = b''.join(body)
if hasattr(body, 'fields'):
Expand Down Expand Up @@ -128,19 +120,19 @@ def _do_request(self, requests_method, request_url, body, params, headers,
headers=headers,
data=body,
query_string=build_query_string(params))
elif 'post' in requests_method.__name__:
elif method == 'POST':
if isinstance(body, types.GeneratorType):
body = b''.join(body)
response = self.app.post(request_url,
headers=headers,
data=body,
query_string=build_query_string(params))
elif 'patch' in requests_method.__name__:
elif method == 'PATCH':
response = self.app.patch(request_url,
headers=headers,
data=body,
query_string=build_query_string(params))
elif 'delete' in requests_method.__name__:
elif method == 'DELETE':
response = self.app.delete(request_url,
headers=headers,
data=body,
Expand All @@ -152,14 +144,18 @@ def _do_request(self, requests_method, request_url, body, params, headers,
expected_status_code = [expected_status_code]
if response.status_code not in expected_status_code:
response.content = response.data
self._raise_client_error(MockClientResponse(response), request_url)
raise CloudifyClientError.from_response(
response, response.status_code, response.content)

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

if response.status_code == 204:
return None
return response.get_json()
response_json = response.get_json()
if wrapper:
return wrapper(response_json)
return response_json


class MockStreamedResponse(object):
Expand Down

0 comments on commit 7054d0c

Please sign in to comment.