Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
Merge branch 'postatum-99269222_refresh_index_tunneling' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
jstoiko committed Aug 25, 2015
2 parents 97bb22e + 9e07c45 commit 3691eca
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
11 changes: 11 additions & 0 deletions nefertari/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@

log = logging.getLogger(__name__)

RESERVED_PARAMS = [
'_start',
'_limit',
'_page',
'_fields',
'_count',
'_sort',
'_search_fields',
'_refresh_index',
]


def includeme(config):
from nefertari.resource import get_root_resource, get_resource_map
Expand Down
15 changes: 2 additions & 13 deletions nefertari/elasticsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,10 @@
dictset, dict2obj, process_limit, split_strip)
from nefertari.json_httpexceptions import (
JHTTPBadRequest, JHTTPNotFound, exception_response)
from nefertari import engine
from nefertari import engine, RESERVED_PARAMS

log = logging.getLogger(__name__)

RESERVED = [
'_start',
'_limit',
'_page',
'_fields',
'_count',
'_sort',
'_search_fields',
'_refresh_index',
]


class IndexNotFoundException(Exception):
pass
Expand Down Expand Up @@ -454,7 +443,7 @@ def build_search_params(self, params):
_raw_terms = params.pop('q', '')

if 'body' not in params:
query_string = build_qs(params.remove(RESERVED), _raw_terms)
query_string = build_qs(params.remove(RESERVED_PARAMS), _raw_terms)
if query_string:
_params['body'] = {
'query': {
Expand Down
11 changes: 6 additions & 5 deletions nefertari/tweens.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import six
from pyramid.settings import asbool
from nefertari.utils import drop_reserved_params

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -31,10 +32,9 @@ def timing(request):


def get_tunneling(handler, registry):
""" Allows all methods to be tunneled via GET for dev/debuging
purposes.
"""
This allows all methods to be tunneled via GET for dev/debuging purposes.
"""

log.info('get_tunneling enabled')

def get_tunneling(request):
Expand All @@ -43,9 +43,10 @@ def get_tunneling(request):
request.method = method

if method in ['POST', 'PUT', 'PATCH']:
request.body = six.b(json.dumps(request.GET.mixed()))
get_params = request.GET.mixed()
valid_params = drop_reserved_params(get_params)
request.body = six.b(json.dumps(valid_params))
request.content_type = 'application/json'
# request.POST.update(request.GET)

return handler(request)

Expand Down
11 changes: 11 additions & 0 deletions nefertari/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,3 +185,14 @@ def validate_data_privacy(request, data):

if not_allowed_fields:
raise wrappers.ValidationError(', '.join(not_allowed_fields))


def drop_reserved_params(params):
""" Drops reserved params """
from nefertari import RESERVED_PARAMS
params = params.copy()
for reserved_param in RESERVED_PARAMS:
if reserved_param in params:
params.pop(reserved_param)
return params

21 changes: 21 additions & 0 deletions tests/test_tweens.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ def mixed(self):
assert request.content_type == 'application/json'
assert request.body == six.b('{"foo": "bar"}')

def test_get_tunneling_reserved_params_dropped(self):
from nefertari import RESERVED_PARAMS

class GET(dict):
def mixed(self):
return self

reserved = RESERVED_PARAMS[0]
get_data = GET({
'_m': 'POST',
'foo': 'bar',
reserved: 'boo',
})
request = Mock(GET=get_data, method='GET')
get_tunneling = tweens.get_tunneling(lambda x: x, None)
get_tunneling(request)
assert request.GET == {'foo': 'bar', reserved: 'boo'}
assert request.method == 'POST'
assert request.content_type == 'application/json'
assert request.body == six.b('{"foo": "bar"}')

def test_get_tunneling_not_allowed_method(self):
class GET(dict):
def mixed(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/test_utils/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,9 @@ def test_validate_data_privacy_invalid(self, mock_wrapper):
assert str(ex.value) == 'qoo'
mock_wrapper.assert_called_once_with(None)
wrapper.assert_called_once_with(result=data)

def test_drop_reserved_params(self):
from nefertari import RESERVED_PARAMS
reserved_param = RESERVED_PARAMS[0]
result = utils.drop_reserved_params({reserved_param: 1, 'foo': 2})
assert result == {'foo': 2}

0 comments on commit 3691eca

Please sign in to comment.