mirror of
https://github.com/floci-io/floci.git
synced 2026-12-01 01:12:48 -05:00
feat(ses): implement email template CRUD with stored, inline, and ARN variants (#573)
* feat(ses): implement email template CRUD with stored, inline, and ARN variants
Adds CreateTemplate / GetTemplate / UpdateTemplate /
DeleteTemplate / ListTemplates on both V2 REST JSON
(/v2/email/templates) and V1 Query.
SendEmail / SendTemplatedEmail resolve templates in three forms
with Mustache-style {{var}} substitution against TemplateData:
- TemplateName references a stored template
- TemplateArn extracts the name from the template/<name>
segment of an SES template ARN (region and account segments
are not validated, mirroring how KMS / Secrets Manager
handle ARNs in Floci)
- TemplateContent (V2 only) supplies inline subject / text /
html without storing the template first
V2 Content.Template requires exactly one of TemplateName,
TemplateArn, or TemplateContent; violations return
BadRequestException (400). V1 SendTemplatedEmail accepts
TemplateArn either alone or alongside Template (name) — the
name is used for resolution when both are present, matching
AWS where boto3 and the Java SDK keep Template as a required
field and treat TemplateArn as supplementary for cross-account
addressing.
Templates persist region-scoped via StorageFactory in
ses-templates.json. Template names reject leading/trailing
whitespace (same rule as identities) and are validated through
a single validateTemplateName helper reachable from every
resolution path. listTemplates is sorted by CreatedTimestamp
with TemplateName as a tiebreaker for deterministic output.
Error codes: the service layer throws V1-style codes
(TemplateDoesNotExist / AlreadyExists / InvalidTemplate /
InvalidParameterValue); SesController remaps them to
NotFoundException (404) / AlreadyExistsException (400) /
BadRequestException (400) for V2 callers.
* test(ses): add Python and Java SDK compatibility tests for templates
Covers boto3 (ses / sesv2) and AWS Java SDK v2 (SesClient /
SesV2Client) against the SES email template CRUD, inline
TemplateContent, and TemplateArn paths. Happy path,
duplicate-create rejection, missing-template lookups,
Mustache-style variable substitution, and 3-way selector
conflicts on V2 Content.Template are exercised on both SDK
surfaces.
Adds:
- Python: ses_client / sesv2_client conftest fixtures and a
tests/test_ses_templates.py suite covering V1 + V2
- Java: sesv2 Maven dependency, SesV2Client factory in
TestFixtures, and a SesTemplateTest covering V1 + V2
* docs(ses): document email template CRUD and templated send
Adds the new V1 template actions (CreateTemplate / GetTemplate
/ UpdateTemplate / DeleteTemplate / ListTemplates /
SendTemplatedEmail) and the V2 /v2/email/templates endpoints
to the SES service reference, and extends the README service
matrix to mention template support on the SES / SES v2 rows.
* fix(ses): add template actions to SES_ACTIONS for unsigned request routing
Unsigned clients (curl, aws --no-sign-request) were falling through to
the SQS handler because the six template Query actions were not
registered in SES_ACTIONS.
* fix(ses): narrow exception catch to JsonProcessingException in template endpoints
Broad Exception catch was masking unexpected errors (e.g. NPE) as 400
responses. Narrowed to JsonProcessingException which is the only
checked exception from objectMapper.readTree().
* test(ses): add template variable substitution edge cases and cross-region isolation
Add SesServiceTemplateTest with 14 unit tests for applyTemplateData
covering undefined variables, spaced/hyphenated names, unclosed braces,
non-string JSON values, regex metacharacters, and case sensitivity.
Add cross-region isolation integration test verifying templates created
in one region are not visible in another.
* test(ses): add awscli compatibility tests for template CRUD and templated send
Cover create, get, update, list, delete template and send-templated-email
via the AWS CLI. Uses JSON parameter format to avoid awscli parser issues
with Mustache-style {{variable}} syntax.
---------
Co-authored-by: Kenshin OKINAKA <kenshin_okinaka@realcross.co.jp>
This commit is contained in:
1 parent
e0dc45e012
commit
763ea379d7
18 files changed
+2583
-14
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
|
||||
import io.github.hectorvent.floci.services.ses.model.EmailTemplate;
|
||||
import io.github.hectorvent.floci.services.ses.model.Identity;
|
||||
import io.github.hectorvent.floci.services.ses.model.SentEmail;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
@@ -29,6 +30,7 @@ class SesServiceSmtpTest {
|
||||
new InMemoryStorage<String, Identity>(),
|
||||
emailStore,
|
||||
new InMemoryStorage<String, Boolean>(),
|
||||
new InMemoryStorage<String, EmailTemplate>(),
|
||||
smtpRelay);
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,114 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SesService#applyTemplateData} covering
|
||||
* template variable substitution edge cases.
|
||||
*/
|
||||
class SesServiceTemplateTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
@Test
|
||||
void undefinedVariable_replacedWithEmptyString() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("name", "Alice");
|
||||
String result = SesService.applyTemplateData("Hello {{name}}, team {{team}}", data);
|
||||
assertEquals("Hello Alice, team ", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void spacedVariable_matchesCorrectly() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("name", "Alice");
|
||||
String result = SesService.applyTemplateData("Hello {{ name }}", data);
|
||||
assertEquals("Hello Alice", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void hyphenatedVariableName() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("first-name", "Alice");
|
||||
String result = SesService.applyTemplateData("Hello {{first-name}}", data);
|
||||
assertEquals("Hello Alice", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void unclosedBraces_leftAsIs() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("name", "Alice");
|
||||
String result = SesService.applyTemplateData("Hello {{name}} and {{foo", data);
|
||||
assertEquals("Hello Alice and {{foo", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nonStringJsonValues() throws Exception {
|
||||
ObjectNode data = MAPPER.createObjectNode();
|
||||
data.put("count", 42);
|
||||
data.put("active", true);
|
||||
data.set("nested", MAPPER.readTree("{\"key\":\"val\"}"));
|
||||
|
||||
assertEquals("Items: 42", SesService.applyTemplateData("Items: {{count}}", data));
|
||||
assertEquals("Active: true", SesService.applyTemplateData("Active: {{active}}", data));
|
||||
assertEquals("Data: {\"key\":\"val\"}", SesService.applyTemplateData("Data: {{nested}}", data));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyTemplateData_allVariablesCleared() {
|
||||
JsonNode data = MAPPER.createObjectNode();
|
||||
String result = SesService.applyTemplateData("Hello {{name}}, {{team}}", data);
|
||||
assertEquals("Hello , ", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullTemplateData_allVariablesCleared() {
|
||||
String result = SesService.applyTemplateData("Hello {{name}}", null);
|
||||
assertEquals("Hello ", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullText_returnsNull() {
|
||||
assertNull(SesService.applyTemplateData(null, MAPPER.createObjectNode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyText_returnsEmpty() {
|
||||
assertEquals("", SesService.applyTemplateData("", MAPPER.createObjectNode()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void noVariables_textUnchanged() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("name", "Alice");
|
||||
assertEquals("Hello world", SesService.applyTemplateData("Hello world", data));
|
||||
}
|
||||
|
||||
@Test
|
||||
void duplicateVariables_allReplaced() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("name", "Alice");
|
||||
String result = SesService.applyTemplateData("{{name}} and {{name}}", data);
|
||||
assertEquals("Alice and Alice", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void replacementWithRegexMetacharacters() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("val", "price is $100 (50% off)");
|
||||
String result = SesService.applyTemplateData("The {{val}}", data);
|
||||
assertEquals("The price is $100 (50% off)", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void variableNameCaseSensitive() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("Name", "Alice");
|
||||
String result = SesService.applyTemplateData("Hello {{name}} and {{Name}}", data);
|
||||
assertEquals("Hello and Alice", result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void emptyStringValue() {
|
||||
JsonNode data = MAPPER.createObjectNode().put("name", "");
|
||||
assertEquals("Hello ", SesService.applyTemplateData("Hello {{name}}", data));
|
||||
}
|
||||
}
|
||||
+326
@@ -0,0 +1,326 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
/**
|
||||
* Integration tests for SES V1 Query-protocol template actions.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class SesTemplateV1IntegrationTest {
|
||||
|
||||
private static final String AUTH =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/email/aws4_request";
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", "v1-welcome")
|
||||
.formParam("Template.SubjectPart", "Hello {{name}}")
|
||||
.formParam("Template.TextPart", "Hi {{name}}!")
|
||||
.formParam("Template.HtmlPart", "<p>Hi <b>{{name}}</b>!</p>")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("CreateTemplateResponse"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createTemplate_duplicateRejected() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", "v1-welcome")
|
||||
.formParam("Template.SubjectPart", "dup")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>AlreadyExists</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void getTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "GetTemplate")
|
||||
.formParam("TemplateName", "v1-welcome")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<TemplateName>v1-welcome</TemplateName>"))
|
||||
.body(containsString("<SubjectPart>Hello {{name}}</SubjectPart>"))
|
||||
.body(containsString("<TextPart>Hi {{name}}!</TextPart>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void getTemplate_notFound() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "GetTemplate")
|
||||
.formParam("TemplateName", "missing-template")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>TemplateDoesNotExist</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void updateTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "UpdateTemplate")
|
||||
.formParam("Template.TemplateName", "v1-welcome")
|
||||
.formParam("Template.SubjectPart", "Welcome {{name}}!")
|
||||
.formParam("Template.TextPart", "Hello {{name}}, from {{team}}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("UpdateTemplateResponse"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "GetTemplate")
|
||||
.formParam("TemplateName", "v1-welcome")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<SubjectPart>Welcome {{name}}!</SubjectPart>"))
|
||||
.body(containsString("{{team}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void listTemplates_includesCreated() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "ListTemplates")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<TemplatesMetadata>"))
|
||||
.body(containsString("<Name>v1-welcome</Name>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void sendTemplatedEmail_substitutesVariables() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "VerifyEmailIdentity")
|
||||
.formParam("EmailAddress", "v1-sender@example.com")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String body = given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendTemplatedEmail")
|
||||
.formParam("Source", "v1-sender@example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", "to@example.com")
|
||||
.formParam("Template", "v1-welcome")
|
||||
.formParam("TemplateData", "{\"name\":\"Alice\",\"team\":\"floci\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("SendTemplatedEmailResponse"))
|
||||
.body(containsString("<MessageId>"))
|
||||
.extract().body().asString();
|
||||
|
||||
String messageId = body.replaceAll("(?s).*<MessageId>([^<]+)</MessageId>.*", "$1");
|
||||
|
||||
given()
|
||||
.queryParam("id", messageId)
|
||||
.when()
|
||||
.get("/_aws/ses")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("messages[0].Subject", equalTo("Welcome Alice!"))
|
||||
.body("messages[0].Body.text_part", equalTo("Hello Alice, from floci"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void sendTemplatedEmail_unknownTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendTemplatedEmail")
|
||||
.formParam("Source", "v1-sender@example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", "to@example.com")
|
||||
.formParam("Template", "ghost")
|
||||
.formParam("TemplateData", "{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>TemplateDoesNotExist</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void deleteTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "DeleteTemplate")
|
||||
.formParam("TemplateName", "v1-welcome")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("DeleteTemplateResponse"));
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "GetTemplate")
|
||||
.formParam("TemplateName", "v1-welcome")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>TemplateDoesNotExist</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void deleteTemplate_notFound() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "DeleteTemplate")
|
||||
.formParam("TemplateName", "already-gone")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>TemplateDoesNotExist</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void createTemplate_rejectsLeadingTrailingWhitespace() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", " padded ")
|
||||
.formParam("Template.SubjectPart", "s")
|
||||
.formParam("Template.TextPart", "t")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidTemplate</Code>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void sendTemplatedEmail_withTemplateArn_resolvesStoredTemplate() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "CreateTemplate")
|
||||
.formParam("Template.TemplateName", "v1-arn-welcome")
|
||||
.formParam("Template.SubjectPart", "Hi {{name}}")
|
||||
.formParam("Template.TextPart", "Hello {{name}}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "VerifyEmailIdentity")
|
||||
.formParam("EmailAddress", "v1-arn-sender@example.com")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendTemplatedEmail")
|
||||
.formParam("Source", "v1-arn-sender@example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", "to@example.com")
|
||||
.formParam("TemplateArn", "arn:aws:ses:us-east-1:000000000000:template/v1-arn-welcome")
|
||||
.formParam("TemplateData", "{\"name\":\"Alice\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<MessageId>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void sendTemplatedEmail_withNameAndArn_accepted() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendTemplatedEmail")
|
||||
.formParam("Source", "v1-arn-sender@example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", "to@example.com")
|
||||
.formParam("Template", "v1-arn-welcome")
|
||||
.formParam("TemplateArn", "arn:aws:ses:us-east-1:000000000000:template/v1-arn-welcome")
|
||||
.formParam("TemplateData", "{\"name\":\"Alice\"}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body(containsString("<MessageId>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void sendTemplatedEmail_withMalformedArn_returns400() {
|
||||
given()
|
||||
.contentType("application/x-www-form-urlencoded")
|
||||
.header("Authorization", AUTH)
|
||||
.formParam("Action", "SendTemplatedEmail")
|
||||
.formParam("Source", "v1-arn-sender@example.com")
|
||||
.formParam("Destination.ToAddresses.member.1", "to@example.com")
|
||||
.formParam("TemplateArn", "not-an-arn")
|
||||
.formParam("TemplateData", "{}")
|
||||
.when()
|
||||
.post("/")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body(containsString("<Code>InvalidParameterValue</Code>"));
|
||||
}
|
||||
}
|
||||
+549
@@ -0,0 +1,549 @@
|
||||
package io.github.hectorvent.floci.services.ses;
|
||||
|
||||
import io.quarkus.test.junit.QuarkusTest;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Order;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
import static org.hamcrest.Matchers.hasItem;
|
||||
import static org.hamcrest.Matchers.notNullValue;
|
||||
|
||||
/**
|
||||
* Integration tests for SES V2 email template CRUD and templated send
|
||||
* via the REST JSON protocol at /v2/email/templates.
|
||||
*/
|
||||
@QuarkusTest
|
||||
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
|
||||
class SesTemplateV2IntegrationTest {
|
||||
|
||||
private static final String AUTH_HEADER =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/us-east-1/ses/aws4_request";
|
||||
|
||||
@Test
|
||||
@Order(1)
|
||||
void createTemplate() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"TemplateName": "v2-welcome",
|
||||
"TemplateContent": {
|
||||
"Subject": "Hello {{name}}",
|
||||
"Text": "Hi {{name}}!",
|
||||
"Html": "<p>Hi <b>{{name}}</b>!</p>"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(2)
|
||||
void createTemplate_duplicateRejected() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"TemplateName": "v2-welcome",
|
||||
"TemplateContent": {"Subject": "dup", "Text": "dup"}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("AlreadyExistsException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(3)
|
||||
void createTemplate_missingName() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{"TemplateContent": {"Subject": "x"}}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
void getTemplate() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.get("/v2/email/templates/v2-welcome")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TemplateName", equalTo("v2-welcome"))
|
||||
.body("TemplateContent.Subject", equalTo("Hello {{name}}"))
|
||||
.body("TemplateContent.Text", equalTo("Hi {{name}}!"))
|
||||
.body("TemplateContent.Html", containsString("{{name}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
void getTemplate_notFound() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.get("/v2/email/templates/does-not-exist")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
void updateTemplate() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"TemplateContent": {
|
||||
"Subject": "Welcome {{name}}!",
|
||||
"Text": "Hello {{name}}, from {{team}}",
|
||||
"Html": "<p>Welcome {{name}}</p>"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.put("/v2/email/templates/v2-welcome")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.get("/v2/email/templates/v2-welcome")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TemplateContent.Subject", equalTo("Welcome {{name}}!"))
|
||||
.body("TemplateContent.Text", containsString("{{team}}"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(7)
|
||||
void updateTemplate_notFound() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{"TemplateContent": {"Subject": "x"}}
|
||||
""")
|
||||
.when()
|
||||
.put("/v2/email/templates/ghost")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
void listTemplates_includesCreated() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.get("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TemplatesMetadata", notNullValue())
|
||||
.body("TemplatesMetadata.TemplateName", hasItem("v2-welcome"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
void sendEmail_withTemplate_substitutesVariables() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{"EmailIdentity": "sender@example.com"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/identities")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String messageId = given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateName": "v2-welcome",
|
||||
"TemplateData": "{\\"name\\":\\"Alice\\",\\"team\\":\\"floci\\"}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("MessageId", notNullValue())
|
||||
.extract().path("MessageId");
|
||||
|
||||
given()
|
||||
.queryParam("id", messageId)
|
||||
.when()
|
||||
.get("/_aws/ses")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("messages[0].Subject", equalTo("Welcome Alice!"))
|
||||
.body("messages[0].Body.text_part", equalTo("Hello Alice, from floci"))
|
||||
.body("messages[0].Body.html_part", equalTo("<p>Welcome Alice</p>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
void sendEmail_withUnknownTemplate_returns404() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateName": "ghost",
|
||||
"TemplateData": "{}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(11)
|
||||
void sendEmail_withTemplate_missingName_returns400() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {"Template": {"TemplateData": "{}"}}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(12)
|
||||
void deleteTemplate() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.delete("/v2/email/templates/v2-welcome")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.get("/v2/email/templates/v2-welcome")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(13)
|
||||
void deleteTemplate_notFound() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.delete("/v2/email/templates/already-gone")
|
||||
.then()
|
||||
.statusCode(404)
|
||||
.body("__type", equalTo("NotFoundException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(14)
|
||||
void createTemplate_rejectsLeadingTrailingWhitespace() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"TemplateName": " padded ",
|
||||
"TemplateContent": {"Subject": "s", "Text": "t"}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(15)
|
||||
void sendEmail_withInlineTemplate_substitutesVariables() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{"EmailIdentity": "inline-sender@example.com"}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/identities")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String messageId = given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "inline-sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateContent": {
|
||||
"Subject": "Inline {{name}}",
|
||||
"Text": "Hello inline {{name}} on {{team}}",
|
||||
"Html": "<p>Hello inline <b>{{name}}</b></p>"
|
||||
},
|
||||
"TemplateData": "{\\"name\\":\\"Alice\\",\\"team\\":\\"floci\\"}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("MessageId", notNullValue())
|
||||
.extract().path("MessageId");
|
||||
|
||||
given()
|
||||
.queryParam("id", messageId)
|
||||
.when()
|
||||
.get("/_aws/ses")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("messages[0].Subject", equalTo("Inline Alice"))
|
||||
.body("messages[0].Body.text_part", equalTo("Hello inline Alice on floci"))
|
||||
.body("messages[0].Body.html_part", equalTo("<p>Hello inline <b>Alice</b></p>"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(16)
|
||||
void sendEmail_templateWithBothNameAndContent_returns400() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "inline-sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateName": "anything",
|
||||
"TemplateContent": {"Subject": "s", "Text": "t"},
|
||||
"TemplateData": "{}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(17)
|
||||
void sendEmail_withTemplateArn_resolvesStoredTemplate() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"TemplateName": "arn-welcome",
|
||||
"TemplateContent": {
|
||||
"Subject": "Hi {{name}}",
|
||||
"Text": "Hello {{name}}"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
String messageId = given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "inline-sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateArn": "arn:aws:ses:us-east-1:000000000000:template/arn-welcome",
|
||||
"TemplateData": "{\\"name\\":\\"Alice\\"}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("MessageId", notNullValue())
|
||||
.extract().path("MessageId");
|
||||
|
||||
given()
|
||||
.queryParam("id", messageId)
|
||||
.when()
|
||||
.get("/_aws/ses")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("messages[0].Subject", equalTo("Hi Alice"))
|
||||
.body("messages[0].Body.text_part", equalTo("Hello Alice"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(18)
|
||||
void sendEmail_templateWithMalformedArn_returns400() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "inline-sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateArn": "not-an-arn",
|
||||
"TemplateData": "{}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(19)
|
||||
void sendEmail_templateWithNameAndArn_returns400() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"FromEmailAddress": "inline-sender@example.com",
|
||||
"Destination": {"ToAddresses": ["recipient@example.com"]},
|
||||
"Content": {
|
||||
"Template": {
|
||||
"TemplateName": "arn-welcome",
|
||||
"TemplateArn": "arn:aws:ses:us-east-1:000000000000:template/arn-welcome",
|
||||
"TemplateData": "{}"
|
||||
}
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/outbound-emails")
|
||||
.then()
|
||||
.statusCode(400)
|
||||
.body("__type", equalTo("BadRequestException"));
|
||||
}
|
||||
|
||||
// ──────────────── Cross-region isolation ────────────────
|
||||
|
||||
private static final String AUTH_EU_WEST_1 =
|
||||
"AWS4-HMAC-SHA256 Credential=AKID/20260101/eu-west-1/ses/aws4_request";
|
||||
|
||||
@Test
|
||||
@Order(20)
|
||||
void crossRegionIsolation_templateNotVisibleInOtherRegion() {
|
||||
// Create template in eu-west-1
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_EU_WEST_1)
|
||||
.body("""
|
||||
{
|
||||
"TemplateName": "region-test",
|
||||
"TemplateContent": {
|
||||
"Subject": "Regional",
|
||||
"Text": "Hello from eu-west-1"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
// Verify it exists in eu-west-1
|
||||
given()
|
||||
.header("Authorization", AUTH_EU_WEST_1)
|
||||
.when()
|
||||
.get("/v2/email/templates/region-test")
|
||||
.then()
|
||||
.statusCode(200)
|
||||
.body("TemplateName", equalTo("region-test"));
|
||||
|
||||
// Verify it does NOT exist in us-east-1
|
||||
given()
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.when()
|
||||
.get("/v2/email/templates/region-test")
|
||||
.then()
|
||||
.statusCode(404);
|
||||
|
||||
// Clean up
|
||||
given()
|
||||
.header("Authorization", AUTH_EU_WEST_1)
|
||||
.when()
|
||||
.delete("/v2/email/templates/region-test")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -260,7 +260,6 @@ class SesV2IntegrationTest {
|
||||
@Test
|
||||
@Order(13)
|
||||
void sendEmail_template() {
|
||||
// Re-create an identity for template test
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
@@ -272,6 +271,23 @@ class SesV2IntegrationTest {
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
.body("""
|
||||
{
|
||||
"TemplateName": "MyTemplate",
|
||||
"TemplateContent": {
|
||||
"Subject": "Hello {{name}}",
|
||||
"Text": "Hi {{name}}!"
|
||||
}
|
||||
}
|
||||
""")
|
||||
.when()
|
||||
.post("/v2/email/templates")
|
||||
.then()
|
||||
.statusCode(200);
|
||||
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("Authorization", AUTH_HEADER)
|
||||
|
||||
Reference in new issue
Block a user