def test__add_parameters_happy(self, mocker):
        from openapi_python_client.openapi_parser.openapi import Endpoint
        from openapi_python_client.openapi_parser.properties import Property

        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )
        path_prop = mocker.MagicMock(autospec=Property)
        path_prop_import = mocker.MagicMock()
        path_prop.get_imports = mocker.MagicMock(
            return_value={path_prop_import})
        query_prop = mocker.MagicMock(autospec=Property)
        query_prop_import = mocker.MagicMock()
        query_prop.get_imports = mocker.MagicMock(
            return_value={query_prop_import})
        property_from_dict = mocker.patch(f"{MODULE_NAME}.property_from_dict",
                                          side_effect=[path_prop, query_prop])
        path_schema = mocker.MagicMock()
        query_schema = mocker.MagicMock()
        data = {
            "parameters": [
                {
                    "name": "path_prop_name",
                    "required": True,
                    "schema": path_schema,
                    "in": "path"
                },
                {
                    "name": "query_prop_name",
                    "required": False,
                    "schema": query_schema,
                    "in": "query"
                },
            ]
        }

        endpoint._add_parameters(data)

        property_from_dict.assert_has_calls([
            mocker.call(name="path_prop_name", required=True,
                        data=path_schema),
            mocker.call(name="query_prop_name",
                        required=False,
                        data=query_schema),
        ])
        path_prop.get_imports.assert_called_once_with(prefix="..models")
        query_prop.get_imports.assert_called_once_with(prefix="..models")
        assert endpoint.relative_imports == {
            "import_3",
            path_prop_import,
            query_prop_import,
        }
        assert endpoint.path_parameters == [path_prop]
        assert endpoint.query_parameters == [query_prop]
    def test__add_parameters_handles_no_params(self):
        from openapi_python_client.openapi_parser.openapi import Endpoint

        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
        )
        endpoint._add_parameters({})  # Just checking there's no exception here
    def test_add_body_happy(self, mocker):
        from openapi_python_client.openapi_parser.openapi import Endpoint, Reference
        from openapi_python_client.openapi_parser.properties import Property

        request_body = mocker.MagicMock()
        form_body_reference = Reference.from_ref(ref="a")
        multipart_body_reference = Reference.from_ref(ref="b")
        parse_request_form_body = mocker.patch.object(
            Endpoint,
            "parse_request_form_body",
            return_value=form_body_reference)
        parse_multipart_body = mocker.patch.object(
            Endpoint,
            "parse_multipart_body",
            return_value=multipart_body_reference)

        json_body = mocker.MagicMock(autospec=Property)
        json_body_imports = mocker.MagicMock()
        json_body.get_imports.return_value = {json_body_imports}
        parse_request_json_body = mocker.patch.object(
            Endpoint, "parse_request_json_body", return_value=json_body)
        import_string_from_reference = mocker.patch(
            f"{MODULE_NAME}.import_string_from_reference",
            side_effect=["import_1", "import_2"])

        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )

        endpoint._add_body({"requestBody": request_body})

        parse_request_form_body.assert_called_once_with(request_body)
        parse_request_json_body.assert_called_once_with(request_body)
        parse_multipart_body.assert_called_once_with(request_body)
        import_string_from_reference.assert_has_calls([
            mocker.call(form_body_reference, prefix="..models"),
            mocker.call(multipart_body_reference, prefix="..models"),
        ])
        json_body.get_imports.assert_called_once_with(prefix="..models")
        assert endpoint.relative_imports == {
            "import_1", "import_2", "import_3", json_body_imports
        }
        assert endpoint.json_body == json_body
        assert endpoint.form_body_reference == form_body_reference
        assert endpoint.multipart_body_reference == multipart_body_reference
    def test_parse_request_json_body_no_data(self):
        body = {"content": {}}

        from openapi_python_client.openapi_parser.openapi import Endpoint

        result = Endpoint.parse_request_json_body(body)

        assert result is None
    def test_add_body_no_data(self, mocker):
        from openapi_python_client.openapi_parser.openapi import Endpoint

        parse_request_form_body = mocker.patch.object(
            Endpoint, "parse_request_form_body")
        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )

        endpoint._add_body({})

        parse_request_form_body.assert_not_called()
    def test__add_responses(self, mocker):
        from openapi_python_client.openapi_parser.openapi import Endpoint, RefResponse, Reference

        response_1_data = mocker.MagicMock()
        response_2_data = mocker.MagicMock()
        data = {
            "responses": {
                "200": response_1_data,
                "404": response_2_data,
            }
        }
        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
            relative_imports={"import_3"},
        )
        ref_1 = Reference.from_ref(ref="ref_1")
        ref_2 = Reference.from_ref(ref="ref_2")
        response_1 = RefResponse(status_code=200, reference=ref_1)
        response_2 = RefResponse(status_code=404, reference=ref_2)
        response_from_dict = mocker.patch(f"{MODULE_NAME}.response_from_dict",
                                          side_effect=[response_1, response_2])
        import_string_from_reference = mocker.patch(
            f"{MODULE_NAME}.import_string_from_reference",
            side_effect=["import_1", "import_2"])

        endpoint._add_responses(data)

        response_from_dict.assert_has_calls([
            mocker.call(status_code=200, data=response_1_data),
            mocker.call(status_code=404, data=response_2_data),
        ])
        import_string_from_reference.assert_has_calls([
            mocker.call(ref_1, prefix="..models"),
            mocker.call(ref_2, prefix="..models"),
        ])
        assert endpoint.responses == [response_1, response_2]
        assert endpoint.relative_imports == {
            "import_1", "import_2", "import_3"
        }
    def test_parse_multipart_body(self, mocker):
        ref = mocker.MagicMock()
        body = {"content": {"multipart/form-data": {"schema": {"$ref": ref}}}}
        from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref")

        from openapi_python_client.openapi_parser.openapi import Endpoint

        result = Endpoint.parse_multipart_body(body)

        from_ref.assert_called_once_with(ref)
        assert result == from_ref()
    def test_from_data(self, mocker):
        from openapi_python_client.openapi_parser.openapi import Endpoint

        path = mocker.MagicMock()
        method = mocker.MagicMock()
        _add_parameters = mocker.patch.object(Endpoint, "_add_parameters")
        _add_responses = mocker.patch.object(Endpoint, "_add_responses")
        _add_body = mocker.patch.object(Endpoint, "_add_body")
        data = {
            "description": mocker.MagicMock(),
            "operationId": mocker.MagicMock(),
            "security": {
                "blah": "bloo"
            },
        }

        endpoint = Endpoint.from_data(data=data,
                                      path=path,
                                      method=method,
                                      tag="default")

        assert endpoint.path == path
        assert endpoint.method == method
        assert endpoint.description == data["description"]
        assert endpoint.name == data["operationId"]
        assert endpoint.requires_security
        assert endpoint.tag == "default"
        _add_parameters.assert_called_once_with(data)
        _add_responses.assert_called_once_with(data)
        _add_body.assert_called_once_with(data)

        del data["security"]

        endpoint = Endpoint.from_data(data=data,
                                      path=path,
                                      method=method,
                                      tag="a")

        assert not endpoint.requires_security
        assert endpoint.tag == "a"
    def test_parse_request_json_body(self, mocker):
        schema = mocker.MagicMock()
        body = {"content": {"application/json": {"schema": schema}}}
        property_from_dict = mocker.patch(f"{MODULE_NAME}.property_from_dict")

        from openapi_python_client.openapi_parser.openapi import Endpoint

        result = Endpoint.parse_request_json_body(body)

        property_from_dict.assert_called_once_with("json_body",
                                                   required=True,
                                                   data=schema)
        assert result == property_from_dict()
    def test__add_parameters_fail_loudly_when_location_not_supported(
            self, mocker):
        from openapi_python_client.openapi_parser.openapi import Endpoint

        endpoint = Endpoint(
            path="path",
            method="method",
            description=None,
            name="name",
            requires_security=False,
            tag="tag",
        )
        mocker.patch(f"{MODULE_NAME}.property_from_dict")

        with pytest.raises(ValueError):
            endpoint._add_parameters({
                "parameters": [{
                    "name": "test",
                    "required": True,
                    "schema": mocker.MagicMock(),
                    "in": "cookie"
                }]
            })
    def test_parse_request_form_body(self, mocker):
        ref = mocker.MagicMock()
        body = {
            "content": {
                "application/x-www-form-urlencoded": {
                    "schema": {
                        "$ref": ref
                    }
                }
            }
        }
        from_ref = mocker.patch(f"{MODULE_NAME}.Reference.from_ref")

        from openapi_python_client.openapi_parser.openapi import Endpoint

        result = Endpoint.parse_request_form_body(body)

        from_ref.assert_called_once_with(ref)
        assert result == from_ref()