From 82f8de27a19cc13c306b2d3b29663ecbbb7a1e3b Mon Sep 17 00:00:00 2001 From: Michael Brewer Date: Tue, 12 Apr 2022 18:22:15 -0700 Subject: [PATCH 1/2] fix(docs): Extract jmespath code examples Changes: - Extract code examples - Run isort, black - Fix python syntax errors - Fix line highlights - Add make task Related to: - #1064 --- Makefile | 10 ++ .../custom_jmespath_function.py | 22 ++++ .../extract_data_built_in_jmespath.py | 8 ++ .../extract_data_jmespath.py | 8 ++ ...owertools_base64_gzip_jmespath_function.py | 13 ++ .../powertools_base64_jmespath_function.py | 13 ++ .../powertools_json_jmespath_function.py | 9 ++ ...ools_json_jmespath_function_idempotency.py | 21 ++++ docs/shared/validation_basic_jsonschema.py | 12 +- docs/utilities/jmespath_functions.md | 118 +++--------------- 10 files changed, 131 insertions(+), 103 deletions(-) create mode 100644 docs/examples/utilities/jmespath_functions/custom_jmespath_function.py create mode 100644 docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py create mode 100644 docs/examples/utilities/jmespath_functions/extract_data_jmespath.py create mode 100644 docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py create mode 100644 docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py create mode 100644 docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py create mode 100644 docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py diff --git a/Makefile b/Makefile index 73667eb5f58..77426614f54 100644 --- a/Makefile +++ b/Makefile @@ -90,3 +90,13 @@ changelog: mypy: poetry run mypy --pretty aws_lambda_powertools + +format-examples: + poetry run isort docs/shared + poetry run black docs/shared/*.py + poetry run isort docs/examples + poetry run black docs/examples/*/*/*.py + +lint-examples: + poetry run python3 -m py_compile docs/shared/*.py + poetry run python3 -m py_compile docs/examples/*/*/*.py diff --git a/docs/examples/utilities/jmespath_functions/custom_jmespath_function.py b/docs/examples/utilities/jmespath_functions/custom_jmespath_function.py new file mode 100644 index 00000000000..b03a095313c --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/custom_jmespath_function.py @@ -0,0 +1,22 @@ +from jmespath.functions import signature + +from aws_lambda_powertools.utilities.jmespath_utils import PowertoolsFunctions, extract_data_from_envelope + + +class CustomFunctions(PowertoolsFunctions): + @signature({"types": ["string"]}) # Only decode if value is a string + def _func_special_decoder(self, s): + return my_custom_decoder_logic(s) + + +custom_jmespath_options = {"custom_functions": CustomFunctions()} + + +def handler(event, context): + # use the custom name after `_func_` + extract_data_from_envelope( + data=event, + envelope="special_decoder(body)", + jmespath_options=custom_jmespath_options, + ) + ... diff --git a/docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py b/docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py new file mode 100644 index 00000000000..d77d08ed49e --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py @@ -0,0 +1,8 @@ +from aws_lambda_powertools.utilities.jmespath_utils import envelopes, extract_data_from_envelope +from aws_lambda_powertools.utilities.typing import LambdaContext + + +def handler(event: dict, context: LambdaContext): + payload = extract_data_from_envelope(data=event, envelope=envelopes.SNS) + customer = payload.get("customerId") # now deserialized + ... diff --git a/docs/examples/utilities/jmespath_functions/extract_data_jmespath.py b/docs/examples/utilities/jmespath_functions/extract_data_jmespath.py new file mode 100644 index 00000000000..bfd1e6af88e --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/extract_data_jmespath.py @@ -0,0 +1,8 @@ +from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope +from aws_lambda_powertools.utilities.typing import LambdaContext + + +def handler(event: dict, context: LambdaContext): + payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)") + customer = payload.get("customerId") # now deserialized + ... diff --git a/docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py b/docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py new file mode 100644 index 00000000000..0da16c62e9d --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py @@ -0,0 +1,13 @@ +import schemas + +from aws_lambda_powertools.utilities.validation import validate + +sample_event = { + "data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA==" +} + +validate( + event=sample_event, + schema=schemas.INPUT, + envelope="powertools_base64_gzip(data) | powertools_json(@)", +) diff --git a/docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py b/docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py new file mode 100644 index 00000000000..eac125c238a --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py @@ -0,0 +1,13 @@ +import schemas + +from aws_lambda_powertools.utilities.validation import validate + +sample_event = { + "data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9=", +} + +validate( + event=sample_event, + schema=schemas.INPUT, + envelope="powertools_json(powertools_base64(data))", +) diff --git a/docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py b/docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py new file mode 100644 index 00000000000..991cd930b68 --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py @@ -0,0 +1,9 @@ +import schemas + +from aws_lambda_powertools.utilities.validation import validate + +sample_event = { + "data": '{"payload": {"message": "hello hello", "username": "blah blah"}}', +} + +validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)") diff --git a/docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py b/docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py new file mode 100644 index 00000000000..04017241416 --- /dev/null +++ b/docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py @@ -0,0 +1,21 @@ +import json + +from aws_lambda_powertools.utilities.idempotency import DynamoDBPersistenceLayer, IdempotencyConfig, idempotent + +persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable") +config = IdempotencyConfig(event_key_jmespath="powertools_json(body)") + + +@idempotent(config=config, persistence_store=persistence_layer) +def handler(event: dict, context): + body = json.loads(event["body"]) + payment = create_subscription_payment( + user=body["user"], + product=body["product_id"], + ) + ... + return { + "payment_id": payment.id, + "message": "success", + "statusCode": 200, + } diff --git a/docs/shared/validation_basic_jsonschema.py b/docs/shared/validation_basic_jsonschema.py index afb8a723d18..e9e3ae8ea4d 100644 --- a/docs/shared/validation_basic_jsonschema.py +++ b/docs/shared/validation_basic_jsonschema.py @@ -33,7 +33,15 @@ "examples": [{"statusCode": 200, "body": "response"}], "required": ["statusCode", "body"], "properties": { - "statusCode": {"$id": "#/properties/statusCode", "type": "integer", "title": "The statusCode"}, - "body": {"$id": "#/properties/body", "type": "string", "title": "The response"}, + "statusCode": { + "$id": "#/properties/statusCode", + "type": "integer", + "title": "The statusCode", + }, + "body": { + "$id": "#/properties/body", + "type": "string", + "title": "The response", + }, }, } diff --git a/docs/utilities/jmespath_functions.md b/docs/utilities/jmespath_functions.md index 03b5fce1fd5..0258d592d4a 100644 --- a/docs/utilities/jmespath_functions.md +++ b/docs/utilities/jmespath_functions.md @@ -28,16 +28,8 @@ You can use the `extract_data_from_envelope` function along with any [JMESPath e === "app.py" - ```python hl_lines="1 7" - from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope - - from aws_lambda_powertools.utilities.typing import LambdaContext - - - def handler(event: dict, context: LambdaContext): - payload = extract_data_from_envelope(data=event, envelope="powertools_json(body)") - customer = payload.get("customerId") # now deserialized - ... + ```python hl_lines="1 6" + --8<-- "docs/examples/utilities/jmespath_functions/extract_data_jmespath.py" ``` === "event.json" @@ -54,16 +46,8 @@ We provide built-in envelopes for popular JMESPath expressions used when looking === "app.py" - ```python hl_lines="1 7" - from aws_lambda_powertools.utilities.jmespath_utils import extract_data_from_envelope, envelopes - - from aws_lambda_powertools.utilities.typing import LambdaContext - - - def handler(event: dict, context: LambdaContext): - payload = extract_data_from_envelope(data=event, envelope=envelopes.SNS) - customer = payload.get("customerId") # now deserialized - ... + ```python hl_lines="1 6" + --8<-- "docs/examples/utilities/jmespath_functions/extract_data_built_in_jmespath.py" ``` === "event.json" @@ -107,6 +91,7 @@ Envelope | JMESPath expression ## Advanced ### Built-in JMESPath functions + You can use our built-in JMESPath functions within your expressions to do exactly that to decode JSON Strings, base64, and uncompress gzip data. ???+ info @@ -123,20 +108,12 @@ This sample will decode the value within the `data` key into a valid JSON before === "powertools_json_jmespath_function.py" ```python hl_lines="9" - from aws_lambda_powertools.utilities.validation import validate - - import schemas - - sample_event = { - 'data': '{"payload": {"message": "hello hello", "username": "blah blah"}}' - } - - validate(event=sample_event, schema=schemas.INPUT, envelope="powertools_json(data)") + --8<-- "docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function.py" ``` === "schemas.py" - ```python hl_lines="7 14 16 23 39 45 47 52" + ```python hl_lines="8 10 17 34 36 41" --8<-- "docs/shared/validation_basic_jsonschema.py" ``` @@ -144,28 +121,8 @@ This sample will decode the value within the `data` key into a valid JSON before This sample will decode the value within the `body` key of an API Gateway event into a valid JSON object to ensure the Idempotency utility processes a JSON object instead of a string. -```python hl_lines="7" title="Deserializing JSON before using as idempotency key" -import json -from aws_lambda_powertools.utilities.idempotency import ( - IdempotencyConfig, DynamoDBPersistenceLayer, idempotent -) - -persistence_layer = DynamoDBPersistenceLayer(table_name="IdempotencyTable") -config = IdempotencyConfig(event_key_jmespath="powertools_json(body)") - -@idempotent(config=config, persistence_store=persistence_layer) -def handler(event:APIGatewayProxyEvent, context): - body = json.loads(event['body']) - payment = create_subscription_payment( - user=body['user'], - product=body['product_id'] - ) - ... - return { - "payment_id": payment.id, - "message": "success", - "statusCode": 200 - } +```python hl_lines="6" title="Deserializing JSON before using as idempotency key" +--8<-- "docs/examples/utilities/jmespath_functions/powertools_json_jmespath_function_idempotency.py" ``` #### powertools_base64 function @@ -174,27 +131,15 @@ Use `powertools_base64` function to decode any base64 data. This sample will decode the base64 value within the `data` key, and decode the JSON string into a valid JSON before we can validate it. -=== "powertools_json_jmespath_function.py" +=== "powertools_base64_jmespath_function.py" ```python hl_lines="12" - from aws_lambda_powertools.utilities.validation import validate - - import schemas - - sample_event = { - "data": "eyJtZXNzYWdlIjogImhlbGxvIGhlbGxvIiwgInVzZXJuYW1lIjogImJsYWggYmxhaCJ9=" - } - - validate( - event=sample_event, - schema=schemas.INPUT, - envelope="powertools_json(powertools_base64(data))" - ) + --8<-- "docs/examples/utilities/jmespath_functions/powertools_base64_jmespath_function.py" ``` === "schemas.py" - ```python hl_lines="7 14 16 23 39 45 47 52" + ```python hl_lines="8 10 17 34 36 41" --8<-- "docs/shared/validation_basic_jsonschema.py" ``` @@ -204,27 +149,15 @@ Use `powertools_base64_gzip` function to decompress and decode base64 data. This sample will decompress and decode base64 data, then use JMESPath pipeline expression to pass the result for decoding its JSON string. -=== "powertools_json_jmespath_function.py" +=== "powertools_base64_gzip_jmespath_function.py" ```python hl_lines="12" - from aws_lambda_powertools.utilities.validation import validate - - import schemas - - sample_event = { - "data": "H4sIACZAXl8C/52PzUrEMBhFX2UILpX8tPbHXWHqIOiq3Q1F0ubrWEiakqTWofTdTYYB0YWL2d5zvnuTFellBIOedoiyKH5M0iwnlKH7HZL6dDB6ngLDfLFYctUKjie9gHFaS/sAX1xNEq525QxwFXRGGMEkx4Th491rUZdV3YiIZ6Ljfd+lfSyAtZloacQgAkqSJCGhxM6t7cwwuUGPz4N0YKyvO6I9WDeMPMSo8Z4Ca/kJ6vMEYW5f1MX7W1lVxaG8vqX8hNFdjlc0iCBBSF4ERT/3Pl7RbMGMXF2KZMh/C+gDpNS7RRsp0OaRGzx0/t8e0jgmcczyLCWEePhni/23JWalzjdu0a3ZvgEaNLXeugEAAA==" - } - - validate( - event=sample_event, - schema=schemas.INPUT, - envelope="powertools_base64_gzip(data) | powertools_json(@)" - ) + --8<-- "docs/examples/utilities/jmespath_functions/powertools_base64_gzip_jmespath_function.py" ``` === "schemas.py" - ```python hl_lines="7 14 16 23 39 45 47 52" + ```python hl_lines="8 10 17 34 36 41" --8<-- "docs/shared/validation_basic_jsonschema.py" ``` @@ -239,25 +172,8 @@ In order to keep the built-in functions from Powertools, you can subclass from ` === "custom_jmespath_function.py" - ```python hl_lines="2-3 6-9 11 17" - from aws_lambda_powertools.utilities.jmespath_utils import ( - PowertoolsFunctions, extract_data_from_envelope) - from jmespath.functions import signature - - - class CustomFunctions(PowertoolsFunctions): - @signature({'types': ['string']}) # Only decode if value is a string - def _func_special_decoder(self, s): - return my_custom_decoder_logic(s) - - custom_jmespath_options = {"custom_functions": CustomFunctions()} - - def handler(event, context): - # use the custom name after `_func_` - extract_data_from_envelope(data=event, - envelope="special_decoder(body)", - jmespath_options=**custom_jmespath_options) - ... + ```python hl_lines="1 3 6-9 12 20" + --8<-- "docs/examples/utilities/jmespath_functions/custom_jmespath_function.py" ``` === "event.json" From 31fe45ec28d7ac9579d509351be214cabecd9c3e Mon Sep 17 00:00:00 2001 From: Simon Thulbourn Date: Thu, 28 Apr 2022 17:34:36 +0100 Subject: [PATCH 2/2] Revert "fix(parser): Add missing fields for SESEvent (#1027)" (#1190) This reverts commit 797a10afac80544e2d69bcb7d624909436f2b12a. --- .../utilities/parser/models/__init__.py | 8 -- .../utilities/parser/models/ses.py | 34 +----- tests/events/sesEventS3.json | 114 ------------------ tests/functional/parser/test_ses.py | 58 +-------- 4 files changed, 8 insertions(+), 206 deletions(-) delete mode 100644 tests/events/sesEventS3.json diff --git a/aws_lambda_powertools/utilities/parser/models/__init__.py b/aws_lambda_powertools/utilities/parser/models/__init__.py index 34c8e6ce6a1..e3fb50a2d5d 100644 --- a/aws_lambda_powertools/utilities/parser/models/__init__.py +++ b/aws_lambda_powertools/utilities/parser/models/__init__.py @@ -37,11 +37,7 @@ SesModel, SesReceipt, SesReceiptAction, - SesReceiptActionBase, - SesReceiptBounceAction, - SesReceiptS3Action, SesReceiptVerdict, - SesReceiptWorkmailAction, SesRecordModel, ) from .sns import SnsModel, SnsNotificationModel, SnsRecordModel @@ -88,10 +84,6 @@ "SesMailHeaders", "SesReceipt", "SesReceiptAction", - "SesReceiptActionBase", - "SesReceiptBounceAction", - "SesReceiptWorkmailAction", - "SesReceiptS3Action", "SesReceiptVerdict", "SnsModel", "SnsNotificationModel", diff --git a/aws_lambda_powertools/utilities/parser/models/ses.py b/aws_lambda_powertools/utilities/parser/models/ses.py index 7cd655ea28c..70fd2e83978 100644 --- a/aws_lambda_powertools/utilities/parser/models/ses.py +++ b/aws_lambda_powertools/utilities/parser/models/ses.py @@ -1,5 +1,5 @@ from datetime import datetime -from typing import List, Optional, Union +from typing import List, Optional from pydantic import BaseModel, Field from pydantic.networks import EmailStr @@ -12,38 +12,12 @@ class SesReceiptVerdict(BaseModel): status: Literal["PASS", "FAIL", "GRAY", "PROCESSING_FAILED"] -class SesReceiptActionBase(BaseModel): - topicArn: Optional[str] - - -class SesReceiptAction(SesReceiptActionBase): +class SesReceiptAction(BaseModel): type: Literal["Lambda"] # noqa A003,VNE003 invocationType: Literal["Event"] functionArn: str -class SesReceiptS3Action(SesReceiptActionBase): - type: Literal["S3"] # noqa A003,VNE003 - topicArn: str - bucketName: str - objectKey: str - - -class SesReceiptBounceAction(SesReceiptActionBase): - type: Literal["Bounce"] # noqa A003,VNE003 - topicArn: str - smtpReplyCode: str - message: str - sender: str - statusCode: str - - -class SesReceiptWorkmailAction(SesReceiptActionBase): - type: Literal["WorkMail"] # noqa A003,VNE003 - topicArn: str - organizationArn: str - - class SesReceipt(BaseModel): timestamp: datetime processingTimeMillis: PositiveInt @@ -51,10 +25,8 @@ class SesReceipt(BaseModel): spamVerdict: SesReceiptVerdict virusVerdict: SesReceiptVerdict spfVerdict: SesReceiptVerdict - dkimVerdict: SesReceiptVerdict dmarcVerdict: SesReceiptVerdict - dmarcPolicy: Optional[Literal["quarantine", "reject", "none"]] - action: Union[SesReceiptAction, SesReceiptS3Action, SesReceiptBounceAction, SesReceiptWorkmailAction] + action: SesReceiptAction class SesMailHeaders(BaseModel): diff --git a/tests/events/sesEventS3.json b/tests/events/sesEventS3.json deleted file mode 100644 index dbea2d42ce1..00000000000 --- a/tests/events/sesEventS3.json +++ /dev/null @@ -1,114 +0,0 @@ -{ - "Records": [ - { - "eventVersion": "1.0", - "ses": { - "receipt": { - "timestamp": "2015-09-11T20:32:33.936Z", - "processingTimeMillis": 406, - "recipients": [ - "recipient@example.com" - ], - "spamVerdict": { - "status": "PASS" - }, - "virusVerdict": { - "status": "PASS" - }, - "spfVerdict": { - "status": "PASS" - }, - "dkimVerdict": { - "status": "PASS" - }, - "dmarcVerdict": { - "status": "PASS" - }, - "dmarcPolicy": "reject", - "action": { - "type": "S3", - "topicArn": "arn:aws:sns:us-east-1:012345678912:example-topic", - "bucketName": "my-S3-bucket", - "objectKey": "email" - } - }, - "mail": { - "timestamp": "2015-09-11T20:32:33.936Z", - "source": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com", - "messageId": "d6iitobk75ur44p8kdnnp7g2n800", - "destination": [ - "recipient@example.com" - ], - "headersTruncated": false, - "headers": [ - { - "name": "Return-Path", - "value": "<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com>" - }, - { - "name": "Received", - "value": "from a9-183.smtp-out.amazonses.com (a9-183.smtp-out.amazonses.com [54.240.9.183]) by inbound-smtp.us-east-1.amazonaws.com with SMTP id d6iitobk75ur44p8kdnnp7g2n800 for recipient@example.com; Fri, 11 Sep 2015 20:32:33 +0000 (UTC)" - }, - { - "name": "DKIM-Signature", - "value": "v=1; a=rsa-sha256; q=dns/txt; c=relaxed/simple; s=ug7nbtf4gccmlpwj322ax3p6ow6yfsug; d=amazonses.com; t=1442003552; h=From:To:Subject:MIME-Version:Content-Type:Content-Transfer-Encoding:Date:Message-ID:Feedback-ID; bh=DWr3IOmYWoXCA9ARqGC/UaODfghffiwFNRIb2Mckyt4=; b=p4ukUDSFqhqiub+zPR0DW1kp7oJZakrzupr6LBe6sUuvqpBkig56UzUwc29rFbJF hlX3Ov7DeYVNoN38stqwsF8ivcajXpQsXRC1cW9z8x875J041rClAjV7EGbLmudVpPX 4hHst1XPyX5wmgdHIhmUuh8oZKpVqGi6bHGzzf7g=" - }, - { - "name": "From", - "value": "sender@example.com" - }, - { - "name": "To", - "value": "recipient@example.com" - }, - { - "name": "Subject", - "value": "Example subject" - }, - { - "name": "MIME-Version", - "value": "1.0" - }, - { - "name": "Content-Type", - "value": "text/plain; charset=UTF-8" - }, - { - "name": "Content-Transfer-Encoding", - "value": "7bit" - }, - { - "name": "Date", - "value": "Fri, 11 Sep 2015 20:32:32 +0000" - }, - { - "name": "Message-ID", - "value": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>" - }, - { - "name": "X-SES-Outgoing", - "value": "2015.09.11-54.240.9.183" - }, - { - "name": "Feedback-ID", - "value": "1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES" - } - ], - "commonHeaders": { - "returnPath": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com", - "from": [ - "sender@example.com" - ], - "date": "Fri, 11 Sep 2015 20:32:32 +0000", - "to": [ - "recipient@example.com" - ], - "messageId": "<61967230-7A45-4A9D-BEC9-87CBCF2211C9@example.com>", - "subject": "Example subject" - } - } - }, - "eventSource": "aws:ses" - } - ] -} diff --git a/tests/functional/parser/test_ses.py b/tests/functional/parser/test_ses.py index 34a44253514..d434e2350f8 100644 --- a/tests/functional/parser/test_ses.py +++ b/tests/functional/parser/test_ses.py @@ -1,22 +1,11 @@ from aws_lambda_powertools.utilities.parser import event_parser -from aws_lambda_powertools.utilities.parser.models import ( - SesModel, - SesReceiptBounceAction, - SesReceiptWorkmailAction, - SesRecordModel, -) +from aws_lambda_powertools.utilities.parser.models import SesModel, SesRecordModel from aws_lambda_powertools.utilities.typing import LambdaContext from tests.functional.utils import load_event @event_parser(model=SesModel) -def handle_ses(event: SesModel, _: LambdaContext) -> SesModel: - return event - - -def test_ses_trigger_lambda_event(): - event_dict = load_event("sesEvent.json") - event = handle_ses(event_dict, LambdaContext()) +def handle_ses(event: SesModel, _: LambdaContext): expected_address = "johndoe@example.com" records = event.Records record: SesRecordModel = records[0] @@ -40,10 +29,6 @@ def test_ses_trigger_lambda_event(): assert common_headers.to == [expected_address] assert common_headers.messageId == "<0123456789example.com>" assert common_headers.subject == "Test Subject" - assert common_headers.cc is None - assert common_headers.bcc is None - assert common_headers.sender is None - assert common_headers.reply_to is None receipt = record.ses.receipt convert_time = int(round(receipt.timestamp.timestamp() * 1000)) assert convert_time == 0 @@ -53,45 +38,12 @@ def test_ses_trigger_lambda_event(): assert receipt.virusVerdict.status == "PASS" assert receipt.spfVerdict.status == "PASS" assert receipt.dmarcVerdict.status == "PASS" - assert receipt.dmarcVerdict.status == "PASS" - assert receipt.dmarcPolicy is None action = receipt.action assert action.type == "Lambda" assert action.functionArn == "arn:aws:lambda:us-west-2:012345678912:function:Example" assert action.invocationType == "Event" - assert action.topicArn is None - -def test_ses_trigger_event_s3(): - event_dict = load_event("sesEventS3.json") - event = handle_ses(event_dict, LambdaContext()) - records = list(event.Records) - record = records[0] - receipt = record.ses.receipt - assert receipt.dmarcPolicy == "reject" - action = record.ses.receipt.action - assert action.type == "S3" - assert action.topicArn == "arn:aws:sns:us-east-1:012345678912:example-topic" - assert action.bucketName == "my-S3-bucket" - assert action.objectKey == "email" - - -def test_ses_trigger_event_bounce(): - event_dict = { - "type": "Bounce", - "topicArn": "arn:aws:sns:us-east-1:123456789012:topic:my-topic", - "smtpReplyCode": "5.1.1", - "message": "message", - "sender": "sender", - "statusCode": "550", - } - SesReceiptBounceAction(**event_dict) - -def test_ses_trigger_event_work_mail(): - event_dict = { - "type": "WorkMail", - "topicArn": "arn:aws:sns:us-east-1:123456789012:topic:my-topic", - "organizationArn": "arn", - } - SesReceiptWorkmailAction(**event_dict) +def test_ses_trigger_event(): + event_dict = load_event("sesEvent.json") + handle_ses(event_dict, LambdaContext())