Skip to content

Commit 12fe361

Browse files
authored
chore(ses): create unit tests for drop spam handler (#27769)
This PR adds unit tests for the SES drop spam handler. Specifically, the unit tests verify that spam will be dropped under the following conditions: 1. SPF verdict is `FAIL` 2. DKIM verdict is `FAIL` 3. Spam verdict is `FAIL` 4. Virus verdict is `FAIL` An additional unit test was added to ensure that null is returned if all the above verdicts are not `FAIL`. Closes #27726 ---- *By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
1 parent 1b86634 commit 12fe361

File tree

1 file changed

+117
-0
lines changed

1 file changed

+117
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import { handler } from '../../lib/drop-spam-handler/index';
2+
3+
describe('handler', () => {
4+
test.each(['spf', 'dkim', 'spam', 'virus'])('drop spam when %s status is FAIL', async (key) => {
5+
const sesEvent = createSesEvent({ [key]: { status: 'FAIL' } });
6+
const response = await handler(sesEvent);
7+
expect(response).toEqual({ disposition: 'STOP_RULE_SET' });
8+
});
9+
10+
test('all spam checks pass', async () => {
11+
const sesEvent = createSesEvent();
12+
const response = await handler(sesEvent);
13+
expect(response).toBe(null);
14+
});
15+
});
16+
17+
interface Verdicts {
18+
spam?: AWSLambda.SESReceiptStatus,
19+
virus?: AWSLambda.SESReceiptStatus,
20+
spf?: AWSLambda.SESReceiptStatus,
21+
dkim?: AWSLambda.SESReceiptStatus,
22+
dmarc?: AWSLambda.SESReceiptStatus,
23+
}
24+
25+
function createSesEvent(verdicts: Verdicts = {}) {
26+
return {
27+
Records: [{
28+
eventSource: 'aws:sns',
29+
eventVersion: '1.0',
30+
ses: {
31+
mail: {
32+
timestamp: '2023-10-30T20:32:33.936Z',
33+
source: '[email protected]',
34+
messageId: 'd6iitobk75ur44p8kdnnp7g2n800',
35+
destination: ['[email protected]'],
36+
headersTruncated: false,
37+
headers: [
38+
{
39+
name: 'Return-Path',
40+
value: '<0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com>',
41+
},
42+
{
43+
name: 'Received',
44+
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]; Mon, 30 Oct 2023 20:32:33 +0000 (UTC)',
45+
},
46+
{
47+
name: 'DKIM-Signature',
48+
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=',
49+
},
50+
{
51+
name: 'From',
52+
53+
},
54+
{
55+
name: 'To',
56+
57+
},
58+
{
59+
name: 'Subject',
60+
value: 'Example subject',
61+
},
62+
{
63+
name: 'MIME-Version',
64+
value: '1.0',
65+
},
66+
{
67+
name: 'Content-Type',
68+
value: 'text/plain; charset=UTF-8',
69+
},
70+
{
71+
name: 'Content-Transfer-Encoding',
72+
value: '7bit',
73+
},
74+
{
75+
name: 'Date',
76+
value: 'Mon, 30 Oct 2023 20:32:32 +0000',
77+
},
78+
{
79+
name: 'Message-ID',
80+
value: '<[email protected]>',
81+
},
82+
{
83+
name: 'X-SES-Outgoing',
84+
value: '2023.10.30-54.240.9.183',
85+
},
86+
{
87+
name: 'Feedback-ID',
88+
value: '1.us-east-1.Krv2FKpFdWV+KUYw3Qd6wcpPJ4Sv/pOPpEPSHn2u2o4=:AmazonSES',
89+
},
90+
],
91+
commonHeaders: {
92+
returnPath: '0000014fbe1c09cf-7cb9f704-7531-4e53-89a1-5fa9744f5eb6-000000@amazonses.com',
93+
from: ['[email protected]'],
94+
date: 'Mon, 30 Oct 2023 20:32:32 +0000',
95+
96+
messageId: '<[email protected]>',
97+
subject: 'Example subject',
98+
},
99+
},
100+
receipt: {
101+
timestamp: '2023-10-30T20:32:33.936Z',
102+
processingTimeMillis: 300,
103+
recipients: ['[email protected]'],
104+
spamVerdict: verdicts.spam ?? { status: 'PASS' },
105+
virusVerdict: verdicts.virus ?? { status: 'PASS' },
106+
spfVerdict: verdicts.spf ?? { status: 'PASS' },
107+
dkimVerdict: verdicts.dkim ?? { status: 'PASS' },
108+
dmarcVerdict: verdicts.dmarc ?? { status: 'PASS' },
109+
action: {
110+
type: 'SNS',
111+
topicArn: 'arn:aws:sns:us-east-1:012345678912:example-topic',
112+
},
113+
},
114+
},
115+
}],
116+
} as unknown as AWSLambda.SESEvent;
117+
}

0 commit comments

Comments
 (0)