Exemplo n.º 1
0
    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,
                        ))),
            )
Exemplo n.º 2
0
    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"),
                        ))),
            )
Exemplo n.º 3
0
    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"),
                        ))),
            )
Exemplo n.º 4
0
    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"),
                ))),
            )
Exemplo n.º 5
0
    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=[]),
                        ))),
            )
Exemplo n.º 6
0
    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"),
                ))),
            )
Exemplo n.º 7
0
    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=[]),
                ))),
            )
Exemplo n.º 8
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",
                ))),
            )
Exemplo n.º 9
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",
                ))),
            )
Exemplo n.º 10
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",
                ))),
            )
Exemplo n.º 11
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",
                ))),
            )
Exemplo n.º 12
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"),
                ))),
            )
Exemplo n.º 13
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"),
                ))),
            )
Exemplo n.º 14
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"),
                ))),
            )
Exemplo n.º 15
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"),
                ))),
            )
Exemplo n.º 16
0
    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,
                ))),
            )