forked from aws-powertools/powertools-lambda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_cloudformation_custom_resource.py
105 lines (76 loc) · 4.32 KB
/
test_cloudformation_custom_resource.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import pytest
from pydantic import BaseModel, Field
from aws_lambda_powertools.utilities.parser import ValidationError
from aws_lambda_powertools.utilities.parser.models import (
CloudFormationCustomResourceCreateModel,
CloudFormationCustomResourceDeleteModel,
CloudFormationCustomResourceUpdateModel,
)
from tests.functional.utils import load_event
def test_cloudformation_custom_resource_create_event():
raw_event = load_event("cloudformationCustomResourceCreate.json")
model = CloudFormationCustomResourceCreateModel(**raw_event)
assert model.request_type == raw_event["RequestType"]
assert model.request_id == raw_event["RequestId"]
assert model.service_token == raw_event["ServiceToken"]
assert str(model.response_url) == raw_event["ResponseURL"]
assert model.stack_id == raw_event["StackId"]
assert model.logical_resource_id == raw_event["LogicalResourceId"]
assert model.resource_type == raw_event["ResourceType"]
assert model.resource_properties == raw_event["ResourceProperties"]
def test_cloudformation_custom_resource_create_event_custom_model():
class MyModel(BaseModel):
MyProps: str
class MyCustomResource(CloudFormationCustomResourceCreateModel):
resource_properties: MyModel = Field(..., alias="ResourceProperties")
raw_event = load_event("cloudformationCustomResourceCreate.json")
model = MyCustomResource(**raw_event)
assert model.resource_properties.MyProps == raw_event["ResourceProperties"].get("MyProps")
def test_cloudformation_custom_resource_create_event_invalid():
raw_event = load_event("cloudformationCustomResourceCreate.json")
raw_event["ResourceProperties"] = ["some_data"]
with pytest.raises(ValidationError):
CloudFormationCustomResourceCreateModel(**raw_event)
def test_cloudformation_custom_resource_update_event():
raw_event = load_event("cloudformationCustomResourceUpdate.json")
model = CloudFormationCustomResourceUpdateModel(**raw_event)
assert model.request_type == raw_event["RequestType"]
assert model.request_id == raw_event["RequestId"]
assert model.service_token == raw_event["ServiceToken"]
assert str(model.response_url) == raw_event["ResponseURL"]
assert model.stack_id == raw_event["StackId"]
assert model.logical_resource_id == raw_event["LogicalResourceId"]
assert model.resource_type == raw_event["ResourceType"]
assert model.resource_properties == raw_event["ResourceProperties"]
assert model.old_resource_properties == raw_event["OldResourceProperties"]
def test_cloudformation_custom_resource_update_event_physical_id_missing():
raw_event = load_event("cloudformationCustomResourceUpdate.json")
del raw_event["PhysicalResourceId"]
with pytest.raises(ValidationError):
CloudFormationCustomResourceUpdateModel(**raw_event)
def test_cloudformation_custom_resource_update_event_invalid():
raw_event = load_event("cloudformationCustomResourceUpdate.json")
raw_event["OldResourceProperties"] = ["some_data"]
with pytest.raises(ValidationError):
CloudFormationCustomResourceUpdateModel(**raw_event)
def test_cloudformation_custom_resource_delete_event():
raw_event = load_event("cloudformationCustomResourceDelete.json")
model = CloudFormationCustomResourceDeleteModel(**raw_event)
assert model.request_type == raw_event["RequestType"]
assert model.request_id == raw_event["RequestId"]
assert model.service_token == raw_event["ServiceToken"]
assert str(model.response_url) == raw_event["ResponseURL"]
assert model.stack_id == raw_event["StackId"]
assert model.logical_resource_id == raw_event["LogicalResourceId"]
assert model.resource_type == raw_event["ResourceType"]
assert model.resource_properties == raw_event["ResourceProperties"]
def test_cloudformation_custom_resource_delete_event_physical_id_missing():
raw_event = load_event("cloudformationCustomResourceDelete.json")
del raw_event["PhysicalResourceId"]
with pytest.raises(ValidationError):
CloudFormationCustomResourceUpdateModel(**raw_event)
def test_cloudformation_custom_resource_delete_event_invalid():
raw_event = load_event("cloudformationCustomResourceDelete.json")
raw_event["ResourceProperties"] = ["some_data"]
with pytest.raises(ValidationError):
CloudFormationCustomResourceDeleteModel(**raw_event)