Exemple #1
0
    def test_invalid_integration(self, test_case_name, method_config, LambdaUriMock):
        LambdaUriMock.get_function_name.return_value = None

        parser = SwaggerParser(self.stack_path, {})
        result = parser._get_integration_function_name(method_config)

        self.assertIsNone(result, "must not parse invalid integration")
Exemple #2
0
    def test_with_one_path_method(self):
        function_name = "myfunction"
        swagger = {
            "paths": {
                "/path1": {
                    "get": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                }
            }
        }

        parser = SwaggerParser(swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = function_name

        expected = [Api(path="/path1", method="get", function_name=function_name, cors=None)]
        result = parser.get_apis()

        self.assertEquals(expected, result)
        parser._get_integration_function_name.assert_called_with({
            "x-amazon-apigateway-integration": {
                "type": "aws_proxy",
                "uri": "someuri"
            }
        })
Exemple #3
0
    def test_with_any_method(self):
        function_name = "myfunction"
        swagger = {
            "paths": {
                "/path1": {
                    "x-amazon-apigateway-any-method": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                }
            }
        }

        parser = SwaggerParser(swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = function_name

        expected = [
            Route(methods=["ANY"], path="/path1", function_name=function_name)
        ]
        result = parser.get_routes()

        self.assertEquals(expected, result)
Exemple #4
0
    def test_invalid_integration(self, test_case_name, method_config, LambdaUriMock):
        LambdaUriMock.get_function_name.return_value = None

        parser = SwaggerParser({})
        result = parser._get_integration_function_name(method_config)

        self.assertIsNone(result, "must not parse invalid integration")
Exemple #5
0
    def test_invalid_swagger(self, test_case_name, swagger):

        parser = SwaggerParser(swagger)
        result = parser.get_apis()

        expected = []
        self.assertEquals(expected, result)
Exemple #6
0
    def test_valid_integration(self, LambdaUriMock):
        function_name = "name"
        LambdaUriMock.get_function_name.return_value = function_name

        method_config = {"x-amazon-apigateway-integration": {"type": "aws_proxy", "uri": "someuri"}}

        parser = SwaggerParser(self.stack_path, {})
        result = parser._get_integration_function_name(method_config)

        self.assertEqual(function_name, result)
        LambdaUriMock.get_function_name.assert_called_with("someuri")
Exemple #7
0
    def test_does_not_have_function_name(self):
        swagger = {
            "paths": {"/path1": {"post": {"x-amazon-apigateway-integration": {"type": "aws_proxy", "uri": "someuri"}}}}
        }

        parser = SwaggerParser(self.stack_path, swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = None  # Function Name could not be resolved

        expected = []
        result = parser.get_routes()

        self.assertEqual(expected, result)
    def _extract_from_serverless_api(self, logical_id, api_resource,
                                     collector):
        """
        Extract APIs from AWS::Serverless::Api resource by reading and parsing Swagger documents. The result is added
        to the collector.

        Parameters
        ----------
        logical_id : str
            Logical ID of the resource

        api_resource : dict
            Resource definition, including its properties

        collector : ApiCollector
            Instance of the API collector that where we will save the API information
        """

        properties = api_resource.get("Properties", {})
        body = properties.get("DefinitionBody")
        uri = properties.get("DefinitionUri")
        binary_media = properties.get("BinaryMediaTypes", [])
        stage_name = properties.get("StageName")
        stage_variables = properties.get("Variables")

        if not body and not uri:
            # Swagger is not found anywhere.
            LOG.debug(
                "Skipping resource '%s'. Swagger document not found in DefinitionBody and DefinitionUri",
                logical_id)
            return

        reader = SamSwaggerReader(definition_body=body,
                                  definition_uri=uri,
                                  working_dir=self.cwd)
        swagger = reader.read()
        parser = SwaggerParser(swagger)
        apis = parser.get_apis()
        LOG.debug("Found '%s' APIs in resource '%s'", len(apis), logical_id)

        collector.add_apis(logical_id, apis)
        collector.add_binary_media_types(
            logical_id,
            parser.get_binary_media_types())  # Binary media from swagger
        collector.add_binary_media_types(
            logical_id,
            binary_media)  # Binary media specified on resource in template

        collector.add_stage_name(logical_id, stage_name)
        collector.add_stage_variables(logical_id, stage_variables)
Exemple #9
0
    def extract_swagger_route(
        stack_path: str,
        logical_id: str,
        body: Dict,
        uri: Union[str, Dict],
        binary_media: Optional[List],
        collector: ApiCollector,
        cwd: Optional[str] = None,
        event_type: str = Route.API,
    ) -> None:
        """
        Parse the Swagger documents and adds it to the ApiCollector.

        Parameters
        ----------
        stack_path : str
            Path of the stack the resource is located

        logical_id : str
            Logical ID of the resource
        body : dict
            The body of the RestApi
        uri : str or dict
            The url to location of the RestApi
        binary_media : list
            The link to the binary media
        collector : samcli.lib.providers.api_collector.ApiCollector
            Instance of the Route collector that where we will save the route information
        cwd : str
            Optional working directory with respect to which we will resolve relative path to Swagger file
        event_type : str
            The event type, 'Api' or 'HttpApi', see samcli/local/apigw/local_apigw_service.py:35
        """
        reader = SwaggerReader(definition_body=body,
                               definition_uri=uri,
                               working_dir=cwd)
        swagger = reader.read()
        parser = SwaggerParser(stack_path, swagger)
        routes = parser.get_routes(event_type)
        LOG.debug("Found '%s' APIs in resource '%s'", len(routes), logical_id)

        collector.add_routes(logical_id, routes)

        collector.add_binary_media_types(
            logical_id,
            parser.get_binary_media_types())  # Binary media from swagger
        collector.add_binary_media_types(
            logical_id,
            binary_media)  # Binary media specified on resource in template
Exemple #10
0
    def test_payload_format_version(self):
        function_name = "myfunction"
        swagger = {
            "paths": {
                "/path1": {
                    "get": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri",
                            "payloadFormatVersion": "1.0",
                        }
                    }
                },
                "/path2": {
                    "get": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri",
                            "payloadFormatVersion": "2.0",
                        }
                    }
                },
            }
        }

        parser = SwaggerParser(self.stack_path, swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = function_name

        expected = [
            Route(
                path="/path1",
                methods=["get"],
                function_name=function_name,
                payload_format_version="1.0",
                stack_path=self.stack_path,
            ),
            Route(
                path="/path2",
                methods=["get"],
                function_name=function_name,
                payload_format_version="2.0",
                stack_path=self.stack_path,
            ),
        ]
        result = parser.get_routes()

        self.assertEqual(expected, result)
Exemple #11
0
    def test_valid_integration(self, LambdaUriMock):
        function_name = "name"
        LambdaUriMock.get_function_name.return_value = function_name

        method_config = {
            "x-amazon-apigateway-integration": {
                "type": "aws_proxy",
                "uri": "someuri"
            }
        }

        parser = SwaggerParser({})
        result = parser._get_integration_function_name(method_config)

        self.assertEquals(function_name, result)
        LambdaUriMock.get_function_name.assert_called_with("someuri")
    def extract_swagger_route(self,
                              logical_id,
                              body,
                              uri,
                              binary_media,
                              collector,
                              cwd=None,
                              event_type=Route.API):
        """
        Parse the Swagger documents and adds it to the ApiCollector.

        Parameters
        ----------
        logical_id : str
            Logical ID of the resource

        body : dict
            The body of the RestApi

        uri : str or dict
            The url to location of the RestApi

        binary_media: list
            The link to the binary media

        collector: samcli.commands.local.lib.route_collector.RouteCollector
            Instance of the Route collector that where we will save the route information

        cwd : str
            Optional working directory with respect to which we will resolve relative path to Swagger file
        """
        reader = SwaggerReader(definition_body=body,
                               definition_uri=uri,
                               working_dir=cwd)
        swagger = reader.read()
        parser = SwaggerParser(swagger)
        routes = parser.get_routes(event_type)
        LOG.debug("Found '%s' APIs in resource '%s'", len(routes), logical_id)

        collector.add_routes(logical_id, routes)

        collector.add_binary_media_types(
            logical_id,
            parser.get_binary_media_types())  # Binary media from swagger
        collector.add_binary_media_types(
            logical_id,
            binary_media)  # Binary media specified on resource in template
Exemple #13
0
    def test_with_one_path_method(self):
        function_name = "myfunction"
        swagger = {
            "paths": {"/path1": {"get": {"x-amazon-apigateway-integration": {"type": "aws_proxy", "uri": "someuri"}}}}
        }

        parser = SwaggerParser(self.stack_path, swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = function_name

        expected = [Route(path="/path1", methods=["get"], function_name=function_name, stack_path=self.stack_path)]
        result = parser.get_routes()

        self.assertEqual(expected, result)
        parser._get_integration_function_name.assert_called_with(
            {"x-amazon-apigateway-integration": {"type": "aws_proxy", "uri": "someuri"}}
        )
Exemple #14
0
    def test_with_combination_of_paths_methods(self):
        function_name = "myfunction"
        swagger = {
            "paths": {
                "/path1": {
                    "get": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    },
                    "delete": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                },
                "/path2": {
                    "post": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                }
            }
        }

        parser = SwaggerParser(swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = function_name

        expected = {
            Route(path="/path1", methods=["get"], function_name=function_name),
            Route(path="/path1",
                  methods=["delete"],
                  function_name=function_name),
            Route(path="/path2", methods=["post"],
                  function_name=function_name),
        }
        result = parser.get_routes()

        self.assertEquals(expected, set(result))
Exemple #15
0
    def test_with_combination_of_paths_methods(self):
        function_name = "myfunction"
        swagger = {
            "paths": {
                "/path1": {
                    "get": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    },
                    "delete": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                },

                "/path2": {
                    "post": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                }
            }
        }

        parser = SwaggerParser(swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = function_name

        expected = {
            Api(path="/path1", method="get", function_name=function_name, cors=None),
            Api(path="/path1", method="delete", function_name=function_name, cors=None),
            Api(path="/path2", method="post", function_name=function_name, cors=None),
        }
        result = parser.get_apis()

        self.assertEquals(expected, set(result))
Exemple #16
0
    def test_does_not_have_function_name(self):
        swagger = {
            "paths": {
                "/path1": {
                    "post": {
                        "x-amazon-apigateway-integration": {
                            "type": "aws_proxy",
                            "uri": "someuri"
                        }
                    }
                }
            }
        }

        parser = SwaggerParser(swagger)
        parser._get_integration_function_name = Mock()
        parser._get_integration_function_name.return_value = None  # Function Name could not be resolved

        expected = []
        result = parser.get_apis()

        self.assertEquals(expected, result)
Exemple #17
0
    def test_binary_media_type_returned(self, test_case_name, swagger, expected_result):
        parser = SwaggerParser(swagger)

        self.assertEquals(parser.get_binary_media_types(), expected_result)
Exemple #18
0
    def test_binary_media_type_returned(self, test_case_name, swagger,
                                        expected_result):
        parser = SwaggerParser(swagger)

        self.assertEquals(parser.get_binary_media_types(), expected_result)
Exemple #19
0
    def test_invalid_swagger(self, test_case_name, swagger):
        parser = SwaggerParser(self.stack_path, swagger)
        result = parser.get_routes()

        expected = []
        self.assertEqual(expected, result)