mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
fix(ses): check the template selectors of a v2 send as SES does (#9083)
* fix(ses): check the template selectors of a v2 send as SES does
This commit is contained in:
1 parent
306bcf8c5b
commit
b9b33f32f6
4 files changed
+399
-97
No files matched your search
@@ -109,8 +109,10 @@ public class SesSendController {
|
||||
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"));
|
||||
JsonNode template = content.path("Template");
|
||||
TemplateSelector selector = TemplateSelector.of(template);
|
||||
if (!content.has("Raw") && !content.has("Simple")) {
|
||||
requireSendEmailTemplateContent(selector, template);
|
||||
}
|
||||
List<MessageTag> emailTags = parseEmailTagsArray(request.path("EmailTags"), "EmailTags");
|
||||
ListManagementOptions listManagement =
|
||||
@@ -162,40 +164,26 @@ public class SesSendController {
|
||||
.build();
|
||||
messageId = sesService.sendEmail(simpleRequest);
|
||||
} else if (content.has("Template")) {
|
||||
if (fromEmailAddress == null || fromEmailAddress.isBlank()) {
|
||||
if (fromEmailAddress == null || fromEmailAddress.isEmpty()) {
|
||||
throw new AwsException("BadRequestException", "Source cannot be empty", 400);
|
||||
}
|
||||
JsonNode template = content.path("Template");
|
||||
String templateName = template.path("TemplateName").asText(null);
|
||||
String templateArn = template.path("TemplateArn").asText(null);
|
||||
boolean hasName = templateName != null && !templateName.isBlank();
|
||||
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) {
|
||||
if (fromEmailAddress.isBlank()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"TemplateName and TemplateArn cannot be provided when TemplateContent is provided.",
|
||||
400);
|
||||
"Invalid email address " + fromEmailAddress + ".", 400);
|
||||
}
|
||||
if (selectorCount > 1) {
|
||||
if (selector.isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Content.Template must specify exactly one of TemplateName, TemplateArn, or TemplateContent.",
|
||||
400);
|
||||
}
|
||||
if (selectorCount == 0) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"Content.Template requires TemplateName, TemplateArn, or TemplateContent.", 400);
|
||||
"TemplateName and TemplateArn cannot be both empty", 400);
|
||||
}
|
||||
selector.requireNameAlignsWithArn(region);
|
||||
JsonNode templateData = parseTemplateData(template, "TemplateData");
|
||||
List<MessageHeader> additionalHeaders =
|
||||
parseHeadersArray(template.path("Headers"), "content.template.headers");
|
||||
if (hasName || hasArn) {
|
||||
String resolvedName = hasName
|
||||
? templateName
|
||||
: SesTemplateService.templateNameFromArn(templateArn);
|
||||
if (selector.namesStoredTemplate()) {
|
||||
requireNoNullAddress(toAddresses, ccAddresses, bccAddresses, replyToAddresses);
|
||||
SendEmailRequest templateRequest = sendRequest
|
||||
.content(new EmailContent.Template(resolvedName, templateData, additionalHeaders))
|
||||
.content(new EmailContent.Template(selector.storedName(region), templateData,
|
||||
additionalHeaders))
|
||||
.build();
|
||||
messageId = sesService.sendEmail(templateRequest);
|
||||
} else {
|
||||
@@ -248,8 +236,7 @@ public class SesSendController {
|
||||
requireInlineTemplateFields(request.path("DefaultContent").path("Template"));
|
||||
String fromEmailAddress = request.path("FromEmailAddress").asText(null);
|
||||
if (fromEmailAddress == null || fromEmailAddress.isBlank()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"FromEmailAddress is required.", 400);
|
||||
throw new AwsException("BadRequestException", "Email address not specified.", 400);
|
||||
}
|
||||
SesAddressSyntax.requireBulkAddress(fromEmailAddress);
|
||||
List<String> replyToAddresses = addressList(request, "ReplyToAddresses");
|
||||
@@ -264,44 +251,33 @@ public class SesSendController {
|
||||
throw new AwsException("BadRequestException",
|
||||
"DefaultContent.Template is required.", 400);
|
||||
}
|
||||
String templateName = template.path("TemplateName").asText(null);
|
||||
String templateArn = template.path("TemplateArn").asText(null);
|
||||
boolean hasName = templateName != null && !templateName.isBlank();
|
||||
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.",
|
||||
400);
|
||||
}
|
||||
if (selectorCount == 0) {
|
||||
TemplateSelector selector = TemplateSelector.of(template);
|
||||
if (selector.isEmpty()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"DefaultContent.Template requires TemplateName, TemplateArn, or TemplateContent.", 400);
|
||||
}
|
||||
// AWS counts the entries after the inline content check and the sender checks, and
|
||||
// before it rejects a stored template named beside the content or looks one up
|
||||
// (probe-confirmed).
|
||||
SesService.requireBulkDestinationCount(bulkEntries.size());
|
||||
if (selector.inline() && selector.namesStoredTemplate()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"TemplateName or TemplateArn cannot be provided with EmailTemplateContent", 400);
|
||||
}
|
||||
selector.requireNameAlignsWithArn(region);
|
||||
|
||||
String subject = null;
|
||||
String text = null;
|
||||
String html = null;
|
||||
// The send looks the stored template up only after its sender checks (probe-confirmed).
|
||||
String storedTemplateName = null;
|
||||
if (hasInline) {
|
||||
if (selector.inline()) {
|
||||
JsonNode inline = template.path("TemplateContent");
|
||||
subject = inline.path("Subject").asText(null);
|
||||
text = inline.path("Text").asText(null);
|
||||
html = inline.path("Html").asText(null);
|
||||
}
|
||||
// AWS counts the entries after the inline content check and before it looks the stored
|
||||
// template up (probe-confirmed).
|
||||
SesService.requireBulkDestinationCount(bulkEntries.size());
|
||||
if (!hasInline) {
|
||||
storedTemplateName = hasName
|
||||
? templateName
|
||||
: SesTemplateService.templateNameFromArn(templateArn);
|
||||
} else {
|
||||
storedTemplateName = selector.storedName(region);
|
||||
}
|
||||
|
||||
JsonNode defaultTemplateData = parseTemplateData(template, "TemplateData");
|
||||
@@ -425,20 +401,68 @@ 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.
|
||||
// on SendEmail and SendBulkEmail). SendBulkEmail reports it even when the template also names
|
||||
// a stored one.
|
||||
private static void requireInlineTemplateFields(JsonNode template) {
|
||||
if (template.has("TemplateContent")) {
|
||||
if (template.hasNonNull("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();
|
||||
// SendEmail rejects a stored template named beside TemplateContent ahead of the sender, the
|
||||
// destinations, the template data, the message tags, the list management options, the feedback
|
||||
// address and the configuration set and tenant lookups, whatever the content holds
|
||||
// (probe-confirmed).
|
||||
private static void requireSendEmailTemplateContent(TemplateSelector selector, JsonNode template) {
|
||||
if (selector.inline() && selector.namesStoredTemplate()) {
|
||||
throw new AwsException("BadRequestException",
|
||||
"TemplateName and TemplateArn cannot be provided when TemplateContent is provided.", 400);
|
||||
}
|
||||
requireInlineTemplateFields(template);
|
||||
}
|
||||
|
||||
/**
|
||||
* Which of TemplateName, TemplateArn and TemplateContent a send's template carries. A member
|
||||
* sent as an explicit null counts as absent (probe-confirmed).
|
||||
*/
|
||||
private record TemplateSelector(String name, String arn, boolean inline) {
|
||||
|
||||
static TemplateSelector of(JsonNode template) {
|
||||
return new TemplateSelector(template.path("TemplateName").asText(null),
|
||||
template.path("TemplateArn").asText(null), template.hasNonNull("TemplateContent"));
|
||||
}
|
||||
|
||||
boolean hasName() {
|
||||
return name != null && !name.isBlank();
|
||||
}
|
||||
|
||||
boolean hasArn() {
|
||||
return arn != null && !arn.isBlank();
|
||||
}
|
||||
|
||||
boolean namesStoredTemplate() {
|
||||
return hasName() || hasArn();
|
||||
}
|
||||
|
||||
boolean isEmpty() {
|
||||
return !inline && !namesStoredTemplate();
|
||||
}
|
||||
|
||||
String storedName(String region) {
|
||||
return hasName() ? name : SesTemplateService.sendTemplateNameFromArn(arn, region);
|
||||
}
|
||||
|
||||
// AWS accepts a TemplateName beside a TemplateArn when both name the same template, and
|
||||
// reports a malformed ARN first (probe-confirmed on SendEmail and SendBulkEmail).
|
||||
void requireNameAlignsWithArn(String region) {
|
||||
if (hasName() && hasArn()
|
||||
&& !name.equals(SesTemplateService.sendTemplateNameFromArn(arn, region))) {
|
||||
throw new AwsException("BadRequestException", "The SES resource '" + name
|
||||
+ "' does not align with the given ARN '" + arn + "'.", 400);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private JsonNode parseTemplateData(JsonNode parent, String fieldName) {
|
||||
|
||||
@@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.floci.aws.core.common.AwsArnUtils;
|
||||
import io.floci.aws.core.common.AwsException;
|
||||
import io.floci.aws.core.common.AwsRegions;
|
||||
import io.floci.aws.core.common.PaginatedResult;
|
||||
import io.floci.aws.core.storage.StorageBackend;
|
||||
import io.floci.aws.core.storage.StorageFactory;
|
||||
@@ -258,6 +259,46 @@ public class SesTemplateService implements SesTaggable {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* The template a v2 send's {@code TemplateArn} names. SES checks the ARN's partition, service,
|
||||
* region and resource type, each with a message of its own, and not its account
|
||||
* (probe-confirmed). The order among several mismatches at once is not probed.
|
||||
*/
|
||||
public static String sendTemplateNameFromArn(String arn, String region) {
|
||||
if (!AwsArnUtils.looksLikeArn(arn)) {
|
||||
throw new AwsException("InvalidParameterValue", "ARNs must start with 'arn:': " + arn, 400);
|
||||
}
|
||||
AwsArnUtils.Arn parsed;
|
||||
try {
|
||||
parsed = AwsArnUtils.parse(arn);
|
||||
} catch (IllegalArgumentException e) {
|
||||
throw new AwsException("InvalidParameterValue",
|
||||
"TemplateArn is not a valid SES template ARN: " + arn, 400);
|
||||
}
|
||||
String partition = AwsRegions.partitionFor(region);
|
||||
if (!partition.equals(parsed.partition())) {
|
||||
throw invalidTemplateArn(arn, "Partition should be <" + partition + ">");
|
||||
}
|
||||
if (!"ses".equals(parsed.service())) {
|
||||
throw invalidTemplateArn(arn, "Vendor should be <ses>");
|
||||
}
|
||||
if (!region.equals(parsed.region())) {
|
||||
throw invalidTemplateArn(arn, "Template must be in region <" + region + ">");
|
||||
}
|
||||
if (!parsed.resource().startsWith("template/")) {
|
||||
throw invalidTemplateArn(arn, "Relative ID should start with <template/>");
|
||||
}
|
||||
String name = parsed.resource().substring("template/".length());
|
||||
if (name.isEmpty()) {
|
||||
throw new AwsException("InvalidParameterValue", "The template name must be specified.", 400);
|
||||
}
|
||||
return name;
|
||||
}
|
||||
|
||||
private static AwsException invalidTemplateArn(String arn, String reason) {
|
||||
return new AwsException("InvalidParameterValue", "Invalid template ARN <" + arn + ">. " + reason, 400);
|
||||
}
|
||||
|
||||
/** The ARN-dispatched tag operations, sharing the store behind {@code CreateEmailTemplate.Tags}. */
|
||||
@Override
|
||||
public List<Tag> listTags(String name, String region) {
|
||||
|
||||
-38
@@ -156,44 +156,6 @@ class SesInlineTemplateRequiredFieldsIntegrationTest {
|
||||
+ ", \"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) {
|
||||
|
||||
@@ -0,0 +1,275 @@
|
||||
package io.floci.aws.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import io.restassured.response.ValidatableResponse;
|
||||
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.Collections;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* How a v2 SendEmail or SendBulkEmail answers a template that names its source more than once:
|
||||
* a stored template beside TemplateContent, or a TemplateName beside a TemplateArn. The messages
|
||||
* and their place among the other checks are probe-confirmed and differ by operation.
|
||||
*/
|
||||
@QuarkusTest
|
||||
class SesTemplateSelectorIntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
|
||||
private static final String SEND = "/v2/email/outbound-emails";
|
||||
private static final String BULK = "/v2/email/outbound-bulk-emails";
|
||||
private static final String UNVERIFIED = "sender@unverified-template-selector.floci.test";
|
||||
private static final String FROM = "\"FromEmailAddress\": \"" + UNVERIFIED + "\"";
|
||||
private static final String DESTINATION =
|
||||
"\"Destination\": {\"ToAddresses\": [\"success@simulator.amazonses.com\"]}";
|
||||
private static final String ENTRY = "{" + DESTINATION + "}";
|
||||
private static final String ENTRIES = "\"BulkEmailEntries\": [" + ENTRY + "]";
|
||||
private static final String BAD_DESTINATION = "\"Destination\": {\"ToAddresses\": [\"not an address\"]}";
|
||||
private static final String NAME = "\"TemplateName\": \"selector-ghost\"";
|
||||
private static final String ARN =
|
||||
"\"TemplateArn\": \"arn:aws:ses:us-east-1:000000000000:template/selector-ghost\"";
|
||||
private static final String OTHER_ARN_VALUE = "arn:aws:ses:us-east-1:000000000000:template/selector-other";
|
||||
private static final String CONTENT = "\"TemplateContent\": {\"Subject\": \"s\", \"Text\": \"t\"}";
|
||||
private static final String DATA = "\"TemplateData\": \"{}\"";
|
||||
private static final String NAME_BESIDE_CONTENT = template(NAME, CONTENT, DATA);
|
||||
private static final String SEND_COMBINATION =
|
||||
"TemplateName and TemplateArn cannot be provided when TemplateContent is provided.";
|
||||
private static final String BULK_COMBINATION =
|
||||
"TemplateName or TemplateArn cannot be provided with EmailTemplateContent";
|
||||
private static final String BULK_COUNT =
|
||||
"You must specify at least 1 destination, and no more than 50 destinations";
|
||||
|
||||
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... members) {
|
||||
return "{\"Template\": {" + String.join(", ", members) + "}}";
|
||||
}
|
||||
|
||||
private static void assertBadRequest(ValidatableResponse response, String message) {
|
||||
response
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"))
|
||||
.body("message", equalTo(message));
|
||||
}
|
||||
|
||||
static Stream<Arguments> sendEmailsWithAnotherError() {
|
||||
String content = "\"Content\": " + NAME_BESIDE_CONTENT;
|
||||
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 + ", " + BAD_DESTINATION + ", " + content + "}"),
|
||||
Arguments.of("unknown configuration set", "{" + FROM + ", " + DESTINATION
|
||||
+ ", \"ConfigurationSetName\": \"selector-ghost\", " + content + "}"),
|
||||
Arguments.of("unknown tenant", "{" + FROM + ", " + DESTINATION
|
||||
+ ", \"TenantName\": \"selector-ghost\", " + content + "}"),
|
||||
Arguments.of("template data that is not JSON", "{" + FROM + ", " + DESTINATION
|
||||
+ ", \"Content\": " + template(NAME, CONTENT, "\"TemplateData\": \"not json\"") + "}"),
|
||||
Arguments.of("invalid tag name", "{" + FROM + ", " + DESTINATION
|
||||
+ ", \"EmailTags\": [{\"Name\": \"bad name!\", \"Value\": \"v\"}], " + content + "}"),
|
||||
Arguments.of("unknown contact list", "{" + FROM + ", " + DESTINATION
|
||||
+ ", \"ListManagementOptions\": {\"ContactListName\": \"selector-ghost\"}, " + content + "}"),
|
||||
Arguments.of("malformed feedback address", "{" + FROM + ", " + DESTINATION
|
||||
+ ", \"FeedbackForwardingEmailAddress\": \"not an address\", " + content + "}"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("sendEmailsWithAnotherError")
|
||||
void sendEmail_storedTemplateBesideContent_winsOverEveryOtherCheck(String label, String body) {
|
||||
assertBadRequest(post(SEND, body), SEND_COMBINATION);
|
||||
}
|
||||
|
||||
static Stream<Arguments> storedTemplateSelectors() {
|
||||
return Stream.of(Arguments.of("name", NAME), Arguments.of("arn", ARN));
|
||||
}
|
||||
|
||||
// SendEmail reports the combination even when the inline content is incomplete.
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("storedTemplateSelectors")
|
||||
void sendEmail_storedTemplateBesideContent_isRejectedWhateverTheContent(String label, String selector) {
|
||||
for (String content : new String[] {CONTENT, "\"TemplateContent\": {}",
|
||||
"\"TemplateContent\": {\"Subject\": \"s\"}"}) {
|
||||
assertBadRequest(post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": "
|
||||
+ template(selector, content, DATA) + "}"), SEND_COMBINATION);
|
||||
}
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("storedTemplateSelectors")
|
||||
void sendBulkEmail_storedTemplateBesideCompleteContent_isRejected(String label, String selector) {
|
||||
assertBadRequest(post(BULK, "{" + FROM + ", \"DefaultContent\": " + template(selector, CONTENT, DATA)
|
||||
+ ", " + ENTRIES + "}"), BULK_COMBINATION);
|
||||
}
|
||||
|
||||
static Stream<Arguments> bulkSendsWithAnEarlierError() {
|
||||
String content = "\"DefaultContent\": " + NAME_BESIDE_CONTENT;
|
||||
String tooMany = "\"BulkEmailEntries\": [" + String.join(", ", Collections.nCopies(51, ENTRY)) + "]";
|
||||
return Stream.of(
|
||||
Arguments.of("no sender", "{" + content + ", " + ENTRIES + "}", "Email address not specified."),
|
||||
Arguments.of("malformed sender",
|
||||
"{\"FromEmailAddress\": \"not an address\", " + content + ", " + ENTRIES + "}",
|
||||
"Invalid email address<not an address>."),
|
||||
Arguments.of("malformed feedback address", "{" + FROM + ", " + content
|
||||
+ ", \"FeedbackForwardingEmailAddress\": \"not an address\", " + ENTRIES + "}",
|
||||
"Invalid email address<not an address>."),
|
||||
Arguments.of("no entries", "{" + FROM + ", " + content + ", \"BulkEmailEntries\": []}", BULK_COUNT),
|
||||
Arguments.of("51 entries", "{" + FROM + ", " + content + ", " + tooMany + "}", BULK_COUNT));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("bulkSendsWithAnEarlierError")
|
||||
void sendBulkEmail_storedTemplateBesideContent_losesToSenderAndCountChecks(String label, String body,
|
||||
String message) {
|
||||
assertBadRequest(post(BULK, body), message);
|
||||
}
|
||||
|
||||
static Stream<Arguments> bulkSendsWithALaterError() {
|
||||
String content = "\"DefaultContent\": " + NAME_BESIDE_CONTENT;
|
||||
return Stream.of(
|
||||
Arguments.of("malformed entry recipient", "{" + FROM + ", " + content
|
||||
+ ", \"BulkEmailEntries\": [{" + BAD_DESTINATION + "}]}"),
|
||||
Arguments.of("unknown configuration set", "{" + FROM + ", " + content
|
||||
+ ", \"ConfigurationSetName\": \"selector-ghost\", " + ENTRIES + "}"),
|
||||
Arguments.of("unknown tenant", "{" + FROM + ", " + content
|
||||
+ ", \"TenantName\": \"selector-ghost\", " + ENTRIES + "}"),
|
||||
Arguments.of("template data that is not JSON", "{" + FROM + ", \"DefaultContent\": "
|
||||
+ template(NAME, CONTENT, "\"TemplateData\": \"not json\"") + ", " + ENTRIES + "}"));
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("bulkSendsWithALaterError")
|
||||
void sendBulkEmail_storedTemplateBesideContent_winsOverLaterChecks(String label, String body) {
|
||||
assertBadRequest(post(BULK, body), BULK_COMBINATION);
|
||||
}
|
||||
|
||||
// The two selectors may name the same template: the send then gets as far as verifying the
|
||||
// sender, which is unverified here.
|
||||
@Test
|
||||
void nameBesideArnOfTheSameTemplate_isAccepted() {
|
||||
String template = template(NAME, ARN, DATA);
|
||||
post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": " + template + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
post(BULK, "{" + FROM + ", \"DefaultContent\": " + template + ", " + ENTRIES + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nameBesideArnOfAnotherTemplate_doesNotAlign() {
|
||||
String template = template(NAME, "\"TemplateArn\": \"" + OTHER_ARN_VALUE + "\"", DATA);
|
||||
String message = "The SES resource 'selector-ghost' does not align with the given ARN '"
|
||||
+ OTHER_ARN_VALUE + "'.";
|
||||
assertBadRequest(post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": " + template + "}"), message);
|
||||
assertBadRequest(post(BULK, "{" + FROM + ", \"DefaultContent\": " + template + ", " + ENTRIES + "}"),
|
||||
message);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nameAndArnBesideContent_isTheCombinationError() {
|
||||
String template = template(NAME, ARN, CONTENT, DATA);
|
||||
assertBadRequest(post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": " + template + "}"),
|
||||
SEND_COMBINATION);
|
||||
assertBadRequest(post(BULK, "{" + FROM + ", \"DefaultContent\": " + template + ", " + ENTRIES + "}"),
|
||||
BULK_COMBINATION);
|
||||
}
|
||||
|
||||
private static String arn(String partition, String service, String region, String resource) {
|
||||
return "arn:" + partition + ":" + service + ":" + region + ":000000000000:" + resource;
|
||||
}
|
||||
|
||||
static Stream<Arguments> invalidTemplateArns() {
|
||||
String otherRegion = arn("aws", "ses", "eu-west-1", "template/selector-ghost");
|
||||
String otherPartition = arn("aws-cn", "ses", "us-east-1", "template/selector-ghost");
|
||||
String otherService = arn("aws", "s3", "us-east-1", "template/selector-ghost");
|
||||
String otherResource = arn("aws", "ses", "us-east-1", "configuration-set/selector-ghost");
|
||||
return Stream.of(
|
||||
Arguments.of("other region", otherRegion,
|
||||
"Invalid template ARN <" + otherRegion + ">. Template must be in region <us-east-1>"),
|
||||
Arguments.of("other partition", otherPartition,
|
||||
"Invalid template ARN <" + otherPartition + ">. Partition should be <aws>"),
|
||||
Arguments.of("other service", otherService,
|
||||
"Invalid template ARN <" + otherService + ">. Vendor should be <ses>"),
|
||||
Arguments.of("other resource type", otherResource,
|
||||
"Invalid template ARN <" + otherResource + ">. Relative ID should start with <template/>"),
|
||||
Arguments.of("not an ARN", "not-an-arn", "ARNs must start with 'arn:': not-an-arn"),
|
||||
Arguments.of("empty template name", arn("aws", "ses", "us-east-1", "template/"),
|
||||
"The template name must be specified."));
|
||||
}
|
||||
|
||||
// The ARN is checked on its own terms before it is compared with the name, so the answer is
|
||||
// the same with and without a TemplateName.
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("invalidTemplateArns")
|
||||
void invalidTemplateArn_isRejectedWithOrWithoutAName(String label, String arn, String message) {
|
||||
String arnMember = "\"TemplateArn\": \"" + arn + "\"";
|
||||
assertBadRequest(post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": "
|
||||
+ template(NAME, arnMember, DATA) + "}"), message);
|
||||
assertBadRequest(post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": "
|
||||
+ template(arnMember, DATA) + "}"), message);
|
||||
assertBadRequest(post(BULK, "{" + FROM + ", \"DefaultContent\": " + template(NAME, arnMember, DATA)
|
||||
+ ", " + ENTRIES + "}"), message);
|
||||
}
|
||||
|
||||
// SES does not check the account of a template ARN.
|
||||
@Test
|
||||
void templateArnOfAnotherAccount_isAccepted() {
|
||||
String template = template(NAME,
|
||||
"\"TemplateArn\": \"arn:aws:ses:us-east-1:999999999999:template/selector-ghost\"", DATA);
|
||||
post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": " + template + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullTemplateContent_countsAsAbsent() {
|
||||
String nullContent = "\"TemplateContent\": null";
|
||||
post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": " + template(NAME, nullContent, DATA) + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
post(BULK, "{" + FROM + ", \"DefaultContent\": " + template(NAME, nullContent, DATA) + ", " + ENTRIES + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": "
|
||||
+ template("\"TemplateName\": null", CONTENT, DATA) + "}")
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("MessageRejected"));
|
||||
assertBadRequest(post(SEND, "{" + FROM + ", " + DESTINATION + ", \"Content\": "
|
||||
+ template(nullContent, DATA) + "}"), "TemplateName and TemplateArn cannot be both empty");
|
||||
}
|
||||
|
||||
@Test
|
||||
void sendEmail_whitespaceOnlySender_isAnInvalidAddress() {
|
||||
assertBadRequest(post(SEND, "{\"FromEmailAddress\": \" \", " + DESTINATION + ", \"Content\": "
|
||||
+ template(CONTENT, DATA) + "}"), "Invalid email address .");
|
||||
assertBadRequest(post(SEND, "{\"FromEmailAddress\": \"\", " + DESTINATION + ", \"Content\": "
|
||||
+ template(CONTENT, DATA) + "}"), "Source cannot be empty");
|
||||
}
|
||||
|
||||
@ParameterizedTest(name = "{0}")
|
||||
@MethodSource("blankSenders")
|
||||
void sendBulkEmail_blankSender_isNotSpecified(String label, String sender) {
|
||||
assertBadRequest(post(BULK, "{\"FromEmailAddress\": \"" + sender + "\", \"DefaultContent\": "
|
||||
+ template(CONTENT, DATA) + ", " + ENTRIES + "}"), "Email address not specified.");
|
||||
}
|
||||
|
||||
static Stream<Arguments> blankSenders() {
|
||||
return Stream.of(Arguments.of("empty", ""), Arguments.of("space", " "));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user