Skip to content

Commit e9b740f

Browse files
authored
Raise InvalidResourceException when UsagePlan type is not correct (#2199)
1 parent 45df088 commit e9b740f

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,9 @@ def _construct_usage_plan(self, rest_api_stage=None):
697697
if auth_properties.UsagePlan is None:
698698
return []
699699
usage_plan_properties = auth_properties.UsagePlan
700+
# throws error if UsagePlan is not a dict
701+
if not isinstance(usage_plan_properties, dict):
702+
raise InvalidResourceException(self.logical_id, "'UsagePlan' must be a dictionary")
700703
# throws error if the property invalid/ unsupported for UsagePlan
701704
if not all(key in UsagePlanProperties._fields for key in usage_plan_properties.keys()):
702705
raise InvalidResourceException(self.logical_id, "Invalid property for 'UsagePlan'")
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
from unittest import TestCase
2+
from mock import Mock, patch
3+
4+
from parameterized import parameterized
5+
6+
from samtranslator.model import InvalidResourceException
7+
from samtranslator.model.api.api_generator import ApiGenerator
8+
9+
10+
class TestApiGenerator(TestCase):
11+
@parameterized.expand([("this should be a dict",), ("123",), ([{}],)])
12+
@patch("samtranslator.model.api.api_generator.AuthProperties")
13+
def test_construct_usage_plan_with_invalid_usage_plan_type(self, invalid_usage_plan, AuthProperties_mock):
14+
AuthProperties_mock.return_value = Mock(UsagePlan=invalid_usage_plan)
15+
api_generator = ApiGenerator(
16+
Mock(),
17+
Mock(),
18+
Mock(),
19+
Mock(),
20+
Mock(),
21+
Mock(),
22+
Mock(),
23+
Mock(),
24+
Mock(),
25+
Mock(),
26+
Mock(),
27+
auth={"some": "value"},
28+
)
29+
with self.assertRaises(InvalidResourceException) as cm:
30+
api_generator._construct_usage_plan()
31+
self.assertIn("Invalid type", str(cm.exception))
32+
33+
@patch("samtranslator.model.api.api_generator.AuthProperties")
34+
def test_construct_usage_plan_with_invalid_usage_plan_fields(self, AuthProperties_mock):
35+
AuthProperties_mock.return_value = Mock(UsagePlan={"Unknown_field": "123"})
36+
api_generator = ApiGenerator(
37+
Mock(),
38+
Mock(),
39+
Mock(),
40+
Mock(),
41+
Mock(),
42+
Mock(),
43+
Mock(),
44+
Mock(),
45+
Mock(),
46+
Mock(),
47+
Mock(),
48+
auth={"some": "value"},
49+
)
50+
with self.assertRaises(InvalidResourceException) as cm:
51+
api_generator._construct_usage_plan()
52+
self.assertIn("Invalid property for", str(cm.exception))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Resources:
2+
MyApiWithCognitoAuth:
3+
Type: "AWS::Serverless::Api"
4+
Properties:
5+
StageName: Prod
6+
Auth:
7+
Authorizers:
8+
MyCognitoAuth:
9+
UserPoolArn: !GetAtt MyUserPool.Arn
10+
UsagePlan: "Should not be a string"
11+
12+
MyUserPool:
13+
Type: AWS::Cognito::UserPool
14+
Properties:
15+
UserPoolName: UserPoolName
16+
Policies:
17+
PasswordPolicy:
18+
MinimumLength: 8
19+
UsernameAttributes:
20+
- email
21+
Schema:
22+
- AttributeDataType: String
23+
Name: email
24+
Required: false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyApiWithCognitoAuth] is invalid. 'UsagePlan' must be a dictionary"
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyApiWithCognitoAuth] is invalid. 'UsagePlan' must be a dictionary"
8+
}

0 commit comments

Comments
 (0)