def test_provider_get_all_with_no_apis(self):
        template = {}

        provider = SamApiProvider(template)

        result = [f for f in provider.get_all()]

        self.assertEquals(result, [])
    def test_provider_get_all_with_no_apis(self):
        template = {}

        provider = SamApiProvider(template)

        result = [f for f in provider.get_all()]

        self.assertEquals(result, [])
    def test_convert_event_api_with_invalid_event_properties(self):
        properties = {
            "Path": "/foo",
            "Method": "get",
            "RestApiId": {
                # This is not supported. Only Ref is supported
                "Fn::Sub": "foo"
            }
        }

        with self.assertRaises(InvalidSamDocumentException):
            SamApiProvider._convert_event_api("logicalId", properties)
    def test_convert_event_api_with_invalid_event_properties(self):
        properties = {
            "Path": "/foo",
            "Method": "get",
            "RestApiId": {
                # This is not supported. Only Ref is supported
                "Fn::Sub": "foo"
            }
        }

        with self.assertRaises(InvalidSamDocumentException):
            SamApiProvider._convert_event_api("logicalId", properties)
    def test_provider_get_all(self):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "GET"
                                }
                            }
                        }
                    }
                },
                "SamFunc2": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "POST"
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        result = [f for f in provider.get_all()]

        api1 = Api(path="/path", method="GET", function_name="SamFunc1")
        api2 = Api(path="/path", method="POST", function_name="SamFunc2")

        self.assertIn(api1, result)
        self.assertIn(api2, result)
    def test_provider_get_all(self):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "GET"
                                }
                            }
                        }
                    }
                },
                "SamFunc2": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "POST"
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        result = [f for f in provider.get_all()]

        api1 = Api(path="/path", method="GET", function_name="SamFunc1", stage_name="Prod")
        api2 = Api(path="/path", method="POST", function_name="SamFunc2", stage_name="Prod")

        self.assertIn(api1, result)
        self.assertIn(api2, result)
    def test_provider_has_correct_api(self, method):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": method
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        self.assertEquals(len(provider.apis), 1)
        self.assertEquals(list(provider.apis)[0], Api(path="/path", method="GET", function_name="SamFunc1", cors=None,
                                                      stage_name="Prod"))
    def test_must_prefer_implicit_with_any_method(self):
        implicit_apis = {
            "Event1": {
                "Type": "Api",
                "Properties": {
                    # This API is duplicated between implicit & explicit
                    "Path": "/path",
                    "Method": "ANY"
                }
            }
        }

        explicit_apis = [
            # Explicit should be over masked completely by implicit, because of "ANY"
            Api(path="/path", method="GET", function_name="explicitfunction", cors=None),
            Api(path="/path", method="DELETE", function_name="explicitfunction", cors=None),
        ]

        self.template["Resources"]["Api1"]["Properties"]["DefinitionBody"] = make_swagger(explicit_apis)
        self.template["Resources"]["ImplicitFunc"]["Properties"]["Events"] = implicit_apis

        expected_apis = [
            Api(path="/path", method="GET", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path", method="POST", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path", method="PUT", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path", method="DELETE", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path", method="HEAD", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path", method="OPTIONS", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path", method="PATCH", function_name="ImplicitFunc", cors=None, stage_name="Prod")
        ]

        provider = SamApiProvider(self.template)
        assertCountEqual(self, expected_apis, provider.apis)
    def test_swagger_with_any_method(self):
        apis = [
            Api(path="/path", method="any", function_name="SamFunc1", cors=None)
        ]

        expected_apis = [
            Api(path="/path", method="GET", function_name="SamFunc1", cors=None, stage_name="Prod"),
            Api(path="/path", method="POST", function_name="SamFunc1", cors=None, stage_name="Prod"),
            Api(path="/path", method="PUT", function_name="SamFunc1", cors=None, stage_name="Prod"),
            Api(path="/path", method="DELETE", function_name="SamFunc1", cors=None, stage_name="Prod"),
            Api(path="/path", method="HEAD", function_name="SamFunc1", cors=None, stage_name="Prod"),
            Api(path="/path", method="OPTIONS", function_name="SamFunc1", cors=None, stage_name="Prod"),
            Api(path="/path", method="PATCH", function_name="SamFunc1", cors=None, stage_name="Prod")
        ]

        template = {
            "Resources": {
                "Api1": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "Prod",
                        "DefinitionBody": make_swagger(apis)
                    }
                }
            }
        }

        provider = SamApiProvider(template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#10
0
    def __init__(self,
                 lambda_invoke_context,
                 port,
                 host,
                 static_dir):
        """
        Initialize the local API service.

        :param samcli.commands.local.cli_common.invoke_context.InvokeContext lambda_invoke_context: Context object
            that can help with Lambda invocation
        :param int port: Port to listen on
        :param string host: Local hostname or IP address to bind to
        :param string static_dir: Optional, directory from which static files will be mounted
        """

        self.port = port
        self.host = host
        self.static_dir = static_dir

        self.cwd = lambda_invoke_context.get_cwd()
        self.api_provider = SamApiProvider(lambda_invoke_context.template,
                                           parameter_overrides=lambda_invoke_context.parameter_overrides,
                                           cwd=self.cwd)
        self.lambda_runner = lambda_invoke_context.local_lambda_runner
        self.stderr_stream = lambda_invoke_context.stderr
示例#11
0
    def test_with_binary_media_types(self):
        template = {
            "Resources": {

                "Api1": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "Prod",
                        "DefinitionBody": make_swagger(self.input_apis, binary_media_types=self.binary_types)
                    }
                }
            }
        }

        expected_binary_types = sorted(self.binary_types)
        expected_apis = [
            Api(path="/path1", method="GET", function_name="SamFunc1", cors=None,
                binary_media_types=expected_binary_types, stage_name="Prod"),
            Api(path="/path1", method="POST", function_name="SamFunc1", cors=None,
                binary_media_types=expected_binary_types, stage_name="Prod"),

            Api(path="/path2", method="PUT", function_name="SamFunc1", cors=None,
                binary_media_types=expected_binary_types, stage_name="Prod"),
            Api(path="/path2", method="GET", function_name="SamFunc1", cors=None,
                binary_media_types=expected_binary_types, stage_name="Prod"),

            Api(path="/path3", method="DELETE", function_name="SamFunc1", cors=None,
                binary_media_types=expected_binary_types, stage_name="Prod")
        ]

        provider = SamApiProvider(template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#12
0
    def test_provider_with_no_api_events(self):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "S3",
                                "Properties": {
                                    "Property1": "value"
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        self.assertEquals(provider.apis, [])
示例#13
0
    def test_with_binary_media_types_in_swagger_and_on_resource(self):
        input_apis = [
            Api(path="/path", method="OPTIONS", function_name="SamFunc1", stage_name="Prod"),
        ]
        extra_binary_types = ["text/html"]

        template = {
            "Resources": {

                "Api1": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "BinaryMediaTypes": extra_binary_types,
                        "StageName": "Prod",
                        "DefinitionBody": make_swagger(input_apis, binary_media_types=self.binary_types)
                    }
                }
            }
        }

        expected_binary_types = sorted(self.binary_types + extra_binary_types)
        expected_apis = [
            Api(path="/path", method="OPTIONS", function_name="SamFunc1", binary_media_types=expected_binary_types,
                stage_name="Prod"),
        ]

        provider = SamApiProvider(template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#14
0
    def test_binary_media_types_with_rest_api_id_reference(self):
        events = {
            "Event1": {
                "Type": "Api",
                "Properties": {
                    "Path": "/connected-to-explicit-path",
                    "Method": "POST",
                    "RestApiId": "Api1"
                }
            },

            "Event2": {
                "Type": "Api",
                "Properties": {
                    "Path": "/true-implicit-path",
                    "Method": "POST"
                }
            }
        }

        # Binary type for implicit
        self.template["Globals"] = {
            "Api": {
                "BinaryMediaTypes": ["image~1gif", "image~1png"]
            }
        }
        self.template["Resources"]["ImplicitFunc"]["Properties"]["Events"] = events

        self.template["Resources"]["Api1"]["Properties"]["DefinitionBody"] = self.swagger
        # Binary type for explicit
        self.template["Resources"]["Api1"]["Properties"]["BinaryMediaTypes"] = ["explicit/type1", "explicit/type2"]

        # Because of Globals, binary types will be concatenated on the explicit API
        expected_explicit_binary_types = ["explicit/type1", "explicit/type2", "image/gif", "image/png"]
        expected_implicit_binary_types = ["image/gif", "image/png"]

        expected_apis = [
            # From Explicit APIs
            Api(path="/path1", method="GET", function_name="explicitfunction",
                binary_media_types=expected_explicit_binary_types, stage_name="Prod"),
            Api(path="/path2", method="GET", function_name="explicitfunction",
                binary_media_types=expected_explicit_binary_types, stage_name="Prod"),
            Api(path="/path3", method="GET", function_name="explicitfunction",
                binary_media_types=expected_explicit_binary_types, stage_name="Prod"),

            # Because of the RestApiId, Implicit APIs will also get the binary media types inherited from
            # the corresponding Explicit API
            Api(path="/connected-to-explicit-path", method="POST", function_name="ImplicitFunc",
                binary_media_types=expected_explicit_binary_types,
                stage_name="Prod"),

            # This is still just a true implicit API because it does not have RestApiId property
            Api(path="/true-implicit-path", method="POST", function_name="ImplicitFunc",
                binary_media_types=expected_implicit_binary_types,
                stage_name="Prod")
        ]

        provider = SamApiProvider(self.template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#15
0
    def test_both_apis_must_get_binary_media_types(self):
        events = {
            "Event1": {
                "Type": "Api",
                "Properties": {
                    "Path": "/newpath1",
                    "Method": "POST"
                }
            },

            "Event2": {
                "Type": "Api",
                "Properties": {
                    "Path": "/newpath2",
                    "Method": "POST"
                }
            }
        }

        # Binary type for implicit
        self.template["Globals"] = {
            "Api": {
                "BinaryMediaTypes": ["image~1gif", "image~1png"]
            }
        }
        self.template["Resources"]["ImplicitFunc"]["Properties"]["Events"] = events

        self.template["Resources"]["Api1"]["Properties"]["DefinitionBody"] = self.swagger
        # Binary type for explicit
        self.template["Resources"]["Api1"]["Properties"]["BinaryMediaTypes"] = ["explicit/type1", "explicit/type2"]

        # Because of Globals, binary types will be concatenated on the explicit API
        expected_explicit_binary_types = ["explicit/type1", "explicit/type2", "image/gif", "image/png"]
        expected_implicit_binary_types = ["image/gif", "image/png"]

        expected_apis = [
            # From Explicit APIs
            Api(path="/path1", method="GET", function_name="explicitfunction",
                binary_media_types=expected_explicit_binary_types, stage_name="Prod"),
            Api(path="/path2", method="GET", function_name="explicitfunction",
                binary_media_types=expected_explicit_binary_types, stage_name="Prod"),
            Api(path="/path3", method="GET", function_name="explicitfunction",
                binary_media_types=expected_explicit_binary_types, stage_name="Prod"),
            # From Implicit APIs
            Api(path="/newpath1", method="POST", function_name="ImplicitFunc",
                binary_media_types=expected_implicit_binary_types,
                stage_name="Prod"),
            Api(path="/newpath2", method="POST", function_name="ImplicitFunc",
                binary_media_types=expected_implicit_binary_types,
                stage_name="Prod")
        ]

        provider = SamApiProvider(self.template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#16
0
    def test_provider_with_valid_template(self, SamBaseProviderMock, extract_api_mock):
        extract_api_mock.return_value = {"set", "of", "values"}

        template = {"Resources": {"a": "b"}}
        SamBaseProviderMock.get_template.return_value = template

        provider = SamApiProvider(template)

        self.assertEquals(len(provider.apis), 3)
        self.assertEquals(provider.apis, set(["set", "of", "values"]))
        self.assertEquals(provider.template_dict, {"Resources": {"a": "b"}})
        self.assertEquals(provider.resources, {"a": "b"})
示例#17
0
    def find_api_provider(resources):
        """
        Finds the ApiProvider given the first api type of the resource

        Parameters
        -----------
        resources: dict
            The dictionary containing the different resources within the template

        Return
        ----------
        Instance of the ApiProvider that will be run on the template with a default of SamApiProvider
        """
        for _, resource in resources.items():
            if resource.get(CfnBaseApiProvider.RESOURCE_TYPE) in SamApiProvider.TYPES:
                return SamApiProvider()

            if resource.get(CfnBaseApiProvider.RESOURCE_TYPE) in CfnApiProvider.TYPES:
                return CfnApiProvider()

        return SamApiProvider()
示例#18
0
    def test_provider_with_no_resource_properties(self):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Lambda::Function"
                }
            }
        }

        provider = SamApiProvider(template)

        self.assertEquals(len(provider.apis), 0)
        self.assertEquals(provider.apis, [])
示例#19
0
    def test_provider_must_support_binary_media_types_with_any_method(self):
        template = {
            "Globals": {
                "Api": {
                    "BinaryMediaTypes": [
                        "image~1gif",
                        "image~1png",
                        "text/html"
                    ]
                }
            },
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "any"
                                }
                            }
                        }
                    }
                }
            }
        }

        binary = ["image/gif", "image/png", "text/html"]

        expected_apis = [
            Api(path="/path", method="GET", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod"),
            Api(path="/path", method="POST", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod"),
            Api(path="/path", method="PUT", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod"),
            Api(path="/path", method="DELETE", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod"),
            Api(path="/path", method="HEAD", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod"),
            Api(path="/path", method="OPTIONS", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod"),
            Api(path="/path", method="PATCH", function_name="SamFunc1", binary_media_types=binary, stage_name="Prod")
        ]

        provider = SamApiProvider(template)

        assertCountEqual(self, provider.apis, expected_apis)
示例#20
0
    def test_provider_has_correct_template(self):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "GET"
                                }
                            }
                        }
                    }
                },
                "SamFunc2": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "POST"
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        api1 = Api(path="/path", method="GET", function_name="SamFunc1", cors=None, stage_name="Prod")
        api2 = Api(path="/path", method="POST", function_name="SamFunc2", cors=None, stage_name="Prod")

        self.assertIn(api1, provider.apis)
        self.assertIn(api2, provider.apis)
示例#21
0
    def test_with_inline_swagger_apis(self):
        template = {
            "Resources": {

                "Api1": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "Prod",
                        "DefinitionBody": make_swagger(self.input_apis)
                    }
                }
            }
        }

        provider = SamApiProvider(template)
        assertCountEqual(self, self.input_apis, provider.apis)
示例#22
0
    def test_with_no_apis(self):
        template = {
            "Resources": {

                "Api1": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "Prod"
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        self.assertEquals(len(provider.apis), 0)
        self.assertEquals(provider.apis, [])
示例#23
0
    def test_provider_stage_variables(self):
        template = {
            "Resources": {

                "TestApi": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "dev",
                        "Variables": {
                            "vis": "data",
                            "random": "test",
                            "foo": "bar"
                        },
                        "DefinitionBody": {
                            "paths": {
                                "/path": {
                                    "get": {
                                        "x-amazon-apigateway-integration": {
                                            "httpMethod": "POST",
                                            "type": "aws_proxy",
                                            "uri": {
                                                "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31"
                                                           "/functions/${NoApiEventFunction.Arn}/invocations",
                                            },
                                            "responses": {},
                                        },
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }
        provider = SamApiProvider(template)
        api1 = Api(path='/path', method='GET', function_name='NoApiEventFunction', cors=None, binary_media_types=[],
                   stage_name='dev',
                   stage_variables={
                       "vis": "data",
                       "random": "test",
                       "foo": "bar"
                   })

        self.assertIn(api1, provider.apis)
示例#24
0
    def test_provider_with_no_serverless_function(self):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Lambda::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler"
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        self.assertEquals(provider.apis, [])
示例#25
0
    def test_must_union_implicit_and_explicit(self):
        events = {
            "Event1": {
                "Type": "Api",
                "Properties": {
                    "Path": "/path1",
                    "Method": "POST"
                }
            },

            "Event2": {
                "Type": "Api",
                "Properties": {
                    "Path": "/path2",
                    "Method": "POST"
                }
            },

            "Event3": {
                "Type": "Api",
                "Properties": {
                    "Path": "/path3",
                    "Method": "POST"
                }
            }
        }

        self.template["Resources"]["Api1"]["Properties"]["DefinitionBody"] = self.swagger
        self.template["Resources"]["ImplicitFunc"]["Properties"]["Events"] = events

        expected_apis = [
            # From Explicit APIs
            Api(path="/path1", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            Api(path="/path2", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            Api(path="/path3", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            # From Implicit APIs
            Api(path="/path1", method="POST", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path2", method="POST", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/path3", method="POST", function_name="ImplicitFunc", cors=None, stage_name="Prod")
        ]

        provider = SamApiProvider(self.template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#26
0
    def test_provider_with_any_method(self, method):
        template = {
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": method
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        api_get = Api(path="/path", method="GET", function_name="SamFunc1", cors=None, stage_name="Prod")
        api_post = Api(path="/path", method="POST", function_name="SamFunc1", cors=None, stage_name="Prod")
        api_put = Api(path="/path", method="PUT", function_name="SamFunc1", cors=None, stage_name="Prod")
        api_delete = Api(path="/path", method="DELETE", function_name="SamFunc1", cors=None, stage_name="Prod")
        api_patch = Api(path="/path", method="PATCH", function_name="SamFunc1", cors=None, stage_name="Prod")
        api_head = Api(path="/path", method="HEAD", function_name="SamFunc1", cors=None, stage_name="Prod")
        api_options = Api(path="/path", method="OPTIONS", function_name="SamFunc1", cors=None, stage_name="Prod")

        self.assertEquals(len(provider.apis), 7)
        self.assertIn(api_get, provider.apis)
        self.assertIn(api_post, provider.apis)
        self.assertIn(api_put, provider.apis)
        self.assertIn(api_delete, provider.apis)
        self.assertIn(api_patch, provider.apis)
        self.assertIn(api_head, provider.apis)
        self.assertIn(api_options, provider.apis)
示例#27
0
    def test_provider_must_support_binary_media_types(self):
        template = {
            "Globals": {
                "Api": {
                    "BinaryMediaTypes": [
                        "image~1gif",
                        "image~1png",
                        "image~1png",  # Duplicates must be ignored
                        {"Ref": "SomeParameter"}  # Refs are ignored as well
                    ]
                }
            },
            "Resources": {

                "SamFunc1": {
                    "Type": "AWS::Serverless::Function",
                    "Properties": {
                        "CodeUri": "/usr/foo/bar",
                        "Runtime": "nodejs4.3",
                        "Handler": "index.handler",
                        "Events": {
                            "Event1": {
                                "Type": "Api",
                                "Properties": {
                                    "Path": "/path",
                                    "Method": "get"
                                }
                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        self.assertEquals(len(provider.apis), 1)
        self.assertEquals(list(provider.apis)[0], Api(path="/path", method="GET", function_name="SamFunc1",
                                                      binary_media_types=["image/gif", "image/png"], cors=None,
                                                      stage_name="Prod"))
示例#28
0
    def test_with_swagger_as_local_file(self):
        with tempfile.NamedTemporaryFile(mode='w') as fp:
            filename = fp.name

            swagger = make_swagger(self.input_apis)
            json.dump(swagger, fp)
            fp.flush()

            template = {
                "Resources": {

                    "Api1": {
                        "Type": "AWS::Serverless::Api",
                        "Properties": {
                            "StageName": "Prod",
                            "DefinitionUri": filename
                        }
                    }
                }
            }

            provider = SamApiProvider(template)
            assertCountEqual(self, self.input_apis, provider.apis)
示例#29
0
    def test_with_swagger_as_both_body_and_uri(self, SamSwaggerReaderMock):
        body = {"some": "body"}
        filename = "somefile.txt"

        template = {
            "Resources": {

                "Api1": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "Prod",
                        "DefinitionUri": filename,
                        "DefinitionBody": body
                    }
                }
            }
        }

        SamSwaggerReaderMock.return_value.read.return_value = make_swagger(self.input_apis)

        cwd = "foo"
        provider = SamApiProvider(template, cwd=cwd)
        assertCountEqual(self, self.input_apis, provider.apis)
        SamSwaggerReaderMock.assert_called_with(definition_body=body, definition_uri=filename, working_dir=cwd)
示例#30
0
    def test_must_add_explicit_api_when_ref_with_rest_api_id(self):
        events = {
            "Event1": {
                "Type": "Api",
                "Properties": {
                    "Path": "/newpath1",
                    "Method": "POST",
                    "RestApiId": "Api1"  # This path must get added to this API
                }
            },

            "Event2": {
                "Type": "Api",
                "Properties": {
                    "Path": "/newpath2",
                    "Method": "POST",
                    "RestApiId": {"Ref": "Api1"}  # This path must get added to this API
                }
            }
        }

        self.template["Resources"]["Api1"]["Properties"]["DefinitionBody"] = self.swagger
        self.template["Resources"]["ImplicitFunc"]["Properties"]["Events"] = events

        expected_apis = [
            # From Explicit APIs
            Api(path="/path1", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            Api(path="/path2", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            Api(path="/path3", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            # From Implicit APIs
            Api(path="/newpath1", method="POST", function_name="ImplicitFunc", cors=None, stage_name="Prod"),
            Api(path="/newpath2", method="POST", function_name="ImplicitFunc", cors=None, stage_name="Prod")
        ]

        provider = SamApiProvider(self.template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#31
0
    def test_must_prefer_implicit_api_over_explicit(self):
        implicit_apis = {
            "Event1": {
                "Type": "Api",
                "Properties": {
                    # This API is duplicated between implicit & explicit
                    "Path": "/path1",
                    "Method": "get"
                }
            },

            "Event2": {
                "Type": "Api",
                "Properties": {
                    "Path": "/path2",
                    "Method": "POST"
                }
            }
        }

        self.template["Resources"]["Api1"]["Properties"]["DefinitionBody"] = self.swagger
        self.template["Resources"]["ImplicitFunc"]["Properties"]["Events"] = implicit_apis

        expected_apis = [
            Api(path="/path1", method="GET", function_name="ImplicitFunc", stage_name="Prod"),
            # Comes from Implicit

            Api(path="/path2", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
            Api(path="/path2", method="POST", function_name="ImplicitFunc", stage_name="Prod"),
            # Comes from implicit

            Api(path="/path3", method="GET", function_name="explicitfunction", cors=None, stage_name="Prod"),
        ]

        provider = SamApiProvider(self.template)
        assertCountEqual(self, expected_apis, provider.apis)
示例#32
0
    def test_multi_stage_get_all(self):
        template = {
            "Resources": {
                "TestApi": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "dev",
                        "Variables": {
                            "vis": "data",
                            "random": "test",
                            "foo": "bar"
                        },
                        "DefinitionBody": {
                            "paths": {
                                "/path2": {
                                    "get": {
                                        "x-amazon-apigateway-integration": {
                                            "httpMethod": "POST",
                                            "type": "aws_proxy",
                                            "uri": {
                                                "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31"
                                                           "/functions/${NoApiEventFunction.Arn}/invocations",
                                            },
                                            "responses": {},
                                        },
                                    }
                                }
                            }
                        }
                    }
                },
                "ProductionApi": {
                    "Type": "AWS::Serverless::Api",
                    "Properties": {
                        "StageName": "Production",
                        "Variables": {
                            "vis": "prod data",
                            "random": "test",
                            "foo": "bar"
                        },
                        "DefinitionBody": {
                            "paths": {
                                "/path": {
                                    "get": {
                                        "x-amazon-apigateway-integration": {
                                            "httpMethod": "POST",
                                            "type": "aws_proxy",
                                            "uri": {
                                                "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31"
                                                           "/functions/${NoApiEventFunction.Arn}/invocations",
                                            },
                                            "responses": {},
                                        },
                                    }
                                },
                                "/anotherpath": {
                                    "post": {
                                        "x-amazon-apigateway-integration": {
                                            "httpMethod": "POST",
                                            "type": "aws_proxy",
                                            "uri": {
                                                "Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31"
                                                           "/functions/${NoApiEventFunction.Arn}/invocations",
                                            },
                                            "responses": {},
                                        },
                                    }
                                }

                            }
                        }
                    }
                }
            }
        }

        provider = SamApiProvider(template)

        result = [f for f in provider.get_all()]

        api1 = Api(path='/path2', method='GET', function_name='NoApiEventFunction', cors=None, binary_media_types=[],
                   stage_name='dev',
                   stage_variables={
                       "vis": "data",
                       "random": "test",
                       "foo": "bar"
                   })
        api2 = Api(path='/path', method='GET', function_name='NoApiEventFunction', cors=None, binary_media_types=[],
                   stage_name='Production', stage_variables={'vis': 'prod data', 'random': 'test', 'foo': 'bar'})
        api3 = Api(path='/anotherpath', method='POST', function_name='NoApiEventFunction', cors=None,
                   binary_media_types=[],
                   stage_name='Production',
                   stage_variables={
                       "vis": "prod data",
                       "random": "test",
                       "foo": "bar"
                   })
        self.assertEquals(len(result), 3)
        self.assertIn(api1, result)
        self.assertIn(api2, result)
        self.assertIn(api3, result)