예제 #1
0
    def test_must_get_body_directly(self, parse_mock):
        body = {'this': 'swagger'}

        reader = SamSwaggerReader(definition_body=body)
        parse_mock.return_value = None  # No location is returned from aws_include parser

        actual = reader._read_from_definition_body()
        self.assertEquals(actual, body)
예제 #2
0
    def test_must_return_none_if_file_not_found(self, yaml_parse_mock):
        expected = "parsed result"
        yaml_parse_mock.return_value = expected

        reader = SamSwaggerReader(definition_uri="somepath")
        actual = reader._download_swagger("abcdefgh.txt")

        self.assertIsNone(actual)
        yaml_parse_mock.assert_not_called()
예제 #3
0
    def test_read_from_definition_uri(self):
        uri = "./file.txt"
        expected = {"some": "value"}

        reader = SamSwaggerReader(definition_uri=uri)
        reader._download_swagger = Mock()
        reader._download_swagger.return_value = expected

        actual = reader.read()
        self.assertEquals(actual, expected)

        reader._download_swagger.assert_called_with(uri)
예제 #4
0
    def test_must_skip_non_s3_dictionaries(self, yaml_parse_mock):

        location = {"some": "value"}

        reader = SamSwaggerReader(definition_uri=location)
        reader._download_from_s3 = Mock()

        actual = reader._download_swagger(location)

        self.assertIsNone(actual)
        reader._download_from_s3.assert_not_called()
        yaml_parse_mock.assert_not_called()
예제 #5
0
    def test_must_work_with_include_transform(self, parse_mock):
        body = {'this': 'swagger'}
        expected = {'k': 'v'}
        location = "some location"

        reader = SamSwaggerReader(definition_body=body)
        reader._download_swagger = Mock()
        reader._download_swagger.return_value = expected
        parse_mock.return_value = location

        actual = reader._read_from_definition_body()
        self.assertEquals(actual, expected)
        parse_mock.assert_called_with(body)
예제 #6
0
    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)
예제 #7
0
    def test_must_read_first_from_definition_body(self):
        body = {"this is": "swagger"}
        uri = "./file.txt"
        expected = {"some": "value"}

        reader = SamSwaggerReader(definition_body=body, definition_uri=uri)
        reader._download_swagger = Mock()
        reader._read_from_definition_body = Mock()
        reader._read_from_definition_body.return_value = expected

        actual = reader.read()
        self.assertEquals(actual, expected)

        reader._read_from_definition_body.assert_called_with()
        reader._download_swagger.assert_not_called()
예제 #8
0
    def test_must_use_definition_uri_if_body_does_not_exist(self):
        body = {"this is": "swagger"}
        uri = "./file.txt"
        expected = {"some": "value"}

        reader = SamSwaggerReader(definition_body=body, definition_uri=uri)
        reader._download_swagger = Mock()
        reader._download_swagger.return_value = expected

        # Set the output of reading the definition body to be None
        reader._read_from_definition_body = Mock()
        reader._read_from_definition_body.return_value = None

        actual = reader.read()
        self.assertEquals(actual, expected)

        reader._read_from_definition_body.assert_called_with()
        reader._download_swagger.assert_called_with(uri)
예제 #9
0
    def test_must_read_from_local_file_without_working_directory(
            self, yaml_parse_mock):
        data = {"some": "value"}
        expected = "parsed result"
        yaml_parse_mock.return_value = expected

        with tempfile.NamedTemporaryFile(mode='w') as fp:
            filepath = fp.name

            json.dump(data, fp)
            fp.flush()

            reader = SamSwaggerReader(definition_uri=filepath)
            actual = reader._download_swagger(filepath)

            self.assertEquals(actual, expected)
            yaml_parse_mock.assert_called_with(
                '{"some": "value"}'
            )  # data was read back from the file as JSON string
예제 #10
0
    def test_must_download_from_s3_for_s3_locations(self, yaml_parse_mock):
        location = {
            "Bucket": "mybucket",
            "Key": "swagger.yaml",
            "Version": "versionId"
        }
        swagger_str = "some swagger str"
        expected = "some data"

        reader = SamSwaggerReader(definition_uri=location)
        reader._download_from_s3 = Mock()
        reader._download_from_s3.return_value = swagger_str
        yaml_parse_mock.return_value = expected

        actual = reader._download_swagger(location)

        self.assertEquals(actual, expected)
        reader._download_from_s3.assert_called_with(location["Bucket"],
                                                    location["Key"],
                                                    location["Version"])
        yaml_parse_mock.assert_called_with(swagger_str)
예제 #11
0
    def test_definition_body_and_uri_required(self):

        with self.assertRaises(ValueError):
            SamSwaggerReader()
예제 #12
0
    def test_with_invalid_location(self):

        reader = SamSwaggerReader(definition_uri="something")
        actual = reader._download_swagger({})

        self.assertIsNone(actual)