def _make_subrequest(self, nested_request: dict) -> Request: path = nested_request['path'] method = nested_request['method'] json_body = nested_request['body'] keywords_args = {'method': method, 'base_url': self.request.host_url} if json_body: keywords_args['body'] = dumps(json_body).encode() if method not in ['GET', 'OPTIONS', 'HEAD']: keywords_args['content_type'] = 'application/json' request = Request.blank(path, **keywords_args) set_batchmode(request) self.copy_attr_if_exists('root', request) self.copy_attr_if_exists('__cached_principals__', request) self.copy_header_if_exists('X-User-Path', request) self.copy_header_if_exists('X-User-Token', request) # properly setup subrequest in case script_name env is set, # see https://github.com/Pylons/pyramid/issues/1434 request.script_name = self.request.script_name # if a script_name (a prefix in front of backend paths, e.g. /api) is # set, subrequest paths also need to start with it. if not request.path_info.startswith(request.script_name): raise Exception('Batch subrequest path (%s) does not start with ' 'script name (%s)' % (request.path_info, request.script_name)) request.path_info = request.path_info[len(self.request.script_name):] return request
def post(self) -> dict: """Create new resource and get response data.""" response_list = [] path_map = {} set_batchmode(self.request) for pos, item in enumerate(self.request.validated): item_response = self._process_nested_request(item, path_map) response_list.append(item_response) if not item_response.was_successful(): error = JSONHTTPClientError([], code=item_response.code, title=item_response.title, request=self.request) # TODO: the error response lists updated resources json_body = error.json_body response_list_json = self._response_list_to_json(response_list) json_body.update(response_list_json) error.json_body = json_body msg = 'Failing batch request item position {0} request {1} {2}' logger.warn(msg.format(pos, item['method'], item['path'])) raise error response = self._response_list_to_json(response_list) return response
def test_batchmode_ignore_if_value_is_last( self, node, last, mock_sheet, changelog, request_, kw): from adhocracy_core.utils import set_batchmode request_.registry.changelog = changelog set_batchmode(request_) mock_sheet.get.return_value = {'LAST': last} assert self.call_fut(node, kw)(node, [last]) is None
def test_batchmode_value_last_versions_is_not_last_version( self, node, last_version, mock_tag_sheet, request): from adhocracy_core.utils import set_batchmode set_batchmode(request) mock_tag_sheet.get.return_value = {'elements': [last_version]} other_version = object() with raises(colander.Invalid): self.call_fut(node, [other_version])
def test_batchmode_value_last_versions_is_not_last_version_but_last_new_version_exists( self, node, last_version, mock_tag_sheet, registry, changelog, request): from adhocracy_core.utils import set_batchmode set_batchmode(request) mock_tag_sheet.get.return_value = {'elements': [last_version]} other_version = object() registry.changelog['/'] = changelog['/']._replace(last_version=other_version) self.call_fut(node, [other_version])
def test_batchmode_ingnore_if_last_version_created_in_transaction( self, node, last, other, mock_sheet, changelog, request_, kw): from adhocracy_core.utils import set_batchmode request_.registry.changelog = changelog set_batchmode(request_) mock_sheet.get.return_value = {'LAST': last} request_.registry.changelog['/last_version'] =\ changelog['/last_version']._replace(created=True) assert self.call_fut(node, kw)(node, [other]) is None
def test_batchmode_raise_if_value_is_not_last_last_version( self, node, last, other, mock_sheet, changelog, request_, kw): from adhocracy_core.utils import set_batchmode request_.registry.changelog = changelog set_batchmode(request_) mock_sheet.get.return_value = {'LAST': last} with raises(colander.Invalid) as err: self.call_fut(node, kw)(node, [other]) assert err.value.msg == 'No fork allowed - valid follows resources '\ 'are: /last_version'
def test_batchmode_value_last_version_is_last_version( self, node, last_version, mock_tag_sheet, request): from adhocracy_core.utils import set_batchmode set_batchmode(request) mock_tag_sheet.get.return_value = {'elements': [last_version]} assert self.call_fut(node, [last_version]) is None