def test_must_get_body_directly(self, parse_mock): body = {'this': 'swagger'} reader = SwaggerReader(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)
def test_must_return_none_if_file_not_found(self, yaml_parse_mock): expected = "parsed result" yaml_parse_mock.return_value = expected reader = SwaggerReader(definition_uri="somepath") actual = reader._download_swagger("abcdefgh.txt") self.assertIsNone(actual) yaml_parse_mock.assert_not_called()
def test_must_skip_non_s3_dictionaries(self, yaml_parse_mock): location = {"some": "value"} reader = SwaggerReader(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()
def test_read_from_definition_uri(self): uri = "./file.txt" expected = {"some": "value"} reader = SwaggerReader(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)
def test_must_work_with_include_transform(self, parse_mock): body = {'this': 'swagger'} expected = {'k': 'v'} location = "some location" reader = SwaggerReader(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)
def test_must_log_on_download_exception(self, tempfilemock, botomock): s3_mock = Mock() botomock.client.return_value = s3_mock fp_mock = Mock() tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock # mocking context manager s3_mock.download_fileobj.side_effect = botocore.exceptions.ClientError( {"Error": {}}, "download_file") with self.assertRaises(botocore.exceptions.ClientError): SwaggerReader._download_from_s3(self.bucket, self.key) fp_mock.read.assert_not_called()
def test_must_fail_on_download_from_s3(self, tempfilemock, botomock): s3_mock = Mock() botomock.client.return_value = s3_mock fp_mock = Mock() tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock # mocking context manager s3_mock.download_fileobj.side_effect = botocore.exceptions.ClientError( {"Error": {}}, "download_file") with self.assertRaises(Exception) as cm: SwaggerReader._download_from_s3(self.bucket, self.key) self.assertIn(cm.exception.__class__, (botocore.exceptions.NoCredentialsError, botocore.exceptions.ClientError))
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
def test_must_read_first_from_definition_body(self): body = {"this is": "swagger"} uri = "./file.txt" expected = {"some": "value"} reader = SwaggerReader(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()
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
def test_must_use_definition_uri_if_body_does_not_exist(self): body = {"this is": "swagger"} uri = "./file.txt" expected = {"some": "value"} reader = SwaggerReader(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)
def test_must_parse_valid_dict(self): location = { "Bucket": self.bucket, "Key": self.key, "Version": self.version } result = SwaggerReader._parse_s3_location(location) self.assertEquals(result, (self.bucket, self.key, self.version))
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', delete=False) as fp: filepath = fp.name json.dump(data, fp) fp.flush() reader = SwaggerReader(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
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 = SwaggerReader(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)
def _auth_definition_body_and_uri(definition_body, definition_uri): """ Parameters ---------- definition_body: dict inline definition body defined in the template definition_uri: string Either an s3 url or a local path to a definition uri Returns ------- bool Is security defined on the swagger or not? """ reader = SwaggerReader(definition_body=definition_body, definition_uri=definition_uri) swagger = reader.read() _auths = [] if not swagger: swagger = {} # NOTE(sriram-mv): Authorization and Authentication is indicated by the `security` scheme. # https://swagger.io/docs/specification/authentication/ for _, verb in swagger.get("paths", {}).items(): for _property in verb.values(): # If there are instrinsics in play, they may not be resolved yet. if isinstance(_property, dict): _auths.append(bool(_property.get("security", False))) _auths.append(bool(swagger.get("security", False))) if swagger: LOG.debug("Auth checks done on swagger are not exhaustive!") # This is not an exhaustive check, but to check if there is some form of security setup. return any(_auths)
def test_must_work_without_object_version_id(self, tempfilemock, botomock): s3_mock = Mock() botomock.client.return_value = s3_mock fp_mock = Mock() tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock # mocking context manager expected = "data from file" fp_mock.read.return_value = expected actual = SwaggerReader._download_from_s3(self.bucket, self.key) self.assertEquals(actual, expected) s3_mock.download_fileobj.assert_called_with(self.bucket, self.key, fp_mock, ExtraArgs={})
def test_must_download_file_from_s3(self, tempfilemock, botomock): s3_mock = Mock() botomock.client.return_value = s3_mock fp_mock = Mock() tempfilemock.TemporaryFile.return_value.__enter__.return_value = fp_mock # mocking context manager expected = "data from file" fp_mock.read.return_value = expected actual = SwaggerReader._download_from_s3(self.bucket, self.key, self.version) self.assertEquals(actual, expected) s3_mock.download_fileobj.assert_called_with( self.bucket, self.key, fp_mock, ExtraArgs={"VersionId": self.version}) fp_mock.seek.assert_called_with( 0) # make sure we seek the file before reading fp_mock.read.assert_called_with()
def test_definition_body_and_uri_required(self): with self.assertRaises(ValueError): SwaggerReader()
def test_must_parse_invalid_location(self, location): result = SwaggerReader._parse_s3_location(location) self.assertEquals(result, (None, None, None))
def test_must_parse_s3_uri_string_without_version_id(self): location = "s3://{}/{}".format(self.bucket, self.key) result = SwaggerReader._parse_s3_location(location) self.assertEquals(result, (self.bucket, self.key, None))
def test_must_parse_s3_uri_string(self): location = "s3://{}/{}?versionId={}".format(self.bucket, self.key, self.version) result = SwaggerReader._parse_s3_location(location) self.assertEquals(result, (self.bucket, self.key, self.version))
def test_must_parse_dict_without_version(self): location = {"Bucket": self.bucket, "Key": self.key} result = SwaggerReader._parse_s3_location(location) self.assertEquals(result, (self.bucket, self.key, None))
def test_with_invalid_location(self): reader = SwaggerReader(definition_uri="something") actual = reader._download_swagger({}) self.assertIsNone(actual)