feat(ses): apply suppression list during send with per-CS SuppressionOptions override (#1109)

When SendEmail / SendRawEmail is called with a recipient on the
account-level suppression list whose reason matches the effective
SuppressedReasons (the configuration set's SuppressionOptions
override if present, else the account-level reasons), Floci now:

1. Emits a synthetic Bounce or Complaint event (per the suppression
   reason) through the configured event destinations alongside the
   always-emitted Send event.
2. Drops the suppressed recipient from the SMTP relay step, while
   still delivering the message to non-suppressed recipients on the
   same send.

The SendEmail API response (200 + MessageId), the stored SentEmail
(used by the LocalStack-compatible /_aws/ses inspection endpoint),
and the published event's mail.destination all continue to reflect
the original recipient list — matching how AWS reports the message
at the API and event layers.

This PR bundles two related pieces:
- The per-configuration-set SuppressionOptions block (Put endpoint +
  GetConfigurationSet response shape + getEffectiveSuppressedReasons
  helper). Mirrors AWS V2 PutConfigurationSetSuppressionOptions:
  empty list = explicit "no suppression filtering" for this CS,
  unset block = fall back to account-level reasons. The validation
  message for the new endpoint matches the AWS V2 natural-language
  form ("Reason <X> is invalid, must be one of [BOUNCE, COMPLAINT].")
  which is distinct from the V1-style nested message returned by the
  three older suppression endpoints; both forms were verified
  against live AWS on 2026-06-03.
- The send-time filter + synthetic-event partitioning, plumbed
  through getEffectiveSuppressedReasons so the override takes effect
  per-send. collectSuppressedReasons and resolveSuppressionReason
  now both take configurationSetName, hitting the per-CS override
  when present and falling back to the account-level reasons
  otherwise.

Per-recipient: when only some recipients are suppressed, the
non-suppressed ones still reach smtpRelay.relay() / relayRaw().
The relay is skipped entirely only when every recipient is
suppressed.

- Recipient suppressed with reason BOUNCE (and BOUNCE in the
  effective reasons) -> synthetic Bounce event; not relayed.
- Recipient suppressed with reason COMPLAINT (and COMPLAINT in the
  effective reasons) -> synthetic Complaint event; not relayed.
- Reason mismatch or address not on the list -> Send only; relayed
  normally.
- Simulator-address recipients and suppression-listed recipients
  are unioned into the same event's recipient list, deduplicated.

Tests
- SesConfigurationSetSuppressionOptionsV2IntegrationTest (16 cases)
  for the CRUD + helper.
- SesConfigurationSetTest SDK compat (6 V2 cases) using
  SesV2Client.PutConfigurationSetSuppressionOptions and the typed
  SuppressionListReason enum (+ the WithStrings escape hatch for
  the server-side validation path).
- SesServiceSmtpTest grows four cases pinning the send-time
  override behaviour: empty list on the CS disables filtering and
  the relay receives the address; BOUNCE-only override leaves
  COMPLAINT-suppressed addresses unfiltered; a CS without an
  override falls back to account-level (parity with no-CS sends);
  raw-send mirrors the JSON-send path.
- SesEventPayload / SesEventPublishingV2IntegrationTest / further
  send-path coverage from the original suppression PR is preserved.
- All 452 SES tests pass.
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-06-03 21:57:20 -05:00
1 parent 2a14f35c2d
commit 19b0432b8c
13 files changed
+1136 -34

No files matched your search

@@ -0,0 +1,280 @@
package io.github.hectorvent.floci.services.ses;
import io.github.hectorvent.floci.core.common.AwsException;
import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.MethodOrderer;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestMethodOrder;
import java.util.List;
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Integration test for the SES V2 per-configuration-set SuppressionOptions
* endpoint: PUT /v2/email/configuration-sets/{name}/suppression-options and the
* matching GET /v2/email/configuration-sets/{name} response shape.
*/
@QuarkusTest
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
class SesConfigurationSetSuppressionOptionsV2IntegrationTest {
private static final String AUTH =
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
private static final String REGION = "us-east-1";
private static final String CS = "cs-suppression-options";
@Inject
SesService sesService;
@Test
@Order(1)
void createConfigurationSet() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"ConfigurationSetName\":\"" + CS + "\"}")
.when()
.post("/v2/email/configuration-sets")
.then()
.statusCode(200);
}
@Test
@Order(2)
void getConfigurationSetBeforePut_doesNotIncludeSuppressionOptions() {
given()
.header("Authorization", AUTH)
.when()
.get("/v2/email/configuration-sets/" + CS)
.then()
.statusCode(200)
.body("ConfigurationSetName", equalTo(CS))
.body("SuppressionOptions", nullValue());
}
@Test
@Order(3)
void putSuppressionOptions_bounceOnly() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"SuppressedReasons\":[\"BOUNCE\"]}")
.when()
.put("/v2/email/configuration-sets/" + CS + "/suppression-options")
.then()
.statusCode(200);
}
@Test
@Order(4)
void getConfigurationSet_afterPut_reflectsSuppressionOptions() {
given()
.header("Authorization", AUTH)
.when()
.get("/v2/email/configuration-sets/" + CS)
.then()
.statusCode(200)
.body("SuppressionOptions.SuppressedReasons", contains("BOUNCE"))
.body("SuppressionOptions.SuppressedReasons", hasSize(1));
}
@Test
@Order(5)
void putSuppressionOptions_emptyList_explicitlyDisablesFiltering() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"SuppressedReasons\":[]}")
.when()
.put("/v2/email/configuration-sets/" + CS + "/suppression-options")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH)
.when()
.get("/v2/email/configuration-sets/" + CS)
.then()
.statusCode(200)
.body("SuppressionOptions.SuppressedReasons", empty());
}
@Test
@Order(6)
void putSuppressionOptions_bothReasons() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"SuppressedReasons\":[\"BOUNCE\",\"COMPLAINT\"]}")
.when()
.put("/v2/email/configuration-sets/" + CS + "/suppression-options")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH)
.when()
.get("/v2/email/configuration-sets/" + CS)
.then()
.statusCode(200)
.body("SuppressionOptions.SuppressedReasons", contains("BOUNCE", "COMPLAINT"));
}
@Test
@Order(7)
void putSuppressionOptions_invalidReason_rejected() {
// Message verified against real AWS V2 SES on 2026-06-03:
// "Reason INVALID is invalid, must be one of [BOUNCE, COMPLAINT]."
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"SuppressedReasons\":[\"BOUNCE\",\"INVALID\"]}")
.when()
.put("/v2/email/configuration-sets/" + CS + "/suppression-options")
.then()
.statusCode(400)
.body("__type", org.hamcrest.Matchers.equalTo("BadRequestException"))
.body("message", org.hamcrest.Matchers.equalTo(
"Reason INVALID is invalid, must be one of [BOUNCE, COMPLAINT]."));
}
@Test
@Order(8)
void putSuppressionOptions_nonArrayReasons_rejected() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"SuppressedReasons\":\"BOUNCE\"}")
.when()
.put("/v2/email/configuration-sets/" + CS + "/suppression-options")
.then()
.statusCode(400);
}
@Test
@Order(9)
void putSuppressionOptions_unknownConfigSet_returns404() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"SuppressedReasons\":[\"BOUNCE\"]}")
.when()
.put("/v2/email/configuration-sets/does-not-exist/suppression-options")
.then()
.statusCode(404);
}
@Test
@Order(10)
void putSuppressionOptions_omittedSuppressedReasons_treatedAsEmptyList() {
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{}")
.when()
.put("/v2/email/configuration-sets/" + CS + "/suppression-options")
.then()
.statusCode(200);
given()
.header("Authorization", AUTH)
.when()
.get("/v2/email/configuration-sets/" + CS)
.then()
.statusCode(200)
.body("SuppressionOptions.SuppressedReasons", empty());
}
// ─── getEffectiveSuppressedReasons helper (forward-looking integration with #1109) ───
@Test
@Order(20)
void effectiveReasons_csWithoutOverride_fallsBackToAccountLevel() {
String csNoOverride = "cs-suppression-no-override";
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"ConfigurationSetName\":\"" + csNoOverride + "\"}")
.when()
.post("/v2/email/configuration-sets")
.then()
.statusCode(200);
List<String> effective = sesService.getEffectiveSuppressedReasons(csNoOverride, REGION);
// Account defaults to [BOUNCE, COMPLAINT]; matches account state when no per-CS override.
assertEquals(sesService.getAccountSuppressionAttributes(REGION).getSuppressedReasons(), effective);
}
@Test
@Order(21)
void effectiveReasons_csWithBounceOverride_returnsBounceOnly() {
String csBounceOnly = "cs-suppression-bounce-only";
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"ConfigurationSetName\":\"" + csBounceOnly + "\"}")
.when()
.post("/v2/email/configuration-sets")
.then()
.statusCode(200);
sesService.putConfigurationSetSuppressionOptions(csBounceOnly, List.of("BOUNCE"), REGION);
List<String> effective = sesService.getEffectiveSuppressedReasons(csBounceOnly, REGION);
assertEquals(List.of("BOUNCE"), effective);
}
@Test
@Order(22)
void effectiveReasons_csWithEmptyOverride_explicitlyDisablesFiltering() {
String csEmpty = "cs-suppression-empty";
given()
.contentType("application/json")
.header("Authorization", AUTH)
.body("{\"ConfigurationSetName\":\"" + csEmpty + "\"}")
.when()
.post("/v2/email/configuration-sets")
.then()
.statusCode(200);
sesService.putConfigurationSetSuppressionOptions(csEmpty, List.of(), REGION);
List<String> effective = sesService.getEffectiveSuppressedReasons(csEmpty, REGION);
assertTrue(effective.isEmpty(),
"An empty SuppressedReasons override must defeat the account-level reasons, "
+ "matching the AWS V2 contract: \"If you specify an empty list, "
+ "Amazon SES doesn't apply any suppression filtering ...\"");
}
@Test
@Order(23)
void effectiveReasons_nullConfigSetName_returnsAccountLevel() {
List<String> effective = sesService.getEffectiveSuppressedReasons(null, REGION);
assertEquals(sesService.getAccountSuppressionAttributes(REGION).getSuppressedReasons(), effective);
}
@Test
@Order(24)
void effectiveReasons_blankConfigSetName_returnsAccountLevel() {
List<String> effective = sesService.getEffectiveSuppressedReasons(" ", REGION);
assertEquals(sesService.getAccountSuppressionAttributes(REGION).getSuppressedReasons(), effective);
}
@Test
@Order(25)
void effectiveReasons_nonExistentConfigSet_throws() {
assertThrows(AwsException.class,
() -> sesService.getEffectiveSuppressedReasons("never-created", REGION));
}
}
@@ -30,7 +30,7 @@ class SesEventPayloadTest {
ObjectNode node = SesEventPayload.build(mapper, "SEND", "msg-1", "from@example.com",
"arn:aws:ses:us-east-1:000000000000:identity/from@example.com",
"000000000000", "Hello", List.of("to@example.com"), List.of("cc@example.com"),
List.of(), List.of("to@example.com", "cc@example.com"), "my-cs", List.of(), List.of(), ts);
List.of(), List.of("to@example.com", "cc@example.com"), List.of(), List.of(), "my-cs", List.of(), List.of(), ts);
assertEquals("Send", node.get("eventType").asText());
assertTrue(node.has("send"));
@@ -68,7 +68,7 @@ class SesEventPayloadTest {
ObjectNode node = SesEventPayload.build(mapper, "DELIVERY", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("success@simulator.amazonses.com"), List.of(), List.of(),
List.of("success@simulator.amazonses.com"), "cs", List.of(), List.of(), ts);
List.of("success@simulator.amazonses.com"), List.of(), List.of(), "cs", List.of(), List.of(), ts);
assertEquals("Delivery", node.get("eventType").asText());
ObjectNode delivery = (ObjectNode) node.get("delivery");
@@ -83,7 +83,7 @@ class SesEventPayloadTest {
ObjectNode node = SesEventPayload.build(mapper, "BOUNCE", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("bounce@simulator.amazonses.com"), List.of(), List.of(),
List.of("bounce@simulator.amazonses.com"), "cs", List.of(), List.of(), ts);
List.of("bounce@simulator.amazonses.com"), List.of(), List.of(), "cs", List.of(), List.of(), ts);
assertEquals("Bounce", node.get("eventType").asText());
ObjectNode bounce = (ObjectNode) node.get("bounce");
@@ -98,7 +98,7 @@ class SesEventPayloadTest {
ObjectNode node = SesEventPayload.build(mapper, "COMPLAINT", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("complaint@simulator.amazonses.com"), List.of(), List.of(),
List.of("complaint@simulator.amazonses.com"), "cs", List.of(), List.of(), ts);
List.of("complaint@simulator.amazonses.com"), List.of(), List.of(), "cs", List.of(), List.of(), ts);
assertEquals("Complaint", node.get("eventType").asText());
assertEquals("complaint@simulator.amazonses.com",
@@ -112,7 +112,7 @@ class SesEventPayloadTest {
List.of("normal@example.com", "bounce@simulator.amazonses.com"),
List.of(), List.of(),
List.of("normal@example.com", "bounce@simulator.amazonses.com"),
"cs", List.of(), List.of(), ts);
List.of(), List.of(), "cs", List.of(), List.of(), ts);
var bounced = (com.fasterxml.jackson.databind.node.ArrayNode)
node.get("bounce").get("bouncedRecipients");
@@ -130,7 +130,7 @@ class SesEventPayloadTest {
List.of("normal@example.com", "success@simulator.amazonses.com"),
List.of(), List.of(),
List.of("normal@example.com", "success@simulator.amazonses.com"),
"cs", List.of(), List.of(), ts);
List.of(), List.of(), "cs", List.of(), List.of(), ts);
var recipients = node.get("delivery").get("recipients");
assertEquals(1, recipients.size());
@@ -144,7 +144,7 @@ class SesEventPayloadTest {
List.of("normal@example.com", "complaint@simulator.amazonses.com"),
List.of(), List.of(),
List.of("normal@example.com", "complaint@simulator.amazonses.com"),
"cs", List.of(), List.of(), ts);
List.of(), List.of(), "cs", List.of(), List.of(), ts);
var complained = node.get("complaint").get("complainedRecipients");
assertEquals(1, complained.size());
@@ -158,6 +158,7 @@ class SesEventPayloadTest {
null, "000000000000", "Hello",
List.of("to@example.com"), List.of(), List.of(),
List.of("to@example.com"),
List.of(), List.of(),
"my-cs",
List.of(new MessageTag("campaign", "launch"), new MessageTag("env", "prod")),
List.of(),
@@ -175,6 +176,7 @@ class SesEventPayloadTest {
null, "000000000000", "",
List.of("to@example.com"), List.of(), List.of(),
List.of("to@example.com"),
List.of(), List.of(),
"cs",
List.of(new MessageTag("k", null), new MessageTag("k", "v2")),
List.of(),
@@ -192,6 +194,7 @@ class SesEventPayloadTest {
null, "000000000000", "Hi",
List.of("to@example.com"), List.of(), List.of(),
List.of("to@example.com"),
List.of(), List.of(),
"cs",
List.of(),
List.of(
@@ -215,6 +218,7 @@ class SesEventPayloadTest {
null, "000000000000", "Hi",
List.of("to@example.com"), List.of(), List.of(),
List.of("to@example.com"),
List.of(), List.of(),
"cs",
List.of(),
List.of(
@@ -236,12 +240,70 @@ class SesEventPayloadTest {
assertTrue(hasXReal);
}
@Test
void bounce_unionsSimulatorAndSuppressionRecipients() {
// simulator-bounce@... is detected from the envelope, while
// suppressed-bounce@... comes from the suppression-list group.
// The emitted bouncedRecipients list contains both, deduplicated.
ObjectNode node = SesEventPayload.build(mapper, "BOUNCE", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("bounce@simulator.amazonses.com", "suppressed-bounce@example.com"),
List.of(), List.of(),
List.of("bounce@simulator.amazonses.com", "suppressed-bounce@example.com"),
List.of("suppressed-bounce@example.com"),
List.of(),
"cs", List.of(), List.of(), ts);
var bounced = node.get("bounce").get("bouncedRecipients");
assertEquals(2, bounced.size());
assertEquals("bounce@simulator.amazonses.com",
bounced.get(0).get("emailAddress").asText());
assertEquals("suppressed-bounce@example.com",
bounced.get(1).get("emailAddress").asText());
}
@Test
void complaint_unionsSimulatorAndSuppressionRecipients() {
ObjectNode node = SesEventPayload.build(mapper, "COMPLAINT", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("complaint@simulator.amazonses.com", "suppressed-complaint@example.com"),
List.of(), List.of(),
List.of("complaint@simulator.amazonses.com", "suppressed-complaint@example.com"),
List.of(),
List.of("suppressed-complaint@example.com"),
"cs", List.of(), List.of(), ts);
var complained = node.get("complaint").get("complainedRecipients");
assertEquals(2, complained.size());
assertEquals("complaint@simulator.amazonses.com",
complained.get(0).get("emailAddress").asText());
assertEquals("suppressed-complaint@example.com",
complained.get(1).get("emailAddress").asText());
}
@Test
void bounce_simulatorAndSuppressionSameAddressNotDuplicated() {
// If an address is both a bounce simulator AND on the suppression list with
// reason BOUNCE, it should appear once in bouncedRecipients.
ObjectNode node = SesEventPayload.build(mapper, "BOUNCE", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("bounce@simulator.amazonses.com"),
List.of(), List.of(),
List.of("bounce@simulator.amazonses.com"),
List.of("bounce@simulator.amazonses.com"),
List.of(),
"cs", List.of(), List.of(), ts);
var bounced = node.get("bounce").get("bouncedRecipients");
assertEquals(1, bounced.size());
}
@Test
void reject_hasReason() {
ObjectNode node = SesEventPayload.build(mapper, "REJECT", "msg-1", "from@example.com",
null, "000000000000", "",
List.of("suppressionlist@simulator.amazonses.com"), List.of(), List.of(),
List.of("suppressionlist@simulator.amazonses.com"), "cs", List.of(), List.of(), ts);
List.of("suppressionlist@simulator.amazonses.com"), List.of(), List.of(), "cs", List.of(), List.of(), ts);
assertEquals("Reject", node.get("eventType").asText());
assertNotNull(node.get("reject").get("reason"));
@@ -445,6 +445,111 @@ class SesEventPublishingV2IntegrationTest {
assertTrue(hasUnsubscribe, "expected List-Unsubscribe header in event mail.headers");
}
@Test
@Order(12)
void sendToSuppressedAddress_withBounceReason_publishesSyntheticBounceEvent() throws Exception {
String suppressed = "bounce-suppressed-" + System.nanoTime() + "@example.com";
// Pre-register the address in the account-level suppression list with reason BOUNCE.
given()
.contentType("application/json")
.header("Authorization", SES_AUTH)
.body("{\"EmailAddress\":\"" + suppressed + "\",\"Reason\":\"BOUNCE\"}")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
try {
drainQueue();
given()
.contentType("application/json")
.header("Authorization", SES_AUTH)
.body("""
{
"FromEmailAddress": "%s",
"Destination": {"ToAddresses": ["%s"]},
"ConfigurationSetName": "%s",
"Content": {
"Simple": {
"Subject": {"Data": "evt"},
"Body": {"Text": {"Data": "hi"}}
}
}
}
""".formatted(SENDER, suppressed, CS))
.when()
.post("/v2/email/outbound-emails")
.then()
.statusCode(200);
List<JsonNode> events = receiveSesEvents(2);
assertEquals(2, events.size(), "expected Send and synthetic Bounce events");
assertTrue(events.stream().anyMatch(e -> "Send".equals(e.path("eventType").asText())));
JsonNode bounce = events.stream()
.filter(e -> "Bounce".equals(e.path("eventType").asText()))
.findFirst().orElseThrow();
assertEquals(suppressed,
bounce.path("bounce").path("bouncedRecipients").get(0).path("emailAddress").asText());
} finally {
// Always run cleanup so the suppression list doesn't leak into subsequent tests.
given()
.header("Authorization", SES_AUTH)
.when()
.delete("/v2/email/suppression/addresses/" + suppressed);
}
}
@Test
@Order(13)
void sendToSuppressedAddress_withComplaintReason_publishesSyntheticComplaintEvent() throws Exception {
String suppressed = "complaint-suppressed-" + System.nanoTime() + "@example.com";
given()
.contentType("application/json")
.header("Authorization", SES_AUTH)
.body("{\"EmailAddress\":\"" + suppressed + "\",\"Reason\":\"COMPLAINT\"}")
.when()
.put("/v2/email/suppression/addresses")
.then()
.statusCode(200);
try {
drainQueue();
given()
.contentType("application/json")
.header("Authorization", SES_AUTH)
.body("""
{
"FromEmailAddress": "%s",
"Destination": {"ToAddresses": ["%s"]},
"ConfigurationSetName": "%s",
"Content": {
"Simple": {
"Subject": {"Data": "evt"},
"Body": {"Text": {"Data": "hi"}}
}
}
}
""".formatted(SENDER, suppressed, CS))
.when()
.post("/v2/email/outbound-emails")
.then()
.statusCode(200);
List<JsonNode> events = receiveSesEvents(2);
assertEquals(2, events.size(), "expected Send and synthetic Complaint events");
JsonNode complaint = events.stream()
.filter(e -> "Complaint".equals(e.path("eventType").asText()))
.findFirst().orElseThrow();
assertEquals(suppressed,
complaint.path("complaint").path("complainedRecipients").get(0).path("emailAddress").asText());
} finally {
given()
.header("Authorization", SES_AUTH)
.when()
.delete("/v2/email/suppression/addresses/" + suppressed);
}
}
private void sendEmail(String to) {
given()
.contentType("application/json")
@@ -105,4 +105,140 @@ class SesServiceSmtpTest {
null, null, null,
"Subject", null, "<p>html only</p>");
}
@Test
void sendEmail_allRecipientsSuppressed_skipsRelayButStillStores() {
service.putSuppressedDestination("us-east-1", "to@example.com", "BOUNCE");
service.putSuppressedDestination("us-east-1", "cc@example.com", "COMPLAINT");
String messageId = service.sendEmail("from@example.com",
List.of("to@example.com"),
List.of("cc@example.com"),
null, null,
"Subject", "text body", null, null, List.of(), List.of(), "us-east-1");
assertNotNull(messageId);
assertFalse(emailStore.scan(k -> true).isEmpty(),
"stored SentEmail should still record the original recipient list");
verify(smtpRelay, never()).relay(any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void sendEmail_partialSuppression_relayCalledWithFilteredRecipients() {
// Only suppress one of the To recipients; the other should still reach the relay.
service.putSuppressedDestination("us-east-1", "suppressed@example.com", "BOUNCE");
service.sendEmail("from@example.com",
List.of("to@example.com", "suppressed@example.com"),
List.of("cc-keep@example.com"),
null, null,
"Subject", "text body", null, null, List.of(), List.of(), "us-east-1");
verify(smtpRelay).relay(
"from@example.com",
List.of("to@example.com"),
List.of("cc-keep@example.com"),
null,
null,
"Subject", "text body", null);
}
@Test
void sendRawEmail_allRecipientsSuppressed_skipsRelayRawButStillStores() {
service.putSuppressedDestination("us-east-1", "to@example.com", "BOUNCE");
String messageId = service.sendRawEmail("from@example.com",
List.of("to@example.com"), "raw MIME", null, List.of(), "us-east-1");
assertNotNull(messageId);
assertFalse(emailStore.scan(k -> true).isEmpty());
verify(smtpRelay, never()).relayRaw(any(), any(), any());
}
@Test
void sendRawEmail_partialSuppression_relayRawCalledWithFilteredRecipients() {
service.putSuppressedDestination("us-east-1", "suppressed@example.com", "COMPLAINT");
service.sendRawEmail("from@example.com",
List.of("to@example.com", "suppressed@example.com"),
"raw MIME", null, List.of(), "us-east-1");
verify(smtpRelay).relayRaw(
"from@example.com",
List.of("to@example.com"),
"raw MIME");
}
// ─────────── Per-CS SuppressionOptions override at send time ───────────
@Test
void sendEmail_csOverridesAccountToEmptyList_suppressionListIsIgnored() {
// Account defaults to [BOUNCE, COMPLAINT] suppression. The CS explicitly
// overrides to an empty list, which is the AWS V2 contract for "disable
// suppression filtering for this configuration set". A recipient on the
// suppression list with reason=BOUNCE should still reach the SMTP relay.
service.putSuppressedDestination("us-east-1", "to@example.com", "BOUNCE");
service.createConfigurationSet(new ConfigurationSet("cs-no-suppression"), "us-east-1");
service.putConfigurationSetSuppressionOptions("cs-no-suppression", List.of(), "us-east-1");
service.sendEmail("from@example.com",
List.of("to@example.com"), null, null, null,
"Subject", "text body", null, "cs-no-suppression", List.of(), List.of(), "us-east-1");
verify(smtpRelay).relay(
"from@example.com",
List.of("to@example.com"),
null, null, null,
"Subject", "text body", null);
}
@Test
void sendEmail_csOverridesAccountToBounceOnly_complaintSuppressedAddressStillRelayed() {
// Account-level reasons include both BOUNCE and COMPLAINT by default.
// The CS narrows the effective reasons to [BOUNCE] only — so a recipient
// suppressed for COMPLAINT is NOT filtered when sending through this CS.
service.putSuppressedDestination("us-east-1", "complainer@example.com", "COMPLAINT");
service.createConfigurationSet(new ConfigurationSet("cs-bounce-only"), "us-east-1");
service.putConfigurationSetSuppressionOptions("cs-bounce-only", List.of("BOUNCE"), "us-east-1");
service.sendEmail("from@example.com",
List.of("complainer@example.com"), null, null, null,
"Subject", "text body", null, "cs-bounce-only", List.of(), List.of(), "us-east-1");
verify(smtpRelay).relay(
"from@example.com",
List.of("complainer@example.com"),
null, null, null,
"Subject", "text body", null);
}
@Test
void sendEmail_csWithoutOverride_fallsBackToAccountLevelSuppression() {
// A configuration set whose SuppressionOptions block was never PUT must
// fall back to account-level reasons — same filtering behaviour as a
// send without any configuration set.
service.putSuppressedDestination("us-east-1", "to@example.com", "BOUNCE");
service.createConfigurationSet(new ConfigurationSet("cs-default"), "us-east-1");
service.sendEmail("from@example.com",
List.of("to@example.com"), null, null, null,
"Subject", "text body", null, "cs-default", List.of(), List.of(), "us-east-1");
verify(smtpRelay, never()).relay(any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void sendRawEmail_csOverridesAccountToEmptyList_suppressionListIsIgnored() {
service.putSuppressedDestination("us-east-1", "to@example.com", "BOUNCE");
service.createConfigurationSet(new ConfigurationSet("cs-no-suppression-raw"), "us-east-1");
service.putConfigurationSetSuppressionOptions("cs-no-suppression-raw", List.of(), "us-east-1");
service.sendRawEmail("from@example.com",
List.of("to@example.com"), "raw MIME", "cs-no-suppression-raw", List.of(), "us-east-1");
verify(smtpRelay).relayRaw(
"from@example.com",
List.of("to@example.com"),
"raw MIME");
}
}
@@ -0,0 +1,97 @@
package io.github.hectorvent.floci.services.ses;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.ses.model.AccountSuppressionAttributes;
import io.github.hectorvent.floci.services.ses.model.ConfigurationSet;
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.SentEmail;
import io.github.hectorvent.floci.services.ses.model.SuppressedDestination;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.mockito.Mockito.mock;
/**
* Covers {@link SesService#resolveSuppressionReason(String, String)} — the per-recipient
* lookup that publishSendEvents uses to map suppressed addresses to synthetic Bounce or
* Complaint events.
*
* <p>The helper returns a reason only when the address is on the suppression list AND
* the address's stored reason intersects {@link AccountSuppressionAttributes#getSuppressedReasons()}.
*/
class SesServiceSuppressionReasonTest {
private static final String REGION = "us-east-1";
private SesService service;
private InMemoryStorage<String, SuppressedDestination> suppressionStore;
private InMemoryStorage<String, AccountSuppressionAttributes> accountSuppressionStore;
@BeforeEach
void setUp() {
suppressionStore = new InMemoryStorage<>();
accountSuppressionStore = new InMemoryStorage<>();
service = new SesService(
new InMemoryStorage<String, Identity>(),
new InMemoryStorage<String, SentEmail>(),
new InMemoryStorage<String, Boolean>(),
new InMemoryStorage<String, EmailTemplate>(),
new InMemoryStorage<String, ConfigurationSet>(),
suppressionStore,
accountSuppressionStore,
mock(SmtpRelay.class),
new ObjectMapper());
}
@Test
void notOnList_returnsNull() {
// Default fresh account: suppressedReasons defaults to [BOUNCE, COMPLAINT], but the
// address is not on the list, so resolution returns null.
assertNull(service.resolveSuppressionReason("unknown@example.com", null, REGION));
}
@Test
void onListAndReasonInAccountSettings_returnsReason() {
service.putSuppressedDestination(REGION, "bouncer@example.com", "BOUNCE");
// Account-level suppressedReasons defaults to [BOUNCE, COMPLAINT].
assertEquals("BOUNCE", service.resolveSuppressionReason("bouncer@example.com", null, REGION));
}
@Test
void onListButReasonNotInAccountSettings_returnsNull() {
service.putSuppressedDestination(REGION, "complainer@example.com", "COMPLAINT");
// Narrow the account settings to BOUNCE only.
service.putAccountSuppressionAttributes(REGION, List.of("BOUNCE"));
assertNull(service.resolveSuppressionReason("complainer@example.com", null, REGION));
}
@Test
void accountSettingsEmpty_returnsNull() {
service.putSuppressedDestination(REGION, "bouncer@example.com", "BOUNCE");
// Disable account-level suppression by passing an empty list.
service.putAccountSuppressionAttributes(REGION, new ArrayList<>());
assertNull(service.resolveSuppressionReason("bouncer@example.com", null, REGION));
}
@Test
void leadingTrailingWhitespaceIsNormalized() {
service.putSuppressedDestination(REGION, "trim-me@example.com", "BOUNCE");
// Caller may pass the recipient with surrounding whitespace (e.g. from a header).
assertEquals("BOUNCE",
service.resolveSuppressionReason(" trim-me@example.com ", null, REGION));
}
@Test
void nullOrBlankInput_returnsNull() {
assertNull(service.resolveSuppressionReason(null, null, REGION));
assertNull(service.resolveSuppressionReason("", null, REGION));
assertNull(service.resolveSuppressionReason(" ", null, REGION));
}
}