Skip to content

Commit 84efa13

Browse files
griffithshbmoffatt
authored andcommitted
Extend events.SimpleEmailReceiptAction fields (#211)
* Extend events.SimpleEmailReceiptAction fields The action object contains different data depending on the type. Previously only the fields relevant to the Lambda type were included, preventing use of the SimpleEmailReceiptAction for other types. * Update ses.go * ReceiptAction FunctionArn -> FunctionARN * ReceiptAction json:"smtpReplayCode" -> json:"smtpReplyCode" * Resolve TestSESEventMarshaling failure Using omitempty like this will prevent the inclusion of fields with empty strings. There are possibly scenarios where adding omitempty to the existing FunctionARN and InvocationType fields would change the behaviour of systems that expect the fields to be present and assigned an empty string. I've decided that the consistency of the fields when Unmarshaling is a bigger priority here though, as the chances of real world applications depending on this behaviour seems pretty remote to me. * Extend TestSESEventMarshaling This test only covered an SES lambda event, and did not cover other types of SES events. Adding SES-S3 and SES-SNS events here is improving the test coverage. * Document events.SimpleEmailReceiptAction
1 parent e7b076e commit 84efa13

File tree

5 files changed

+263
-12
lines changed

5 files changed

+263
-12
lines changed

events/ses.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,22 @@ type SimpleEmailCommonHeaders struct {
5555
Subject string `json:"subject"`
5656
}
5757

58+
// SimpleEmailReceiptAction is a logical union of fields present in all action
59+
// Types. For example, the FunctionARN and InvocationType fields are only
60+
// present for the Lambda Type, and the BucketName and ObjectKey fields are only
61+
// present for the S3 Type.
5862
type SimpleEmailReceiptAction struct {
59-
Type string `json:"type"`
60-
InvocationType string `json:"invocationType"`
61-
FunctionArn string `json:"functionArn"`
63+
Type string `json:"type"`
64+
TopicARN string `json:"topicArn,omitempty"`
65+
BucketName string `json:"bucketName,omitempty"`
66+
ObjectKey string `json:"objectKey,omitempty"`
67+
SMTPReplyCode string `json:"smtpReplyCode,omitempty"`
68+
StatusCode string `json:"statusCode,omitempty"`
69+
Message string `json:"message,omitempty"`
70+
Sender string `json:"sender,omitempty"`
71+
InvocationType string `json:"invocationType,omitempty"`
72+
FunctionARN string `json:"functionArn,omitempty"`
73+
OrganizationARN string `json:"organizationArn,omitempty"`
6274
}
6375

6476
type SimpleEmailVerdict struct {

events/ses_test.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,32 @@ import (
99
)
1010

1111
func TestSESEventMarshaling(t *testing.T) {
12-
inputJSON := test.ReadJSONFromFile(t, "./testdata/ses-event.json")
1312

14-
var inputEvent SimpleEmailEvent
15-
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
16-
t.Errorf("could not unmarshal event. details: %v", err)
13+
tests := []struct {
14+
file string
15+
}{
16+
{"./testdata/ses-lambda-event.json"},
17+
{"./testdata/ses-s3-event.json"},
18+
{"./testdata/ses-sns-event.json"},
1719
}
1820

19-
outputJSON, err := json.Marshal(inputEvent)
20-
if err != nil {
21-
t.Errorf("could not marshal event. details: %v", err)
22-
}
21+
for _, tc := range tests {
22+
t.Run(tc.file, func(t *testing.T) {
23+
inputJSON := test.ReadJSONFromFile(t, tc.file)
24+
25+
var inputEvent SimpleEmailEvent
26+
if err := json.Unmarshal(inputJSON, &inputEvent); err != nil {
27+
t.Errorf("could not unmarshal event. details: %v", err)
28+
}
2329

24-
assert.JSONEq(t, string(inputJSON), string(outputJSON))
30+
outputJSON, err := json.Marshal(inputEvent)
31+
if err != nil {
32+
t.Errorf("could not marshal event. details: %v", err)
33+
}
34+
35+
assert.JSONEq(t, string(inputJSON), string(outputJSON))
36+
})
37+
}
2538
}
2639

2740
func TestSESMarshalingMalformedJson(t *testing.T) {

events/testdata/ses-s3-event.json

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "1.0",
5+
"ses": {
6+
"receipt": {
7+
"timestamp": "2015-09-11T20:32:33.936Z",
8+
"processingTimeMillis": 406,
9+
"recipients": [
10+
11+
],
12+
"spamVerdict": {
13+
"status": "PASS"
14+
},
15+
"virusVerdict": {
16+
"status": "PASS"
17+
},
18+
"spfVerdict": {
19+
"status": "PASS"
20+
},
21+
"dkimVerdict": {
22+
"status": "PASS"
23+
},
24+
"dmarcVerdict": {
25+
"status": "PASS"
26+
},
27+
"dmarcPolicy": "reject",
28+
"action": {
29+
"type": "S3",
30+
"topicArn": "arn:aws:sns:us-east-1:012345678912:example-topic",
31+
"bucketName": "my-S3-bucket",
32+
"objectKey": "email"
33+
}
34+
},
35+
"mail": {
36+
"timestamp": "2015-09-11T20:32:33.936Z",
37+
"source": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com",
38+
"messageId": "d6iitobk75ur44p8kdnnp7g2n800",
39+
"destination": [
40+
41+
],
42+
"headersTruncated": false,
43+
"headers": [
44+
{
45+
"name": "Return-Path",
46+
"value": "<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com>"
47+
},
48+
{
49+
"name": "Received",
50+
"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 [email protected]; Fri, 11 Sep 2015 20:32:33 +0000 (UTC)"
51+
},
52+
{
53+
"name": "DKIM-Signature",
54+
"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="
55+
},
56+
{
57+
"name": "From",
58+
"value": "[email protected]"
59+
},
60+
{
61+
"name": "To",
62+
"value": "[email protected]"
63+
},
64+
{
65+
"name": "Subject",
66+
"value": "Example subject"
67+
},
68+
{
69+
"name": "MIME-Version",
70+
"value": "1.0"
71+
},
72+
{
73+
"name": "Content-Type",
74+
"value": "text/plain; charset=UTF-8"
75+
},
76+
{
77+
"name": "Content-Transfer-Encoding",
78+
"value": "7bit"
79+
},
80+
{
81+
"name": "Date",
82+
"value": "Fri, 11 Sep 2015 20:32:32 +0000"
83+
},
84+
{
85+
"name": "Message-ID",
86+
"value": "<[email protected]>"
87+
},
88+
{
89+
"name": "X-SES-Outgoing",
90+
"value": "2015.09.11-54.240.9.183"
91+
},
92+
{
93+
"name": "Feedback-ID",
94+
"value": "1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES"
95+
}
96+
],
97+
"commonHeaders": {
98+
"returnPath": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com",
99+
"from": [
100+
101+
],
102+
"date": "Fri, 11 Sep 2015 20:32:32 +0000",
103+
"to": [
104+
105+
],
106+
"messageId": "<[email protected]>",
107+
"subject": "Example subject"
108+
}
109+
}
110+
},
111+
"eventSource": "aws:ses"
112+
}
113+
]
114+
}

events/testdata/ses-sns-event.json

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
{
2+
"Records": [
3+
{
4+
"eventVersion": "1.0",
5+
"ses": {
6+
"receipt": {
7+
"timestamp": "2015-09-11T20:32:33.936Z",
8+
"processingTimeMillis": 222,
9+
"recipients": [
10+
11+
],
12+
"spamVerdict": {
13+
"status": "PASS"
14+
},
15+
"virusVerdict": {
16+
"status": "PASS"
17+
},
18+
"spfVerdict": {
19+
"status": "PASS"
20+
},
21+
"dkimVerdict": {
22+
"status": "PASS"
23+
},
24+
"dmarcVerdict": {
25+
"status": "PASS"
26+
},
27+
"dmarcPolicy": "reject",
28+
"action": {
29+
"type": "SNS",
30+
"topicArn": "arn:aws:sns:us-east-1:012345678912:example-topic"
31+
}
32+
},
33+
"mail": {
34+
"timestamp": "2015-09-11T20:32:33.936Z",
35+
"source": "[email protected]",
36+
"messageId": "d6iitobk75ur44p8kdnnp7g2n800",
37+
"destination": [
38+
39+
],
40+
"headersTruncated": false,
41+
"headers": [
42+
{
43+
"name": "Return-Path",
44+
"value": "<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com>"
45+
},
46+
{
47+
"name": "Received",
48+
"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 [email protected]; Fri, 11 Sep 2015 20:32:33 +0000 (UTC)"
49+
},
50+
{
51+
"name": "DKIM-Signature",
52+
"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="
53+
},
54+
{
55+
"name": "From",
56+
"value": "[email protected]"
57+
},
58+
{
59+
"name": "To",
60+
"value": "[email protected]"
61+
},
62+
{
63+
"name": "Subject",
64+
"value": "Example subject"
65+
},
66+
{
67+
"name": "MIME-Version",
68+
"value": "1.0"
69+
},
70+
{
71+
"name": "Content-Type",
72+
"value": "text/plain; charset=UTF-8"
73+
},
74+
{
75+
"name": "Content-Transfer-Encoding",
76+
"value": "7bit"
77+
},
78+
{
79+
"name": "Date",
80+
"value": "Fri, 11 Sep 2015 20:32:32 +0000"
81+
},
82+
{
83+
"name": "Message-ID",
84+
"value": "<[email protected]>"
85+
},
86+
{
87+
"name": "X-SES-Outgoing",
88+
"value": "2015.09.11-54.240.9.183"
89+
},
90+
{
91+
"name": "Feedback-ID",
92+
"value": "1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES"
93+
}
94+
],
95+
"commonHeaders": {
96+
"returnPath": "0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com",
97+
"from": [
98+
99+
],
100+
"date": "Fri, 11 Sep 2015 20:32:32 +0000",
101+
"to": [
102+
103+
],
104+
"messageId": "<[email protected]>",
105+
"subject": "Example subject"
106+
}
107+
}
108+
},
109+
"eventSource": "aws:ses"
110+
}
111+
]
112+
}

0 commit comments

Comments
 (0)