|
| 1 | +from typing import Any, List |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from aws_lambda_powertools.utilities.parser import ValidationError, envelopes, event_parser |
| 6 | +from aws_lambda_powertools.utilities.parser.models import KinesisStreamModel, KinesisStreamRecordPayload |
| 7 | +from aws_lambda_powertools.utilities.typing import LambdaContext |
| 8 | +from tests.functional.parser.schemas import MyKinesisBusiness |
| 9 | +from tests.functional.parser.utils import load_event |
| 10 | + |
| 11 | + |
| 12 | +@event_parser(model=MyKinesisBusiness, envelope=envelopes.KinesisEnvelope) |
| 13 | +def handle_kinesis(event: List[MyKinesisBusiness], _: LambdaContext): |
| 14 | + assert len(event) == 1 |
| 15 | + record: KinesisStreamModel = event[0] |
| 16 | + assert record.message == "test message" |
| 17 | + assert record.username == "test" |
| 18 | + |
| 19 | + |
| 20 | +@event_parser(model=KinesisStreamModel) |
| 21 | +def handle_kinesis_no_envelope(event: KinesisStreamModel, _: LambdaContext): |
| 22 | + records = event.Records |
| 23 | + assert len(records) == 2 |
| 24 | + record: KinesisStreamModel = records[0] |
| 25 | + |
| 26 | + assert record.awsRegion == "us-east-2" |
| 27 | + assert record.eventID == "shardId-000000000006:49590338271490256608559692538361571095921575989136588898" |
| 28 | + assert record.eventName == "aws:kinesis:record" |
| 29 | + assert record.eventSource == "aws:kinesis" |
| 30 | + assert record.eventSourceARN == "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream" |
| 31 | + assert record.eventVersion == "1.0" |
| 32 | + assert record.invokeIdentityArn == "arn:aws:iam::123456789012:role/lambda-role" |
| 33 | + |
| 34 | + kinesis: KinesisStreamRecordPayload = record.kinesis |
| 35 | + assert kinesis.approximateArrivalTimestamp == 1545084650.987 |
| 36 | + assert kinesis.kinesisSchemaVersion == "1.0" |
| 37 | + assert kinesis.partitionKey == "1" |
| 38 | + assert kinesis.sequenceNumber == 49590338271490256608559692538361571095921575989136588898 |
| 39 | + assert kinesis.data == b"Hello, this is a test." |
| 40 | + |
| 41 | + |
| 42 | +def test_kinesis_trigger_event(): |
| 43 | + event_dict = { |
| 44 | + "Records": [ |
| 45 | + { |
| 46 | + "kinesis": { |
| 47 | + "kinesisSchemaVersion": "1.0", |
| 48 | + "partitionKey": "1", |
| 49 | + "sequenceNumber": "49590338271490256608559692538361571095921575989136588898", |
| 50 | + "data": "eyJtZXNzYWdlIjogInRlc3QgbWVzc2FnZSIsICJ1c2VybmFtZSI6ICJ0ZXN0In0=", |
| 51 | + "approximateArrivalTimestamp": 1545084650.987, |
| 52 | + }, |
| 53 | + "eventSource": "aws:kinesis", |
| 54 | + "eventVersion": "1.0", |
| 55 | + "eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898", |
| 56 | + "eventName": "aws:kinesis:record", |
| 57 | + "invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role", |
| 58 | + "awsRegion": "us-east-2", |
| 59 | + "eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream", |
| 60 | + } |
| 61 | + ] |
| 62 | + } |
| 63 | + |
| 64 | + handle_kinesis(event_dict, LambdaContext()) |
| 65 | + |
| 66 | + |
| 67 | +def test_kinesis_trigger_bad_base64_event(): |
| 68 | + event_dict = { |
| 69 | + "Records": [ |
| 70 | + { |
| 71 | + "kinesis": { |
| 72 | + "kinesisSchemaVersion": "1.0", |
| 73 | + "partitionKey": "1", |
| 74 | + "sequenceNumber": "49590338271490256608559692538361571095921575989136588898", |
| 75 | + "data": "bad", |
| 76 | + "approximateArrivalTimestamp": 1545084650.987, |
| 77 | + }, |
| 78 | + "eventSource": "aws:kinesis", |
| 79 | + "eventVersion": "1.0", |
| 80 | + "eventID": "shardId-000000000006:49590338271490256608559692538361571095921575989136588898", |
| 81 | + "eventName": "aws:kinesis:record", |
| 82 | + "invokeIdentityArn": "arn:aws:iam::123456789012:role/lambda-role", |
| 83 | + "awsRegion": "us-east-2", |
| 84 | + "eventSourceARN": "arn:aws:kinesis:us-east-2:123456789012:stream/lambda-stream", |
| 85 | + } |
| 86 | + ] |
| 87 | + } |
| 88 | + with pytest.raises(ValidationError): |
| 89 | + handle_kinesis_no_envelope(event_dict, LambdaContext()) |
| 90 | + |
| 91 | + |
| 92 | +def test_kinesis_trigger_event_no_envelope(): |
| 93 | + event_dict = load_event("kinesisStreamEvent.json") |
| 94 | + handle_kinesis_no_envelope(event_dict, LambdaContext()) |
| 95 | + |
| 96 | + |
| 97 | +def test_validate_event_does_not_conform_with_model_no_envelope(): |
| 98 | + event_dict: Any = {"hello": "s"} |
| 99 | + with pytest.raises(ValidationError): |
| 100 | + handle_kinesis_no_envelope(event_dict, LambdaContext()) |
| 101 | + |
| 102 | + |
| 103 | +def test_validate_event_does_not_conform_with_model(): |
| 104 | + event_dict: Any = {"hello": "s"} |
| 105 | + with pytest.raises(ValidationError): |
| 106 | + handle_kinesis(event_dict, LambdaContext()) |
0 commit comments