diff --git a/swh/web/ui/renderers.py b/swh/web/ui/renderers.py index f5495c8d..28c8c855 100644 --- a/swh/web/ui/renderers.py +++ b/swh/web/ui/renderers.py @@ -1,110 +1,129 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information import yaml from flask import make_response, request from flask.ext.api import renderers, parsers from flask_api.mediatypes import MediaType from swh.web.ui import utils class SWHFilterRenderer(): """Base renderer for swh's common behavior. """ def filter_by_fields(self, data): fields = request.args.get('fields') if fields: fields = set(fields.split(',')) data = utils.filter_field_keys(data, fields) return data class PlainRenderer(renderers.BaseRenderer): """Renderer for plain/text, do nothing but send the data as is. """ media_type = 'text/plain' def render(self, data, media_type, **options): return data class YAMLRenderer(renderers.BaseRenderer, SWHFilterRenderer): """Renderer for application/yaml. Orchestrate from python data structure to yaml. """ media_type = 'application/yaml' def render(self, data, media_type, **options): data = self.filter_by_fields(data) return yaml.dump(data, encoding=self.charset) -class JSONPRenderer(renderers.JSONRenderer, SWHFilterRenderer): +class JSONPEnricher(): + """JSONP rendering. + + Defines a jsonp function that extracts a potential 'callback' + request parameter holding the function name and wraps the data + inside a call to such function + + e.g: + GET /blah/foo/bar renders: {'output': 'wrapped'} + GET /blah/foo/bar?callback=fn renders: fn({'output': 'wrapped'}) + + """ + def enrich(self, data): + jsonp = request.args.get('callback') + if jsonp: + return '%s(%s)' % (jsonp, data) + + return data + + +class SWHJSONRenderer(renderers.JSONRenderer, + SWHFilterRenderer, + JSONPEnricher): """Renderer for application/json. Serializes in json the data and returns it. Also deals with jsonp. If callback is found in request parameter, wrap the result as a function with name the value of the parameter query 'callback'. """ def render(self, data, media_type, **options): data = self.filter_by_fields(data) res = super().render(data, media_type, **options) - jsonp = request.args.get('callback') - if jsonp: - return '%s(%s)' % (jsonp, res) - return res + return self.enrich(res) RENDERERS = [ - 'swh.web.ui.renderers.JSONPRenderer', + 'swh.web.ui.renderers.SWHJSONRenderer', 'flask.ext.api.renderers.BrowsableAPIRenderer', 'flask.ext.api.parsers.URLEncodedParser', 'swh.web.ui.renderers.YAMLRenderer', 'swh.web.ui.renderers.PlainRenderer', ] RENDERERS_INSTANCE = [ - JSONPRenderer(), + SWHJSONRenderer(), renderers.BrowsableAPIRenderer(), parsers.URLEncodedParser(), YAMLRenderer(), ] RENDERERS_BY_TYPE = { r.media_type: r for r in RENDERERS_INSTANCE } def error_response(default_error_msg, error_code, error): """Private function to create a custom error response. """ # if nothing is requested by client, use json default_application_type = 'application/json' accept_type = request.headers.get('Accept', default_application_type) renderer = RENDERERS_BY_TYPE.get( accept_type, RENDERERS_BY_TYPE[default_application_type]) # for edge cases, use the elected renderer's media type accept_type = renderer.media_type response = make_response(default_error_msg, error_code) response.headers['Content-Type'] = accept_type response.data = renderer.render({"error": str(error)}, media_type=MediaType(accept_type), status=error_code, headers={'Content-Type': accept_type}) return response diff --git a/swh/web/ui/tests/test_renderers.py b/swh/web/ui/tests/test_renderers.py index 931cc166..21ce7b7c 100644 --- a/swh/web/ui/tests/test_renderers.py +++ b/swh/web/ui/tests/test_renderers.py @@ -1,164 +1,190 @@ # Copyright (C) 2015 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information import json import unittest import yaml from flask_api.mediatypes import MediaType from nose.tools import istest from unittest.mock import patch from swh.web.ui import renderers class RendererTestCase(unittest.TestCase): @patch('swh.web.ui.renderers.request') @istest def SWHFilterRenderer_do_nothing(self, mock_request): # given mock_request.args = {} swhFilterRenderer = renderers.SWHFilterRenderer() input_data = {'a': 'some-data'} # when actual_data = swhFilterRenderer.filter_by_fields(input_data) # then self.assertEquals(actual_data, input_data) @patch('swh.web.ui.renderers.utils') @patch('swh.web.ui.renderers.request') @istest def SWHFilterRenderer_do_filter(self, mock_request, mock_utils): # given mock_request.args = {'fields': 'a,c'} mock_utils.filter_field_keys.return_value = {'a': 'some-data'} swhFilterRenderer = renderers.SWHFilterRenderer() input_data = {'a': 'some-data', 'b': 'some-other-data'} # when actual_data = swhFilterRenderer.filter_by_fields(input_data) # then self.assertEquals(actual_data, {'a': 'some-data'}) mock_utils.filter_field_keys.assert_called_once_with(input_data, {'a', 'c'}) @istest def plainRenderer(self): # given plainRenderer = renderers.PlainRenderer() input_data = 'some data' # when actual_data = plainRenderer.render(input_data, 'some-media-type') # then self.assertEqual(actual_data, input_data) # do nothing on data @patch('swh.web.ui.renderers.request') @istest def yamlRenderer_without_filter(self, mock_request): # given mock_request.args = {} yamlRenderer = renderers.YAMLRenderer() input_data = {'target': 'sha1-dir', 'type': 'dir', 'dir-id': 'dir-id-sha1-git'} expected_data = input_data # when actual_data = yamlRenderer.render(input_data, 'application/yaml') # then self.assertEqual(yaml.load(actual_data), expected_data) @patch('swh.web.ui.renderers.request') @istest def yamlRenderer(self, mock_request): # given mock_request.args = {'fields': 'type,target'} yamlRenderer = renderers.YAMLRenderer() input_data = {'target': 'sha1-dir', 'type': 'dir', 'dir-id': 'dir-id-sha1-git'} expected_data = {'target': 'sha1-dir', 'type': 'dir'} # when actual_data = yamlRenderer.render(input_data, 'application/yaml') # then self.assertEqual(yaml.load(actual_data), expected_data) @patch('swh.web.ui.renderers.request') @istest def jsonRenderer_basic(self, mock_request): # given mock_request.args = {} - jsonRenderer = renderers.JSONPRenderer() + jsonRenderer = renderers.SWHJSONRenderer() input_data = {'target': 'sha1-dir', 'type': 'dir', 'dir-id': 'dir-id-sha1-git'} expected_data = input_data # when actual_data = jsonRenderer.render(input_data, MediaType( 'application/json')) # then self.assertEqual(json.loads(actual_data), expected_data) @patch('swh.web.ui.renderers.request') @istest def jsonRenderer_basic_with_filter(self, mock_request): # given mock_request.args = {'fields': 'target'} - jsonRenderer = renderers.JSONPRenderer() + jsonRenderer = renderers.SWHJSONRenderer() input_data = {'target': 'sha1-dir', 'type': 'dir', 'dir-id': 'dir-id-sha1-git'} expected_data = {'target': 'sha1-dir'} # when actual_data = jsonRenderer.render(input_data, MediaType( 'application/json')) # then self.assertEqual(json.loads(actual_data), expected_data) @patch('swh.web.ui.renderers.request') @istest def jsonRenderer_basic_with_filter_and_jsonp(self, mock_request): # given mock_request.args = {'fields': 'target', 'callback': 'jsonpfn'} - jsonRenderer = renderers.JSONPRenderer() + jsonRenderer = renderers.SWHJSONRenderer() input_data = {'target': 'sha1-dir', 'type': 'dir', 'dir-id': 'dir-id-sha1-git'} # when actual_data = jsonRenderer.render(input_data, MediaType( 'application/json')) # then self.assertEqual(actual_data, 'jsonpfn({"target": "sha1-dir"})') + + @patch('swh.web.ui.renderers.request') + @istest + def jsonpEnricher_basic_with_filter_and_jsonp(self, mock_request): + # given + mock_request.args = {'callback': 'jsonpfn'} + jsonpEnricher = renderers.JSONPEnricher() + + # when + actual_output = jsonpEnricher.enrich({'output': 'test'}) + + # then + self.assertEqual(actual_output, "jsonpfn({'output': 'test'})") + + @patch('swh.web.ui.renderers.request') + @istest + def jsonpEnricher_do_nothing(self, mock_request): + # given + mock_request.args = {} + jsonpEnricher = renderers.JSONPEnricher() + + # when + actual_output = jsonpEnricher.enrich({'output': 'test'}) + + # then + self.assertEqual(actual_output, {'output': 'test'})