-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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*
- Loading branch information
Showing
1 changed file
with
117 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
packages/aws-cdk-lib/aws-ses/test/drop-spam-handler/drop-spam-handler.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { handler } from '../../lib/drop-spam-handler/index'; | ||
|
||
describe('handler', () => { | ||
test.each(['spf', 'dkim', 'spam', 'virus'])('drop spam when %s status is FAIL', async (key) => { | ||
const sesEvent = createSesEvent({ [key]: { status: 'FAIL' } }); | ||
const response = await handler(sesEvent); | ||
expect(response).toEqual({ disposition: 'STOP_RULE_SET' }); | ||
}); | ||
|
||
test('all spam checks pass', async () => { | ||
const sesEvent = createSesEvent(); | ||
const response = await handler(sesEvent); | ||
expect(response).toBe(null); | ||
}); | ||
}); | ||
|
||
interface Verdicts { | ||
spam?: AWSLambda.SESReceiptStatus, | ||
virus?: AWSLambda.SESReceiptStatus, | ||
spf?: AWSLambda.SESReceiptStatus, | ||
dkim?: AWSLambda.SESReceiptStatus, | ||
dmarc?: AWSLambda.SESReceiptStatus, | ||
} | ||
|
||
function createSesEvent(verdicts: Verdicts = {}) { | ||
return { | ||
Records: [{ | ||
eventSource: 'aws:sns', | ||
eventVersion: '1.0', | ||
ses: { | ||
mail: { | ||
timestamp: '2023-10-30T20:32:33.936Z', | ||
source: '[email protected]', | ||
messageId: 'd6iitobk75ur44p8kdnnp7g2n800', | ||
destination: ['[email protected]'], | ||
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 [email protected]; Mon, 30 Oct 2023 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: '[email protected]', | ||
}, | ||
{ | ||
name: 'To', | ||
value: '[email protected]', | ||
}, | ||
{ | ||
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: 'Mon, 30 Oct 2023 20:32:32 +0000', | ||
}, | ||
{ | ||
name: 'Message-ID', | ||
value: '<[email protected]>', | ||
}, | ||
{ | ||
name: 'X-SES-Outgoing', | ||
value: '2023.10.30-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: ['[email protected]'], | ||
date: 'Mon, 30 Oct 2023 20:32:32 +0000', | ||
to: ['[email protected]'], | ||
messageId: '<[email protected]>', | ||
subject: 'Example subject', | ||
}, | ||
}, | ||
receipt: { | ||
timestamp: '2023-10-30T20:32:33.936Z', | ||
processingTimeMillis: 300, | ||
recipients: ['[email protected]'], | ||
spamVerdict: verdicts.spam ?? { status: 'PASS' }, | ||
virusVerdict: verdicts.virus ?? { status: 'PASS' }, | ||
spfVerdict: verdicts.spf ?? { status: 'PASS' }, | ||
dkimVerdict: verdicts.dkim ?? { status: 'PASS' }, | ||
dmarcVerdict: verdicts.dmarc ?? { status: 'PASS' }, | ||
action: { | ||
type: 'SNS', | ||
topicArn: 'arn:aws:sns:us-east-1:012345678912:example-topic', | ||
}, | ||
}, | ||
}, | ||
}], | ||
} as unknown as AWSLambda.SESEvent; | ||
} |