misc: add support for PATCH action to json cells (#22255)

This commit is contained in:
Frédéric Péters 2018-03-04 23:01:01 +01:00
parent 53146ac618
commit ec28d1e6e2
2 changed files with 29 additions and 9 deletions

View File

@ -997,6 +997,7 @@ class JsonCellBase(CellBase):
error_message = self.actions[action].get('error-message')
timeout = self.actions[action].get('timeout', self.timeout)
method = self.actions[action].get('method', 'POST')
logger = logging.getLogger(__name__)
content = {}
@ -1018,7 +1019,9 @@ class JsonCellBase(CellBase):
logger.warning('error in templated URL (%s): %s', self.actions[action]['url'], e)
raise PostException(error_message)
json_response = utils.requests.post(url,
json_response = utils.requests.request(
method,
url,
headers={'Accept': 'application/json'},
remote_service='auto',
json=content,

View File

@ -381,6 +381,10 @@ def test_post_cell(app):
'actions': {
'create': {
'url': 'http://test-post-cell/create/',
},
'update': {
'url': 'http://test-post-cell/update/',
'method': 'PATCH',
}
}
}},
@ -397,23 +401,25 @@ def test_post_cell(app):
resp.form['value'] = 'plop'
resp.form.fields['items[]'][0].checked = True
with mock.patch('combo.utils.requests.post') as requests_post:
with mock.patch('combo.utils.requests.request') as requests_post:
requests_post.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=200)
resp2 = resp.form.submit()
assert requests_post.call_args[0][0] == 'http://test-post-cell/create/'
assert requests_post.call_args[0][0] == 'POST'
assert requests_post.call_args[0][1] == 'http://test-post-cell/create/'
assert requests_post.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
assert urlparse(resp2.location).path == '/'
# check ajax call
with mock.patch('combo.utils.requests.post') as requests_post:
with mock.patch('combo.utils.requests.request') as requests_post:
requests_post.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=200)
resp2 = resp.form.submit(headers={'x-requested-with': 'XMLHttpRequest'})
assert requests_post.call_args[0][0] == 'http://test-post-cell/create/'
assert requests_post.call_args[0][0] == 'POST'
assert requests_post.call_args[0][1] == 'http://test-post-cell/create/'
assert requests_post.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
assert resp2.content.startswith('<form')
# check error on POST
with mock.patch('combo.utils.requests.post') as requests_post:
with mock.patch('combo.utils.requests.request') as requests_post:
requests_post.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=400)
resp2 = resp.form.submit()
assert urlparse(resp2.location).path == '/'
@ -426,18 +432,29 @@ def test_post_cell(app):
resp2 = resp2.follow()
assert 'Failed to create stuff.' in resp2.content
with mock.patch('combo.utils.requests.post') as requests_post:
with mock.patch('combo.utils.requests.request') as requests_post:
requests_post.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=400)
resp2 = resp.form.submit(headers={'x-requested-with': 'XMLHttpRequest'})
assert resp2.content.startswith('<form')
assert resp2.headers['x-error-message'] == 'Failed to create stuff.'
# check variable substitution in URL
with mock.patch('combo.utils.requests.post') as requests_post:
with mock.patch('combo.utils.requests.request') as requests_post:
settings.JSON_CELL_TYPES['test-post-cell']['actions']['create'] = {
'url': 'http://test-post-cell/[value]/delete'}
resp2 = resp.form.submit()
assert requests_post.call_args[0][0] == 'http://test-post-cell/plop/delete'
assert requests_post.call_args[0][0] == 'POST'
assert requests_post.call_args[0][1] == 'http://test-post-cell/plop/delete'
# check patch method
with mock.patch('combo.utils.requests.request') as requests_patch:
resp.form['action'] = 'update'
requests_patch.return_value = mock.Mock(content=json.dumps({'err': 0}), status_code=200)
resp2 = resp.form.submit()
assert requests_patch.call_args[0][0] == 'PATCH'
assert requests_patch.call_args[0][1] == 'http://test-post-cell/update/'
assert requests_patch.call_args[1]['json'] == {'value': 'plop', 'items': ['1']}
assert urlparse(resp2.location).path == '/'
def test_familyinfos_cell_with_placeholders(app, admin_user):
Page.objects.all().delete()