Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions samtranslator/model/api/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,9 @@ def _construct_usage_plan(self, rest_api_stage=None):
if auth_properties.UsagePlan is None:
return []
usage_plan_properties = auth_properties.UsagePlan
# throws error if UsagePlan is not a dict
if not isinstance(usage_plan_properties, dict):
raise InvalidResourceException(self.logical_id, "'UsagePlan' must be a dictionary")
# throws error if the property invalid/ unsupported for UsagePlan
if not all(key in UsagePlanProperties._fields for key in usage_plan_properties.keys()):
raise InvalidResourceException(self.logical_id, "Invalid property for 'UsagePlan'")
Expand Down
52 changes: 52 additions & 0 deletions tests/model/api/test_api_generator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from unittest import TestCase
from mock import Mock, patch

from parameterized import parameterized

from samtranslator.model import InvalidResourceException
from samtranslator.model.api.api_generator import ApiGenerator


class TestApiGenerator(TestCase):
@parameterized.expand([("this should be a dict",), ("123",), ([{}],)])
@patch("samtranslator.model.api.api_generator.AuthProperties")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we have some way (functional tests?) to pass a template and ensure it fails some expected manner? Instead of requiring all this mocking.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Added one for the invalid template I intended to fix.

def test_construct_usage_plan_with_invalid_usage_plan_type(self, invalid_usage_plan, AuthProperties_mock):
AuthProperties_mock.return_value = Mock(UsagePlan=invalid_usage_plan)
api_generator = ApiGenerator(
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
auth={"some": "value"},
)
with self.assertRaises(InvalidResourceException) as cm:
api_generator._construct_usage_plan()
self.assertIn("Invalid type", str(cm.exception))

@patch("samtranslator.model.api.api_generator.AuthProperties")
def test_construct_usage_plan_with_invalid_usage_plan_fields(self, AuthProperties_mock):
AuthProperties_mock.return_value = Mock(UsagePlan={"Unknown_field": "123"})
api_generator = ApiGenerator(
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
Mock(),
auth={"some": "value"},
)
with self.assertRaises(InvalidResourceException) as cm:
api_generator._construct_usage_plan()
self.assertIn("Invalid property for", str(cm.exception))
24 changes: 24 additions & 0 deletions tests/translator/input/error_api_invalid_usage_plan.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Resources:
MyApiWithCognitoAuth:
Type: "AWS::Serverless::Api"
Properties:
StageName: Prod
Auth:
Authorizers:
MyCognitoAuth:
UserPoolArn: !GetAtt MyUserPool.Arn
UsagePlan: "Should not be a string"

MyUserPool:
Type: AWS::Cognito::UserPool
Properties:
UserPoolName: UserPoolName
Policies:
PasswordPolicy:
MinimumLength: 8
UsernameAttributes:
- email
Schema:
- AttributeDataType: String
Name: email
Required: false
8 changes: 8 additions & 0 deletions tests/translator/output/error_api_invalid_usage_plan.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"errors": [
{
"errorMessage": "Resource with id [MyApiWithCognitoAuth] is invalid. 'UsagePlan' must be a dictionary"
}
],
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApiWithCognitoAuth] is invalid. 'UsagePlan' must be a dictionary"
}