def test_set_public_limits_no_params(self, mock_set): request = Mock() request.registry.settings = {} view = Mock(request=request, _query_params={}) wrappers.set_public_limits(view) mock_set.assert_called_once_with(view.request, total=100) view.add_after_call.assert_called_once_with('index', mock_set(), pos=0) assert '_limit' not in view._query_params
def test_set_public_limits_value_err(self): from nefertari.json_httpexceptions import JHTTPBadRequest request = Mock() request.registry.settings = {} view = Mock(request=request, _query_params={}) view.add_after_call.side_effect = ValueError with pytest.raises(JHTTPBadRequest): wrappers.set_public_limits(view)
def test_set_public_limits_count(self, mock_count, mock_set): request = Mock() request.registry.settings = {"public_max_limit": 123} view = Mock(request=request, _query_params={"_limit": 100, "_page": 1, "_start": 90, "_count": ""}) wrappers.set_public_limits(view) mock_count.assert_called_once_with(request, public_max=123) view.add_after_call.assert_called_with("index", mock_count(), pos=0) assert view.add_after_call.call_count == 2
def set_public_limits(self): """ Set public limits if auth is enabled and user is not authenticated. """ root_resource = getattr(self, 'root_resource', None) auth_enabled = root_resource is not None and root_resource.auth if auth_enabled and not getattr(self.request, 'user', None): wrappers.set_public_limits(self)
def test_set_public_limits_value_err(self, mock_set): from nefertari.json_httpexceptions import JHTTPBadRequest request = Mock() request.registry.settings = {} view = Mock(request=request, _query_params={}) mock_set.side_effect = ValueError with pytest.raises(JHTTPBadRequest): wrappers.set_public_limits(view)
def test_set_public_limits_no_params(self, mock_set): request = Mock() request.registry.settings = {} view = Mock(request=request, _query_params={}) wrappers.set_public_limits(view) mock_set.assert_called_once_with(view.request, total=100) view.add_after_call.assert_called_once_with("index", mock_set(), pos=0) assert "_limit" not in view._query_params
def test_set_public_limits(self, mock_set): request = Mock() request.registry.settings = {"public_max_limit": 123} view = Mock(request=request, _query_params={"_limit": 100, "_page": 1, "_start": 90}) wrappers.set_public_limits(view) mock_set.assert_called_once_with(view.request, total=123) view.add_after_call.assert_called_once_with("index", mock_set(), pos=0) assert view._query_params["_limit"] == 33
def set_public_limits(self): """ Set public limits if auth is enabled and user is not authenticated. Also sets default limit for GET, HEAD requests. """ if self.request.method.upper() in ['GET', 'HEAD']: self._query_params.process_int_param('_limit', 20) if self._auth_enabled and not getattr(self.request, 'user', None): wrappers.set_public_limits(self)
def test_set_public_limits(self, mock_set): request = Mock() request.registry.settings = {'public_max_limit': 123} view = Mock( request=request, _query_params={'_limit': 100, '_page': 1, '_start': 90}) wrappers.set_public_limits(view) mock_set.assert_called_once_with(view.request, total=123) view.add_after_call.assert_called_once_with( 'index', mock_set(), pos=0) assert view._query_params['_limit'] == 33
def test_set_public_limits_count(self, mock_count, mock_set): request = Mock() request.registry.settings = {'public_max_limit': 123} view = Mock( request=request, _query_params={ '_limit': 100, '_page': 1, '_start': 90, '_count': ''}) wrappers.set_public_limits(view) mock_count.assert_called_once_with(request, public_max=123) view.add_after_call.assert_called_with( 'index', mock_count(), pos=0) assert view.add_after_call.call_count == 2
def __init__(self, context, request, _params={}): self.context = context self.request = request self._params = dictset(_params or request.params.mixed()) ctype = request.content_type if request.method in ['POST', 'PUT', 'PATCH']: if ctype == 'application/json': try: self._params.update(request.json) except simplejson.JSONDecodeError: log.error( "Expecting JSON. Received: '{}'. Request: {} {}".format( request.body, request.method, request.url)) # dict of the callables {'action':[callable1, callable2..]} # as name implies, before calls are executed before the action is called # after_calls are called after the action returns. self._before_calls = defaultdict(list) self._after_calls = defaultdict(list) # no accept headers, use default if '' in request.accept: request.override_renderer = self._default_renderer elif 'application/json' in request.accept: request.override_renderer = 'nefertari_json' elif 'text/plain' in request.accept: request.override_renderer = 'string' self.setup_default_wrappers() self.convert_ids2objects() if not getattr(self.request, 'user', None): wrappers.set_public_limits(self)
def __init__(self, context, request, _params={}): self.context = context self.request = request self._params = dictset(_params or request.params.mixed()) ctype = request.content_type if request.method in ['POST', 'PUT', 'PATCH']: if ctype == 'application/json': try: self._params.update(request.json) except simplejson.JSONDecodeError: log.error("Expecting JSON. Received: '{}'. Request: {} {}". format(request.body, request.method, request.url)) # dict of the callables {'action':[callable1, callable2..]} # as name implies, before calls are executed before the action is called # after_calls are called after the action returns. self._before_calls = defaultdict(list) self._after_calls = defaultdict(list) # no accept headers, use default if '' in request.accept: request.override_renderer = self._default_renderer elif 'application/json' in request.accept: request.override_renderer = 'nefertari_json' elif 'text/plain' in request.accept: request.override_renderer = 'string' self.setup_default_wrappers() self.convert_ids2objects() if not getattr(self.request, 'user', None): wrappers.set_public_limits(self)