Beispiel #1
0
    def test_unit__marshmallow_input_processor__ok__process_success(self):
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())

        tested_data = {"first_name": "Alan", "last_name": "Turing"}
        data = processor.load(tested_data)

        assert data == tested_data
Beispiel #2
0
    def test_unit__marshmallow_input_processor__ok__completed_data(self):
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())

        tested_data = {"first_name": "Alan"}

        data = processor.load(tested_data)
        assert {"first_name": "Alan", "last_name": "Doe"} == data
Beispiel #3
0
 def test_unit_file_output_processor_err__wrong_type(self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = None
     with pytest.raises(ValidationException):
         processor.dump_output_file(tested_data)
Beispiel #4
0
 def test_unit_file_output_processor_err__no_path_no_object(self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = HapicFile(mimetype="image/png")
     with pytest.raises(ValidationException):
         processor.dump_output_file(tested_data)
Beispiel #5
0
 def test_unit_file_output_processor_ok__process_success_fileobject_as_stream(
         self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = HapicFile(file_object=file, mimetype="image/png")
     data = processor.dump_output_file(tested_data)
     assert data == tested_data
Beispiel #6
0
    def test_unit__exception_handler__error__error_content_malformed(self):
        class MyException(Exception):
            pass

        class MyErrorBuilder(MarshmallowDefaultErrorBuilder):
            def build_from_exception(
                self, exception: Exception, include_traceback: bool = False
            ) -> dict:
                # this is not matching with DefaultErrorBuilder schema
                return {}

        context = AgnosticContext(app=None)
        error_builder = MyErrorBuilder()
        wrapper = ExceptionHandlerControllerWrapper(
            MyException,
            context,
            error_builder=error_builder,
            processor_factory=lambda schema_: MarshmallowProcessor(error_builder.get_schema()),
        )

        def raise_it():
            raise MyException()

        wrapper = wrapper.get_wrapper(raise_it)
        with pytest.raises(OutputValidationException):
            wrapper()
Beispiel #7
0
    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 = AgnosticContext(app=None)
        error_builder = MarshmallowDefaultErrorBuilder()
        wrapper = ExceptionHandlerControllerWrapper(
            MyException,
            context,
            error_builder=error_builder,
            http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
            processor_factory=lambda schema_: MarshmallowProcessor(error_builder.get_schema()),
        )

        @wrapper.get_wrapper
        def func(foo):
            exc = MyException("We are testing")
            exc.error_detail = {"foo": "bar"}
            raise exc

        response = func(42)
        assert response.status_code == HTTPStatus.INTERNAL_SERVER_ERROR
        assert {
            "message": "We are testing",
            "details": {"error_detail": {"foo": "bar"}},
            "code": None,
        } == json.loads(response.body)
Beispiel #8
0
 def test_unit_file_output_processor_ok__process_success_fileobject_as_sized_file(
         self):
     processor = MarshmallowProcessor()
     file = BytesIO()
     image = Image.new("RGBA", size=(1000, 1000), color=(0, 0, 0))
     image.save(file, "png")
     file.name = "test_image.png"
     file.seek(0)
     tested_data = HapicFile(
         file_object=file,
         mimetype="image/png",
         content_length=file.getbuffer().nbytes,
         last_modified=datetime.utcnow(),
     )
     data = processor.dump_output_file(tested_data)
     assert data == tested_data
Beispiel #9
0
    def test_unit__output_data_wrapping__fail__error_response(self):
        context = AgnosticContext(app=None)
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())
        wrapper = OutputControllerWrapper(context, lambda: processor)

        @wrapper.get_wrapper
        def func(foo):
            return "wrong result format"

        result = func(42)
        assert HTTPStatus.INTERNAL_SERVER_ERROR == result.status_code
        assert {
            "original_error": {
                "details": {"name": ["Missing data for required field."]},
                "message": "Validation error of output data",
            },
            "http_code": 500,
        } == json.loads(result.body)
Beispiel #10
0
    def test_unit__marshmallow_output_processor__ok__missing_data(self):
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())

        tested_data = {"last_name": "Turing"}

        with pytest.raises(ValidationException):
            processor.dump(tested_data)
Beispiel #11
0
    def test_unit__marshmallow_input_processor__error__validation_error_no_data_empty_string(
            self):
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())

        # Schema will not valid it because require first_name field
        tested_data = ""

        with pytest.raises(ProcessException):
            processor.load(tested_data)

        errors = processor.get_input_validation_error(tested_data)
        assert errors.details
        assert {"_schema": ["Invalid input type."]} == errors.details
Beispiel #12
0
    def test_unit__marshmallow_input_processor__error__validation_error_no_data_none(
            self):
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())

        # Schema will not valid it because require first_name field
        tested_data = None

        with pytest.raises(ValidationException):
            processor.load(tested_data)

        errors = processor.get_input_validation_error(tested_data)
        assert errors.details
        assert "first_name" in errors.details
Beispiel #13
0
    def test_unit__marshmallow_input_processor__error__validation_error(self):
        processor = MarshmallowProcessor()
        processor.set_schema(MySchema())

        tested_data = {
            # Missing 'first_name' key
            "last_name": "Turing"
        }

        with pytest.raises(ValidationException):
            processor.load(tested_data)

        errors = processor.get_input_validation_error(tested_data)
        assert errors.details
        assert "first_name" in errors.details
Beispiel #14
0
    def test_unit__exception_handled__ok__nominal_case(self):
        context = AgnosticContext(app=None)
        error_builder = MarshmallowDefaultErrorBuilder()
        wrapper = ExceptionHandlerControllerWrapper(
            ZeroDivisionError,
            context,
            error_builder=error_builder,
            http_code=HTTPStatus.INTERNAL_SERVER_ERROR,
            processor_factory=lambda schema_: MarshmallowProcessor(error_builder.get_schema()),
        )

        @wrapper.get_wrapper
        def func(foo):
            raise ZeroDivisionError("We are testing")

        response = func(42)
        assert HTTPStatus.INTERNAL_SERVER_ERROR == response.status_code
        assert {
            "details": {"error_detail": {}},
            "message": "We are testing",
            "code": None,
        } == json.loads(response.body)
Beispiel #15
0
    def test_unit_file_output_processor_ok__process_success_filepath(self):
        processor = MarshmallowProcessor()

        tested_data = HapicFile(file_path=os.path.abspath(__file__))
        data = processor.dump_output_file(tested_data)
        assert data == tested_data
Beispiel #16
0
 def test_unit_file_output_processor_err__file_do_not_exist(self):
     processor = MarshmallowProcessor()
     tested_data = HapicFile(file_path="_____________")
     with pytest.raises(ValidationException):
         processor.dump_output_file(tested_data)