From d96c6a701c675df04f213829219cfab42d21de91 Mon Sep 17 00:00:00 2001 From: Michael Lavers Date: Wed, 12 Jun 2019 15:08:25 -0700 Subject: [PATCH] Add SES Event Info Closes #294 --- iopipe/contrib/eventinfo/event_types.py | 21 +++++ tests/contrib/eventinfo/conftest.py | 5 ++ tests/contrib/eventinfo/events/ses.json | 97 +++++++++++++++++++++ tests/contrib/eventinfo/test_event_types.py | 16 ++++ 4 files changed, 139 insertions(+) create mode 100644 tests/contrib/eventinfo/events/ses.json diff --git a/iopipe/contrib/eventinfo/event_types.py b/iopipe/contrib/eventinfo/event_types.py index 3b7666cd..ce1ba957 100644 --- a/iopipe/contrib/eventinfo/event_types.py +++ b/iopipe/contrib/eventinfo/event_types.py @@ -174,6 +174,26 @@ class ServerlessLambda(EventType): required_keys = ["identity.userAgent", "identity.sourceIp", "identity.accountId"] +class SES(EventType): + type = "ses" + keys = [ + "Records[0].ses.mail.commonHeaders.date", + "Records[0].ses.mail.commonHeaders.messageId", + "Records[0].ses.mail.commonHeaders.returnPath", + "Records[0].ses.mail.commonHeaders.subject", + "Records[0].ses.mail.messageId", + "Records[0].ses.mail.source", + ] + required_keys = ["Records[0].eventVersion", "Records[0].eventSource"] + + def has_required_keys(self): + return ( + super(SES, self).has_required_keys() + and get_value(self.event, "Records[0].eventVersion") == "1.0" + and get_value(self.event, "Records[0].eventSource") == "aws:ses" + ) + + class SNS(EventType): type = "sns" keys = [ @@ -228,6 +248,7 @@ def has_required_keys(self): Firehose, Kinesis, S3, + SES, SNS, SQS, Scheduled, diff --git a/tests/contrib/eventinfo/conftest.py b/tests/contrib/eventinfo/conftest.py index 5cfd3693..83c01eff 100644 --- a/tests/contrib/eventinfo/conftest.py +++ b/tests/contrib/eventinfo/conftest.py @@ -58,6 +58,11 @@ def event_kinesis(): return _load_event("kinesis") +@pytest.fixture +def event_ses(): + return _load_event("ses") + + @pytest.fixture def event_sns(): return _load_event("sns") diff --git a/tests/contrib/eventinfo/events/ses.json b/tests/contrib/eventinfo/events/ses.json new file mode 100644 index 00000000..9312281e --- /dev/null +++ b/tests/contrib/eventinfo/events/ses.json @@ -0,0 +1,97 @@ +{ + "Records": [ + { + "eventVersion": "1.0", + "ses": { + "mail": { + "commonHeaders": { + "from": [ + "Jane Doe " + ], + "to": [ + "johndoe@example.com" + ], + "returnPath": "janedoe@example.com", + "messageId": "<0123456789example.com>", + "date": "Wed, 7 Oct 2015 12:34:56 -0700", + "subject": "Test Subject" + }, + "source": "janedoe@example.com", + "timestamp": "1970-01-01T00:00:00.000Z", + "destination": [ + "johndoe@example.com" + ], + "headers": [ + { + "name": "Return-Path", + "value": "" + }, + { + "name": "Received", + "value": "from mailer.example.com (mailer.example.com [203.0.113.1]) by inbound-smtp.us-west-2.amazonaws.com with SMTP id o3vrnil0e2ic for johndoe@example.com; Wed, 07 Oct 2015 12:34:56 +0000 (UTC)" + }, + { + "name": "DKIM-Signature", + "value": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com; s=example; h=mime-version:from:date:message-id:subject:to:content-type; bh=jX3F0bCAI7sIbkHyy3mLYO28ieDQz2R0P8HwQkklFj4=; b=sQwJ+LMe9RjkesGu+vqU56asvMhrLRRYrWCbV" + }, + { + "name": "MIME-Version", + "value": "1.0" + }, + { + "name": "From", + "value": "Jane Doe " + }, + { + "name": "Date", + "value": "Wed, 7 Oct 2015 12:34:56 -0700" + }, + { + "name": "Message-ID", + "value": "<0123456789example.com>" + }, + { + "name": "Subject", + "value": "Test Subject" + }, + { + "name": "To", + "value": "johndoe@example.com" + }, + { + "name": "Content-Type", + "value": "text/plain; charset=UTF-8" + } + ], + "headersTruncated": false, + "messageId": "o3vrnil0e2ic28tr" + }, + "receipt": { + "recipients": [ + "johndoe@example.com" + ], + "timestamp": "1970-01-01T00:00:00.000Z", + "spamVerdict": { + "status": "PASS" + }, + "dkimVerdict": { + "status": "PASS" + }, + "processingTimeMillis": 574, + "action": { + "type": "Lambda", + "invocationType": "Event", + "functionArn": "arn:aws:lambda:us-west-2:012345678912:function:Example" + }, + "spfVerdict": { + "status": "PASS" + }, + "virusVerdict": { + "status": "PASS" + } + } + }, + "eventSource": "aws:ses" + } + ] +} diff --git a/tests/contrib/eventinfo/test_event_types.py b/tests/contrib/eventinfo/test_event_types.py index 83ee5082..54b32f14 100644 --- a/tests/contrib/eventinfo/test_event_types.py +++ b/tests/contrib/eventinfo/test_event_types.py @@ -102,6 +102,22 @@ def test__event_types__serverless_lambda(event_serverless_lambda): assert event_info["@iopipe/event-info.eventType.source"] == event.source +def test__event_types__ses(event_ses): + event = et.SES(event_ses) + + assert event.has_required_keys() is True + + event_info = event.collect() + + assert event_info != {} + + expected_keys = ["@iopipe/event-info.eventType"] + [ + "@iopipe/event-info.ses.%s" % key for key in event.keys + ] + + assert list(event_info.keys()).sort() == expected_keys.sort() + + def test__event_types__sns(event_sns): event = et.SNS(event_sns) assert event.has_required_keys() is True