def test_get_example_values_get_http_method_with_list_and_dict_vals(self):
        """
        This test verifies if the route we are generating example values for
        is a GET endpoint and the example values are lists or dicts, that we
        json.dumps them when they are returned.  If the HTTP method is any
        other method, they will be returned as normal lists or dicts.
        """
        def mock_logic(e: ExampleArray, f: ExampleObject):
            pass

        mock_logic = add_doctor_attrs(mock_logic)
        route = Rule('/foo/bar/')

        annotation = ResourceAnnotation(mock_logic, 'GET')
        harness = base.BaseHarness('http://foo/bar/')
        example_values = harness._get_example_values(route, annotation)
        expected = {
            'e': json.dumps(['ex', 'array']),
            'f': json.dumps({'str': 'ex str'}),
        }
        assert expected == example_values

        # Change the http method to something other than GET, they should
        # not be json dumped.
        annotation.http_method = 'POST'
        example_values = harness._get_example_values(route, annotation)
        expected = {
            'e': ['ex', 'array'],
            'f': {
                'str': 'ex str'
            },
        }
        assert expected == example_values
    def test_get_example_values_when_logic_defines_req_obj_type(self):
        """
        This tests that we generate example values appropriately when the
        route defineds a req_obj_type which will pass all request params as
        that object instance to the logic function.

        If a req_obj_type was not defined for the logic, it would expect
        the json body to look like:

        {
            "foo": {
                "foo": "foo",
                "foo_id": 1
            }
        }

        Defining a req_obj_type tells the code that the request body should
        contain those attributes rather than a sub-key within the request.
        """
        def mock_logic(foo: FooInstance):
            pass

        mock_logic = add_doctor_attrs(mock_logic, req_obj_type=FooInstance)
        route = Rule('/foo/bar/')

        annotation = ResourceAnnotation(mock_logic, 'POST')
        harness = base.BaseHarness('http://foo/bar/')
        example_values = harness._get_example_values(route, annotation)
        expected = {
            'foo': 'foo',
            'foo_id': 1,
        }
        assert expected == example_values
 def test_get_annotation_heading_doctor_heading(self):
     """
     This test verifies we use the _doctor_heading attribute of the
     handler if it is present.
     """
     handler = mock.Mock(_doctor_heading='Test Title')
     route = '^foo/?$'
     harness = base.BaseHarness('http://foo/')
     actual = harness._get_annotation_heading(handler, route)
     assert 'Test Title' == actual
    def test_get_annotation_heading_class_path_internal(self):
        """
        This test verifies the path where the class path has the resource
        name in it and it's an internal route.
        """
        handler = mock.Mock(spec_set=base.BaseHarness)
        handler.__str__ = mock.Mock(
            return_value='<class api.foo_bar.InternalFooBarListHandler>')
        route = '^internal/r/foo_bar/?$'
        harness = base.BaseHarness('http://foo/')
        actual = harness._get_annotation_heading(handler, route)

        expected = 'Foo Bar (Internal)'
        assert expected == actual
    def test_get_annotation_heading_class_name_only(self):
        """
        This test verifies that if our handler has no path and is just
        the class name that we get the heading from the name.
        e.g. class <FooBarListHandler> becomes `Foo Bar`
        """
        handler = mock.Mock(spec_set=base.BaseHarness)
        handler.__str__ = mock.Mock(return_value='<class FooBarListHandler>')
        route = '^foo_bar/?$'

        harness = base.BaseHarness('http://foo/')
        actual = harness._get_annotation_heading(handler, route)

        expected = 'Foo Bar'
        assert expected == actual
    def test_get_annotation_heading_class_path(self):
        """
        This test verifies if the class path has a resource name in it,
        that we use it for the heading.
        e.g. class <api.handlers.foo_bar.FooListHandler> becomes `Foo Bar`
        """
        handler = mock.Mock(spec_set=base.BaseHarness)
        handler.__str__ = mock.Mock(
            return_value='<class api.foo_bar.FooBarListHandler>')
        route = '^foo_bar/?$'
        harness = base.BaseHarness('http://foo/')
        actual = harness._get_annotation_heading(handler, route)

        expected = 'Foo Bar'
        assert expected == actual
    def test_get_annotation_heading_class_name_only_internal(self, mock_has):
        """
        This test verifies the path where our handler has no path and just
        the class name and that class is internal.
        """
        mock_has.return_value = False
        handler = mock.MagicMock(spec_set=base.BaseHarness)
        handler.__str__ = mock.Mock(
            return_value='<class InternalFooBarListHandler>')
        route = '^/internal/foo_bar/?$'

        harness = base.BaseHarness('http://foo/')
        actual = harness._get_annotation_heading(handler, route)

        expected = 'Foo Bar (Internal)'
        assert expected == actual
    def test_get_annotation_heading_generic_handlers(self):
        """
        This test verifies if our handlers are not in their own resource
        modules that we get the heading from the handler class name.
        e.g. class <api.handlers.handlers.FooBarListHandler> becomes `Foo Bar`
        """
        handler = mock.Mock(spec_set=base.BaseHarness)
        handler.__str__ = mock.Mock(
            return_value='<class api.handlers.FooBarListHandler>')
        route = '^foo_bar/?$'

        harness = base.BaseHarness('http://foo/')
        actual = harness._get_annotation_heading(handler, route)

        expected = 'Foo Bar'
        assert expected == actual
 def test_init(self):
     harness = base.BaseHarness('http://foo/')
     assert harness.url_prefix == 'http://foo'