|
| 1 | +import pytest |
| 2 | +from pydantic import BaseModel, Field |
| 3 | + |
| 4 | +from aws_lambda_powertools.utilities.parser import ValidationError |
| 5 | +from aws_lambda_powertools.utilities.parser.models import ( |
| 6 | + CloudFormationCustomResourceCreateModel, |
| 7 | + CloudFormationCustomResourceDeleteModel, |
| 8 | + CloudFormationCustomResourceUpdateModel, |
| 9 | +) |
| 10 | +from tests.functional.utils import load_event |
| 11 | + |
| 12 | + |
| 13 | +def test_cloudformation_custom_resource_create_event(): |
| 14 | + raw_event = load_event("cloudformationCustomResourceCreate.json") |
| 15 | + model = CloudFormationCustomResourceCreateModel(**raw_event) |
| 16 | + |
| 17 | + assert model.request_type == raw_event["RequestType"] |
| 18 | + assert model.request_id == raw_event["RequestId"] |
| 19 | + assert model.service_token == raw_event["ServiceToken"] |
| 20 | + assert str(model.response_url) == raw_event["ResponseURL"] |
| 21 | + assert model.stack_id == raw_event["StackId"] |
| 22 | + assert model.logical_resource_id == raw_event["LogicalResourceId"] |
| 23 | + assert model.resource_type == raw_event["ResourceType"] |
| 24 | + assert model.resource_properties == raw_event["ResourceProperties"] |
| 25 | + |
| 26 | + |
| 27 | +def test_cloudformation_custom_resource_create_event_custom_model(): |
| 28 | + class MyModel(BaseModel): |
| 29 | + MyProps: str |
| 30 | + |
| 31 | + class MyCustomResource(CloudFormationCustomResourceCreateModel): |
| 32 | + resource_properties: MyModel = Field(..., alias="ResourceProperties") |
| 33 | + |
| 34 | + raw_event = load_event("cloudformationCustomResourceCreate.json") |
| 35 | + model = MyCustomResource(**raw_event) |
| 36 | + |
| 37 | + assert model.resource_properties.MyProps == raw_event["ResourceProperties"].get("MyProps") |
| 38 | + |
| 39 | + |
| 40 | +def test_cloudformation_custom_resource_create_event_invalid(): |
| 41 | + raw_event = load_event("cloudformationCustomResourceCreate.json") |
| 42 | + raw_event["ResourceProperties"] = ["some_data"] |
| 43 | + |
| 44 | + with pytest.raises(ValidationError): |
| 45 | + CloudFormationCustomResourceCreateModel(**raw_event) |
| 46 | + |
| 47 | + |
| 48 | +def test_cloudformation_custom_resource_update_event(): |
| 49 | + raw_event = load_event("cloudformationCustomResourceUpdate.json") |
| 50 | + model = CloudFormationCustomResourceUpdateModel(**raw_event) |
| 51 | + |
| 52 | + assert model.request_type == raw_event["RequestType"] |
| 53 | + assert model.request_id == raw_event["RequestId"] |
| 54 | + assert model.service_token == raw_event["ServiceToken"] |
| 55 | + assert str(model.response_url) == raw_event["ResponseURL"] |
| 56 | + assert model.stack_id == raw_event["StackId"] |
| 57 | + assert model.logical_resource_id == raw_event["LogicalResourceId"] |
| 58 | + assert model.resource_type == raw_event["ResourceType"] |
| 59 | + assert model.resource_properties == raw_event["ResourceProperties"] |
| 60 | + assert model.old_resource_properties == raw_event["OldResourceProperties"] |
| 61 | + |
| 62 | + |
| 63 | +def test_cloudformation_custom_resource_update_event_invalid(): |
| 64 | + raw_event = load_event("cloudformationCustomResourceUpdate.json") |
| 65 | + raw_event["OldResourceProperties"] = ["some_data"] |
| 66 | + |
| 67 | + with pytest.raises(ValidationError): |
| 68 | + CloudFormationCustomResourceUpdateModel(**raw_event) |
| 69 | + |
| 70 | + |
| 71 | +def test_cloudformation_custom_resource_delete_event(): |
| 72 | + raw_event = load_event("cloudformationCustomResourceDelete.json") |
| 73 | + model = CloudFormationCustomResourceDeleteModel(**raw_event) |
| 74 | + |
| 75 | + assert model.request_type == raw_event["RequestType"] |
| 76 | + assert model.request_id == raw_event["RequestId"] |
| 77 | + assert model.service_token == raw_event["ServiceToken"] |
| 78 | + assert str(model.response_url) == raw_event["ResponseURL"] |
| 79 | + assert model.stack_id == raw_event["StackId"] |
| 80 | + assert model.logical_resource_id == raw_event["LogicalResourceId"] |
| 81 | + assert model.resource_type == raw_event["ResourceType"] |
| 82 | + assert model.resource_properties == raw_event["ResourceProperties"] |
| 83 | + |
| 84 | + |
| 85 | +def test_cloudformation_custom_resource_delete_event_invalid(): |
| 86 | + raw_event = load_event("cloudformationCustomResourceDelete.json") |
| 87 | + raw_event["ResourceProperties"] = ["some_data"] |
| 88 | + with pytest.raises(ValidationError): |
| 89 | + CloudFormationCustomResourceDeleteModel(**raw_event) |
0 commit comments