diff --git a/combo/data/models.py b/combo/data/models.py index 027c89e3..66b9393b 100644 --- a/combo/data/models.py +++ b/combo/data/models.py @@ -1170,7 +1170,7 @@ class JsonCellBase(CellBase): if json_response.status_code // 100 == 2: if json_response.status_code != 204: # 204 = No Content try: - extra_context[data_key] = json.loads(json_response.content) + extra_context[data_key] = json_response.json() except ValueError: extra_context[data_key + '_error'] = 'invalid_json' logger = logging.getLogger(__name__) @@ -1181,7 +1181,7 @@ class JsonCellBase(CellBase): continue elif json_response.headers.get('content-type') == 'application/json': try: - extra_context[data_key + '_error'] = json.loads(json_response.content) + extra_context[data_key + '_error'] = json_response.json() except ValueError: extra_context[data_key + '_error'] = 'invalid_json' diff --git a/tests/test_cells.py b/tests/test_cells.py index 6f2f5f53..dd46fa8c 100644 --- a/tests/test_cells.py +++ b/tests/test_cells.py @@ -14,7 +14,7 @@ from django.test.client import RequestFactory from django.test.utils import CaptureQueriesContext from django.contrib.auth.models import User from django.core.urlresolvers import reverse -from django.utils.encoding import force_text +from django.utils.encoding import force_text, force_bytes from combo.data.library import get_cell_classes from combo.utils import NothingInCacheException @@ -23,6 +23,10 @@ from .test_manager import login pytestmark = pytest.mark.django_db +def mock_json_response(content, **kwargs): + content = force_bytes(content) + text = force_text(content) + return mock.Mock(content=content, text=text, json=lambda: json.loads(text), **kwargs) def test_cell_reference(): page = Page() @@ -215,21 +219,21 @@ def test_json_cell(): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': [{'url': 'xxx', 'text': 'xxx'}]} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) context = cell.get_cell_extra_context({}) assert context['json'] == data assert context['json_url'] == 'http://example.net/' assert context['json_status'] == 200 assert 'json_error' not in context - requests_get.return_value = mock.Mock(status_code=204) # 204 : No Content + requests_get.return_value = mock_json_response(content='', status_code=204) # 204 : No Content context = cell.get_cell_extra_context({}) assert context['json'] is None assert context['json_url'] == 'http://example.net/' assert context['json_status'] == 204 assert 'json_error' not in context - requests_get.return_value = mock.Mock(content='not found', status_code=404, + requests_get.return_value = mock_json_response(content='not found', status_code=404, headers={}) context = cell.get_cell_extra_context({}) assert context['json'] is None @@ -237,7 +241,7 @@ def test_json_cell(): assert context['json_status'] == 404 assert 'json_error' not in context - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=404, + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=404, headers={'content-type': 'application/json'}) context = cell.get_cell_extra_context({}) assert context['json'] is None @@ -256,7 +260,7 @@ def test_json_cell(): assert isinstance(context['json_exception'], requests.ConnectionError) cell.url = '' # no URL -> no request, no data, no status - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) context = cell.get_cell_extra_context({}) assert context['json'] is None assert context['json_url'] == '' @@ -269,7 +273,7 @@ def test_json_cell(): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': [{'url': 'http://a.b', 'text': 'xxx'}]} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) cell.url = 'http://test4' result = cell.render({'synchronous': True}) assert 'http://a.b' in result @@ -280,7 +284,7 @@ def test_json_cell(): assert result == 'xxx' with mock.patch('combo.utils.requests.get') as requests_get: - requests_get.return_value = mock.Mock(content='garbage', status_code=200) + requests_get.return_value = mock_json_response(content='garbage', status_code=200) cell.url = 'http://test5' result = cell.render({'synchronous': True}) assert result == '' @@ -296,7 +300,7 @@ def test_json_cell(): data = {'data': [{'url': 'xxx', 'text': 'xxx'}]} cell.url = 'http://testuser?[foobar]' with mock.patch('combo.utils.requests.get') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) context = cell.get_cell_extra_context({'foobar': 'barfoo'}) assert context['json'] == data assert context['json_url'] == 'http://testuser?barfoo' @@ -313,7 +317,7 @@ def test_json_cell(): request.user = User(username='foo', email='foo@example.net') cell.url = 'http://testuser?email=[user_email]' with mock.patch('combo.utils.requests.get') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) context = cell.get_cell_extra_context({'request': request}) assert context['json'] == data assert context['json_url'] == 'http://testuser?email=foo%40example.net' @@ -338,7 +342,7 @@ def test_json_cell_with_varnames(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': []} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) resp = app.get(url + '?var1=foo&var2=bar') # request query string is here @@ -363,7 +367,7 @@ def test_config_json_cell(): assert cell.css_class_names.split() == ['configjsoncell', 'foobar'] with mock.patch('combo.utils.requests.get') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps({'hello': 'world'}), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps({'hello': 'world'}), status_code=200) context = cell.get_cell_extra_context({'request': request}) assert context['json'] == {'hello': 'world'} assert context['json_url'] == 'http://test/' @@ -404,7 +408,7 @@ def test_config_json_cell_with_varnames(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': []} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) resp = app.get(url + '?var1=foo&var2=bar') # request query string is here @@ -458,7 +462,7 @@ def test_config_json_cell_with_param_in_url(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': []} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) resp = app.get(url) @@ -473,7 +477,7 @@ def test_json_force_async(): cell.template_string = '{{json.hello}}' cell.force_async = True with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps({'hello': 'world'}), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps({'hello': 'world'}), status_code=200) with pytest.raises(NothingInCacheException): cell.render({}) assert cell.render({'synchronous': True}) == 'world' @@ -489,7 +493,7 @@ def test_json_force_async(): cell.template_string = '{{json.hello}}' cell.force_async = False with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps({'hello': 'world2'}), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps({'hello': 'world2'}), status_code=200) # raise if nothing in cache with pytest.raises(NothingInCacheException): cell.render({}) @@ -522,7 +526,7 @@ def test_config_json_cell_additional_url(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': 'toto'} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) resp = app.get(url) @@ -537,7 +541,7 @@ def test_config_json_cell_additional_url(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': 'toto'} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=404, + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=404, headers={'content-type': 'application/json'}) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) @@ -558,7 +562,7 @@ def test_config_json_cell_additional_url(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': 'toto'} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) context = cell.get_cell_extra_context({}) assert context['json'] == data assert context['json_url'] == 'http://foo' @@ -591,7 +595,7 @@ def test_config_json_cell_additional_url(app): data = {'data': 'bar'} with mock.patch('combo.utils.requests.get') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=200) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) resp = app.get(url) @@ -618,7 +622,7 @@ def test_config_json_cell_additional_url(app): assert context['plop2_status'] == 200 with mock.patch('combo.utils.requests.get') as requests_get: - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=404, + requests_get.return_value = mock_json_response(content=json.dumps(data), status_code=404, headers={'content-type': 'application/json'}) url = reverse('combo-public-ajax-page-cell', kwargs={'page_pk': page.id, 'cell_reference': cell.get_reference()}) diff --git a/tests/test_manager.py b/tests/test_manager.py index 1f87e4b7..f9426f72 100644 --- a/tests/test_manager.py +++ b/tests/test_manager.py @@ -1102,7 +1102,7 @@ def test_page_versionning(app, admin_user): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': [{'url': 'xxx', 'text': 'xxx'}]} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock.Mock(json=lambda: data, status_code=200) resp3 = app.get(json_cell_url) assert resp3.text.strip() == 'CxxxD' @@ -1112,7 +1112,7 @@ def test_page_versionning(app, admin_user): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': [{'url': 'xxx', 'text': 'xxx'}]} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock.Mock(json=lambda: data, status_code=200) resp3 = app.get(json_cell_url) assert resp3.text.strip() == 'AxxxB' diff --git a/tests/test_search.py b/tests/test_search.py index 12ac50bf..2ee67ebd 100644 --- a/tests/test_search.py +++ b/tests/test_search.py @@ -157,7 +157,7 @@ def test_search_global_context(app): with mock.patch('combo.utils.requests.get') as requests_get: data = {'data': []} - requests_get.return_value = mock.Mock(content=json.dumps(data), status_code=200) + requests_get.return_value = mock.Mock(json=lambda: data, status_code=200) resp = app.get(url) assert requests_get.call_args[0][0] == 'http://www.example.net/search/foo/'