def test_success(self):
        """
        Succeessful responses capture status code.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_response(
                MagicMock(
                    data="{}",
                    status_code=201,
                ))
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(
                    equal_to(
                        dict(
                            operation="test_func",
                            method="GET",
                            func="test_func",
                            success=True,
                            status_code=201,
                        ))),
            )
    def test_error(self):
        """
        Errors responses capture the error

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            try:
                raise NotFound("Not Found")
            except Exception as error:
                request_info.capture_error(error)

            dct = request_info.to_dict()
            # NB: stack trace is hard (and pointless) to compare
            stack_trace = dct.pop("stack_trace")
            assert_that(stack_trace, is_not(none()))
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    success=False,
                    status_code=404,
                    message="Not Found",
                    context=dict(errors=[]),
                ))),
            )
    def test_error(self):
        """
        Errors responses capture the error

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            try:
                raise NotFound("Not Found")
            except Exception as error:
                request_info.capture_error(error)

            dct = request_info.to_dict()
            # NB: stack trace is hard (and pointless) to compare
            stack_trace = dct.pop("stack_trace")
            assert_that(stack_trace, is_not(none()))
            assert_that(
                dct,
                is_(
                    equal_to(
                        dict(
                            operation="test_func",
                            method="GET",
                            func="test_func",
                            success=False,
                            status_code=404,
                            message="Not Found",
                            context=dict(errors=[]),
                        ))),
            )
    def test_response_body_with_field_renaming(self):
        """
        Can capture the response body with field renaming

        """
        with self.graph.flask.test_request_context("/"):
            g.show_response_fields = dict(foo="baz")

            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_response(MagicMock(
                data='{"foo": "bar"}',
                status_code=200,
            ))
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    success=True,
                    status_code=200,
                    response_body=dict(baz="bar"),
                ))),
            )
    def test_response_body_with_field_renaming(self):
        """
        Can capture the response body with field renaming

        """
        with self.graph.flask.test_request_context("/"):
            g.show_response_fields = dict(foo="baz")

            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_response(
                MagicMock(
                    data='{"foo": "bar"}',
                    status_code=200,
                ))
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(
                    equal_to(
                        dict(
                            operation="test_func",
                            method="GET",
                            func="test_func",
                            success=True,
                            status_code=200,
                            response_body=dict(baz="bar"),
                        ))),
            )
    def test_response_body_with_field_deletion(self):
        """
        Can capture the response body with fields removed

        """
        with self.graph.flask.test_request_context("/"):
            g.hide_response_fields = ["foo"]

            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_response(MagicMock(
                data='{"foo": "bar", "this": "that"}',
                status_code=200,
            ))
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    success=True,
                    status_code=200,
                    response_body=dict(this="that"),
                ))),
            )
    def test_log_debug(self):
        """
        Log at DEBUG when configured to.

        """
        debug_options = AuditOptions(
            include_request_body=True,
            include_response_body=True,
            include_path=True,
            include_query_string=True,
            log_as_debug=True,
        )

        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(debug_options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.debug.assert_called_with(
                dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",
                ))
            logger.info.assert_not_called()
            logger.warning.assert_not_called()
    def test_response_body_with_field_deletion(self):
        """
        Can capture the response body with fields removed

        """
        with self.graph.flask.test_request_context("/"):
            g.hide_response_fields = ["foo"]

            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_response(
                MagicMock(
                    data='{"foo": "bar", "this": "that"}',
                    status_code=200,
                ))
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(
                    equal_to(
                        dict(
                            operation="test_func",
                            method="GET",
                            func="test_func",
                            success=True,
                            status_code=200,
                            response_body=dict(this="that"),
                        ))),
            )
    def test_log_path(self):
        with self.graph.flask.test_request_context("/bar"):
            request_info = RequestInfo(self.options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
                foo="bar",
            ))
Exemple #10
0
    def test_log_path(self):
        with self.graph.flask.test_request_context("/bar"):
            request_info = RequestInfo(self.options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
                foo="bar",
            ))
    def test_log_response_id_header(self):
        new_id = str(uuid4())
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.response_headers = {"X-FooBar-Id": new_id}

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
                foo_bar_id=new_id,
            ))
    def test_log_query_string(self):
        ref_id = str(uuid4())

        with self.graph.flask.test_request_context("/", query_string=dict(foo=ref_id)):
            request_info = RequestInfo(self.options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
                foo=ref_id
            ))
    def test_log_query_string(self):
        ref_id = str(uuid4())

        with self.graph.flask.test_request_context(
                "/", query_string=dict(foo=ref_id)):
            request_info = RequestInfo(self.options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(
                dict(operation="test_func",
                     method="GET",
                     func="test_func",
                     foo=ref_id))
Exemple #14
0
    def test_log_response_id_header(self):
        new_id = str(uuid4())
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.response_headers = {"X-FooBar-Id": new_id}

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
                foo_bar_id=new_id,
            ))
Exemple #15
0
    def test_base_info(self):
        """
        Every log entry identifies the request.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",
                ))),
            )
    def test_log_default(self):
        """
        Log at INFO by default.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
            ))
            logger.warning.assert_not_called()
Exemple #17
0
    def test_log_default(self):
        """
        Log at INFO by default.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)

            logger = MagicMock()
            request_info.log(logger)
            logger.info.assert_called_with(dict(
                operation="test_func",
                method="GET",
                func="test_func",
            ))
            logger.warning.assert_not_called()
    def test_base_info(self):
        """
        Every log entry identifies the request.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",
                ))),
            )
Exemple #19
0
    def test_request_context(self):
        """
        Log entries can include context from headers.

        """
        with self.graph.flask.test_request_context("/", headers={"X-Request-Id": "request-id"}):
            request_info = RequestInfo(self.options, test_func, self.graph.request_context)
            dct = request_info.to_dict()
            request_id = dct.pop("X-Request-Id")
            assert_that(request_id, is_(equal_to("request-id")))
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",
                ))),
            )
    def test_request_context(self):
        """
        Log entries can include context from headers.

        """
        with self.graph.flask.test_request_context("/", headers={"X-Request-Id": "request-id"}):
            request_info = RequestInfo(self.options, test_func, self.graph.request_context)
            dct = request_info.to_dict()
            request_id = dct.pop("X-Request-Id")
            assert_that(request_id, is_(equal_to("request-id")))
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",
                ))),
            )
Exemple #21
0
    def test_log_internal_server_error(self):
        """
        Log at WARNING on internal server error.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.status_code = 500

            logger = MagicMock()
            request_info.log(logger)
            logger.warning.assert_called_with(
                dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",
                ))
            logger.info.assert_not_called()
Exemple #22
0
    def test_request_body(self):
        """
        Can capture the request body.

        """
        with self.graph.flask.test_request_context("/", data='{"foo": "bar"}'):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_request()
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    request_body=dict(foo="bar"),
                ))),
            )
    def test_request_body(self):
        """
        Can capture the request body.

        """
        with self.graph.flask.test_request_context("/", data='{"foo": "bar"}'):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_request()
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    request_body=dict(foo="bar"),
                ))),
            )
Exemple #24
0
    def test_request_body_with_field_deletion(self):
        """
        Can capture the request body with fields removed

        """
        with self.graph.flask.test_request_context("/", data='{"foo": "bar", "this": "that"}'):
            g.hide_request_fields = ["foo"]

            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_request()
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    request_body=dict(this="that"),
                ))),
            )
    def test_request_body_with_field_deletion(self):
        """
        Can capture the request body with fields removed

        """
        with self.graph.flask.test_request_context("/", data='{"foo": "bar", "this": "that"}'):
            g.hide_request_fields = ["foo"]

            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_request()
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    request_body=dict(this="that"),
                ))),
            )
    def test_success(self):
        """
        Succeessful responses capture status code.

        """
        with self.graph.flask.test_request_context("/"):
            request_info = RequestInfo(self.options, test_func, None)
            request_info.capture_response(MagicMock(
                data="{}",
                status_code=201,
            ))
            dct = request_info.to_dict()
            assert_that(
                dct,
                is_(equal_to(dict(
                    operation="test_func",
                    method="GET",
                    func="test_func",

                    success=True,
                    status_code=201,
                ))),
            )