From 1d74e9adc80b8e91792f33bf480cf8a6d3c6ae89 Mon Sep 17 00:00:00 2001 From: Kenshin Okinaka Date: Wed, 25 Nov 2026 19:45:59 +0900 Subject: [PATCH] fix(ses): answer a paused account with the AWS message and order (#9417) A send while account sending is paused now answers AWS's message, "Sending is paused for this account.", on v1 and v2, in place of "Account sending is disabled." The codes are unchanged. v2 SendEmail with Raw content, v2 SendBulkEmail and v1 SendBulkTemplatedEmail check the sender before refusing a paused account, as AWS does, so an unverified sender gets MessageRejected first. Every other send still refuses a paused account ahead of every check. --- docs/services/ses.md | 2 +- .../aws/services/ses/SesAccountService.java | 10 ++ .../aws/services/ses/SesQueryHandler.java | 24 +--- .../aws/services/ses/SesSendController.java | 16 +-- .../io/floci/aws/services/ses/SesService.java | 20 ++- .../io/floci/aws/services/ses/SesV2Json.java | 15 ++- ...AccountSendingPausedV2IntegrationTest.java | 123 ++++++++++++++++++ .../ses/SesBulkV1IntegrationTest.java | 1 + .../SesServiceAccountSendingPausedTest.java | 114 ++++++++++++++++ .../services/ses/SesServiceTestBuilder.java | 7 + .../ses/SesV1AccountSendingPausedTest.java | 46 ++++++- .../floci/aws/services/ses/SesV2JsonTest.java | 11 ++ 12 files changed, 348 insertions(+), 41 deletions(-) create mode 100644 src/test/java/io/floci/aws/services/ses/SesAccountSendingPausedV2IntegrationTest.java create mode 100644 src/test/java/io/floci/aws/services/ses/SesServiceAccountSendingPausedTest.java diff --git a/docs/services/ses.md b/docs/services/ses.md index 224f0e166b..87972eeb21 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -542,7 +542,7 @@ The operations whose body has a structure member holding another structure, or h | map | array / any other non-object | `Start of list found where not expected` / `Expected map or null` | | list | object / any other non-array | `Start of structure or map found where not expected.` / `Expected list or null` | -A JSON `null` member counts as absent. A `null` address list element passes this check and fails later, as on AWS: `SendEmail` answers `Invalid email address null.` after its content and sender checks and before the tenant lookup, and `SendBulkEmail` reports it as the `INVALID_PARAMETER` status of the entry it belongs to (of every entry for a `null` Reply-To), or as `Destination cannot be empty` when no entry of the request has a non-null recipient, the answer every entry gets then, whatever its lists hold. A `null` element of `Topics` (`CreateContactList`, `UpdateContactList`) or `TopicPreferences` (`CreateContact`, `UpdateContact`) also passes this check, but there AWS answers it with a `500 InternalFailure`, which Floci replaces with `BadRequestException` naming the element ("1 validation error detected: Value at 'topics.1.member.topicName' failed to satisfy constraint: Member must not be null"). The account-level sending switch is still checked first, so a paused account answers `SendingPausedException` even for a malformed body. Likewise `BatchGetMetricData` checks that Virtual Deliverability Manager is enabled before it reads the body. +A JSON `null` member counts as absent. A `null` address list element passes this check and fails later, as on AWS: `SendEmail` answers `Invalid email address null.` after its content and sender checks and before the tenant lookup, and `SendBulkEmail` reports it as the `INVALID_PARAMETER` status of the entry it belongs to (of every entry for a `null` Reply-To), or as `Destination cannot be empty` when no entry of the request has a non-null recipient, the answer every entry gets then, whatever its lists hold. A `null` element of `Topics` (`CreateContactList`, `UpdateContactList`) or `TopicPreferences` (`CreateContact`, `UpdateContact`) also passes this check, but there AWS answers it with a `500 InternalFailure`, which Floci replaces with `BadRequestException` naming the element ("1 validation error detected: Value at 'topics.1.member.topicName' failed to satisfy constraint: Member must not be null"). The account-level sending switch is still checked first on `SendEmail` with `Simple` or `Template` content and on `SendCustomVerificationEmail`, so a paused account answers `SendingPausedException` even for a malformed body. `SendEmail` with `Raw` content and `SendBulkEmail` (and v1 `SendBulkTemplatedEmail`) refuse a paused account only after the sender check, so an unverified sender gets `MessageRejected` first, as on AWS. A bulk send in which no entry names a recipient checks no sender and is still refused. Likewise `BatchGetMetricData` checks that Virtual Deliverability Manager is enabled before it reads the body. The other operations check their members one at a time, and some of them still answer a wrong JSON type with `BadRequestException`. The list operations' map `Filter` answers as in the table. Their integer `PageSize`, like `CreateExportJob`'s `MaxResults`, answers a string with `STRING_VALUE can not be converted to an Integer` (`TRUE_VALUE` / `FALSE_VALUE` for a boolean, and as in the table for an object or an array), while a number is read as an int first, as on AWS: `1.5` is accepted, and a value above the int range is checked against the page-size bounds as `2147483647`. diff --git a/src/main/java/io/floci/aws/services/ses/SesAccountService.java b/src/main/java/io/floci/aws/services/ses/SesAccountService.java index 3be3f8bbdc..6f3098a413 100644 --- a/src/main/java/io/floci/aws/services/ses/SesAccountService.java +++ b/src/main/java/io/floci/aws/services/ses/SesAccountService.java @@ -60,6 +60,16 @@ public class SesAccountService { return accountSettingsStore.get("sending::" + region).orElse(true); } + public void requireAccountSendingEnabled(String region) { + if (!isAccountSendingEnabled(region)) { + throw sendingPaused(); + } + } + + static AwsException sendingPaused() { + return new AwsException("AccountSendingPausedException", "Sending is paused for this account.", 400); + } + public void setAccountSendingEnabled(String region, boolean enabled) { accountSettingsStore.put("sending::" + region, enabled); LOG.infov("Updated account sending enabled for region {0}: {1}", region, enabled); diff --git a/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java b/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java index 37b9ca0a4d..b609d95503 100644 --- a/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java +++ b/src/main/java/io/floci/aws/services/ses/SesQueryHandler.java @@ -277,10 +277,7 @@ public class SesQueryHandler { } private Response handleSendEmail(MultivaluedMap params, String region) { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("AccountSendingPausedException", - "Account sending is disabled.", 400); - } + accountService.requireAccountSendingEnabled(region); String source = getParam(params, "Source"); List toAddresses = extractMembers(params, "Destination.ToAddresses"); List ccAddresses = extractMembers(params, "Destination.CcAddresses"); @@ -314,10 +311,7 @@ public class SesQueryHandler { } private Response handleSendRawEmail(MultivaluedMap params, String region) { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("AccountSendingPausedException", - "Account sending is disabled.", 400); - } + accountService.requireAccountSendingEnabled(region); String source = getParam(params, "Source"); List destinations = extractMembers(params, "Destinations"); String rawMessage = getParam(params, "RawMessage.Data"); @@ -635,10 +629,7 @@ public class SesQueryHandler { } private Response handleSendTemplatedEmail(MultivaluedMap params, String region) { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("AccountSendingPausedException", - "Account sending is disabled.", 400); - } + accountService.requireAccountSendingEnabled(region); String source = getParam(params, "Source"); List toAddresses = extractMembers(params, "Destination.ToAddresses"); List ccAddresses = extractMembers(params, "Destination.CcAddresses"); @@ -746,10 +737,7 @@ public class SesQueryHandler { } private Response handleSendCustomVerificationEmail(MultivaluedMap params, String region) { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("AccountSendingPausedException", - "Account sending is disabled.", 400); - } + accountService.requireAccountSendingEnabled(region); // getParam (not requireParam) so the service is the single validation authority and returns // the AWS-faithful "Email address not specified." / "Invalid email address<...>." shapes. String messageId = sesService.sendCustomVerificationEmail( @@ -772,10 +760,6 @@ public class SesQueryHandler { } private Response handleSendBulkTemplatedEmail(MultivaluedMap params, String region) { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("AccountSendingPausedException", - "Account sending is disabled.", 400); - } String templateName = getParam(params, "Template"); String templateArn = getParam(params, "TemplateArn"); BulkDestinationIndexes destinations = bulkDestinationIndexes(params); diff --git a/src/main/java/io/floci/aws/services/ses/SesSendController.java b/src/main/java/io/floci/aws/services/ses/SesSendController.java index 028e6d7493..db7a31b8a6 100644 --- a/src/main/java/io/floci/aws/services/ses/SesSendController.java +++ b/src/main/java/io/floci/aws/services/ses/SesSendController.java @@ -39,6 +39,7 @@ import java.util.Base64; import java.util.List; import java.util.Map; +import static io.floci.aws.services.ses.SesV2Json.namesRawContent; import static io.floci.aws.services.ses.SesV2Json.readOptionBody; import static io.floci.aws.services.ses.SesV2Json.remapCvetException; import static io.floci.aws.services.ses.SesV2Json.remapV1Exception; @@ -82,9 +83,8 @@ public class SesSendController { public Response sendEmail(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("SendingPausedException", - "Account sending is disabled.", 400); + if (!accountService.isAccountSendingEnabled(region) && !namesRawContent(objectMapper, body)) { + throw SesAccountService.sendingPaused(); } JsonNode request = readOptionBody(objectMapper, body); @@ -221,11 +221,6 @@ public class SesSendController { public Response sendBulkEmail(@Context HttpHeaders headers, String body) { String region = regionResolver.resolveRegion(headers); try { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("SendingPausedException", - "Account sending is disabled.", 400); - } - JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("SendBulkEmail", request); validateTrackingOverrides(request); @@ -362,10 +357,7 @@ public class SesSendController { String region = regionResolver.resolveRegion(headers); String templateName = null; try { - if (!accountService.isAccountSendingEnabled(region)) { - throw new AwsException("SendingPausedException", - "Account sending is disabled.", 400); - } + accountService.requireAccountSendingEnabled(region); JsonNode request = readOptionBody(objectMapper, body); SesV2RequestShapes.check("SendCustomVerificationEmail", request); templateName = request.path("TemplateName").asText(null); diff --git a/src/main/java/io/floci/aws/services/ses/SesService.java b/src/main/java/io/floci/aws/services/ses/SesService.java index cee862892f..1ae5da46cf 100644 --- a/src/main/java/io/floci/aws/services/ses/SesService.java +++ b/src/main/java/io/floci/aws/services/ses/SesService.java @@ -136,6 +136,7 @@ public class SesService { // S/MIME certificate associations live in SesIdentityCertificateService, which the v2 controller // calls directly; the facade only reaches it for the identity delete-guard. private final SesIdentityCertificateService certificateService; + private final SesAccountService accountService; private final SmtpRelay smtpRelay; private final SesEventPublisher eventPublisher; private final String defaultAccountId; @@ -154,8 +155,8 @@ public class SesService { SesSuppressionService suppressionService, SesDedicatedIpService dedicatedIpService, SesTemplateService templateService, SesSentEmailService sentEmailService, SesTenantService tenantService, SesConfigurationSetService configSetService, - SesIdentityCertificateService certificateService, SmtpRelay smtpRelay, - SesEventPublisher eventPublisher, EmulatorConfig config, + SesIdentityCertificateService certificateService, SesAccountService accountService, + SmtpRelay smtpRelay, SesEventPublisher eventPublisher, EmulatorConfig config, RegionResolver regionResolver) { this.identityService = identityService; this.sentEmailService = sentEmailService; @@ -168,6 +169,7 @@ public class SesService { this.cvetService = cvetService; this.tenantService = tenantService; this.certificateService = certificateService; + this.accountService = accountService; this.smtpRelay = smtpRelay; this.eventPublisher = eventPublisher; this.defaultAccountId = config.defaultAccountId(); @@ -187,6 +189,7 @@ public class SesService { SesCvetService cvetService, SesTenantService tenantService, SesIdentityCertificateService certificateService, + SesAccountService accountService, SmtpRelay smtpRelay) { this.identityService = identityService; this.sentEmailService = sentEmailService; @@ -199,6 +202,7 @@ public class SesService { this.cvetService = cvetService; this.tenantService = tenantService; this.certificateService = certificateService; + this.accountService = accountService; this.smtpRelay = smtpRelay; this.eventPublisher = null; this.defaultAccountId = "000000000000"; @@ -590,6 +594,9 @@ public class SesService { SesAddressLength.requireRaw(request, raw, parsed.message()); if (requireVerifiedSender) { requireVerifiedSenders(rawSendingAddresses(request, raw, headers), region); + // v2 refuses a paused account only here, after the sender check; v1 SendRawEmail has + // already refused it ahead of every check (probe-confirmed). + accountService.requireAccountSendingEnabled(region); } requireTenantAssociations(tenant, effectiveSource, effectiveConfigSet, null, region); // Resolve suppression before recording the message so a bad ListManagementOptions (e.g. an @@ -1723,8 +1730,10 @@ public class SesService { } // When no entry names a recipient, SES answers every entry the same, whatever its lists hold - // (probe-confirmed for null elements, empty lists and an empty Destination). + // (probe-confirmed for null elements, empty lists and an empty Destination). No sender is + // checked then, so a paused account is still refused (unprobed). if (!anyBulkRecipient(request.entries())) { + accountService.requireAccountSendingEnabled(request.region()); return request.entries().stream() .map(entry -> BulkEmailEntryResult.failure(BulkEmailEntryResult.Status.INVALID_PARAMETER, "Destination cannot be empty")) @@ -1734,10 +1743,13 @@ public class SesService { SesTemplateService.requireSendTemplateName(request.templateName()); } // The sender and return path are checked once for the whole call, then a stored template is - // looked up, then the tenant associations, as on a single send (probe-confirmed). + // looked up, then the tenant associations, as on a single send (probe-confirmed). A paused + // account is refused after the sender check (probe-confirmed); placing it ahead of the + // template lookup is a choice. requireVerifiedSenders(List.of( new SendingAddress(request.source(), request.sourceArn()), new SendingAddress(request.returnPath(), request.returnPathArn())), request.region()); + accountService.requireAccountSendingEnabled(request.region()); EmailContent.InlineTemplate template = switch (request.defaultContent()) { case EmailContent.Template stored -> templateService.inline(stored, request.region()); case EmailContent.InlineTemplate inline -> inline; diff --git a/src/main/java/io/floci/aws/services/ses/SesV2Json.java b/src/main/java/io/floci/aws/services/ses/SesV2Json.java index c7e6b3d383..db76b505ed 100644 --- a/src/main/java/io/floci/aws/services/ses/SesV2Json.java +++ b/src/main/java/io/floci/aws/services/ses/SesV2Json.java @@ -51,7 +51,7 @@ final class SesV2Json { case "AlreadyExists", "ConfigurationSetAlreadyExists", "CustomVerificationEmailTemplateAlreadyExists" -> new AwsException("AlreadyExistsException", e.getMessage(), 400); - case "ConfigurationSetSendingPausedException" -> + case "ConfigurationSetSendingPausedException", "AccountSendingPausedException" -> new AwsException("SendingPausedException", e.getMessage(), 400); default -> e; }; @@ -146,6 +146,19 @@ final class SesV2Json { return request; } + /** + * Whether a SendEmail body names Raw content, which a paused account refuses only after the + * sender check. A body that does not parse names none. + */ + static boolean namesRawContent(ObjectMapper objectMapper, String body) { + try { + return readOptionBody(objectMapper, body).path("Content").has("Raw"); + } catch (AwsException ignored) { + // A body that does not parse is not a raw send, so a paused account still answers first. + return false; + } + } + /** Read an optional string member, rejecting a non-string value the way the AWS deserialization layer does. */ static String parseOptionString(JsonNode node, String field) { if (node.isMissingNode() || node.isNull()) { diff --git a/src/test/java/io/floci/aws/services/ses/SesAccountSendingPausedV2IntegrationTest.java b/src/test/java/io/floci/aws/services/ses/SesAccountSendingPausedV2IntegrationTest.java new file mode 100644 index 0000000000..029a4c8389 --- /dev/null +++ b/src/test/java/io/floci/aws/services/ses/SesAccountSendingPausedV2IntegrationTest.java @@ -0,0 +1,123 @@ +package io.floci.aws.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.nio.charset.StandardCharsets; +import java.util.Base64; + +import static io.floci.aws.services.ses.SesV2Requests.post; +import static io.floci.aws.services.ses.SesV2Requests.v2; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.startsWith; + +/** + * Where a paused account is refused among the v2 send checks (probe-confirmed): SendEmail with Simple + * or Template content refuses it first, SendEmail with Raw content and SendBulkEmail only after the + * sender check. + */ +@QuarkusTest +class SesAccountSendingPausedV2IntegrationTest { + + private static final String VERIFIED = "paused-v2-verified@floci.test"; + private static final String UNVERIFIED = "paused-v2-unverified@floci.test"; + private static final String PAUSED = "Sending is paused for this account."; + private static final String NOT_VERIFIED = "Email address is not verified."; + + @BeforeEach + void pauseSending() { + post("/v2/email/identities", "{\"EmailIdentity\":\"" + VERIFIED + "\"}"); + setSending(false); + } + + @AfterEach + void resumeSending() { + setSending(true); + } + + @Test + void sendEmailSimple_unverifiedSender_isRefusedByThePause() { + post("/v2/email/outbound-emails", """ + {"FromEmailAddress": "%s", "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "s"}, "Body": {"Text": {"Data": "b"}}}}} + """.formatted(UNVERIFIED)) + .statusCode(400) + .body("__type", equalTo("SendingPausedException")) + .body("message", equalTo(PAUSED)); + } + + @Test + void sendEmailTemplate_unverifiedSenderAndMissingTemplate_isRefusedByThePause() { + post("/v2/email/outbound-emails", """ + {"FromEmailAddress": "%s", "Destination": {"ToAddresses": ["to@example.com"]}, + "Content": {"Template": {"TemplateName": "paused-v2-missing", "TemplateData": "{}"}}} + """.formatted(UNVERIFIED)) + .statusCode(400) + .body("__type", equalTo("SendingPausedException")) + .body("message", equalTo(PAUSED)); + } + + @Test + void sendEmail_malformedBody_isRefusedByThePause() { + post("/v2/email/outbound-emails", "{not json") + .statusCode(400) + .body("__type", equalTo("SendingPausedException")); + } + + @Test + void sendEmailRaw_unverifiedSender_isRejectedBeforeThePause() { + post("/v2/email/outbound-emails", rawBody(UNVERIFIED)) + .statusCode(400) + .body("__type", equalTo("MessageRejected")) + .body("message", startsWith(NOT_VERIFIED)); + } + + @Test + void sendEmailRaw_verifiedSender_isRefusedByThePause() { + post("/v2/email/outbound-emails", rawBody(VERIFIED)) + .statusCode(400) + .body("__type", equalTo("SendingPausedException")) + .body("message", equalTo(PAUSED)); + } + + @Test + void sendBulkEmail_unverifiedSender_isRejectedBeforeThePause() { + post("/v2/email/outbound-bulk-emails", bulkBody(UNVERIFIED)) + .statusCode(400) + .body("__type", equalTo("MessageRejected")) + .body("message", startsWith(NOT_VERIFIED)); + } + + @Test + void sendBulkEmail_verifiedSender_isRefusedByThePause() { + post("/v2/email/outbound-bulk-emails", bulkBody(VERIFIED)) + .statusCode(400) + .body("__type", equalTo("SendingPausedException")) + .body("message", equalTo(PAUSED)); + } + + private static String rawBody(String sender) { + String mime = "From: " + sender + "\r\nTo: to@example.com\r\nSubject: s\r\n\r\nbody"; + String data = Base64.getEncoder().encodeToString(mime.getBytes(StandardCharsets.UTF_8)); + return "{\"Content\": {\"Raw\": {\"Data\": \"" + data + "\"}}}"; + } + + private static String bulkBody(String sender) { + return """ + {"FromEmailAddress": "%s", + "DefaultContent": {"Template": {"TemplateContent": {"Subject": "s", "Text": "t"}}}, + "BulkEmailEntries": [{"Destination": {"ToAddresses": ["to@example.com"]}}]} + """.formatted(sender); + } + + private static void setSending(boolean enabled) { + v2() + .body("{\"SendingEnabled\":" + enabled + "}") + .when() + .put("/v2/email/account/sending") + .then() + .statusCode(200); + } +} diff --git a/src/test/java/io/floci/aws/services/ses/SesBulkV1IntegrationTest.java b/src/test/java/io/floci/aws/services/ses/SesBulkV1IntegrationTest.java index 585007759f..d517a3d9ab 100644 --- a/src/test/java/io/floci/aws/services/ses/SesBulkV1IntegrationTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesBulkV1IntegrationTest.java @@ -222,6 +222,7 @@ class SesBulkV1IntegrationTest { .formParam("Action", "SendBulkTemplatedEmail") .formParam("Source", SENDER) .formParam("Template", "v1-bulk-welcome") + .formParam("DefaultTemplateData", "{}") .formParam("Destinations.member.1.Destination.ToAddresses.member.1", "alice@example.com") .when() .post("/") diff --git a/src/test/java/io/floci/aws/services/ses/SesServiceAccountSendingPausedTest.java b/src/test/java/io/floci/aws/services/ses/SesServiceAccountSendingPausedTest.java new file mode 100644 index 0000000000..8efbe7d6ea --- /dev/null +++ b/src/test/java/io/floci/aws/services/ses/SesServiceAccountSendingPausedTest.java @@ -0,0 +1,114 @@ +package io.floci.aws.services.ses; + +import io.floci.aws.core.common.AwsException; +import io.floci.aws.services.ses.model.BulkEmailEntry; +import io.floci.aws.services.ses.model.EmailContent; +import io.floci.aws.services.ses.model.SendBulkEmailRequest; +import io.floci.aws.services.ses.model.SendEmailRequest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * A raw send and a bulk send check the sender before they refuse a paused account + * (probe-confirmed on v2 SendEmail Raw, v2 SendBulkEmail and v1 SendBulkTemplatedEmail). + */ +class SesServiceAccountSendingPausedTest { + + private static final String REGION = "us-east-1"; + private static final String SENDER = "from@example.com"; + private static final String TO = "to@example.com"; + private static final EmailContent.InlineTemplate INLINE = + new EmailContent.InlineTemplate("s", "t", null, null, List.of()); + private static final String RAW = "From: " + SENDER + "\r\nTo: " + TO + "\r\nSubject: s\r\n\r\nbody"; + + private SesServiceTestBuilder builder; + private SesService service; + + @BeforeEach + void setUp() { + builder = SesServiceTestBuilder.create(); + service = builder.build(); + builder.accountService().setAccountSendingEnabled(REGION, false); + } + + @Test + void rawSend_unverifiedSender_isRejectedBeforeThePause() { + AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(rawRequest())); + + assertEquals("MessageRejected", e.getErrorCode()); + } + + @Test + void rawSend_verifiedSender_isRefusedByThePause() { + builder.identityService().verifyEmailIdentity(SENDER, REGION); + + AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(rawRequest())); + + assertPaused(e); + } + + @Test + void bulkSend_unverifiedSender_isRejectedBeforeThePause() { + AwsException e = assertThrows(AwsException.class, () -> service.sendBulkEmail(bulkRequest(INLINE))); + + assertEquals("MessageRejected", e.getErrorCode()); + } + + @Test + void bulkSend_verifiedSender_isRefusedByThePause() { + builder.identityService().verifyEmailIdentity(SENDER, REGION); + + AwsException e = assertThrows(AwsException.class, () -> service.sendBulkEmail(bulkRequest(INLINE))); + + assertPaused(e); + } + + @Test + void bulkSend_verifiedSender_isRefusedByThePauseBeforeAMissingTemplate() { + builder.identityService().verifyEmailIdentity(SENDER, REGION); + + AwsException e = assertThrows(AwsException.class, () -> service.sendBulkEmail( + bulkRequest(new EmailContent.Template("missing", null, List.of())))); + + assertPaused(e); + } + + @Test + void bulkSend_noRecipient_isRefusedByThePause() { + AwsException e = assertThrows(AwsException.class, () -> service.sendBulkEmail(SendBulkEmailRequest.builder() + .source(SENDER) + .region(REGION) + .defaultContent(INLINE) + .entries(List.of(new BulkEmailEntry(List.of(), null, null, null, null, null))) + .build())); + + assertPaused(e); + } + + private static SendEmailRequest rawRequest() { + return SendEmailRequest.builder() + .region(REGION) + .content(new EmailContent.Raw(RAW)) + .build(); + } + + private static SendBulkEmailRequest bulkRequest(EmailContent.TemplateContent content) { + return SendBulkEmailRequest.builder() + .source(SENDER) + .region(REGION) + .defaultContent(content) + .entries(List.of(new BulkEmailEntry(List.of(TO), null, null, null, null, null))) + .build(); + } + + private static void assertPaused(AwsException e) { + assertEquals("AccountSendingPausedException", e.getErrorCode()); + assertEquals("Sending is paused for this account.", e.getMessage()); + assertEquals(400, e.getHttpStatus()); + } +} diff --git a/src/test/java/io/floci/aws/services/ses/SesServiceTestBuilder.java b/src/test/java/io/floci/aws/services/ses/SesServiceTestBuilder.java index 1857e3fb31..2b2c7022f5 100644 --- a/src/test/java/io/floci/aws/services/ses/SesServiceTestBuilder.java +++ b/src/test/java/io/floci/aws/services/ses/SesServiceTestBuilder.java @@ -65,6 +65,8 @@ final class SesServiceTestBuilder { private SesCvetService cvetService; private SesSentEmailService sentEmailService; private SesIdentityCertificateService certificateService; + private final SesAccountService accountService = + new SesAccountService(new InMemoryStorage<>(), new InMemoryStorage<>(), new InMemoryStorage<>()); static SesServiceTestBuilder create() { return new SesServiceTestBuilder(); @@ -166,6 +168,10 @@ final class SesServiceTestBuilder { return identityService; } + SesAccountService accountService() { + return accountService; + } + SesIdentityCertificateService certificateService() { if (certificateService == null) { throw new IllegalStateException("call build() first"); @@ -196,6 +202,7 @@ final class SesServiceTestBuilder { cvetService, new SesTenantService(tenantStore, tenantAssociationStore, clock, new SecureRandom()), certificateService, + accountService, smtpRelay); } } diff --git a/src/test/java/io/floci/aws/services/ses/SesV1AccountSendingPausedTest.java b/src/test/java/io/floci/aws/services/ses/SesV1AccountSendingPausedTest.java index 0b8808cf19..f489860292 100644 --- a/src/test/java/io/floci/aws/services/ses/SesV1AccountSendingPausedTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesV1AccountSendingPausedTest.java @@ -1,10 +1,12 @@ package io.floci.aws.services.ses; import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.ValidatableResponse; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import static io.floci.aws.services.ses.SesV1Requests.query; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.containsString; @@ -51,7 +53,8 @@ class SesV1AccountSendingPausedTest { .post("/") .then() .statusCode(400) - .body(containsString("AccountSendingPausedException")); + .body(containsString("AccountSendingPausedException")) + .body(containsString("Sending is paused for this account.")); } @Test @@ -67,7 +70,8 @@ class SesV1AccountSendingPausedTest { .post("/") .then() .statusCode(400) - .body(containsString("AccountSendingPausedException")); + .body(containsString("AccountSendingPausedException")) + .body(containsString("Sending is paused for this account.")); } @Test @@ -84,6 +88,42 @@ class SesV1AccountSendingPausedTest { .post("/") .then() .statusCode(400) - .body(containsString("AccountSendingPausedException")); + .body(containsString("AccountSendingPausedException")) + .body(containsString("Sending is paused for this account.")); + } + + @Test + void sendBulkTemplatedEmail_pausedUnverifiedSender_isRejectedBeforeThePause() { + sendBulkTemplatedEmail("paused-v1-unverified@floci.test") + .statusCode(400) + .body(containsString("MessageRejected")) + .body(containsString("Email address is not verified.")); + } + + @Test + void sendBulkTemplatedEmail_pausedVerifiedSender_returnsAccountSendingPausedException() { + String sender = "paused-v1-verified@floci.test"; + query("VerifyEmailIdentity") + .formParam("EmailAddress", sender) + .when() + .post("/") + .then() + .statusCode(200); + + sendBulkTemplatedEmail(sender) + .statusCode(400) + .body(containsString("AccountSendingPausedException")) + .body(containsString("Sending is paused for this account.")); + } + + private static ValidatableResponse sendBulkTemplatedEmail(String sender) { + return query("SendBulkTemplatedEmail") + .formParam("Source", sender) + .formParam("Template", "any-template") + .formParam("DefaultTemplateData", "{}") + .formParam("Destinations.member.1.Destination.ToAddresses.member.1", "recipient@example.com") + .when() + .post("/") + .then(); } } diff --git a/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java b/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java index 0d14e2bd5d..d200891308 100644 --- a/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java +++ b/src/test/java/io/floci/aws/services/ses/SesV2JsonTest.java @@ -63,6 +63,8 @@ class SesV2JsonTest { "CustomVerificationEmailTemplateAlreadyExists", "m", 400)).getErrorCode()); assertEquals("SendingPausedException", SesV2Json.remapV1Exception(new AwsException( "ConfigurationSetSendingPausedException", "m", 400)).getErrorCode()); + assertEquals("SendingPausedException", SesV2Json.remapV1Exception(new AwsException( + "AccountSendingPausedException", "m", 400)).getErrorCode()); AwsException restatedByCallers = new AwsException( "CustomVerificationEmailTemplateDoesNotExist", "v1 sentence", 400); @@ -72,6 +74,15 @@ class SesV2JsonTest { assertSame(untouched, SesV2Json.remapV1Exception(untouched)); } + @Test + void namesRawContent_readsTheContentMember() { + assertTrue(SesV2Json.namesRawContent(MAPPER, "{\"Content\": {\"Raw\": {\"Data\": \"x\"}}}")); + assertFalse(SesV2Json.namesRawContent(MAPPER, "{\"Content\": {\"Simple\": {}}}")); + assertFalse(SesV2Json.namesRawContent(MAPPER, "")); + assertFalse(SesV2Json.namesRawContent(MAPPER, "{not json")); + assertFalse(SesV2Json.namesRawContent(MAPPER, "[]")); + } + @Test void remapCvetException_restatesAMissingTemplateAndRemapsTheRest() { AwsException missing = SesV2Json.remapCvetException(new AwsException(