def test_func__input_files_doc__ok__one_file_and_text(self): hapic = Hapic() hapic.set_context(MyContext()) app = bottle.Bottle() class MySchema(marshmallow.Schema): name = marshmallow.fields.String(required=True) class MyFilesSchema(marshmallow.Schema): file_abc = marshmallow.fields.Raw(required=True) @hapic.with_api_doc() @hapic.input_files(MyFilesSchema()) @hapic.input_body(MySchema()) def my_controller(hapic_data=None): assert hapic_data assert hapic_data.files app.route('/upload', method='POST', callback=my_controller) doc = hapic.generate_doc(app) assert doc assert '/upload' in doc['paths'] assert 'consume' in doc['paths']['/upload']['post'] assert 'multipart/form-data' in doc['paths']['/upload']['post'][ 'consume'] assert 'parameters' in doc['paths']['/upload']['post'] assert { 'name': 'file_abc', 'required': True, 'in': 'formData', 'type': 'file', } in doc['paths']['/upload']['post']['parameters']
def test_unit__exception_handled__ok__exception_error_dict(self): class MyException(Exception): def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) self.error_dict = {} context = MyContext() wrapper = ExceptionHandlerControllerWrapper( MyException, context, schema=ErrorResponseSchema(), http_code=HTTPStatus.INTERNAL_SERVER_ERROR, ) @wrapper.get_wrapper def func(foo): exc = MyException('We are testing') exc.error_detail = {'foo': 'bar'} raise exc response = func(42) assert 'http_code' in response assert response['http_code'] == HTTPStatus.INTERNAL_SERVER_ERROR assert 'original_response' in response assert response['original_response'] == { 'message': 'We are testing', 'code': None, 'detail': { 'foo': 'bar' }, }
def test_unit__base_controller_wrapper__ok__no_behaviour(self): context = MyContext() processor = MyProcessor() wrapper = InputOutputControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo): return foo result = func(42) assert result == 42
def test_unit__base_controller__ok__replaced_response(self): context = MyContext() processor = MyProcessor() wrapper = MyControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo): return foo # see MyControllerWrapper#before_wrapped_func result = func(666) # result have been replaced by MyControllerWrapper#before_wrapped_func assert {'error_response': 'we are testing'} == result
def test_unit__controller_wrapper__ok__overload_input(self): context = MyContext() processor = MyProcessor() wrapper = MyControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo, added_parameter=None): # see MyControllerWrapper#before_wrapped_func assert added_parameter == 'a value' return foo result = func(42) # See MyControllerWrapper#after_wrapped_function assert result == 84
def test_unit__output_data_wrapping__ok__nominal_case(self): context = MyContext() processor = MyProcessor() wrapper = OutputControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo, hapic_data=None): # If no use of input wrapper, no hapic_data is given assert not hapic_data return foo result = func(42) # see MyProcessor#process assert { 'http_code': HTTPStatus.OK, 'original_response': 43, } == result
def test_unit__input_data_wrapping__ok__nominal_case(self): context = MyContext(fake_query_parameters={ 'foo': 'bar', }) processor = MyProcessor() wrapper = MyInputQueryControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo, hapic_data=None): assert hapic_data assert isinstance(hapic_data, HapicData) # see MyControllerWrapper#before_wrapped_func assert hapic_data.query == {'foo': 'bar'} return foo result = func(42) assert result == 42
def test_func__output_file_doc__ok__nominal_case(self): hapic = Hapic() hapic.set_context(MyContext()) app = bottle.Bottle() @hapic.with_api_doc() @hapic.output_file('image/jpeg') def my_controller(): return b'101010100101' app.route('/avatar', method='GET', callback=my_controller) doc = hapic.generate_doc(app) assert doc assert '/avatar' in doc['paths'] assert 'produce' in doc['paths']['/avatar']['get'] assert 'image/jpeg' in doc['paths']['/avatar']['get']['produce'] assert 200 in doc['paths']['/avatar']['get']['responses']
def test_unit__input_files__ok__file_is_present(self): hapic = Hapic() hapic.set_context( MyContext(fake_files_parameters={ 'file_abc': '10101010101', })) class MySchema(marshmallow.Schema): file_abc = marshmallow.fields.Raw(required=True) @hapic.with_api_doc() @hapic.input_files(MySchema()) def my_controller(hapic_data=None): assert hapic_data assert hapic_data.files return 'OK' result = my_controller() assert 'OK' == result
def test_unit__output_data_wrapping__fail__error_response(self): context = MyContext() processor = MarshmallowOutputProcessor() processor.schema = MySchema() wrapper = OutputControllerWrapper(context, processor) @wrapper.get_wrapper def func(foo): return 'wrong result format' result = func(42) # see MyProcessor#process assert isinstance(result, dict) assert 'http_code' in result assert result['http_code'] == HTTPStatus.INTERNAL_SERVER_ERROR assert 'original_error' in result assert result['original_error'].details == { 'name': ['Missing data for required field.'] }
def test_unit__input_files__ok__file_is_empty_string(self): hapic = Hapic() hapic.set_context(MyContext(fake_files_parameters={ 'file_abc': '', })) class MySchema(marshmallow.Schema): file_abc = marshmallow.fields.Raw(required=True) @hapic.with_api_doc() @hapic.input_files(MySchema()) def my_controller(hapic_data=None): assert hapic_data assert hapic_data.files return 'OK' result = my_controller() assert 'http_code' in result assert HTTPStatus.BAD_REQUEST == result['http_code'] assert { 'file_abc': ['Missing data for required field'] } == result['original_error'].details
def test_unit__exception_handled__ok__nominal_case(self): context = MyContext() wrapper = ExceptionHandlerControllerWrapper( ZeroDivisionError, context, schema=ErrorResponseSchema(), http_code=HTTPStatus.INTERNAL_SERVER_ERROR, ) @wrapper.get_wrapper def func(foo): raise ZeroDivisionError('We are testing') response = func(42) assert 'http_code' in response assert response['http_code'] == HTTPStatus.INTERNAL_SERVER_ERROR assert 'original_response' in response assert response['original_response'] == { 'message': 'We are testing', 'code': None, 'detail': {}, }