|
| 1 | +import pytest |
| 2 | + |
| 3 | +from aws_lambda_powertools.utilities.parser import ( |
| 4 | + ValidationError, |
| 5 | + envelopes, |
| 6 | + event_parser, |
| 7 | +) |
| 8 | +from aws_lambda_powertools.utilities.parser.models import VpcLatticeModel |
| 9 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 10 | +from tests.functional.parser.schemas import MyVpcLatticeBusiness |
| 11 | +from tests.functional.utils import load_event |
| 12 | + |
| 13 | + |
| 14 | +@event_parser(model=MyVpcLatticeBusiness, envelope=envelopes.VpcLatticeEnvelope) |
| 15 | +def handle_lambda_vpclattice_with_envelope(event: MyVpcLatticeBusiness, context: LambdaContext): |
| 16 | + assert event.username == "Leandro" |
| 17 | + assert event.name == "Damascena" |
| 18 | + |
| 19 | + |
| 20 | +def test_vpc_lattice_event_with_envelope(): |
| 21 | + event = load_event("vpcLatticeEvent.json") |
| 22 | + event["body"] = '{"username": "Leandro", "name": "Damascena"}' |
| 23 | + handle_lambda_vpclattice_with_envelope(event, LambdaContext()) |
| 24 | + |
| 25 | + |
| 26 | +def test_vpc_lattice_event(): |
| 27 | + raw_event = load_event("vpcLatticeEvent.json") |
| 28 | + model = VpcLatticeModel(**raw_event) |
| 29 | + |
| 30 | + assert model.body == raw_event["body"] |
| 31 | + assert model.method == raw_event["method"] |
| 32 | + assert model.raw_path == raw_event["raw_path"] |
| 33 | + assert model.is_base64_encoded == raw_event["is_base64_encoded"] |
| 34 | + assert model.headers == raw_event["headers"] |
| 35 | + assert model.query_string_parameters == raw_event["query_string_parameters"] |
| 36 | + |
| 37 | + |
| 38 | +def test_vpc_lattice_event_custom_model(): |
| 39 | + class MyCustomResource(VpcLatticeModel): |
| 40 | + body: str |
| 41 | + |
| 42 | + raw_event = load_event("vpcLatticeEvent.json") |
| 43 | + model = MyCustomResource(**raw_event) |
| 44 | + |
| 45 | + assert model.body == raw_event["body"] |
| 46 | + |
| 47 | + |
| 48 | +def test_vpc_lattice_event_invalid(): |
| 49 | + raw_event = load_event("vpcLatticeEvent.json") |
| 50 | + raw_event["body"] = ["some_data"] |
| 51 | + |
| 52 | + with pytest.raises(ValidationError): |
| 53 | + VpcLatticeModel(**raw_event) |
0 commit comments