mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): require a subject and a part in an inline send template (#9064)
A v2 SendEmail or SendBulkEmail with inline TemplateContent was accepted as soon as one of the subject, text or html was set, and an empty one was answered with a message of Floci's own. SES needs a subject and a text or html part, each with a non-whitespace character, and answers every incomplete shape with "Template is missing required fields." It reports that ahead of a missing or malformed sender, the destinations, the template data and the configuration set and tenant lookups, so the check now runs first on both operations. A stored template is no longer checked again when a send renders it: it was validated when it was stored. A template that names a stored template beside TemplateContent now answers with the message SES uses on each operation.
This commit is contained in:
1 parent
42fad08915
commit
0af048a099
7 files changed
+382
-30
No files matched your search
@@ -107,6 +107,10 @@ public class SesSendController {
|
||||
String fromIdentityArn = request.path("FromEmailAddressIdentityArn").asText(null);
|
||||
String configurationSetName = request.path("ConfigurationSetName").asText(null);
|
||||
String tenantName = request.path("TenantName").asText(null);
|
||||
JsonNode content = request.path("Content");
|
||||
if (!content.has("Raw") && !content.has("Simple") && !namesStoredTemplate(content.path("Template"))) {
|
||||
requireInlineTemplateFields(content.path("Template"));
|
||||
}
|
||||
List<MessageTag> emailTags = parseEmailTagsArray(request.path("EmailTags"), "EmailTags");
|
||||
ListManagementOptions listManagement =
|
||||
parseListManagementOptions(request.path("ListManagementOptions"));
|
||||
@@ -126,7 +130,6 @@ public class SesSendController {
|
||||
.listManagement(listManagement)
|
||||
.tenantName(tenantName)
|
||||
.region(region);
|
||||
JsonNode content = request.path("Content");
|
||||
String messageId;
|
||||
|
||||
if (content.has("Raw")) {
|
||||
@@ -168,6 +171,11 @@ public class SesSendController {
|
||||
boolean hasArn = templateArn != null && !templateArn.isBlank();
|
||||
boolean hasInline = template.has("TemplateContent");
|
||||
int selectorCount = (hasName ? 1 : 0) + (hasArn ? 1 : 0) + (hasInline ? 1 : 0);
|
||||
if (hasInline && selectorCount > 1) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"TemplateName and TemplateArn cannot be provided when TemplateContent is provided.",
|
||||
400);
|
||||
}
|
||||
if (selectorCount > 1) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Content.Template must specify exactly one of TemplateName, TemplateArn, or TemplateContent.",
|
||||
@@ -194,10 +202,6 @@ public class SesSendController {
|
||||
String subject = inline.path("Subject").asText(null);
|
||||
String text = inline.path("Text").asText(null);
|
||||
String html = inline.path("Html").asText(null);
|
||||
// An empty inline template is reported before the tenant lookup on AWS; the
|
||||
// inline content is not a stored template resource, so only the identity and
|
||||
// configuration set pass through the gate.
|
||||
SesService.requireInlineTemplateContent(subject, text, html);
|
||||
requireNoNullAddress(toAddresses, ccAddresses, bccAddresses, replyToAddresses);
|
||||
SendEmailRequest inlineRequest = sendRequest
|
||||
.content(new EmailContent.InlineTemplate(subject, text, html, templateData,
|
||||
@@ -240,6 +244,7 @@ public class SesSendController {
|
||||
if (bulkEntries.isMissingNode() || bulkEntries.isNull()) {
|
||||
throw SmithyValidationErrors.badRequest(SmithyValidationErrors.notNullWithoutValue("bulkEmailEntries"));
|
||||
}
|
||||
requireInlineTemplateFields(request.path("DefaultContent").path("Template"));
|
||||
String fromEmailAddress = request.path("FromEmailAddress").asText(null);
|
||||
if (fromEmailAddress == null || fromEmailAddress.isBlank()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
@@ -264,6 +269,10 @@ public class SesSendController {
|
||||
boolean hasArn = templateArn != null && !templateArn.isBlank();
|
||||
boolean hasInline = template.has("TemplateContent");
|
||||
int selectorCount = (hasName ? 1 : 0) + (hasArn ? 1 : 0) + (hasInline ? 1 : 0);
|
||||
if (hasInline && selectorCount > 1) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"TemplateName or TemplateArn cannot be provided with EmailTemplateContent", 400);
|
||||
}
|
||||
if (selectorCount > 1) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"DefaultContent.Template must specify exactly one of TemplateName, TemplateArn, or TemplateContent.",
|
||||
@@ -284,8 +293,6 @@ public class SesSendController {
|
||||
subject = inline.path("Subject").asText(null);
|
||||
text = inline.path("Text").asText(null);
|
||||
html = inline.path("Html").asText(null);
|
||||
// Shape validation belongs before the send's tenant lookup, as on SendEmail.
|
||||
SesService.requireInlineTemplateContent(subject, text, html);
|
||||
}
|
||||
// AWS counts the entries after the inline content check and before it looks the stored
|
||||
// template up (probe-confirmed).
|
||||
@@ -420,6 +427,25 @@ public class SesSendController {
|
||||
}
|
||||
}
|
||||
|
||||
// AWS reports an incomplete inline template ahead of a missing or malformed sender, the
|
||||
// destinations, the entry count, the template data, the message tags, the list management
|
||||
// options, the feedback address and the configuration set and tenant lookups (probe-confirmed
|
||||
// on SendEmail and SendBulkEmail). A template that also names a stored one differs by
|
||||
// operation: SendBulkEmail still reports the incomplete content first, SendEmail reports the
|
||||
// combination, so SendEmail skips this check for it.
|
||||
private static void requireInlineTemplateFields(JsonNode template) {
|
||||
if (template.has("TemplateContent")) {
|
||||
JsonNode inline = template.path("TemplateContent");
|
||||
SesService.requireInlineTemplateContent(inline.path("Subject").asText(null),
|
||||
inline.path("Text").asText(null), inline.path("Html").asText(null));
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean namesStoredTemplate(JsonNode template) {
|
||||
return !template.path("TemplateName").asText("").isBlank()
|
||||
|| !template.path("TemplateArn").asText("").isBlank();
|
||||
}
|
||||
|
||||
private JsonNode parseTemplateData(JsonNode parent, String fieldName) {
|
||||
return parseTemplateData(parent.path(fieldName).asText(null));
|
||||
}
|
||||
|
||||
@@ -279,11 +279,11 @@ public class SesService {
|
||||
if (request.content() instanceof EmailContent.Raw raw) {
|
||||
return sendRawEmail(request, raw, requireVerifiedSender);
|
||||
}
|
||||
Tenant tenant = requireSendingTenant(request.tenantName(), request.region(), request,
|
||||
request::configurationSetName);
|
||||
if (request.content() instanceof EmailContent.InlineTemplate inline) {
|
||||
requireInlineTemplateContent(inline.subject(), inline.textPart(), inline.htmlPart());
|
||||
}
|
||||
Tenant tenant = requireSendingTenant(request.tenantName(), request.region(), request,
|
||||
request::configurationSetName);
|
||||
// After a missing sender, AWS reports a malformed address ahead of the destination and
|
||||
// configuration-set checks, then an over-long address, then an unverified sender, and only
|
||||
// then a missing stored template (probe-confirmed), so the content is resolved last.
|
||||
@@ -299,7 +299,7 @@ public class SesService {
|
||||
EmailContent.Simple content = switch (request.content()) {
|
||||
case EmailContent.Simple simple -> simple;
|
||||
case EmailContent.Template template ->
|
||||
renderInlineTemplate(templateService.inline(template, request.region()));
|
||||
SesTemplateService.render(templateService.inline(template, request.region()));
|
||||
case EmailContent.InlineTemplate inline -> SesTemplateService.render(inline);
|
||||
case EmailContent.Raw raw -> throw new IllegalStateException("Raw content is sent above");
|
||||
};
|
||||
@@ -377,11 +377,6 @@ public class SesService {
|
||||
return senders;
|
||||
}
|
||||
|
||||
private static EmailContent.Simple renderInlineTemplate(EmailContent.InlineTemplate template) {
|
||||
requireInlineTemplateContent(template.subject(), template.textPart(), template.htmlPart());
|
||||
return SesTemplateService.render(template);
|
||||
}
|
||||
|
||||
private static void requireSource(SendEmailRequest request) {
|
||||
if (request.source() == null || request.source().isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
|
||||
@@ -1163,7 +1158,7 @@ public class SesService {
|
||||
* The first half of the tenant send gate, run where each send starts: resolves the
|
||||
* tenant with the send-flavored not-found wording and refuses a tenant whose sending is DISABLED,
|
||||
* or returns null for a send with no tenant. Placement is probe-confirmed: request-shape
|
||||
* validation (a missing Content, an empty inline template, a missing FromEmailAddress on Simple,
|
||||
* validation (a missing Content, an incomplete inline template, a missing FromEmailAddress on Simple,
|
||||
* the bulk template-content checks) runs before the tenant lookup, while the recipient checks,
|
||||
* the raw MIME-From derivation, and identity verification all lose to the tenant 404. The
|
||||
* DISABLED refusal also beats identity verification, the association check and a raw message's
|
||||
@@ -1663,20 +1658,21 @@ public class SesService {
|
||||
}
|
||||
|
||||
/**
|
||||
* Also called by the controller ahead of the tenant send gate: AWS reports an empty inline
|
||||
* template before it looks the tenant up (probe-confirmed), so the check must not stay behind
|
||||
* the gate for tenant sends.
|
||||
* An inline template needs a subject and a text or html part, each with a non-whitespace
|
||||
* character, and AWS answers every incomplete shape with the one message (probe-confirmed on
|
||||
* v2 SendEmail and SendBulkEmail). It is the first check of a send, and the controller runs
|
||||
* it as well, ahead of its own sender and template data checks.
|
||||
*/
|
||||
static void requireInlineTemplateContent(String subject, String textPart, String htmlPart) {
|
||||
boolean hasSubject = subject != null && !subject.isBlank();
|
||||
boolean hasText = textPart != null && !textPart.isBlank();
|
||||
boolean hasHtml = htmlPart != null && !htmlPart.isBlank();
|
||||
if (!hasSubject && !hasText && !hasHtml) {
|
||||
throw new AwsException("InvalidTemplate",
|
||||
"Template must have at least a subject, text, or html part.", 400);
|
||||
if (isBlank(subject) || (isBlank(textPart) && isBlank(htmlPart))) {
|
||||
throw new AwsException("InvalidTemplate", "Template is missing required fields.", 400);
|
||||
}
|
||||
}
|
||||
|
||||
private static boolean isBlank(String value) {
|
||||
return value == null || value.isBlank();
|
||||
}
|
||||
|
||||
// Probe-confirmed: both bulk sends count the destinations before the stored template lookup,
|
||||
// v2 also before the configuration set and tenant lookups, and v2 answers an empty list with
|
||||
// the same message. The v1 order against the configuration set has not been probed.
|
||||
@@ -1697,12 +1693,12 @@ public class SesService {
|
||||
}
|
||||
|
||||
public List<BulkEmailEntryResult> sendBulkEmail(SendBulkEmailRequest request) {
|
||||
if (request.source() == null || request.source().isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
|
||||
}
|
||||
if (request.defaultContent() instanceof EmailContent.InlineTemplate inline) {
|
||||
requireInlineTemplateContent(inline.subject(), inline.textPart(), inline.htmlPart());
|
||||
}
|
||||
if (request.source() == null || request.source().isBlank()) {
|
||||
throw new AwsException("InvalidParameterValue", "Source email is required.", 400);
|
||||
}
|
||||
requireBulkDestinationCount(sizeOf(request.entries()));
|
||||
// SendBulkEmail refuses a DISABLED tenant before it looks up the configuration set, unlike
|
||||
// SendEmail, and a request in which no entry names a recipient skips the gate altogether,
|
||||
|
||||
@@ -640,7 +640,7 @@ class SesBulkV2IntegrationTest {
|
||||
{"FromEmailAddress": "bulk@example.com",
|
||||
"DefaultContent": {"Template": {"TemplateContent": {}}},
|
||||
"BulkEmailEntries": [%s]}
|
||||
""".formatted(entries(51)), "Template must have at least a subject, text, or html part."));
|
||||
""".formatted(entries(51)), "Template is missing required fields."));
|
||||
}
|
||||
|
||||
// An entry with no recipient beside one with a recipient fails on its own (probed 2026-10-25).
|
||||
|
||||
+202
@@ -0,0 +1,202 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* A v2 SendEmail or SendBulkEmail with inline TemplateContent needs a subject and a text or html
|
||||
* part. AWS answers every incomplete shape with the same message, ahead of the sender, the
|
||||
* destinations, the template data, the tags, the list management options, the feedback address and
|
||||
* the configuration set and tenant lookups (probe-confirmed).
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesInlineTemplateRequiredFieldsIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
|
||||
private static final String UNVERIFIED = "sender@unverified-inline-template.floci.test";
|
||||
private static final String TO = "success@simulator.amazonses.com";
|
||||
private static final String DESTINATION = "\"Destination\": {\"ToAddresses\": [\"" + TO + "\"]}";
|
||||
private static final String ENTRY = "{" + DESTINATION + "}";
|
||||
private static final String SUBJECT_ONLY = "{\"Subject\": \"s\"}";
|
||||
private static final String MESSAGE = "Template is missing required fields.";
|
||||
|
||||
private static ValidatableResponse post(String path, String body) {
|
||||
return given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH)
|
||||
.body(body)
|
||||
.when()
|
||||
.post(path)
|
||||
.then();
|
||||
}
|
||||
|
||||
private static String template(String templateContent) {
|
||||
return "{\"Template\": {\"TemplateContent\": " + templateContent + ", \"TemplateData\": \"{}\"}}";
|
||||
}
|
||||
|
||||
private static void assertMissingRequiredFields(ValidatableResponse response) {
|
||||
response
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(MESSAGE));
|
||||
}
|
||||
|
||||
static Stream<Arguments> incompleteContents() {
|
||||
return Stream.of(
|
||||
Arguments.of("empty", "{}"),
|
||||
Arguments.of("subject only", SUBJECT_ONLY),
|
||||
Arguments.of("text only", "{\"Text\": \"t\"}"),
|
||||
Arguments.of("html only", "{\"Html\": \"<p>h</p>\"}"),
|
||||
Arguments.of("blank subject beside a part", "{\"Subject\": \" \", \"Text\": \"t\"}"),
|
||||
Arguments.of("subject beside blank parts", "{\"Subject\": \"s\", \"Text\": \" \", \"Html\": \"\"}"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("incompleteContents")
|
||||
void sendEmail_incompleteInlineTemplate_isMissingRequiredFields(String label, String templateContent) {
|
||||
assertMissingRequiredFields(post("/v2/email/outbound-emails", "{\"FromEmailAddress\": \"" + UNVERIFIED
|
||||
+ "\", " + DESTINATION + ", \"Content\": " + template(templateContent) + "}"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("incompleteContents")
|
||||
void sendBulkEmail_incompleteInlineTemplate_isMissingRequiredFields(String label, String templateContent) {
|
||||
assertMissingRequiredFields(post("/v2/email/outbound-bulk-emails", "{\"FromEmailAddress\": \"" + UNVERIFIED
|
||||
+ "\", \"DefaultContent\": " + template(templateContent) + ", \"BulkEmailEntries\": [" + ENTRY
|
||||
+ "]}"));
|
||||
}
|
||||
|
||||
// The check sees the content before the sender is verified: a complete inline template from
|
||||
// the same unverified sender gets as far as the verification.
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("completeContents")
|
||||
void completeInlineTemplate_reachesSenderVerification(String label, String templateContent) {
|
||||
post("/v2/email/outbound-emails", "{\"FromEmailAddress\": \"" + UNVERIFIED + "\", " + DESTINATION
|
||||
+ ", \"Content\": " + template(templateContent) + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
post("/v2/email/outbound-bulk-emails", "{\"FromEmailAddress\": \"" + UNVERIFIED
|
||||
+ "\", \"DefaultContent\": " + template(templateContent) + ", \"BulkEmailEntries\": [" + ENTRY + "]}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
}
|
||||
|
||||
static Stream<Arguments> completeContents() {
|
||||
return Stream.of(
|
||||
Arguments.of("subject and text", "{\"Subject\": \"s\", \"Text\": \"t\"}"),
|
||||
Arguments.of("subject, empty text and html", "{\"Subject\": \"s\", \"Text\": \"\", \"Html\": \"<p>h</p>\"}"));
|
||||
}
|
||||
|
||||
static Stream<Arguments> sendEmailsWithAnotherError() {
|
||||
String content = "\"Content\": " + template(SUBJECT_ONLY);
|
||||
String from = "\"FromEmailAddress\": \"" + UNVERIFIED + "\"";
|
||||
return Stream.of(
|
||||
Arguments.of("no sender", "{" + DESTINATION + ", " + content + "}"),
|
||||
Arguments.of("malformed sender",
|
||||
"{\"FromEmailAddress\": \"not an address\", " + DESTINATION + ", " + content + "}"),
|
||||
Arguments.of("no destination", "{" + from + ", " + content + "}"),
|
||||
Arguments.of("malformed recipient",
|
||||
"{" + from + ", \"Destination\": {\"ToAddresses\": [\"not an address\"]}, " + content + "}"),
|
||||
Arguments.of("unknown configuration set", "{" + from + ", " + DESTINATION
|
||||
+ ", \"ConfigurationSetName\": \"inline-template-ghost\", " + content + "}"),
|
||||
Arguments.of("unknown tenant", "{" + from + ", " + DESTINATION
|
||||
+ ", \"TenantName\": \"inline-template-ghost\", " + content + "}"),
|
||||
Arguments.of("invalid tag name", "{" + from + ", " + DESTINATION
|
||||
+ ", \"EmailTags\": [{\"Name\": \"bad name!\", \"Value\": \"v\"}], " + content + "}"),
|
||||
Arguments.of("duplicate tag name", "{" + from + ", " + DESTINATION
|
||||
+ ", \"EmailTags\": [{\"Name\": \"dup\", \"Value\": \"a\"}, "
|
||||
+ "{\"Name\": \"dup\", \"Value\": \"b\"}], " + content + "}"),
|
||||
Arguments.of("unknown contact list", "{" + from + ", " + DESTINATION
|
||||
+ ", \"ListManagementOptions\": {\"ContactListName\": \"inline-template-ghost\"}, "
|
||||
+ content + "}"),
|
||||
Arguments.of("malformed feedback address", "{" + from + ", " + DESTINATION
|
||||
+ ", \"FeedbackForwardingEmailAddress\": \"not an address\", " + content + "}"),
|
||||
Arguments.of("template data that is not JSON", "{" + from + ", " + DESTINATION
|
||||
+ ", \"Content\": {\"Template\": {\"TemplateContent\": " + SUBJECT_ONLY
|
||||
+ ", \"TemplateData\": \"not json\"}}}"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("sendEmailsWithAnotherError")
|
||||
void sendEmail_incompleteInlineTemplate_winsOverLaterChecks(String label, String body) {
|
||||
assertMissingRequiredFields(post("/v2/email/outbound-emails", body));
|
||||
}
|
||||
|
||||
static Stream<Arguments> bulkSendsWithAnotherError() {
|
||||
String content = "\"DefaultContent\": " + template(SUBJECT_ONLY);
|
||||
String from = "\"FromEmailAddress\": \"" + UNVERIFIED + "\"";
|
||||
String entries = "\"BulkEmailEntries\": [" + ENTRY + "]";
|
||||
return Stream.of(
|
||||
Arguments.of("no sender", "{" + content + ", " + entries + "}"),
|
||||
Arguments.of("malformed sender",
|
||||
"{\"FromEmailAddress\": \"not an address\", " + content + ", " + entries + "}"),
|
||||
Arguments.of("no entries", "{" + from + ", " + content + ", \"BulkEmailEntries\": []}"),
|
||||
Arguments.of("malformed entry recipient", "{" + from + ", " + content
|
||||
+ ", \"BulkEmailEntries\": [{\"Destination\": {\"ToAddresses\": [\"not an address\"]}}]}"),
|
||||
Arguments.of("unknown configuration set", "{" + from + ", " + content
|
||||
+ ", \"ConfigurationSetName\": \"inline-template-ghost\", " + entries + "}"),
|
||||
Arguments.of("unknown tenant", "{" + from + ", " + content
|
||||
+ ", \"TenantName\": \"inline-template-ghost\", " + entries + "}"),
|
||||
Arguments.of("malformed feedback address", "{" + from + ", " + content
|
||||
+ ", \"FeedbackForwardingEmailAddress\": \"not an address\", " + entries + "}"),
|
||||
Arguments.of("stored template named beside the content", "{" + from
|
||||
+ ", \"DefaultContent\": {\"Template\": {\"TemplateName\": \"inline-template-ghost\", "
|
||||
+ "\"TemplateContent\": " + SUBJECT_ONLY + ", \"TemplateData\": \"{}\"}}, " + entries + "}"),
|
||||
Arguments.of("template data that is not JSON", "{" + from
|
||||
+ ", \"DefaultContent\": {\"Template\": {\"TemplateContent\": " + SUBJECT_ONLY
|
||||
+ ", \"TemplateData\": \"not json\"}}, " + entries + "}"));
|
||||
}
|
||||
|
||||
static Stream<Arguments> storedTemplateSelectors() {
|
||||
return Stream.of(
|
||||
Arguments.of("name", "\"TemplateName\": \"inline-template-ghost\""),
|
||||
Arguments.of("arn", "\"TemplateArn\": "
|
||||
+ "\"arn:aws:ses:us-east-1:000000000000:template/inline-template-ghost\""));
|
||||
}
|
||||
|
||||
private static String templateWithSelector(String selector, String templateContent) {
|
||||
return "{\"Template\": {" + selector + ", \"TemplateContent\": " + templateContent
|
||||
+ ", \"TemplateData\": \"{}\"}}";
|
||||
}
|
||||
|
||||
// AWS words the combination differently on the two operations, and SendEmail reports it even
|
||||
// when the inline content is incomplete (probe-confirmed).
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("storedTemplateSelectors")
|
||||
void sendEmail_storedTemplateBesideContent_isRejectedWhateverTheContent(String label, String selector) {
|
||||
for (String templateContent : new String[] {"{\"Subject\": \"s\", \"Text\": \"t\"}", "{}", SUBJECT_ONLY}) {
|
||||
post("/v2/email/outbound-emails", "{\"FromEmailAddress\": \"" + UNVERIFIED + "\", " + DESTINATION
|
||||
+ ", \"Content\": " + templateWithSelector(selector, templateContent) + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(
|
||||
"TemplateName and TemplateArn cannot be provided when TemplateContent is provided."));
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("storedTemplateSelectors")
|
||||
void sendBulkEmail_storedTemplateBesideCompleteContent_isRejected(String label, String selector) {
|
||||
post("/v2/email/outbound-bulk-emails", "{\"FromEmailAddress\": \"" + UNVERIFIED
|
||||
+ "\", \"DefaultContent\": " + templateWithSelector(selector, "{\"Subject\": \"s\", \"Text\": \"t\"}")
|
||||
+ ", \"BulkEmailEntries\": [" + ENTRY + "]}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo("TemplateName or TemplateArn cannot be provided with EmailTemplateContent"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("bulkSendsWithAnotherError")
|
||||
void sendBulkEmail_incompleteInlineTemplate_winsOverLaterChecks(String label, String body) {
|
||||
assertMissingRequiredFields(post("/v2/email/outbound-bulk-emails", body));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
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.EmailTemplate;
|
||||
import io.floci.aws.services.ses.model.SendBulkEmailRequest;
|
||||
import io.floci.aws.services.ses.model.SendEmailRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
|
||||
/**
|
||||
* The shapes of an inline template a send accepts: a subject and a text or html part, each with a
|
||||
* non-whitespace character (probe-confirmed on v2 SendEmail and SendBulkEmail).
|
||||
*/
|
||||
class SesServiceInlineTemplateTest {
|
||||
|
||||
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 SUBJECT_ONLY =
|
||||
new EmailContent.InlineTemplate("s", null, null, null, List.of());
|
||||
|
||||
static Stream<Arguments> incompleteTemplates() {
|
||||
return Stream.of(
|
||||
Arguments.of("nothing", null, null, null),
|
||||
Arguments.of("all empty", "", "", ""),
|
||||
Arguments.of("subject only", "s", null, null),
|
||||
Arguments.of("text only", null, "t", null),
|
||||
Arguments.of("html only", null, null, "<p>h</p>"),
|
||||
Arguments.of("text and html", null, "t", "<p>h</p>"),
|
||||
Arguments.of("empty subject", "", "t", null),
|
||||
Arguments.of("space subject", " ", "t", null),
|
||||
Arguments.of("ideographic space subject", " ", null, "<p>h</p>"),
|
||||
Arguments.of("empty text", "s", "", null),
|
||||
Arguments.of("space text", "s", " ", null),
|
||||
Arguments.of("newline html", "s", null, "\n"),
|
||||
Arguments.of("space text and empty html", "s", " ", ""));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("incompleteTemplates")
|
||||
void incompleteTemplate_isMissingRequiredFields(String label, String subject, String text, String html) {
|
||||
AwsException e = assertThrows(AwsException.class,
|
||||
() -> SesService.requireInlineTemplateContent(subject, text, html));
|
||||
|
||||
assertEquals("InvalidTemplate", e.getErrorCode());
|
||||
assertEquals("Template is missing required fields.", e.getMessage());
|
||||
assertEquals(400, e.getHttpStatus());
|
||||
}
|
||||
|
||||
static Stream<Arguments> completeTemplates() {
|
||||
return Stream.of(
|
||||
Arguments.of("subject and text", "s", "t", null),
|
||||
Arguments.of("subject and html", "s", null, "<p>h</p>"),
|
||||
Arguments.of("subject and both parts", "s", "t", "<p>h</p>"),
|
||||
Arguments.of("empty text beside html", "s", "", "<p>h</p>"),
|
||||
Arguments.of("blank html beside text", "s", "t", " "));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("completeTemplates")
|
||||
void completeTemplate_isAccepted(String label, String subject, String text, String html) {
|
||||
assertDoesNotThrow(() -> SesService.requireInlineTemplateContent(subject, text, html));
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_incompleteInlineTemplate_isReportedBeforeAnUnknownTenant() {
|
||||
SesService service = SesServiceTestBuilder.create().build();
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.sendEmail(SendEmailRequest.builder()
|
||||
.source(SENDER)
|
||||
.toAddresses(List.of(TO))
|
||||
.tenantName("ghost-tenant")
|
||||
.region(REGION)
|
||||
.content(SUBJECT_ONLY)
|
||||
.build()));
|
||||
|
||||
assertEquals("Template is missing required fields.", e.getMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendBulkEmail_incompleteInlineTemplate_isReportedBeforeAMissingSource() {
|
||||
SesService service = SesServiceTestBuilder.create().build();
|
||||
|
||||
AwsException e = assertThrows(AwsException.class, () -> service.sendBulkEmail(SendBulkEmailRequest.builder()
|
||||
.defaultContent(SUBJECT_ONLY)
|
||||
.entries(List.of(new BulkEmailEntry(List.of(TO), null, null, null, null, null)))
|
||||
.region(REGION)
|
||||
.build()));
|
||||
|
||||
assertEquals("Template is missing required fields.", e.getMessage());
|
||||
}
|
||||
|
||||
// The rule belongs to inline content: a stored template is checked when it is stored, and a
|
||||
// send does not hold one that predates a stricter rule to the inline message.
|
||||
@Test
|
||||
void sendEmail_storedTemplateWithoutAPart_isNotHeldToTheInlineRule() {
|
||||
SesServiceTestBuilder builder = SesServiceTestBuilder.create();
|
||||
SesService service = builder.build();
|
||||
builder.identityService().verifyEmailIdentity(SENDER, REGION);
|
||||
builder.templateStore().put("template::" + REGION + "::subject-only",
|
||||
new EmailTemplate("subject-only", "s", null, null));
|
||||
|
||||
String messageId = service.sendEmail(SendEmailRequest.builder()
|
||||
.source(SENDER)
|
||||
.toAddresses(List.of(TO))
|
||||
.region(REGION)
|
||||
.content(new EmailContent.Template("subject-only", null, List.of()))
|
||||
.build());
|
||||
|
||||
assertNotNull(messageId);
|
||||
}
|
||||
}
|
||||
@@ -96,6 +96,10 @@ final class SesServiceTestBuilder {
|
||||
return identityStore;
|
||||
}
|
||||
|
||||
InMemoryStorage<String, EmailTemplate> templateStore() {
|
||||
return templateStore;
|
||||
}
|
||||
|
||||
InMemoryStorage<String, SentEmail> emailStore() {
|
||||
return emailStore;
|
||||
}
|
||||
|
||||
@@ -252,7 +252,7 @@ class SesTenantSendV2IntegrationTest {
|
||||
+ "\"DefaultContent\":{\"Template\":{\"TemplateName\":\"" + TEMPLATE + "\","
|
||||
+ "\"TemplateData\":\"{}\"}},\"BulkEmailEntries\":[]}")
|
||||
.when().post("/v2/email/outbound-bulk-emails").then().statusCode(400);
|
||||
// An all-blank inline template is a shape error too — 400 before the ghost tenant's 404.
|
||||
// An incomplete inline template is a shape error too: 400 before the ghost tenant's 404.
|
||||
v2().body("{\"FromEmailAddress\":\"" + FROM + "\",\"TenantName\":\"ghost-tenant\","
|
||||
+ "\"DefaultContent\":{\"Template\":{\"TemplateContent\":{},"
|
||||
+ "\"TemplateData\":\"{}\"}},"
|
||||
|
||||
Reference in new issue
Block a user