def test_get_swagger_spec_passes_absolute_url(mock_spec, mock_simple, mock_abs, mock_open): get_swagger_spec({'pyramid_swagger.schema_directory': 'foo/'}) mock_abs.assert_called_once_with('foo/swagger.json') expected_url = "file:///bar/foo/swagger.json" mock_spec.assert_called_once_with(mock.ANY, config=mock.ANY, origin_url=expected_url)
def test_get_swagger_spec_passes_absolute_url( mock_spec, mock_abs, mock_http_handlers, ): get_swagger_spec({'pyramid_swagger.schema_directory': 'foo/'}) mock_abs.assert_called_once_with('foo/swagger.json') expected_url = "file:///bar/foo/swagger.json" mock_spec.assert_called_once_with(mock.ANY, config=mock.ANY, origin_url=expected_url)
def _validate_against_tween(request, response=None, path_pattern='/', **overrides): """ Acceptance testing helper for testing the swagger tween with Swagger 2.0 responses. :param request: pyramid request :param response: standard fixture by default :param path_pattern: Path pattern eg. /foo/{bar} :param overrides: dict of overrides for `pyramid_swagger` config """ def handler(request): return response or Response() settings = dict( { 'pyramid_swagger.schema_directory': 'tests/sample_schemas/good_app/', 'pyramid_swagger.enable_swagger_spec_validation': False, 'pyramid_swagger.enable_response_validation': True, 'pyramid_swagger.swagger_versions': ['2.0'] }, **overrides) spec = get_swagger_spec(settings) settings['pyramid_swagger.schema12'] = None settings['pyramid_swagger.schema20'] = spec registry = get_registry(settings) # This is a little messy because the current flow of execution doesn't # set up the route_info in pyramid. Have to mock out the `route_info` # so that usages in the tween meet expectations. Holler if you know a # better way to do this! op = spec.get_op_for_request(request.method, path_pattern) mock_route_info = {'match': request.matchdict, 'route': None} mock_route_mapper = Mock(spec=IRoutesMapper, return_value=mock_route_info) with patch('pyramid_swagger.tween.get_op_for_request', return_value=op): with patch('pyramid.registry.Registry.queryUtility', return_value=mock_route_mapper): validation_tween_factory(handler, registry)(request)
def includeme(config): """ :type config: :class:`pyramid.config.Configurator` """ settings = config.registry.settings swagger_versions = get_swagger_versions(settings) # for rendering /swagger.yaml config.add_renderer( 'yaml', 'pyramid_swagger.api.YamlRendererFactory', ) # Add the SwaggerSchema to settings to make it available to the validation # tween and `register_api_doc_endpoints` settings['pyramid_swagger.schema12'] = None settings['pyramid_swagger.schema20'] = None # Store under two keys so that 1.2 and 2.0 can co-exist. if SWAGGER_12 in swagger_versions: settings['pyramid_swagger.schema12'] = get_swagger_schema(settings) if SWAGGER_20 in swagger_versions: settings['pyramid_swagger.schema20'] = get_swagger_spec(settings) config.add_tween("pyramid_swagger.tween.validation_tween_factory", under=pyramid.tweens.EXCVIEW) config.add_renderer('pyramid_swagger', PyramidSwaggerRendererFactory()) if settings.get('pyramid_swagger.enable_api_doc_views', True): if SWAGGER_12 in swagger_versions: register_api_doc_endpoints( config, settings['pyramid_swagger.schema12'].get_api_doc_endpoints()) if SWAGGER_20 in swagger_versions: register_api_doc_endpoints( config, build_swagger_20_swagger_schema_views(config), base_path=settings.get('pyramid_swagger.base_path_api_docs', ''))
def includeme(config): """ :type config: :class:`pyramid.config.Configurator` """ settings = config.registry.settings swagger_versions = get_swagger_versions(settings) # for rendering /swagger.yaml config.add_renderer( 'yaml', 'pyramid_swagger.api.YamlRendererFactory', ) # Add the SwaggerSchema to settings to make it available to the validation # tween and `register_api_doc_endpoints` settings['pyramid_swagger.schema12'] = None settings['pyramid_swagger.schema20'] = None # Store under two keys so that 1.2 and 2.0 can co-exist. if SWAGGER_12 in swagger_versions: settings['pyramid_swagger.schema12'] = get_swagger_schema(settings) if SWAGGER_20 in swagger_versions: settings['pyramid_swagger.schema20'] = get_swagger_spec(settings) config.add_tween( "pyramid_swagger.tween.validation_tween_factory", under=pyramid.tweens.EXCVIEW ) config.add_renderer('pyramid_swagger', PyramidSwaggerRendererFactory()) if settings.get('pyramid_swagger.enable_api_doc_views', True): if SWAGGER_12 in swagger_versions: register_api_doc_endpoints( config, settings['pyramid_swagger.schema12'].get_api_doc_endpoints()) if SWAGGER_20 in swagger_versions: register_api_doc_endpoints( config, build_swagger_20_swagger_schema_views(config), base_path=settings.get('pyramid_swagger.base_path_api_docs', ''))
def _validate_against_tween(request, response=None, path_pattern='/', **overrides): """ Acceptance testing helper for testing the swagger tween with Swagger 2.0 responses. :param request: pyramid request :param response: standard fixture by default :param path_pattern: Path pattern eg. /foo/{bar} :param overrides: dict of overrides for `pyramid_swagger` config """ def handler(request): return response or Response() settings = dict({ 'pyramid_swagger.schema_directory': 'tests/sample_schemas/good_app/', 'pyramid_swagger.enable_swagger_spec_validation': False, 'pyramid_swagger.enable_response_validation': True, 'pyramid_swagger.swagger_versions': ['2.0']}, **overrides ) spec = get_swagger_spec(settings) settings['pyramid_swagger.schema12'] = None settings['pyramid_swagger.schema20'] = spec registry = get_registry(settings) # This is a little messy because the current flow of execution doesn't # set up the route_info in pyramid. Have to mock out the `route_info` # so that usages in the tween meet expectations. Holler if you know a # better way to do this! op = spec.get_op_for_request(request.method, path_pattern) mock_route_info = {'match': request.matchdict, 'route': None} mock_route_mapper = Mock(spec=IRoutesMapper, return_value=mock_route_info) with patch('pyramid_swagger.tween.get_op_for_request', return_value=op): with patch('pyramid.registry.Registry.queryUtility', return_value=mock_route_mapper): validation_tween_factory(handler, registry)(request)