def test_invalid_template(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": "s3://lambda-code.zip", "Timeout": 60 }, } }, } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock, region="us-east-1") with self.assertRaises(InvalidSamDocumentException): validator.is_valid()
def test_is_valid_returns_true(self, sam_parser, sam_translator, boto_session_patch): managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"policy": "SomePolicy"} template = {"a": "b"} parser = Mock() sam_parser.Parser.return_value = parser boto_session_mock = Mock() boto_session_patch.return_value = boto_session_mock translate_mock = Mock() translate_mock.translate.return_value = {"c": "d"} sam_translator.return_value = translate_mock validator = SamTemplateValidator(template, managed_policy_mock, profile="profile", region="region") # Should not throw an Exception validator.is_valid() boto_session_patch.assert_called_once_with(profile_name="profile", region_name="region") sam_translator.assert_called_once_with( managed_policy_map={"policy": "SomePolicy"}, sam_parser=parser, plugins=[], boto_session=boto_session_mock) translate_mock.translate.assert_called_once_with(sam_template=template, parameter_values={}) sam_parser.Parser.assert_called_once()
def test_valid_template_with_local_code_for_function(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": "./", "Runtime": "nodejs6.10", "Timeout": 60 }, } }, } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock, region="us-east-1") # Should not throw an exception validator.is_valid()
def test_is_valid_raises_exception(self, sam_parser, sam_translator): managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"policy": "SomePolicy"} template = {"a": "b"} parser = Mock() sam_parser.Parser.return_value = parser translate_mock = Mock() translate_mock.translate.side_effect = InvalidDocumentException( [Exception("message")]) sam_translator.return_value = translate_mock validator = SamTemplateValidator(template, managed_policy_mock) with self.assertRaises(InvalidSamDocumentException): validator.is_valid() sam_translator.assert_called_once_with( managed_policy_map={"policy": "SomePolicy"}, sam_parser=parser, plugins=[]) translate_mock.translate.assert_called_once_with(sam_template=template, parameter_values={}) sam_parser.Parser.assert_called_once()
def test_valid_template_with_DefinitionBody_for_api(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessApi": { "Type": "AWS::Serverless::Api", "Properties": { "StageName": "Prod", "DefinitionBody": { "swagger": "2.0" } }, } }, } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock, region="us-east-1") # Should not throw an exception validator.is_valid()
def test_valid_template_with_s3_object_passed(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessApi": { "Type": "AWS::Serverless::Api", "Properties": { "StageName": "Prod", "DefinitionUri": { "Bucket": "mybucket-name", "Key": "swagger", "Version": 121212 }, }, }, "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": { "Bucket": "mybucket-name", "Key": "code.zip", "Version": 121212 }, "Runtime": "nodejs6.10", "Timeout": 60, }, }, }, } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid() # validate the CodeUri was not changed self.assertEqual( validator.sam_template.get("Resources").get("ServerlessApi").get( "Properties").get("DefinitionUri"), { "Bucket": "mybucket-name", "Key": "swagger", "Version": 121212 }, ) self.assertEqual( validator.sam_template.get("Resources").get( "ServerlessFunction").get("Properties").get("CodeUri"), { "Bucket": "mybucket-name", "Key": "code.zip", "Version": 121212 }, )
def test_valid_api_request_model_template(self, template_path): with open(template_path) as f: template = yamlhelper.yaml_parse(f.read()) managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid()
def test_valid_template_with_local_code_for_layer_version(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessLayerVersion": {"Type": "AWS::Serverless::LayerVersion", "Properties": {"ContentUri": "./"}} }, } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid()
def test_valid_template_with_s3_object_passed(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessApi": { "Type": "AWS::Serverless::Api", "Properties": { "StageName": "Prod", "DefinitionUri": { "Bucket": "mybucket-name", "Key": "swagger", "Version": 121212 } } }, "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": { "Bucket": "mybucket-name", "Key": "code.zip", "Version": 121212 }, "Runtime": "nodejs6.10", "Timeout": 60 } } } } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid() # validate the CodeUri was not changed self.assertEquals(validator.sam_template.get("Resources").get("ServerlessApi").get("Properties").get("DefinitionUri"), {"Bucket": "mybucket-name", "Key": "swagger", "Version": 121212}) self.assertEquals(validator.sam_template.get("Resources").get("ServerlessFunction").get("Properties").get("CodeUri"), {"Bucket": "mybucket-name", "Key": "code.zip", "Version": 121212})
def test_valid_template_with_local_code_for_layer_version(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessLayerVersion": { "Type": "AWS::Serverless::LayerVersion", "Properties": { "ContentUri": "./" } } } } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid()
def test_is_valid_raises_exception(self, sam_parser, sam_translator): managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"policy": "SomePolicy"} template = {"a": "b"} parser = Mock() sam_parser.Parser.return_value = parser translate_mock = Mock() translate_mock.translate.side_effect = InvalidDocumentException([Exception('message')]) sam_translator.return_value = translate_mock validator = SamTemplateValidator(template, managed_policy_mock) with self.assertRaises(InvalidSamDocumentException): validator.is_valid() sam_translator.assert_called_once_with(managed_policy_map={"policy": "SomePolicy"}, sam_parser=parser, plugins=[]) translate_mock.translate.assert_called_once_with(sam_template=template, parameter_values={}) sam_parser.Parser.assert_called_once()
def test_is_valid_returns_true(self, sam_parser, sam_translator): managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"policy": "SomePolicy"} template = {"a": "b"} parser = Mock() sam_parser.Parser.return_value = parser translate_mock = Mock() translate_mock.translate.return_value = {"c": "d"} sam_translator.return_value = translate_mock validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an Exception validator.is_valid() sam_translator.assert_called_once_with(managed_policy_map={"policy": "SomePolicy"}, sam_parser=parser, plugins=[]) translate_mock.translate.assert_called_once_with(sam_template=template, parameter_values={}) sam_parser.Parser.assert_called_once()
def test_is_valid_returns_true(self, sam_parser, sam_translator): managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"policy": "SomePolicy"} template = {"a": "b"} parser = Mock() sam_parser.Parser.return_value = parser translate_mock = Mock() translate_mock.translate.return_value = {"c": "d"} sam_translator.return_value = translate_mock validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an Exception validator.is_valid() sam_translator.assert_called_once_with( managed_policy_map={"policy": "SomePolicy"}, sam_parser=parser, plugins=[] ) translate_mock.translate.assert_called_once_with(sam_template=template, parameter_values={}) sam_parser.Parser.assert_called_once()
def test_valid_template_with_DefinitionBody_for_api(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessApi": { "Type": "AWS::Serverless::Api", "Properties": { "StageName": "Prod", "DefinitionBody": {"swagger": "2.0"} } } } } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid()
def test_invalid_template(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": "s3://lambda-code.zip", "Timeout": 60 } } } } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) with self.assertRaises(InvalidSamDocumentException): validator.is_valid()
def test_valid_template(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": "s3://fake-bucket/lambda-code.zip", "Runtime": "nodejs6.10", "Timeout": 60 } } } } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid()
def test_valid_template(self): template = { "AWSTemplateFormatVersion": "2010-09-09", "Transform": "AWS::Serverless-2016-10-31", "Resources": { "ServerlessFunction": { "Type": "AWS::Serverless::Function", "Properties": { "Handler": "index.handler", "CodeUri": "s3://fake-bucket/lambda-code.zip", "Runtime": "nodejs6.10", "Timeout": 60 } } } } managed_policy_mock = Mock() managed_policy_mock.load.return_value = {"PolicyName": "FakePolicy"} validator = SamTemplateValidator(template, managed_policy_mock) # Should not throw an exception validator.is_valid()