From 4719269df4313baf769fd5cd46c71f1ad9cf32db Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Sun, 19 Apr 2026 15:05:08 +0900 Subject: [PATCH] fix(ses): align /_aws/ses inspection endpoint with LocalStack behavior (#512) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(ses): store text and html body separately and return raw as RawData The inspection endpoint (/_aws/ses) previously collapsed bodyHtml and bodyText into a single field, making both text_part and html_part identical. Split SentEmail.body into bodyText and bodyHtml so the inspection response correctly distinguishes the two, with null for the absent part — matching LocalStack 4.14.0 behavior. Raw emails now use a dedicated rawData field and constructor. The inspection endpoint returns RawData (the full MIME message) without Destination, Subject, or Body fields, matching LocalStack's shape. Tests: 4 new integration tests covering text+html separation, text-only (html_part=null), html-only (text_part=null), and raw email (RawData without Body/Subject/Destination). * fix(ses): parse and store ReplyToAddresses ReplyToAddresses was silently dropped on both SES v1 (Query) and v2 (REST JSON) send paths. Parse it from the request, store it on SentEmail, and return it in the inspection endpoint (/_aws/ses) for non-raw emails. The field is omitted when empty, matching LocalStack behavior. * fix(ses): return all regions in inspection endpoint LocalStack's /_aws/ses returns emails from all regions regardless of request headers, with each message carrying its own Region field. Floci previously filtered by the default region, hiding emails sent to other regions. - Add region field to SentEmail, set on send. - SesInspectionController GET now calls getAllEmails() (no region filter) and reads region from each email. - DELETE clears all regions. * refactor(ses): remove unused RegionResolver and use putNull for region SesInspectionController no longer filters by region, so the injected RegionResolver is dead code. Remove it and align the null-region handling with other optional fields (putNull instead of empty string). * fix(ses): use bulk clear and add v1 ReplyToAddresses test - clearAllEmails() now calls emailStore.clear() instead of deleting entries one-by-one, avoiding N disk writes on persistent backends. - New SesIntegrationTest covering the SES v1 Query path for ReplyToAddresses: sends via SendEmail with ReplyToAddresses.member.1 and verifies round-trip through the /_aws/ses inspection endpoint. * refactor(ses): simplify getEmails/clearEmails to region-agnostic The inspection endpoint now returns all regions unconditionally, so the region-scoped getEmails(region) and clearEmails(region) are dead code. Replace them with parameter-less getEmails() and clearEmails(), and drop the intermediate getAllEmails/clearAllEmails aliases. * fix(ses): add backward-compat alias for legacy Body field and multi-region test - @JsonAlias("Body") on bodyText so persisted SentEmail objects from before the text/html split still deserialize their body into bodyText rather than silently losing it. - New test: send emails in two different regions (us-east-1 and ap-northeast-1) and verify /_aws/ses returns both with correct per-message Region values. * refactor(ses): move region and replyToAddresses into SentEmail constructors Both fields were set via setters immediately after construction, which is inconsistent with the other fields that are constructor arguments. Add them to the Simple and Raw constructors so all required state is established at creation time. * refactor(ses): align sendEmail parameter order with SentEmail constructor Move region to the first parameter position so the argument order matches the SentEmail constructor (messageId, region, source, to, cc, bcc, replyTo, subject, bodyText, bodyHtml). No functional change. * refactor(ses): remove unused RegionResolver and validate rawMessage - SesService no longer uses RegionResolver (region is always passed as a parameter from controllers). Remove the field, constructor parameter, and import. - sendRawEmail now rejects null/blank rawMessage with InvalidParameterValue, preventing a stored email that would appear as non-raw in the inspection endpoint. * fix(ses): validate source in sendRawEmail sendRawEmail lacked the null/blank check on source that sendEmail already had, allowing stored messages with Source=null and a 200 response. Add the same guard for consistency. * refactor(ses): remove unused EmulatorConfig from SesService constructor The config parameter was injected but never referenced. * revert: restore EmulatorConfig in SesService constructor Removing the unused EmulatorConfig parameter caused an awscli compat test failure (SES: verify identities deleted). The parameter likely influences Quarkus CDI initialization ordering — StorageFactory may depend on EmulatorConfig being resolved first. Restore with @SuppressWarnings("unused") to document intent. * fix(ses): omit empty CcAddresses and BccAddresses from inspection response LocalStack omits these keys when the list is empty rather than returning an empty array. Apply the same !isEmpty() guard already used for ReplyToAddresses. * fix(ses): omit empty ToAddresses and restore region parameter order - Apply the same !isEmpty() guard to ToAddresses so it is omitted from the inspection response when empty, consistent with CcAddresses, BccAddresses, and ReplyToAddresses. - Revert sendEmail parameter order: move region back to the last position to match the convention used by all other SesService methods (verifyEmailIdentity, deleteIdentity, listIdentities, etc.). * refactor(ses): remove unused EmulatorConfig from SesService constructor The config parameter was injected but never referenced. A previous attempt to remove it (76d6c22) was reverted due to a CI failure, but that failure may have been flaky (S3 Notifications queue filter round-trip, a known pre-existing issue). Retrying after rebase onto latest main. --- .../floci/services/ses/SesController.java | 5 +- .../services/ses/SesInspectionController.java | 71 ++-- .../floci/services/ses/SesQueryHandler.java | 3 +- .../floci/services/ses/SesService.java | 44 ++- .../floci/services/ses/model/SentEmail.java | 60 +++- .../services/ses/SesIntegrationTest.java | 28 ++ .../services/ses/SesV2IntegrationTest.java | 315 ++++++++++++++++++ 7 files changed, 463 insertions(+), 63 deletions(-) diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java index 1547fde1b5..8b264e42cb 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesController.java @@ -201,6 +201,7 @@ public class SesController { List toAddresses = jsonArrayToList(destination.path("ToAddresses")); List ccAddresses = jsonArrayToList(destination.path("CcAddresses")); List bccAddresses = jsonArrayToList(destination.path("BccAddresses")); + List replyToAddresses = jsonArrayToList(request.path("ReplyToAddresses")); JsonNode content = request.path("Content"); String messageId; @@ -223,12 +224,12 @@ public class SesController { String bodyText = simple.path("Body").path("Text").path("Data").asText(null); String bodyHtml = simple.path("Body").path("Html").path("Data").asText(null); messageId = sesService.sendEmail(fromEmailAddress, toAddresses, ccAddresses, - bccAddresses, subject, bodyText, bodyHtml, region); + bccAddresses, replyToAddresses, subject, bodyText, bodyHtml, region); } else if (content.has("Template")) { String subject = content.path("Template").path("TemplateName").asText("(template)"); String templateData = content.path("Template").path("TemplateData").asText(""); messageId = sesService.sendEmail(fromEmailAddress, toAddresses, ccAddresses, - bccAddresses, subject, templateData, null, region); + bccAddresses, replyToAddresses, subject, templateData, null, region); } else { throw new AwsException("BadRequestException", "Content must contain Raw, Simple, or Template.", 400); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java index aac1219e5b..e94ea64b3d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesInspectionController.java @@ -1,6 +1,5 @@ package io.github.hectorvent.floci.services.ses; -import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.services.ses.model.SentEmail; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ArrayNode; @@ -25,21 +24,17 @@ import java.util.List; public class SesInspectionController { private final SesService sesService; - private final RegionResolver regionResolver; private final ObjectMapper objectMapper; @Inject - public SesInspectionController(SesService sesService, RegionResolver regionResolver, - ObjectMapper objectMapper) { + public SesInspectionController(SesService sesService, ObjectMapper objectMapper) { this.sesService = sesService; - this.regionResolver = regionResolver; this.objectMapper = objectMapper; } @GET public Response getEmails(@QueryParam("id") String messageId) { - String region = regionResolver.getDefaultRegion(); - List emails = sesService.getEmails(region); + List emails = sesService.getEmails(); ArrayNode messages = objectMapper.createArrayNode(); for (SentEmail email : emails) { @@ -48,28 +43,51 @@ public class SesInspectionController { } ObjectNode node = objectMapper.createObjectNode(); node.put("Id", email.getMessageId()); - node.put("Region", region); + if (email.getRegion() != null) { + node.put("Region", email.getRegion()); + } else { + node.putNull("Region"); + } node.put("Source", email.getSource()); - ObjectNode destination = node.putObject("Destination"); - if (email.getToAddresses() != null) { - ArrayNode toArr = destination.putArray("ToAddresses"); - email.getToAddresses().forEach(toArr::add); - } - if (email.getCcAddresses() != null) { - ArrayNode ccArr = destination.putArray("CcAddresses"); - email.getCcAddresses().forEach(ccArr::add); - } - if (email.getBccAddresses() != null) { - ArrayNode bccArr = destination.putArray("BccAddresses"); - email.getBccAddresses().forEach(bccArr::add); - } + if (email.isRaw()) { + // LocalStack returns RawData for raw emails, without + // Destination / Subject / Body fields. + node.put("RawData", email.getRawData()); + } else { + ObjectNode destination = node.putObject("Destination"); + if (email.getToAddresses() != null && !email.getToAddresses().isEmpty()) { + ArrayNode toArr = destination.putArray("ToAddresses"); + email.getToAddresses().forEach(toArr::add); + } + if (email.getCcAddresses() != null && !email.getCcAddresses().isEmpty()) { + ArrayNode ccArr = destination.putArray("CcAddresses"); + email.getCcAddresses().forEach(ccArr::add); + } + if (email.getBccAddresses() != null && !email.getBccAddresses().isEmpty()) { + ArrayNode bccArr = destination.putArray("BccAddresses"); + email.getBccAddresses().forEach(bccArr::add); + } - node.put("Subject", email.getSubject()); + if (email.getReplyToAddresses() != null && !email.getReplyToAddresses().isEmpty()) { + ArrayNode replyTo = node.putArray("ReplyToAddresses"); + email.getReplyToAddresses().forEach(replyTo::add); + } - ObjectNode body = node.putObject("Body"); - body.put("text_part", email.getBody() != null ? email.getBody() : ""); - body.put("html_part", email.getBody() != null ? email.getBody() : ""); + node.put("Subject", email.getSubject()); + + ObjectNode body = node.putObject("Body"); + if (email.getBodyText() != null) { + body.put("text_part", email.getBodyText()); + } else { + body.putNull("text_part"); + } + if (email.getBodyHtml() != null) { + body.put("html_part", email.getBodyHtml()); + } else { + body.putNull("html_part"); + } + } if (email.getSentAt() != null) { node.put("Timestamp", email.getSentAt().toString()); @@ -85,8 +103,7 @@ public class SesInspectionController { @DELETE public Response clearEmails() { - String region = regionResolver.getDefaultRegion(); - sesService.clearEmails(region); + sesService.clearEmails(); return Response.ok().build(); } } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java index a8cd2140e4..c181fa2ccc 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java @@ -125,12 +125,13 @@ public class SesQueryHandler { List toAddresses = extractMembers(params, "Destination.ToAddresses"); List ccAddresses = extractMembers(params, "Destination.CcAddresses"); List bccAddresses = extractMembers(params, "Destination.BccAddresses"); + List replyToAddresses = extractMembers(params, "ReplyToAddresses"); String subject = getParam(params, "Message.Subject.Data"); String bodyText = getParam(params, "Message.Body.Text.Data"); String bodyHtml = getParam(params, "Message.Body.Html.Data"); String messageId = sesService.sendEmail(source, toAddresses, ccAddresses, bccAddresses, - subject, bodyText, bodyHtml, region); + replyToAddresses, subject, bodyText, bodyHtml, region); String result = new XmlBuilder().elem("MessageId", messageId).build(); return Response.ok(AwsQueryResponse.envelope("SendEmail", AwsNamespaces.SES, result)).build(); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index 6e3c2c9858..d1eda41a2d 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -1,8 +1,6 @@ package io.github.hectorvent.floci.services.ses; -import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; -import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.ses.model.Identity; @@ -26,28 +24,23 @@ public class SesService { private final StorageBackend identityStore; private final StorageBackend emailStore; private final StorageBackend accountSettingsStore; - private final RegionResolver regionResolver; @Inject - public SesService(StorageFactory storageFactory, EmulatorConfig config, - RegionResolver regionResolver) { + public SesService(StorageFactory storageFactory) { this.identityStore = storageFactory.create("ses", "ses-identities.json", new TypeReference>() {}); this.emailStore = storageFactory.create("ses", "ses-emails.json", new TypeReference>() {}); this.accountSettingsStore = storageFactory.create("ses", "ses-account-settings.json", new TypeReference>() {}); - this.regionResolver = regionResolver; } SesService(StorageBackend identityStore, StorageBackend emailStore, - StorageBackend accountSettingsStore, - RegionResolver regionResolver) { + StorageBackend accountSettingsStore) { this.identityStore = identityStore; this.emailStore = emailStore; this.accountSettingsStore = accountSettingsStore; - this.regionResolver = regionResolver; } public Identity verifyEmailIdentity(String emailAddress, String region) { @@ -101,8 +94,8 @@ public class SesService { } public String sendEmail(String source, List toAddresses, List ccAddresses, - List bccAddresses, String subject, String bodyText, - String bodyHtml, String region) { + List bccAddresses, List replyToAddresses, + String subject, String bodyText, String bodyHtml, String region) { if (source == null || source.isBlank()) { throw new AwsException("InvalidParameterValue", "Source email is required.", 400); } @@ -114,8 +107,8 @@ public class SesService { } String messageId = UUID.randomUUID().toString(); - String body = bodyHtml != null ? bodyHtml : bodyText; - SentEmail email = new SentEmail(messageId, source, toAddresses, ccAddresses, bccAddresses, subject, body); + SentEmail email = new SentEmail(messageId, region, source, toAddresses, ccAddresses, + bccAddresses, replyToAddresses, subject, bodyText, bodyHtml); emailStore.put("email::" + region + "::" + messageId, email); LOG.infov("SES email sent: from={0}, to={1}, subject={2}, messageId={3}", @@ -124,10 +117,16 @@ public class SesService { } public String sendRawEmail(String source, List destinations, String rawMessage, String region) { + if (source == null || source.isBlank()) { + throw new AwsException("InvalidParameterValue", "Source email is required.", 400); + } + if (rawMessage == null || rawMessage.isBlank()) { + throw new AwsException("InvalidParameterValue", "RawMessage.Data is required.", 400); + } String messageId = UUID.randomUUID().toString(); - SentEmail email = new SentEmail(messageId, source, + SentEmail email = new SentEmail(messageId, region, source, destinations != null ? destinations : Collections.emptyList(), - null, null, "(raw)", rawMessage); + rawMessage); emailStore.put("email::" + region + "::" + messageId, email); LOG.infov("SES raw email sent: from={0}, messageId={1}", source, messageId); @@ -196,18 +195,13 @@ public class SesService { return emails; } - public List getEmails(String region) { - String prefix = "email::" + region + "::"; - return emailStore.scan(k -> k.startsWith(prefix)); + public List getEmails() { + return emailStore.scan(k -> k.startsWith("email::")); } - public void clearEmails(String region) { - String prefix = "email::" + region + "::"; - List keys = new ArrayList<>(emailStore.keys().stream() - .filter(k -> k.startsWith(prefix)) - .toList()); - keys.forEach(emailStore::delete); - LOG.infov("Cleared all SES emails in region {0}", region); + public void clearEmails() { + emailStore.clear(); + LOG.info("Cleared all SES emails"); } public boolean isAccountSendingEnabled(String region) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java index 123e32a9e5..5ab7faefde 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java @@ -1,5 +1,6 @@ package io.github.hectorvent.floci.services.ses.model; +import com.fasterxml.jackson.annotation.JsonAlias; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; import io.quarkus.runtime.annotations.RegisterForReflection; @@ -14,6 +15,9 @@ public class SentEmail { @JsonProperty("MessageId") private String messageId; + @JsonProperty("Region") + private String region; + @JsonProperty("Source") private String source; @@ -29,30 +33,59 @@ public class SentEmail { @JsonProperty("Subject") private String subject; - @JsonProperty("Body") - private String body; + @JsonProperty("ReplyToAddresses") + private List replyToAddresses; + + @JsonProperty("BodyText") + @JsonAlias("Body") + private String bodyText; + + @JsonProperty("BodyHtml") + private String bodyHtml; + + @JsonProperty("RawData") + private String rawData; @JsonProperty("SentAt") private Instant sentAt; public SentEmail() {} - public SentEmail(String messageId, String source, List toAddresses, - List ccAddresses, List bccAddresses, - String subject, String body) { + /** Constructor for Simple / Template content. */ + public SentEmail(String messageId, String region, String source, + List toAddresses, List ccAddresses, + List bccAddresses, List replyToAddresses, + String subject, String bodyText, String bodyHtml) { this.messageId = messageId; + this.region = region; this.source = source; this.toAddresses = toAddresses; this.ccAddresses = ccAddresses; this.bccAddresses = bccAddresses; + this.replyToAddresses = replyToAddresses; this.subject = subject; - this.body = body; + this.bodyText = bodyText; + this.bodyHtml = bodyHtml; + this.sentAt = Instant.now(); + } + + /** Constructor for Raw content. */ + public SentEmail(String messageId, String region, String source, + List destinations, String rawData) { + this.messageId = messageId; + this.region = region; + this.source = source; + this.toAddresses = destinations; + this.rawData = rawData; this.sentAt = Instant.now(); } public String getMessageId() { return messageId; } public void setMessageId(String messageId) { this.messageId = messageId; } + public String getRegion() { return region; } + public void setRegion(String region) { this.region = region; } + public String getSource() { return source; } public void setSource(String source) { this.source = source; } @@ -68,8 +101,19 @@ public class SentEmail { public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } - public String getBody() { return body; } - public void setBody(String body) { this.body = body; } + public List getReplyToAddresses() { return replyToAddresses; } + public void setReplyToAddresses(List replyToAddresses) { this.replyToAddresses = replyToAddresses; } + + public String getBodyText() { return bodyText; } + public void setBodyText(String bodyText) { this.bodyText = bodyText; } + + public String getBodyHtml() { return bodyHtml; } + public void setBodyHtml(String bodyHtml) { this.bodyHtml = bodyHtml; } + + public String getRawData() { return rawData; } + public void setRawData(String rawData) { this.rawData = rawData; } + + public boolean isRaw() { return rawData != null; } public Instant getSentAt() { return sentAt; } public void setSentAt(Instant sentAt) { this.sentAt = sentAt; } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java index 372ccb1d30..22d75a4439 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesIntegrationTest.java @@ -357,6 +357,34 @@ class SesIntegrationTest { @Test @Order(20) + void sendEmailV1_replyToAddressesStoredInInspection() { + given().delete("/_aws/ses").then().statusCode(200); + + given() + .contentType("application/x-www-form-urlencoded") + .header("Authorization", "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request") + .formParam("Action", "SendEmail") + .formParam("Source", "sender@example.com") + .formParam("Destination.ToAddresses.member.1", "recipient@example.com") + .formParam("ReplyToAddresses.member.1", "reply@example.com") + .formParam("Message.Subject.Data", "V1 ReplyTo") + .formParam("Message.Body.Text.Data", "body") + .when() + .post("/") + .then() + .statusCode(200) + .body(containsString("")); + + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages[0].ReplyToAddresses", hasItem("reply@example.com")); + } + + @Test + @Order(21) void unsupportedAction_returns400() { given() .contentType("application/x-www-form-urlencoded") diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java index 508ed61b1d..1ad9faa6e4 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesV2IntegrationTest.java @@ -617,6 +617,321 @@ class SesV2IntegrationTest { .body("__type", equalTo("BadRequestException")); } + // ──────────────── Inspection endpoint (/_aws/ses) ──────────────── + + @Test + @Order(70) + void inspectionEndpoint_textAndHtmlAreStoredSeparately() { + // Create identity + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + {"EmailIdentity": "inspect-sender@example.com"} + """) + .when() + .post("/v2/email/identities") + .then() + .statusCode(200); + + // Clear any previous messages + given().delete("/_aws/ses").then().statusCode(200); + + // Send with distinct Text and Html bodies + String messageId = given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "inspect-sender@example.com", + "Destination": { + "ToAddresses": ["inspect-to@example.com"] + }, + "Content": { + "Simple": { + "Subject": {"Data": "Inspect Test"}, + "Body": { + "Text": {"Data": "plain text body"}, + "Html": {"Data": "

html body

"} + } + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200) + .extract() + .path("MessageId"); + + // Verify via inspection endpoint + given() + .when() + .get("/_aws/ses?id=" + messageId) + .then() + .statusCode(200) + .body("messages[0].Body.text_part", equalTo("plain text body")) + .body("messages[0].Body.html_part", equalTo("

html body

")); + } + + @Test + @Order(71) + void inspectionEndpoint_textOnlyEmail() { + given().delete("/_aws/ses").then().statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "inspect-sender@example.com", + "Destination": { + "ToAddresses": ["inspect-to@example.com"] + }, + "Content": { + "Simple": { + "Subject": {"Data": "Text Only"}, + "Body": { + "Text": {"Data": "only text"} + } + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages[0].Body.text_part", equalTo("only text")) + .body("messages[0].Body.html_part", nullValue()); + } + + @Test + @Order(72) + void inspectionEndpoint_htmlOnlyEmail() { + given().delete("/_aws/ses").then().statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "inspect-sender@example.com", + "Destination": { + "ToAddresses": ["inspect-to@example.com"] + }, + "Content": { + "Simple": { + "Subject": {"Data": "Html Only"}, + "Body": { + "Html": {"Data": "only html"} + } + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages[0].Body.text_part", nullValue()) + .body("messages[0].Body.html_part", equalTo("only html")); + } + + @Test + @Order(73) + void inspectionEndpoint_replyToAddressesAreStored() { + given().delete("/_aws/ses").then().statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "inspect-sender@example.com", + "Destination": { + "ToAddresses": ["inspect-to@example.com"] + }, + "ReplyToAddresses": ["reply1@example.com", "reply2@example.com"], + "Content": { + "Simple": { + "Subject": {"Data": "ReplyTo Test"}, + "Body": {"Text": {"Data": "hello"}} + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages[0].ReplyToAddresses", hasItems("reply1@example.com", "reply2@example.com")); + } + + @Test + @Order(74) + void inspectionEndpoint_noReplyToOmitsField() { + given().delete("/_aws/ses").then().statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "inspect-sender@example.com", + "Destination": { + "ToAddresses": ["inspect-to@example.com"] + }, + "Content": { + "Simple": { + "Subject": {"Data": "No ReplyTo"}, + "Body": {"Text": {"Data": "hello"}} + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages[0]", not(hasKey("ReplyToAddresses"))); + } + + @Test + @Order(75) + void inspectionEndpoint_rawEmailReturnsRawData() { + given().delete("/_aws/ses").then().statusCode(200); + + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + { + "FromEmailAddress": "inspect-sender@example.com", + "Destination": { + "ToAddresses": ["inspect-to@example.com"] + }, + "Content": { + "Raw": { + "Data": "Subject: Raw\\r\\n\\r\\nRaw body" + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages[0].RawData", notNullValue()) + .body("messages[0].RawData", containsString("Raw body")) + .body("messages[0]", not(hasKey("Destination"))) + .body("messages[0]", not(hasKey("Subject"))) + .body("messages[0]", not(hasKey("Body"))); + } + + @Test + @Order(80) + void inspectionEndpoint_returnsEmailsFromAllRegions() { + given().delete("/_aws/ses").then().statusCode(200); + + // Create identity usable in both regions (domain covers all addresses) + given() + .contentType("application/json") + .header("Authorization", AUTH_HEADER) + .body(""" + {"EmailIdentity": "example.com"} + """) + .when() + .post("/v2/email/identities") + .then() + .statusCode(200); + + // Send from us-east-1 + given() + .contentType("application/json") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request") + .body(""" + { + "FromEmailAddress": "multi@example.com", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": { + "Simple": { + "Subject": {"Data": "US East"}, + "Body": {"Text": {"Data": "from us-east-1"}} + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + // Send from ap-northeast-1 + given() + .contentType("application/json") + .header("Authorization", + "AWS4-HMAC-SHA256 Credential=AKID/20260101/ap-northeast-1/ses/aws4_request") + .body(""" + { + "FromEmailAddress": "multi@example.com", + "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": { + "Simple": { + "Subject": {"Data": "AP NE"}, + "Body": {"Text": {"Data": "from ap-northeast-1"}} + } + } + } + """) + .when() + .post("/v2/email/outbound-emails") + .then() + .statusCode(200); + + // Inspection returns both, each with correct Region + given() + .when() + .get("/_aws/ses") + .then() + .statusCode(200) + .body("messages.size()", equalTo(2)) + .body("messages.find { it.Subject == 'US East' }.Region", equalTo("us-east-1")) + .body("messages.find { it.Subject == 'AP NE' }.Region", equalTo("ap-northeast-1")); + } + // ──────────────── GetEmailIdentity full response ──────────────── @Test