|
5 | 5 | from samtranslator.intrinsics.resolver import IntrinsicsResolver
|
6 | 6 | from samtranslator.model import InvalidResourceException
|
7 | 7 | from samtranslator.model.apigatewayv2 import ApiGatewayV2HttpApi
|
8 |
| -from samtranslator.model.lambda_ import LambdaFunction, LambdaLayerVersion, LambdaVersion |
| 8 | +from samtranslator.model.lambda_ import LambdaFunction, LambdaLayerVersion, LambdaVersion, LambdaUrl |
9 | 9 | from samtranslator.model.apigateway import ApiGatewayDeployment, ApiGatewayRestApi
|
10 | 10 | from samtranslator.model.apigateway import ApiGatewayStage
|
11 | 11 | from samtranslator.model.iam import IAMRole
|
@@ -461,3 +461,162 @@ def test_invalid_compatible_architectures(self):
|
461 | 461 | layer.CompatibleArchitectures = architecturea
|
462 | 462 | with pytest.raises(InvalidResourceException):
|
463 | 463 | layer.to_cloudformation(**self.kwargs)
|
| 464 | + |
| 465 | + |
| 466 | +class TestFunctionUrlConfig(TestCase): |
| 467 | + kwargs = { |
| 468 | + "intrinsics_resolver": IntrinsicsResolver({}), |
| 469 | + "event_resources": [], |
| 470 | + "managed_policy_map": {"foo": "bar"}, |
| 471 | + } |
| 472 | + |
| 473 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 474 | + def test_with_function_url_config_with_no_authorization_type(self): |
| 475 | + function = SamFunction("foo") |
| 476 | + function.CodeUri = "s3://foobar/foo.zip" |
| 477 | + function.Runtime = "foo" |
| 478 | + function.Handler = "bar" |
| 479 | + function.FunctionUrlConfig = {"Cors": {"AllowOrigins": ["example1.com"]}} |
| 480 | + with pytest.raises(InvalidResourceException) as e: |
| 481 | + function.to_cloudformation(**self.kwargs) |
| 482 | + self.assertEqual( |
| 483 | + str(e.value.message), |
| 484 | + "Resource with id [foo] is invalid. AuthorizationType is required to configure" |
| 485 | + + " function property `FunctionUrlConfig`. Please provide either an IAM role name or NONE.", |
| 486 | + ) |
| 487 | + |
| 488 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 489 | + def test_with_function_url_config_with_no_cors_config(self): |
| 490 | + function = SamFunction("foo") |
| 491 | + function.CodeUri = "s3://foobar/foo.zip" |
| 492 | + function.Runtime = "foo" |
| 493 | + function.Handler = "bar" |
| 494 | + function.FunctionUrlConfig = {"AuthorizationType": "sample_IAM_role"} |
| 495 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 496 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 497 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 498 | + self.assertEqual(generatedUrlList[0].AuthorizationType, "sample_IAM_role") |
| 499 | + |
| 500 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 501 | + def test_validate_function_url_config_properties_with_intrinsic(self): |
| 502 | + function = SamFunction("foo") |
| 503 | + function.CodeUri = "s3://foobar/foo.zip" |
| 504 | + function.Runtime = "foo" |
| 505 | + function.Handler = "bar" |
| 506 | + function.FunctionUrlConfig = {"AuthorizationType": {"Ref": "MyIAMRef"}, "Cors": {"Ref": "MyCorConfigRef"}} |
| 507 | + |
| 508 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 509 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 510 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 511 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 512 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 513 | + self.assertEqual(generatedUrlList[0].AuthorizationType, {"Ref": "MyIAMRef"}) |
| 514 | + self.assertEqual(generatedUrlList[0].Cors, {"Ref": "MyCorConfigRef"}) |
| 515 | + |
| 516 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 517 | + def test_with_valid_function_url_config(self): |
| 518 | + cors = { |
| 519 | + "AllowOrigins": ["example1.com", "example2.com", "example2.com"], |
| 520 | + "AllowMethods": ["GET"], |
| 521 | + "AllowCredentials": True, |
| 522 | + "AllowHeaders": ["X-Custom-Header"], |
| 523 | + "ExposeHeaders": ["x-amzn-header"], |
| 524 | + "MaxAge": 10, |
| 525 | + } |
| 526 | + function = SamFunction("foo") |
| 527 | + function.CodeUri = "s3://foobar/foo.zip" |
| 528 | + function.Runtime = "foo" |
| 529 | + function.Handler = "bar" |
| 530 | + function.FunctionUrlConfig = {"AuthorizationType": "NONE", "Cors": cors} |
| 531 | + |
| 532 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 533 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 534 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 535 | + self.assertEqual(generatedUrlList[0].AuthorizationType, "NONE") |
| 536 | + self.assertEqual(generatedUrlList[0].Cors, cors) |
| 537 | + |
| 538 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 539 | + def test_with_valid_function_url_config_with_Intrinsics(self): |
| 540 | + function = SamFunction("foo") |
| 541 | + function.CodeUri = "s3://foobar/foo.zip" |
| 542 | + function.Runtime = "foo" |
| 543 | + function.Handler = "bar" |
| 544 | + function.FunctionUrlConfig = {"Ref": "MyFunctionUrlConfig"} |
| 545 | + |
| 546 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 547 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 548 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 549 | + |
| 550 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 551 | + def test_with_function_url_config_with_invalid_cors_parameter(self): |
| 552 | + function = SamFunction("foo") |
| 553 | + function.CodeUri = "s3://foobar/foo.zip" |
| 554 | + function.Runtime = "foo" |
| 555 | + function.Handler = "bar" |
| 556 | + function.FunctionUrlConfig = {"AuthorizationType": "NONE", "Cors": {"AllowOrigin": ["example1.com"]}} |
| 557 | + with pytest.raises(InvalidResourceException) as e: |
| 558 | + function.to_cloudformation(**self.kwargs) |
| 559 | + self.assertEqual( |
| 560 | + str(e.value.message), |
| 561 | + "Resource with id [foo] is invalid. AllowOrigin is not a valid property for configuring Cors.", |
| 562 | + ) |
| 563 | + |
| 564 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 565 | + def test_with_function_url_config_with_invalid_cors_parameter_data_type(self): |
| 566 | + function = SamFunction("foo") |
| 567 | + function.CodeUri = "s3://foobar/foo.zip" |
| 568 | + function.Runtime = "foo" |
| 569 | + function.Handler = "bar" |
| 570 | + function.FunctionUrlConfig = {"AuthorizationType": "NONE", "Cors": {"AllowOrigins": "example1.com"}} |
| 571 | + with pytest.raises(InvalidResourceException) as e: |
| 572 | + function.to_cloudformation(**self.kwargs) |
| 573 | + self.assertEqual( |
| 574 | + str(e.value.message), |
| 575 | + "Resource with id [foo] is invalid. AllowOrigins must be of type list.", |
| 576 | + ) |
| 577 | + |
| 578 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 579 | + def test_with_valid_function_url_config_without_auto_publish_alias(self): |
| 580 | + function = SamFunction("foo") |
| 581 | + function.CodeUri = "s3://foobar/foo.zip" |
| 582 | + function.Runtime = "foo" |
| 583 | + function.Handler = "bar" |
| 584 | + function.FunctionUrlConfig = {"AuthorizationType": "NONE", "Cors": {"AllowOrigins": ["example1.com"]}} |
| 585 | + |
| 586 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 587 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 588 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 589 | + expected_url_logicalid = {"Ref": "foo"} |
| 590 | + self.assertEqual(generatedUrlList[0].TargetFunctionArn, expected_url_logicalid) |
| 591 | + |
| 592 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 593 | + def test_with_valid_function_url_config_with_auto_publish_alias(self): |
| 594 | + function = SamFunction("foo") |
| 595 | + function.CodeUri = "s3://foobar/foo.zip" |
| 596 | + function.Runtime = "foo" |
| 597 | + function.Handler = "bar" |
| 598 | + function.AutoPublishAlias = "live" |
| 599 | + function.FunctionUrlConfig = {"AuthorizationType": "NONE", "Cors": {"AllowOrigins": ["example1.com"]}} |
| 600 | + |
| 601 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 602 | + generatedUrlList = [x for x in cfnResources if isinstance(x, LambdaUrl)] |
| 603 | + self.assertEqual(generatedUrlList.__len__(), 1) |
| 604 | + expected_url_logicalid = {"Ref": "fooAliaslive"} |
| 605 | + self.assertEqual(generatedUrlList[0].TargetFunctionArn, expected_url_logicalid) |
| 606 | + |
| 607 | + @patch("boto3.session.Session.region_name", "ap-southeast-1") |
| 608 | + def test_with_valid_function_url_config_with_authorization_type_value_as_None(self): |
| 609 | + |
| 610 | + function = SamFunction("foo") |
| 611 | + function.CodeUri = "s3://foobar/foo.zip" |
| 612 | + function.Runtime = "foo" |
| 613 | + function.Handler = "bar" |
| 614 | + function.FunctionUrlConfig = {"AuthorizationType": None} |
| 615 | + |
| 616 | + with pytest.raises(InvalidResourceException) as e: |
| 617 | + cfnResources = function.to_cloudformation(**self.kwargs) |
| 618 | + self.assertEqual( |
| 619 | + str(e.value.message), |
| 620 | + "Resource with id [foo] is invalid. AuthorizationType is required to configure function property " |
| 621 | + + "`FunctionUrlConfig`. Please provide either an IAM role name or NONE.", |
| 622 | + ) |
0 commit comments