diff --git a/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesBatchGetMetricDataTest.java b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesBatchGetMetricDataTest.java new file mode 100644 index 0000000000..1f89c9057d --- /dev/null +++ b/compatibility-tests/sdk-test-java/src/test/java/com/floci/test/SesBatchGetMetricDataTest.java @@ -0,0 +1,248 @@ +package com.floci.test; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.DisplayName; +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 software.amazon.awssdk.services.sesv2.SesV2Client; +import software.amazon.awssdk.services.sesv2.model.BatchGetMetricDataQuery; +import software.amazon.awssdk.services.sesv2.model.BatchGetMetricDataRequest; +import software.amazon.awssdk.services.sesv2.model.BatchGetMetricDataResponse; +import software.amazon.awssdk.services.sesv2.model.Body; +import software.amazon.awssdk.services.sesv2.model.Content; +import software.amazon.awssdk.services.sesv2.model.CreateEmailIdentityRequest; +import software.amazon.awssdk.services.sesv2.model.DeleteEmailIdentityRequest; +import software.amazon.awssdk.services.sesv2.model.Destination; +import software.amazon.awssdk.services.sesv2.model.EmailContent; +import software.amazon.awssdk.services.sesv2.model.GetAccountRequest; +import software.amazon.awssdk.services.sesv2.model.GetAccountResponse; +import software.amazon.awssdk.services.sesv2.model.Message; +import software.amazon.awssdk.services.sesv2.model.Metric; +import software.amazon.awssdk.services.sesv2.model.MetricDataResult; +import software.amazon.awssdk.services.sesv2.model.MetricDimensionName; +import software.amazon.awssdk.services.sesv2.model.MetricNamespace; +import software.amazon.awssdk.services.sesv2.model.NotFoundException; +import software.amazon.awssdk.services.sesv2.model.PutAccountVdmAttributesRequest; +import software.amazon.awssdk.services.sesv2.model.SendEmailRequest; +import software.amazon.awssdk.services.sesv2.model.SesV2Exception; +import software.amazon.awssdk.services.sesv2.model.VdmAttributes; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +/** + * SDK compatibility test for SES v2 BatchGetMetricData against a live Floci instance: the Virtual + * Deliverability Manager gate, the daily counts a send produces, the shape of an empty series, and + * the server-side duplicate-identifier check the SDK cannot catch on the client. + * + *

Every query carries this test's own sender as the EMAIL_IDENTITY dimension, so sends made by + * other tests in the same region never reach these counts. + */ +@DisplayName("SES v2 BatchGetMetricData") +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class SesBatchGetMetricDataTest { + + private static final String SENDER = "compat-metrics@example.com"; + + private static SesV2Client sesV2; + private static VdmAttributes originalVdm; + + @BeforeAll + static void setup() { + sesV2 = TestFixtures.sesV2Client(); + GetAccountResponse account = sesV2.getAccount(GetAccountRequest.builder().build()); + originalVdm = account.vdmAttributes(); + quietly(() -> sesV2.deleteEmailIdentity( + DeleteEmailIdentityRequest.builder().emailIdentity(SENDER).build())); + setVdmEnabled(false); + sesV2.createEmailIdentity(CreateEmailIdentityRequest.builder().emailIdentity(SENDER).build()); + } + + @AfterAll + static void cleanup() { + if (sesV2 == null) { + return; + } + quietly(() -> sesV2.deleteEmailIdentity( + DeleteEmailIdentityRequest.builder().emailIdentity(SENDER).build())); + quietly(SesBatchGetMetricDataTest::restoreVdm); + sesV2.close(); + } + + @Test + @Order(1) + @DisplayName("reports the VDM gate before it validates the request") + void vdmDisabledIsReportedFirst() { + assertThatThrownBy(() -> sesV2.batchGetMetricData(request(query("gate", Metric.SEND)))) + .isInstanceOf(NotFoundException.class) + .hasMessageContaining("Virtual Deliverability Manager"); + } + + @Test + @Order(2) + @DisplayName("counts each outcome once per recipient, in a daily bucket") + void countsEachOutcome() { + setVdmEnabled(true); + send("user@example.com"); + send("bounce@simulator.amazonses.com"); + send("complaint@simulator.amazonses.com"); + + BatchGetMetricDataResponse response = sesV2.batchGetMetricData(request( + query("send", Metric.SEND), + query("delivery", Metric.DELIVERY), + query("bounce", Metric.PERMANENT_BOUNCE), + query("complaint", Metric.COMPLAINT), + query("delivered", Metric.DELIVERY_COMPLAINT))); + + assertThat(response.errors()).isEmpty(); + assertThat(total(response, "send")).isEqualTo(3L); + assertThat(total(response, "delivery")).isEqualTo(2L); + assertThat(total(response, "bounce")).isEqualTo(1L); + assertThat(total(response, "complaint")).isEqualTo(1L); + assertThat(total(response, "delivered")).isEqualTo(1L); + // The SDK deserializes the buckets as instants, which is the shape the wire has to carry. + assertThat(result(response, "send").timestamps()).isNotEmpty() + .allSatisfy(timestamp -> assertThat(timestamp).isNotNull()); + } + + @Test + @Order(3) + @DisplayName("a metric with no data keeps its members rather than being omitted") + void emptySeriesKeepsItsMembers() { + BatchGetMetricDataResponse response = sesV2.batchGetMetricData(request( + query("open", Metric.OPEN), query("transient", Metric.TRANSIENT_BOUNCE))); + + assertThat(response.results()).hasSize(2); + assertThat(result(response, "open").timestamps()).isEmpty(); + assertThat(result(response, "open").values()).isEmpty(); + assertThat(result(response, "transient").values()).isEmpty(); + assertThat(response.errors()).isEmpty(); + } + + @Test + @Order(4) + @DisplayName("TENANT_NAME is accepted although this suite's SDK version lacks the constant") + void tenantDimensionIsAccepted() { + // The string-keyed builder sends the wire value directly, which is the point here: the + // typed enum would prove the SDK knows the constant, not that Floci accepts the key. + BatchGetMetricDataResponse response = sesV2.batchGetMetricData( + BatchGetMetricDataRequest.builder() + .queries(BatchGetMetricDataQuery.builder() + .id("tenant") + .namespace(MetricNamespace.VDM) + .metric(Metric.SEND) + .dimensionsWithStrings(Map.of("TENANT_NAME", "no-such-tenant")) + .startDate(Instant.now().minus(Duration.ofDays(1))) + .endDate(Instant.now().plus(Duration.ofDays(1))) + .build()) + .build()); + + assertThat(response.errors()).isEmpty(); + assertThat(result(response, "tenant").values()).isEmpty(); + } + + @Test + @Order(5) + @DisplayName("a duplicate query identifier is rejected by the service") + void duplicateIdentifierIsRejected() { + assertThatThrownBy(() -> sesV2.batchGetMetricData( + request(query("same", Metric.SEND), query("same", Metric.DELIVERY)))) + .isInstanceOf(SesV2Exception.class) + .hasMessageContaining("All queries must have a unique identifier."); + } + + @Test + @Order(6) + @DisplayName("an interval longer than sixty days is rejected") + void intervalCapIsEnforced() { + BatchGetMetricDataQuery wide = BatchGetMetricDataQuery.builder() + .id("wide") + .namespace(MetricNamespace.VDM) + .metric(Metric.SEND) + .startDate(Instant.now().minus(Duration.ofDays(61))) + .endDate(Instant.now()) + .build(); + + assertThatThrownBy(() -> sesV2.batchGetMetricData( + BatchGetMetricDataRequest.builder().queries(wide).build())) + .isInstanceOf(SesV2Exception.class) + .hasMessageContaining("can request at most 60 days"); + } + + private static BatchGetMetricDataRequest request(BatchGetMetricDataQuery... queries) { + return BatchGetMetricDataRequest.builder().queries(queries).build(); + } + + private static BatchGetMetricDataQuery query(String id, Metric metric) { + return BatchGetMetricDataQuery.builder() + .id(id) + .namespace(MetricNamespace.VDM) + .metric(metric) + .dimensions(Map.of(MetricDimensionName.EMAIL_IDENTITY, SENDER)) + .startDate(Instant.now().minus(Duration.ofDays(1))) + .endDate(Instant.now().plus(Duration.ofDays(1))) + .build(); + } + + private static MetricDataResult result(BatchGetMetricDataResponse response, String id) { + return response.results().stream() + .filter(candidate -> id.equals(candidate.id())) + .findFirst() + .orElseThrow(() -> new AssertionError("no result carried the id " + id)); + } + + private static long total(BatchGetMetricDataResponse response, String id) { + List values = result(response, id).values(); + return values.stream().mapToLong(Long::longValue).sum(); + } + + private static void send(String destination) { + sesV2.sendEmail(SendEmailRequest.builder() + .fromEmailAddress(SENDER) + .destination(Destination.builder().toAddresses(destination).build()) + .content(EmailContent.builder() + .simple(Message.builder() + .subject(Content.builder().data("compat-metrics").build()) + .body(Body.builder() + .text(Content.builder().data("hi").build()) + .build()) + .build()) + .build()) + .build()); + } + + private static void setVdmEnabled(boolean enabled) { + sesV2.putAccountVdmAttributes(PutAccountVdmAttributesRequest.builder() + .vdmAttributes(VdmAttributes.builder() + .vdmEnabled(enabled ? "ENABLED" : "DISABLED") + .build()) + .build()); + } + + private static void restoreVdm() { + if (originalVdm == null || originalVdm.vdmEnabled() == null) { + setVdmEnabled(false); + return; + } + sesV2.putAccountVdmAttributes(PutAccountVdmAttributesRequest.builder() + .vdmAttributes(originalVdm) + .build()); + } + + private static void quietly(Runnable action) { + try { + action.run(); + } catch (RuntimeException expected) { + // Best effort teardown: the identity or the VDM state may already be gone. + } + } +} diff --git a/docs/services/ses.md b/docs/services/ses.md index 14545fdce3..807f99f83d 100644 --- a/docs/services/ses.md +++ b/docs/services/ses.md @@ -378,6 +378,7 @@ Alongside the classic Query API, Floci implements a subset of the SES v2 REST JS | `GET` | `/v2/email/import-jobs/{JobId}` | `GetImportJob` | | `POST` | `/v2/email/import-jobs/list` | `ListImportJobs` | | `GET` | `/v2/email/insights/{MessageId}` | `GetMessageInsights` | +| `POST` | `/v2/email/metrics/batch` | `BatchGetMetricData` | Floci models no leased dedicated IPs: `GetDedicatedIps` is empty and IP-targeted operations return `NotFoundException`, as real AWS does for an account with no leased IPs, with required request members validated first (`BadRequestException`). `PutDedicatedIpPoolScalingAttributes` rejects downgrading a `MANAGED` pool to `STANDARD`, and `PutAccountDedicatedIpWarmupAttributes` stores the flag behind `GetAccount.DedicatedIpAutoWarmupEnabled` (default `true`). @@ -419,6 +420,8 @@ Suppressed recipients are filtered out of the SMTP relay step (non-suppressed re `GetMessageInsights` derives its per-recipient timeline at send time from the same classification as event publishing, adding `DELIVERY` for an ordinary recipient, and spells the suppression subtype `ON_ACCOUNT_SUPPRESSION_LIST` where the published events say `OnAccountSuppressionList` (bounce probed 2026-09-21, complaint by analogy). It is gated on Virtual Deliverability Manager as on AWS: 404 `NotFoundException`, checked before the message id. Deviations from AWS: Floci records the mailbox-simulator and multi-recipient sends that VDM drops (otherwise insights would stay empty here), serves a message immediately, returns only the caller's `EmailTags` without the injected `ses:*` entries, applies no 30-day retention cutoff, and derives a timeline whether or not VDM was enabled at send time. `Isp` is always `UNKNOWN_ISP`, AWS's own value for a provider it cannot identify. A message the content scan rejected reports `SEND` then `REJECT` and carries nothing read off it: no subject, and only the tags the request itself supplied. +`BatchGetMetricData` reads those same stored timelines, so it and `GetMessageInsights` cannot disagree, and it shares the VDM gate. Counts are per recipient, in whole UTC days, and sparse: a day with no data is absent, not zero. `OPEN`, `CLICK` and their delivery variants stay empty because they need a real mailbox, and `TRANSIENT_BOUNCE` because every bounce Floci derives is permanent. Dimensions filter the send: `EMAIL_IDENTITY` on the address or its domain, `CONFIGURATION_SET` on the set it resolved to, `TENANT_NAME` on the tenant it named, and `ISP` only ever on `UNKNOWN_ISP`. Each value is validated with AWS's own rule and message: the configuration set, the tenant and the ISP take ASCII letters, digits, `_` and `-`, and an identity is a bare name or `local@domain`. Where AWS's validation order could not be observed, Floci ranks the member constraints below a duplicate `Id` and the interval cap below the start-before-end check, an absent date fails with a null-constraint message, and a query with two invalid dimension values reports the first in `EMAIL_IDENTITY`, `ISP`, `CONFIGURATION_SET`, `TENANT_NAME` order. + Import jobs read their source from Floci's own S3 emulation, so the object has to be uploaded there rather than to real S3; the CSV and newline-delimited JSON record formats are AWS's own ([suppression list](https://docs.aws.amazon.com/ses/latest/dg/sending-email-suppression-list.html#sending-email-suppression-list-manual-add-bulk), [contact list](https://docs.aws.amazon.com/ses/latest/dg/sending-email-list-management.html#configuring-list-management-bulk-import)). A job still running when the emulator restarts is marked `FAILED` on the next start. Not emulated: `FailureInfo.FailedRecordsS3Url` (Floci writes no failed-record file), the per-file record limits, the same-region bucket rule, and `ListImportJobs` pagination, which returns every job in one page. `SendEmail` honors `ListManagementOptions` (`ContactListName`, optional `TopicName`). When present, each recipient is matched against the named contact list and suppressed as a `Bounce` when opted out — reusing the same relay-exclusion and Bounce-event path as suppression-list filtering, matching AWS ("SES will issue a bounce event for a message that is sent to an unsubscribed contact"). A contact is opted out when `UnsubscribeAll` is set; with a `TopicName`, an explicit `OPT_OUT` preference for that topic (or, absent an explicit preference, the topic's `DefaultSubscriptionStatus` being `OPT_OUT`) suppresses; without a `TopicName`, only `UnsubscribeAll` contacts are suppressed. A recipient that is not yet a contact is created on the list automatically (as on AWS) and then evaluated. Referencing a contact list that does not exist fails the send with `NotFoundException`. The topic-default fallback at send time is an intentional deviation — it is not documented by AWS and mirrors the effective-status model AWS uses for `ListContacts`. diff --git a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java index ef7bb3d806..41d06f1a2c 100644 --- a/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java +++ b/src/main/java/io/github/hectorvent/floci/core/common/ResolvedServiceCatalog.java @@ -41,6 +41,7 @@ import io.github.hectorvent.floci.services.ses.SesDedicatedIpController; import io.github.hectorvent.floci.services.ses.SesIdentityController; import io.github.hectorvent.floci.services.ses.SesImportJobController; import io.github.hectorvent.floci.services.ses.SesInsightsController; +import io.github.hectorvent.floci.services.ses.SesMetricsController; import io.github.hectorvent.floci.services.ses.SesSendController; import io.github.hectorvent.floci.services.ses.SesSuppressionController; import io.github.hectorvent.floci.services.ses.SesTagController; @@ -306,7 +307,8 @@ public class ResolvedServiceCatalog { Set.of(SesAccountController.class, SesConfigurationSetController.class, SesContactController.class, SesCvetController.class, SesDedicatedIpController.class, SesIdentityController.class, - SesInsightsController.class, SesSendController.class, + SesInsightsController.class, SesMetricsController.class, + SesSendController.class, SesSuppressionController.class, SesTagController.class, SesTemplateController.class, SesImportJobController.class, SesTenantController.class)), diff --git a/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java b/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java index b5a9dab41a..ffd1c4de8f 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java +++ b/src/main/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcher.java @@ -79,6 +79,7 @@ public final class CognitoMessageDispatcher { List.of(), // emailTags List.of(), // additionalHeaders null, // listManagement + null, // tenantName DEFAULT_REGION ); } else if ("SMS".equalsIgnoreCase(medium) && phone != null) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java index a4c50d32d1..220d08e575 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesAccountService.java @@ -85,6 +85,15 @@ public class SesAccountService { LOG.infov("Updated account VDM attributes for region {0}: enabled={1}", region, vdm.vdmEnabled()); } + /** The probe-confirmed gate the VDM-only operations check before reading the request. */ + public void requireVdmEnabled(String region) { + Optional vdm = findAccountVdmAttributes(region); + if (vdm.isEmpty() || !vdm.get().vdmEnabled()) { + throw new AwsException("NotFoundException", + "To use this feature you must enable Virtual Deliverability Manager", 404); + } + } + private static String accountVdmKey(String region) { return "account-vdm::" + region; } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java index 66f1026f2e..7b0000962a 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesInsightsController.java @@ -5,7 +5,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; -import io.github.hectorvent.floci.services.ses.model.AccountVdmAttributes; import io.github.hectorvent.floci.services.ses.model.EmailInsights; import io.github.hectorvent.floci.services.ses.model.InsightsBounce; import io.github.hectorvent.floci.services.ses.model.InsightsComplaint; @@ -25,7 +24,6 @@ import jakarta.ws.rs.core.MediaType; import jakarta.ws.rs.core.Response; import java.util.List; -import java.util.Optional; /** * SES V2 message insights ({@code /v2/email/insights/{MessageId}/}), the read side of the @@ -41,9 +39,6 @@ import java.util.Optional; @Consumes(MediaType.APPLICATION_JSON) public class SesInsightsController { - private static final String VDM_DISABLED_MESSAGE = - "To use this feature you must enable Virtual Deliverability Manager"; - private final SesSentEmailService sentEmailService; private final SesAccountService accountService; private final RegionResolver regionResolver; @@ -63,7 +58,7 @@ public class SesInsightsController { public Response getMessageInsights(@Context HttpHeaders headers, @PathParam("messageId") String messageId) { String region = regionResolver.resolveRegion(headers); - requireVdmEnabled(region); + accountService.requireVdmEnabled(region); SentEmail email = sentEmailService.find(region, messageId) .orElseThrow(() -> new AwsException("NotFoundException", "Message <" + messageId + "> not found for account <" @@ -71,13 +66,6 @@ public class SesInsightsController { return Response.ok(render(email)).build(); } - private void requireVdmEnabled(String region) { - Optional vdm = accountService.findAccountVdmAttributes(region); - if (vdm.isEmpty() || !vdm.get().vdmEnabled()) { - throw new AwsException("NotFoundException", VDM_DISABLED_MESSAGE, 404); - } - } - private ObjectNode render(SentEmail email) { ObjectNode result = objectMapper.createObjectNode(); result.put("MessageId", email.getMessageId()); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricData.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricData.java new file mode 100644 index 0000000000..a48c4dd804 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricData.java @@ -0,0 +1,190 @@ +package io.github.hectorvent.floci.services.ses; + +import io.github.hectorvent.floci.services.ses.model.EmailInsights; +import io.github.hectorvent.floci.services.ses.model.InsightsBounce; +import io.github.hectorvent.floci.services.ses.model.InsightsEvent; +import io.github.hectorvent.floci.services.ses.model.InsightsEventDetails; +import io.github.hectorvent.floci.services.ses.model.SentEmail; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; + +/** + * Aggregates the stored per-recipient timelines into the daily counts {@code BatchGetMetricData} + * serves. The timelines are the ones {@link SesMessageInsights} derives at send time, so the two + * operations cannot disagree about what a send produced. + * + *

Probe-confirmed against real SES (2026-09-23, us-east-1): buckets are whole UTC days, the + * series is sparse rather than zero filled (a day with no data is simply absent), and the end of + * the requested range is exclusive, so a range ending at midnight omits that day. + * + *

Floci reports only what a send without a real recipient can produce. OPEN, CLICK, + * DELIVERY_OPEN and DELIVERY_CLICK need a mailbox to act, so they always come back empty; the + * probed account returned empty series for those metrics too. Every bounce Floci derives is + * PERMANENT, so TRANSIENT_BOUNCE is likewise always empty. + */ +final class SesMetricData { + + static final Set METRICS = Set.of( + "SEND", "COMPLAINT", "PERMANENT_BOUNCE", "TRANSIENT_BOUNCE", "OPEN", "CLICK", + "DELIVERY", "DELIVERY_OPEN", "DELIVERY_CLICK", "DELIVERY_COMPLAINT"); + + static final Set DIMENSION_NAMES = + Set.of("EMAIL_IDENTITY", "CONFIGURATION_SET", "ISP", "TENANT_NAME"); + + private static final String EMAIL_IDENTITY = "EMAIL_IDENTITY"; + private static final String CONFIGURATION_SET = "CONFIGURATION_SET"; + private static final String ISP = "ISP"; + private static final String TENANT_NAME = "TENANT_NAME"; + + private SesMetricData() {} + + /** + * One query from the batch, already validated by the controller. + */ + record Query(String id, String metric, Map dimensions, + Instant startDate, Instant endDate) { + + Query { + dimensions = dimensions == null ? Map.of() : Map.copyOf(dimensions); + } + } + + /** + * The counts for one query, in ascending bucket order. Both lists have the same length, and + * both are empty when nothing matched. + */ + record Series(List timestamps, List values) { + + static final Series EMPTY = new Series(List.of(), List.of()); + } + + static Series aggregate(List emails, Query query) { + Map buckets = new TreeMap<>(); + for (SentEmail email : emails) { + if (!matchesMessageDimensions(email, query.dimensions())) { + continue; + } + for (EmailInsights recipient : orEmpty(email.getInsights())) { + if (!matchesIsp(recipient, query.dimensions())) { + continue; + } + countRecipient(recipient, query, buckets); + } + } + if (buckets.isEmpty()) { + return Series.EMPTY; + } + List timestamps = new ArrayList<>(buckets.size()); + List values = new ArrayList<>(buckets.size()); + for (Map.Entry bucket : buckets.entrySet()) { + timestamps.add(bucket.getKey()); + values.add(bucket.getValue()); + } + return new Series(timestamps, values); + } + + private static void countRecipient(EmailInsights recipient, Query query, + Map buckets) { + if ("DELIVERY_COMPLAINT".equals(query.metric())) { + countDeliveredComplaint(recipient, query, buckets); + return; + } + for (InsightsEvent event : orEmpty(recipient.events())) { + if (counts(event, query.metric())) { + add(buckets, event.timestamp(), query); + } + } + } + + /** + * DELIVERY_COMPLAINT is a complaint about a message that was delivered, so it counts the + * recipient once, in the bucket the complaint itself falls in. + */ + private static void countDeliveredComplaint(EmailInsights recipient, Query query, + Map buckets) { + boolean delivered = false; + for (InsightsEvent event : orEmpty(recipient.events())) { + if ("DELIVERY".equals(event.type())) { + delivered = true; + } else if (delivered && "COMPLAINT".equals(event.type())) { + add(buckets, event.timestamp(), query); + return; + } + } + } + + private static boolean counts(InsightsEvent event, String metric) { + String type = event.type(); + if (type == null) { + return false; + } + return switch (metric) { + case "SEND", "DELIVERY", "COMPLAINT" -> type.equals(metric); + case "PERMANENT_BOUNCE" -> "BOUNCE".equals(type) && isBounceType(event, "PERMANENT"); + case "TRANSIENT_BOUNCE" -> "BOUNCE".equals(type) && isBounceType(event, "TRANSIENT"); + default -> false; + }; + } + + private static boolean isBounceType(InsightsEvent event, String bounceType) { + InsightsEventDetails details = event.details(); + if (details == null) { + return false; + } + InsightsBounce bounce = details.bounce(); + return bounce != null && bounceType.equals(bounce.bounceType()); + } + + private static void add(Map buckets, Instant timestamp, Query query) { + if (timestamp == null || timestamp.isBefore(query.startDate()) + || !timestamp.isBefore(query.endDate())) { + return; + } + buckets.merge(timestamp.truncatedTo(ChronoUnit.DAYS), 1L, Long::sum); + } + + private static boolean matchesMessageDimensions(SentEmail email, Map dimensions) { + String identity = dimensions.get(EMAIL_IDENTITY); + if (identity != null && !matchesIdentity(email.getSource(), identity)) { + return false; + } + String configurationSet = dimensions.get(CONFIGURATION_SET); + if (configurationSet != null && !configurationSet.equals(email.getConfigurationSetName())) { + return false; + } + String tenant = dimensions.get(TENANT_NAME); + return tenant == null || tenant.equals(email.getTenantName()); + } + + /** + * An identity dimension names either the sending address or its domain, the way the identity + * itself can be either; the probe filtered a domain and matched sends from an address in it. + */ + private static boolean matchesIdentity(String source, String identity) { + if (source == null) { + return false; + } + // A stored Source can carry display-name syntax, so compare the bare mailbox. + String address = SesService.extractEmailAddress(source); + if (address.equalsIgnoreCase(identity)) { + return true; + } + int at = address.lastIndexOf('@'); + return at >= 0 && address.substring(at + 1).equalsIgnoreCase(identity); + } + + private static boolean matchesIsp(EmailInsights recipient, Map dimensions) { + String isp = dimensions.get(ISP); + return isp == null || isp.equals(recipient.isp()); + } + + private static List orEmpty(List values) { + return values == null ? List.of() : values; + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.java new file mode 100644 index 0000000000..e389c916f1 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesMetricsController.java @@ -0,0 +1,388 @@ +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.ArrayNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.RegionResolver; +import io.github.hectorvent.floci.services.ses.model.SentEmail; +import jakarta.inject.Inject; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.POST; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.HttpHeaders; +import jakarta.ws.rs.core.MediaType; +import jakarta.ws.rs.core.Response; + +import java.time.DateTimeException; +import java.time.Duration; +import java.time.Instant; +import java.time.format.DateTimeParseException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.regex.Pattern; + +import static io.github.hectorvent.floci.services.ses.SesV2Json.readOptionBody; +import static io.github.hectorvent.floci.services.ses.SesV2Json.stringMemberOrAbsent; + +/** + * SES V2 metric data ({@code POST /v2/email/metrics/batch}), the aggregate view of the same stored + * timelines {@code GetMessageInsights} serves one message at a time. + * + *

Probe-confirmed against real SES (2026-09-23, us-east-1). The operation is gated on Virtual + * Deliverability Manager, like message insights. Results come back in an unspecified order, both + * {@code Results} and {@code Errors} are always present, and a result always carries its three + * members even when the series is empty. {@link SesMetricData} documents the bucketing. + * + *

Validation order is probed, strongest first: the size of {@code Queries}, a duplicate + * {@code Id}, an unsupported dimension name, the aggregated member constraints, the dimension + * value, then the dates. Two orderings could not be observed and are noted in the service docs. + */ +@Path("/v2/email") +@Produces(MediaType.APPLICATION_JSON) +@Consumes(MediaType.APPLICATION_JSON) +public class SesMetricsController { + + private static final int MAX_QUERIES = 10; + private static final int MAX_DIMENSIONS = 3; + private static final Pattern NAME_CHARSET = Pattern.compile("[A-Za-z0-9_-]+"); + private static final Duration MAX_INTERVAL = Duration.ofDays(60); + private static final Duration WINDOW_SLACK = Duration.ofDays(1); + + // The enum sets AWS spells out in its validation messages, in AWS's own order. + private static final String METRIC_ENUM = + "[DELIVERY_COMPLAINT, CLICK, SEND, OPEN, COMPLAINT, DELIVERY, DELIVERY_OPEN, " + + "PERMANENT_BOUNCE, DELIVERY_CLICK, TRANSIENT_BOUNCE]"; + private static final String NAMESPACE_ENUM = "[VDM]"; + + private final SesSentEmailService sentEmailService; + private final SesAccountService accountService; + private final RegionResolver regionResolver; + private final ObjectMapper objectMapper; + + @Inject + public SesMetricsController(SesSentEmailService sentEmailService, SesAccountService accountService, + RegionResolver regionResolver, ObjectMapper objectMapper) { + this.sentEmailService = sentEmailService; + this.accountService = accountService; + this.regionResolver = regionResolver; + this.objectMapper = objectMapper; + } + + @POST + @Path("/metrics/batch") + public Response batchGetMetricData(@Context HttpHeaders headers, String body) { + String region = regionResolver.resolveRegion(headers); + accountService.requireVdmEnabled(region); + + JsonNode request = readOptionBody(objectMapper, body); + List queryNodes = requireQueries(request); + requireUniqueIds(queryNodes); + requireKnownDimensionNames(queryNodes); + requireMemberConstraints(queryNodes); + // Probe-confirmed request-wide: a blank identity in the second query outranks a reversed + // date range in the first, and the other way round too. + for (JsonNode query : queryNodes) { + requireDimensionValues(dimensionsOf(query)); + } + + List queries = new ArrayList<>(queryNodes.size()); + for (int i = 0; i < queryNodes.size(); i++) { + queries.add(parseQuery(queryNodes.get(i), i + 1)); + } + + List emails = withinAnyWindow(sentEmailService.listInRegion(region), queries); + ObjectNode result = objectMapper.createObjectNode(); + ArrayNode results = result.putArray("Results"); + for (SesMetricData.Query query : queries) { + results.add(render(query, SesMetricData.aggregate(emails, query))); + } + // Never populated in any probe: a bad request fails the whole call rather than one query. + result.putArray("Errors"); + return Response.ok(result).build(); + } + + /** + * Drops the messages no query in the batch can reach, so the per-query aggregation passes walk + * only the requested window rather than every message the region retains. The region's messages + * are already listed and held by then, so this bounds the repeated walks, not the initial scan. + * Events are derived at send time and sit within milliseconds of {@code sentAt}, so a day of + * slack on each side is far more than any timeline needs; the per-event range check in + * {@link SesMetricData} remains the authority. + */ + private static List withinAnyWindow(List emails, + List queries) { + Instant earliest = null; + Instant latest = null; + for (SesMetricData.Query query : queries) { + if (earliest == null || query.startDate().isBefore(earliest)) { + earliest = query.startDate(); + } + if (latest == null || query.endDate().isAfter(latest)) { + latest = query.endDate(); + } + } + // A caller may legitimately name a timestamp within a day of Instant's own bounds, and the + // slack must not run off them and turn that into a server error. + Instant from = earliest.isBefore(Instant.MIN.plus(WINDOW_SLACK)) + ? Instant.MIN : earliest.minus(WINDOW_SLACK); + Instant to = latest.isAfter(Instant.MAX.minus(WINDOW_SLACK)) + ? Instant.MAX : latest.plus(WINDOW_SLACK); + List kept = new ArrayList<>(); + for (SentEmail email : emails) { + Instant sentAt = email.getSentAt(); + if (sentAt == null || (!sentAt.isBefore(from) && sentAt.isBefore(to))) { + kept.add(email); + } + } + return kept; + } + + private ObjectNode render(SesMetricData.Query query, SesMetricData.Series series) { + ObjectNode node = objectMapper.createObjectNode(); + node.put("Id", query.id()); + ArrayNode timestamps = node.putArray("Timestamps"); + for (Instant timestamp : series.timestamps()) { + timestamps.add(timestamp.toEpochMilli() / 1000.0); + } + ArrayNode values = node.putArray("Values"); + for (Long value : series.values()) { + values.add(value); + } + return node; + } + + private static List requireQueries(JsonNode request) { + JsonNode queries = request.path("Queries"); + // Absent and null fail the list constraint, but a present value of the wrong shape is a + // deserialization failure, the same treatment the Dimensions map gets below. + if (!queries.isMissingNode() && !queries.isNull() && !queries.isArray()) { + throw SesV2Json.unexpectedStartError(queries); + } + if (!queries.isArray() || queries.isEmpty()) { + throw listConstraint("Member must have length greater than or equal to 1"); + } + if (queries.size() > MAX_QUERIES) { + throw listConstraint("Member must have length less than or equal to " + MAX_QUERIES); + } + List nodes = new ArrayList<>(queries.size()); + for (JsonNode query : queries) { + if (!query.isObject()) { + throw SesV2Json.unexpectedStartError(query); + } + nodes.add(query); + } + return nodes; + } + + private static void requireUniqueIds(List queries) { + Set seen = new LinkedHashSet<>(); + for (JsonNode query : queries) { + String id = stringMemberOrAbsent(query, "Id"); + if (id != null && !seen.add(id)) { + throw new AwsException("BadRequestException", + "All queries must have a unique identifier.", 400); + } + } + } + + /** + * An unknown dimension name outranks the member constraints: a query carrying both an unknown + * dimension and an invalid Metric reports the dimension. + */ + private static void requireKnownDimensionNames(List queries) { + for (JsonNode query : queries) { + JsonNode dimensions = query.path("Dimensions"); + if (dimensions.isMissingNode() || dimensions.isNull()) { + continue; + } + // A map member has to be a JSON object: an array or a scalar is a deserialization + // failure, not an absent Dimensions that silently drops the filter. + if (!dimensions.isObject()) { + throw SesV2Json.unexpectedStartError(dimensions); + } + Iterator names = dimensions.fieldNames(); + while (names.hasNext()) { + if (!SesMetricData.DIMENSION_NAMES.contains(names.next())) { + throw new AwsException("BadRequestException", + "Unsupported dimension provided.", 400); + } + } + } + } + + /** + * The Smithy member constraints, aggregated into one message the way AWS reports them. The + * probed order is metric, namespace, id; the position of an empty Dimensions map among them was + * not observed. + */ + private static void requireMemberConstraints(List queries) { + List violations = new ArrayList<>(); + for (int i = 0; i < queries.size(); i++) { + JsonNode query = queries.get(i); + int position = i + 1; + String metric = stringMemberOrAbsent(query, "Metric"); + if (metric == null || !SesMetricData.METRICS.contains(metric)) { + violations.add(memberConstraint(position, "metric", + "Member must satisfy enum value set: " + METRIC_ENUM)); + } + String namespace = stringMemberOrAbsent(query, "Namespace"); + if (namespace == null || !"VDM".equals(namespace)) { + violations.add(memberConstraint(position, "namespace", + "Member must satisfy enum value set: " + NAMESPACE_ENUM)); + } + String id = stringMemberOrAbsent(query, "Id"); + if (id == null || id.isEmpty()) { + violations.add(memberConstraint(position, "id", + "Member must have length greater than or equal to 1")); + } else if (id.length() > 255) { + violations.add(memberConstraint(position, "id", + "Member must have length less than or equal to 255")); + } + JsonNode dimensions = query.path("Dimensions"); + if (dimensions.isObject() && dimensions.isEmpty()) { + violations.add(memberConstraint(position, "dimensions", + "Member must have length greater than or equal to 1")); + } + // Reachable only since TENANT_NAME made a fourth key possible; probe-confirmed. + if (dimensions.isObject() && dimensions.size() > MAX_DIMENSIONS) { + violations.add(memberConstraint(position, "dimensions", + "Member must have length less than or equal to " + MAX_DIMENSIONS)); + } + } + if (!violations.isEmpty()) { + throw aggregated(violations); + } + } + + private static Map dimensionsOf(JsonNode query) { + Map dimensions = new LinkedHashMap<>(); + JsonNode node = query.path("Dimensions"); + if (node.isObject()) { + Iterator> fields = node.fields(); + while (fields.hasNext()) { + Map.Entry field = fields.next(); + if (!field.getValue().isTextual()) { + throw new AwsException("SerializationException", null, 400); + } + dimensions.put(field.getKey(), field.getValue().textValue()); + } + } + return dimensions; + } + + private SesMetricData.Query parseQuery(JsonNode query, int position) { + Map dimensions = dimensionsOf(query); + + Instant startDate = requireTimestamp(query, "StartDate", "startDate", position); + Instant endDate = requireTimestamp(query, "EndDate", "endDate", position); + if (!startDate.isBefore(endDate)) { + throw new AwsException("BadRequestException", + "Expected start date to be before the end date.", 400); + } + if (Duration.between(startDate, endDate).compareTo(MAX_INTERVAL) > 0) { + throw new AwsException("BadRequestException", + "Invalid interval, can request at most 60 days. Please partition your " + + "requested interval.", 400); + } + return new SesMetricData.Query(stringMemberOrAbsent(query, "Id"), + stringMemberOrAbsent(query, "Metric"), dimensions, startDate, endDate); + } + + /** + * Each dimension validates its own value, probe-confirmed 2026-09-24 against real SES. ISP, + * the configuration set and the tenant share one charset, ASCII alphanumerics plus underscore + * and hyphen, which is the rule AWS spells out in its own two messages. An identity is either + * a bare name or {@code local@domain} with both halves present. The order in which two bad + * values in one query are reported was not observed and is Floci's choice. + */ + private static void requireDimensionValues(Map dimensions) { + String identity = dimensions.get("EMAIL_IDENTITY"); + if (identity != null && !isIdentity(identity)) { + throw new AwsException("BadRequestException", "Invalid email identity provided.", 400); + } + String isp = dimensions.get("ISP"); + if (isp != null && !NAME_CHARSET.matcher(isp).matches()) { + throw new AwsException("BadRequestException", "Invalid ISP name provided.", 400); + } + requireName(dimensions.get("CONFIGURATION_SET"), "configuration set name", + "The configuration set name must be specified."); + requireName(dimensions.get("TENANT_NAME"), "tenant name", "TenantName cannot be empty"); + } + + private static void requireName(String value, String noun, String blankMessage) { + if (value == null) { + return; + } + if (value.isEmpty()) { + throw new AwsException("BadRequestException", blankMessage, 400); + } + if (!NAME_CHARSET.matcher(value).matches()) { + throw new AwsException("BadRequestException", "Invalid " + noun + " <" + value + + ">: only alphanumeric ASCII characters, '_', and '-' are allowed.", 400); + } + } + + /** + * Accepts every identity the probe accepted (a bare label, a domain, and local@domain in any + * case) and rejects every one it refused (blank, a missing half, a space, a star). + */ + private static boolean isIdentity(String value) { + if (value.isEmpty() || value.chars().anyMatch(c -> c == '*' || Character.isWhitespace(c))) { + return false; + } + int at = value.indexOf('@'); + if (at < 0) { + return true; + } + return at > 0 && at < value.length() - 1 && value.indexOf('@', at + 1) < 0; + } + + private static Instant requireTimestamp(JsonNode query, String field, String member, int position) { + JsonNode node = query.path(field); + if (node.isMissingNode() || node.isNull()) { + throw aggregated(List.of(memberConstraint(position, member, "Member must not be null"))); + } + if (node.isNumber()) { + try { + return Instant.ofEpochMilli(Math.round(node.doubleValue() * 1000.0)); + } catch (DateTimeException e) { + // A value outside Instant's range is the caller's, so it is a 400, not a 500. + throw new AwsException("SerializationException", e.getMessage(), 400); + } + } + if (node.isTextual()) { + try { + return Instant.parse(node.asText()); + } catch (DateTimeParseException e) { + throw new AwsException("SerializationException", e.getMessage(), 400); + } + } + throw SesV2Json.unexpectedStartError(node); + } + + private static String memberConstraint(int position, String member, String constraint) { + return "Value at 'queries." + position + ".member." + member + + "' failed to satisfy constraint: " + constraint; + } + + private static AwsException listConstraint(String constraint) { + return aggregated(List.of("Value at 'queries' failed to satisfy constraint: " + constraint)); + } + + private static AwsException aggregated(List violations) { + String header = violations.size() == 1 + ? "1 validation error detected: " + : violations.size() + " validation errors detected: "; + return new AwsException("BadRequestException", + header + String.join("; ", violations), 400); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java index aabcbedae9..c69cd19f58 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesQueryHandler.java @@ -254,7 +254,7 @@ public class SesQueryHandler { // ListManagementOptions is a v2-only SendEmail field; the v1 Query API has no equivalent. String messageId = sesService.sendEmail(source, toAddresses, ccAddresses, bccAddresses, replyToAddresses, returnPath, subject, bodyText, bodyHtml, configurationSetName, - emailTags, List.of(), null, region); + emailTags, List.of(), null, null, region); String result = new XmlBuilder().elem("MessageId", messageId).build(); return Response.ok(AwsQueryResponse.envelope("SendEmail", AwsNamespaces.SES, result)).build(); @@ -272,7 +272,7 @@ public class SesQueryHandler { List emailTags = extractMessageTags(params, "Tags"); String messageId = sesService.sendRawEmail(source, destinations, rawMessage, - null, configurationSetName, emailTags, null, region); + null, configurationSetName, emailTags, null, null, region); String result = new XmlBuilder().elem("MessageId", messageId).build(); return Response.ok(AwsQueryResponse.envelope("SendRawEmail", AwsNamespaces.SES, result)).build(); @@ -606,7 +606,7 @@ public class SesQueryHandler { List emailTags = extractMessageTags(params, "Tags"); String messageId = sesService.sendTemplatedEmail(source, toAddresses, ccAddresses, bccAddresses, replyToAddresses, returnPath, resolvedName, templateData, - configurationSetName, emailTags, List.of(), null, region); + configurationSetName, emailTags, List.of(), null, null, region); String result = new XmlBuilder().elem("MessageId", messageId).build(); return Response.ok(AwsQueryResponse.envelope("SendTemplatedEmail", AwsNamespaces.SES, result)).build(); @@ -753,7 +753,7 @@ public class SesQueryHandler { List results = sesService.sendBulkTemplatedEmail(source, replyToAddresses, returnPath, template.getSubject(), template.getTextPart(), template.getHtmlPart(), defaultTemplateData, entries, configurationSetName, - defaultEmailTags, List.of(), region); + defaultEmailTags, List.of(), null, region); XmlBuilder xml = new XmlBuilder().start("Status"); for (BulkEmailEntryResult result : results) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java index 3faa1f837e..dc0704ad29 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSendController.java @@ -125,7 +125,7 @@ public class SesSendController { configurationSetName, regionResolver.getAccountId(), region); messageId = sesService.sendRawEmail(fromEmailAddress, allDestinations, rawData, feedbackForwardingAddress, configurationSetName, emailTags, listManagement, - region); + tenantName, region); } else if (content.has("Simple")) { if (fromEmailAddress == null || fromEmailAddress.isBlank()) { // AWS returns BadRequestException with a null message body here. @@ -142,7 +142,8 @@ public class SesSendController { messageId = sesService.sendEmail(fromEmailAddress, toAddresses, ccAddresses, bccAddresses, replyToAddresses, feedbackForwardingAddress, subject, bodyText, bodyHtml, - configurationSetName, emailTags, additionalHeaders, listManagement, region); + configurationSetName, emailTags, additionalHeaders, listManagement, + tenantName, region); } else if (content.has("Template")) { if (fromEmailAddress == null || fromEmailAddress.isBlank()) { throw new AwsException("BadRequestException", "Source cannot be empty", 400); @@ -175,7 +176,7 @@ public class SesSendController { messageId = sesService.sendTemplatedEmail(fromEmailAddress, toAddresses, ccAddresses, bccAddresses, replyToAddresses, feedbackForwardingAddress, resolvedName, templateData, - configurationSetName, emailTags, additionalHeaders, listManagement, region); + configurationSetName, emailTags, additionalHeaders, listManagement, tenantName, region); } else { JsonNode inline = template.path("TemplateContent"); String subject = inline.path("Subject").asText(null); @@ -190,7 +191,7 @@ public class SesSendController { messageId = sesService.sendInlineTemplatedEmail(fromEmailAddress, toAddresses, ccAddresses, bccAddresses, replyToAddresses, feedbackForwardingAddress, subject, text, html, templateData, - configurationSetName, emailTags, additionalHeaders, listManagement, region); + configurationSetName, emailTags, additionalHeaders, listManagement, tenantName, region); } } else { throw new AwsException("BadRequestException", @@ -318,7 +319,7 @@ public class SesSendController { List results = sesService.sendBulkTemplatedEmail(fromEmailAddress, replyToAddresses, feedbackForwardingAddress, subject, text, html, defaultTemplateData, entries, configurationSetName, - defaultEmailTags, defaultHeaders, region); + defaultEmailTags, defaultHeaders, tenantName, region); ObjectNode response = objectMapper.createObjectNode(); ArrayNode arr = response.putArray("BulkEmailEntryResults"); diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesSentEmailService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesSentEmailService.java index 6ffc15bb85..72ae5ebec6 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesSentEmailService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesSentEmailService.java @@ -56,6 +56,14 @@ public class SesSentEmailService { return emailStore.scan(k -> k.startsWith(prefix)).size(); } + /** + * Every stored message for one region, for the metric aggregation behind BatchGetMetricData. + */ + public List listInRegion(String region) { + String prefix = "email::" + region + "::"; + return emailStore.scan(k -> k.startsWith(prefix)); + } + public List listAll() { return emailStore.scan(k -> k.startsWith("email::")); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java index d64b58da98..04d378901b 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesService.java @@ -216,7 +216,7 @@ public class SesService { String subject, String bodyText, String bodyHtml, String configurationSetName, List emailTags, List additionalHeaders, ListManagementOptions listManagement, - String region) { + String tenantName, String region) { if (source == null || source.isBlank()) { throw new AwsException("InvalidParameterValue", "Source email is required.", 400); } @@ -266,6 +266,8 @@ public class SesService { SentEmail email = new SentEmail(messageId, region, source, toAddresses, ccAddresses, bccAddresses, replyToAddresses, subject, bodyText, bodyHtml); email.setReturnPath(effectiveReturnPath); + email.setConfigurationSetName(firstNonBlank(effectiveConfigSet)); + email.setTenantName(firstNonBlank(tenantName)); if (additionalHeaders != null && !additionalHeaders.isEmpty()) { email.setHeaders(additionalHeaders); } @@ -310,7 +312,7 @@ public class SesService { public String sendRawEmail(String source, List destinations, String rawMessage, String returnPath, String configurationSetName, List emailTags, - ListManagementOptions listManagement, String region) { + ListManagementOptions listManagement, String tenantName, String region) { if (rawMessage == null || rawMessage.isBlank()) { throw new AwsException("InvalidParameterValue", "RawMessage.Data is required.", 400); } @@ -384,6 +386,8 @@ public class SesService { : firstNonBlank(headers.returnPath(), returnPath, effectiveSource); SentEmail email = new SentEmail(messageId, region, effectiveSource, effectiveDestinations, rawMessage); email.setReturnPath(effectiveReturnPath); + email.setConfigurationSetName(firstNonBlank(effectiveConfigSet)); + email.setTenantName(firstNonBlank(tenantName)); // The MIME subject is already parsed for the published events; store it too so // GetMessageInsights reports it for a raw send as it does for a simple one. The parser // yields "" for a missing header, and an absent subject must stay absent. @@ -550,7 +554,9 @@ public class SesService { private record IdentityNotificationTarget(String topicArn, boolean includeHeaders) {} - private static String extractEmailAddress(String source) { + // Package-private: the metric aggregation normalizes a stored Source the same way the + // send path does, so "Name " still matches an EMAIL_IDENTITY dimension. + static String extractEmailAddress(String source) { int open = source.indexOf('<'); int close = source.indexOf('>', open + 1); if (open >= 0 && close > open) { @@ -717,6 +723,7 @@ public class SesService { List.of(emailAddress), List.of(), List.of(), List.of(), template.getTemplateSubject(), null, renderedHtml); email.setReturnPath(template.getFromEmailAddress()); + email.setConfigurationSetName(firstNonBlank(effectiveConfigSet)); email.setInsights(SesMessageInsights.build(List.of(emailAddress), SesRecipientEvents.classify(List.of(emailAddress), suppressedReasons, rejected), email.getSentAt())); @@ -1426,12 +1433,13 @@ public class SesService { String returnPath, String templateName, JsonNode templateData, String configurationSetName, List emailTags, List additionalHeaders, - ListManagementOptions listManagement, String region) { + ListManagementOptions listManagement, String tenantName, String region) { EmailTemplate template = templateService.getTemplate(templateName, region); return sendInlineTemplatedEmail(source, toAddresses, ccAddresses, bccAddresses, replyToAddresses, returnPath, template.getSubject(), template.getTextPart(), template.getHtmlPart(), templateData, - configurationSetName, emailTags, additionalHeaders, listManagement, region); + configurationSetName, emailTags, additionalHeaders, listManagement, + tenantName, region); } /** @@ -1456,13 +1464,14 @@ public class SesService { JsonNode templateData, String configurationSetName, List emailTags, List additionalHeaders, - ListManagementOptions listManagement, String region) { + ListManagementOptions listManagement, String tenantName, String region) { requireInlineTemplateContent(subject, textPart, htmlPart); return sendEmail(source, toAddresses, ccAddresses, bccAddresses, replyToAddresses, returnPath, SesTemplateService.applyTemplateData(subject, templateData), SesTemplateService.applyTemplateData(textPart, templateData), SesTemplateService.applyTemplateData(htmlPart, templateData), - configurationSetName, emailTags, additionalHeaders, listManagement, region); + configurationSetName, emailTags, additionalHeaders, listManagement, + tenantName, region); } public List sendBulkTemplatedEmail(String source, @@ -1474,7 +1483,7 @@ public class SesService { String configurationSetName, List defaultEmailTags, List defaultHeaders, - String region) { + String tenantName, String region) { if (source == null || source.isBlank()) { throw new AwsException("InvalidParameterValue", "Source email is required.", 400); } @@ -1514,7 +1523,7 @@ public class SesService { SesTemplateService.applyTemplateData(subject, merged), SesTemplateService.applyTemplateData(textPart, merged), SesTemplateService.applyTemplateData(htmlPart, merged), - configurationSetName, mergedTags, mergedHeaders, null, region); + configurationSetName, mergedTags, mergedHeaders, null, tenantName, region); results.add(BulkEmailEntryResult.success(messageId)); } catch (AwsException e) { results.add(BulkEmailEntryResult.failure( diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java index c358f0fcc7..d8ce058be0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/model/SentEmail.java @@ -66,6 +66,20 @@ public class SentEmail { @JsonProperty("EmailTags") private List emailTags; + /** + * The configuration set the send resolved to, which BatchGetMetricData filters on. Absent when + * the send named none and no default applied. + */ + @JsonProperty("ConfigurationSetName") + private String configurationSetName; + + /** + * The tenant the send named, which the TENANT_NAME metric dimension filters on. Absent for a + * send that named none, and always absent on the v1 surface, which has no tenants. + */ + @JsonProperty("TenantName") + private String tenantName; + /** * Per-recipient event timelines, derived at send time and served by {@code GetMessageInsights}. */ @@ -167,6 +181,14 @@ public class SentEmail { public List getEmailTags() { return emailTags; } public void setEmailTags(List emailTags) { this.emailTags = emailTags; } + public String getTenantName() { return tenantName; } + public void setTenantName(String tenantName) { this.tenantName = tenantName; } + + public String getConfigurationSetName() { return configurationSetName; } + public void setConfigurationSetName(String configurationSetName) { + this.configurationSetName = configurationSetName; + } + public List getInsights() { return insights; } public void setInsights(List insights) { this.insights = insights; } } diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java index 87c55ae72b..977355f347 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/CognitoLambdaTriggersTest.java @@ -1130,7 +1130,7 @@ class CognitoLambdaTriggersTest { eq(List.of()), eq(List.of()), eq(List.of()), isNull(), eq("TRIGGER-FIRED"), eq("TRIGGER-FIRED code=246962"), - any(), any(), eq(List.of()), eq(List.of()), any(), anyString()); + any(), any(), eq(List.of()), eq(List.of()), any(), isNull(), anyString()); } @Test @@ -1187,7 +1187,7 @@ class CognitoLambdaTriggersTest { any(), any(), any(), isNull(), eq("Your verification code"), eq("Your verification code is 246962."), - any(), any(), any(), any(), any(), anyString()); + any(), any(), any(), any(), any(), isNull(), anyString()); } @Test @@ -1211,7 +1211,7 @@ class CognitoLambdaTriggersTest { any(), any(), any(), isNull(), eq("Your verification code"), eq("Your verification code is 246962."), - any(), any(), any(), any(), any(), anyString()); + any(), any(), any(), any(), any(), isNull(), anyString()); } @Test diff --git a/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java b/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java index 458a827037..13e68bf03d 100644 --- a/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/cognito/verification/CognitoMessageDispatcherTest.java @@ -50,7 +50,7 @@ class CognitoMessageDispatcherTest { eq("Verify your account"), eq("Hi! Your code is 123456."), isNull(), isNull(), eq(List.of()), eq(List.of()), - isNull(), eq("us-east-1")); + isNull(), isNull(), eq("us-east-1")); verifyNoInteractions(sns); } @@ -123,7 +123,7 @@ class CognitoMessageDispatcherTest { eq(List.of()), eq(List.of()), eq(List.of()), isNull(), eq("Your verification code"), eq("Your verification code is 111222."), - isNull(), isNull(), eq(List.of()), eq(List.of()), isNull(), eq("us-east-1")); + isNull(), isNull(), eq(List.of()), eq(List.of()), isNull(), isNull(), eq("us-east-1")); } @Test @@ -139,7 +139,7 @@ class CognitoMessageDispatcherTest { eq(List.of()), eq(List.of()), eq(List.of()), isNull(), anyString(), eq("Welcome, please verify.\nCode: 999000"), - isNull(), isNull(), eq(List.of()), eq(List.of()), isNull(), eq("us-east-1")); + isNull(), isNull(), eq(List.of()), eq(List.of()), isNull(), isNull(), eq("us-east-1")); } @Test @@ -152,7 +152,7 @@ class CognitoMessageDispatcherTest { verify(ses).sendEmail(anyString(), eq(List.of("alice@example.com")), any(), any(), any(), isNull(), anyString(), anyString(), isNull(), isNull(), any(), any(), - isNull(), anyString()); + isNull(), isNull(), anyString()); verifyNoInteractions(sns); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesAccountServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesAccountServiceTest.java index 55a88b6464..1280949f2f 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesAccountServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesAccountServiceTest.java @@ -99,4 +99,19 @@ class SesAccountServiceTest { assertFalse(service.isDedicatedIpAutoWarmupEnabled(REGION)); assertTrue(service.isDedicatedIpAutoWarmupEnabled("eu-west-1")); } + + @Test + void requireVdmEnabled_rejectsAnUnconfiguredRegionAndADisabledOne() { + AwsException unconfigured = assertThrows(AwsException.class, + () -> service.requireVdmEnabled("eu-north-1")); + assertEquals("NotFoundException", unconfigured.getErrorCode()); + assertEquals("To use this feature you must enable Virtual Deliverability Manager", + unconfigured.getMessage()); + + service.putAccountVdmAttributes("eu-north-1", new AccountVdmAttributes(false, false, false)); + assertThrows(AwsException.class, () -> service.requireVdmEnabled("eu-north-1")); + + service.putAccountVdmAttributes("eu-north-1", new AccountVdmAttributes(true, false, false)); + service.requireVdmEnabled("eu-north-1"); + } } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesInsightsControllerTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesInsightsControllerTest.java index 6476344322..a40ead9deb 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesInsightsControllerTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesInsightsControllerTest.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -45,9 +46,12 @@ class SesInsightsControllerTest { } private void vdmEnabled(boolean enabled) { - when(accountService.findAccountVdmAttributes(REGION)) - .thenReturn(enabled ? Optional.of(new AccountVdmAttributes(true, false, false)) - : Optional.empty()); + if (enabled) { + return; + } + doThrow(new AwsException("NotFoundException", + "To use this feature you must enable Virtual Deliverability Manager", 404)) + .when(accountService).requireVdmEnabled(REGION); } @Test @@ -59,19 +63,8 @@ class SesInsightsControllerTest { assertEquals("NotFoundException", thrown.getErrorCode()); assertTrue(thrown.getMessage().contains("Virtual Deliverability Manager"), thrown.getMessage()); - // The integration test can only infer the ordering from the response; this pins it. - verifyNoInteractions(sentEmailService); - } - - @Test - void vdmConfiguredButDisabledIsStillGated() { - when(accountService.findAccountVdmAttributes(REGION)) - .thenReturn(Optional.of(new AccountVdmAttributes(false, false, false))); - - AwsException thrown = assertThrows(AwsException.class, - () -> controller.getMessageInsights(null, "any-id")); - - assertTrue(thrown.getMessage().contains("Virtual Deliverability Manager"), thrown.getMessage()); + // The integration test can only infer the ordering from the response; this pins it. What + // the gate itself decides is SesAccountServiceTest's business. verifyNoInteractions(sentEmailService); } diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesMetricDataTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesMetricDataTest.java new file mode 100644 index 0000000000..25266e1d60 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesMetricDataTest.java @@ -0,0 +1,244 @@ +package io.github.hectorvent.floci.services.ses; + +import io.github.hectorvent.floci.services.ses.model.EmailInsights; +import io.github.hectorvent.floci.services.ses.model.InsightsBounce; +import io.github.hectorvent.floci.services.ses.model.InsightsEvent; +import io.github.hectorvent.floci.services.ses.model.InsightsEventDetails; +import io.github.hectorvent.floci.services.ses.model.SentEmail; +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * The daily aggregation behind BatchGetMetricData. The bucketing rules asserted here are + * probe-confirmed: whole UTC days, a sparse series, and an exclusive end of range. + */ +class SesMetricDataTest { + + private static final Instant DAY_ONE = Instant.parse("2026-09-21T00:00:00Z"); + private static final Instant DAY_TWO = DAY_ONE.plus(1, ChronoUnit.DAYS); + private static final Instant DAY_FOUR = DAY_ONE.plus(3, ChronoUnit.DAYS); + + private static SentEmail email(Instant sentAt, String source, String configurationSet, + EmailInsights... recipients) { + return emailForTenant(sentAt, source, configurationSet, null, recipients); + } + + private static SentEmail emailForTenant(Instant sentAt, String source, String configurationSet, + String tenant, EmailInsights... recipients) { + SentEmail email = new SentEmail(); + email.setSource(source); + email.setConfigurationSetName(configurationSet); + email.setTenantName(tenant); + email.setSentAt(sentAt); + email.setInsights(List.of(recipients)); + return email; + } + + private static EmailInsights recipient(Instant at, String destination, String... types) { + List events = new ArrayList<>(); + for (String type : types) { + events.add(InsightsEvent.of(at, type)); + } + return new EmailInsights(destination, "UNKNOWN_ISP", events); + } + + private static EmailInsights bounced(Instant at, String destination, String bounceType) { + return new EmailInsights(destination, "UNKNOWN_ISP", List.of( + InsightsEvent.of(at, "SEND"), + new InsightsEvent(at, "BOUNCE", new InsightsEventDetails( + new InsightsBounce(bounceType, "General", null), null)))); + } + + private static SesMetricData.Query query(String metric, Instant start, Instant end) { + return new SesMetricData.Query("q", metric, Map.of(), start, end); + } + + @Test + void countsOneBucketPerUtcDay() { + // Two sends four hours apart on the same day land in one bucket at that day's midnight. + List emails = List.of( + email(DAY_ONE.plus(1, ChronoUnit.HOURS), "sender@example.com", null, + recipient(DAY_ONE.plus(1, ChronoUnit.HOURS), "a@example.com", "SEND")), + email(DAY_ONE.plus(5, ChronoUnit.HOURS), "sender@example.com", null, + recipient(DAY_ONE.plus(5, ChronoUnit.HOURS), "b@example.com", "SEND"))); + + SesMetricData.Series series = SesMetricData.aggregate(emails, + query("SEND", DAY_ONE, DAY_ONE.plus(7, ChronoUnit.DAYS))); + + assertEquals(List.of(DAY_ONE), series.timestamps()); + assertEquals(List.of(2L), series.values()); + } + + @Test + void skipsDaysWithoutData() { + List emails = List.of( + email(DAY_ONE, "sender@example.com", null, recipient(DAY_ONE, "a@example.com", "SEND")), + email(DAY_FOUR, "sender@example.com", null, recipient(DAY_FOUR, "b@example.com", "SEND"))); + + SesMetricData.Series series = SesMetricData.aggregate(emails, + query("SEND", DAY_ONE, DAY_ONE.plus(7, ChronoUnit.DAYS))); + + // Sparse, not zero filled: the two quiet days in between are absent entirely. + assertEquals(List.of(DAY_ONE, DAY_FOUR), series.timestamps()); + assertEquals(List.of(1L, 1L), series.values()); + } + + @Test + void endOfRangeIsExclusive() { + List emails = List.of( + email(DAY_ONE, "sender@example.com", null, recipient(DAY_ONE, "a@example.com", "SEND")), + email(DAY_TWO, "sender@example.com", null, recipient(DAY_TWO, "b@example.com", "SEND"))); + + SesMetricData.Series series = SesMetricData.aggregate(emails, query("SEND", DAY_ONE, DAY_TWO)); + + assertEquals(List.of(DAY_ONE), series.timestamps()); + } + + @Test + void countsOnePerRecipientNotPerMessage() { + List emails = List.of(email(DAY_ONE, "sender@example.com", null, + recipient(DAY_ONE, "a@example.com", "SEND"), + recipient(DAY_ONE, "b@example.com", "SEND"), + recipient(DAY_ONE, "c@example.com", "SEND"))); + + SesMetricData.Series series = SesMetricData.aggregate(emails, + query("SEND", DAY_ONE, DAY_TWO)); + + assertEquals(List.of(3L), series.values()); + } + + @Test + void separatesPermanentFromTransientBounces() { + List emails = List.of(email(DAY_ONE, "sender@example.com", null, + bounced(DAY_ONE, "a@example.com", "PERMANENT"), + bounced(DAY_ONE, "b@example.com", "TRANSIENT"))); + + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + query("PERMANENT_BOUNCE", DAY_ONE, DAY_TWO)).values()); + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + query("TRANSIENT_BOUNCE", DAY_ONE, DAY_TWO)).values()); + } + + @Test + void deliveryComplaintCountsADeliveredRecipientThatThenComplained() { + List emails = List.of(email(DAY_ONE, "sender@example.com", null, + recipient(DAY_ONE, "a@example.com", "SEND", "DELIVERY", "COMPLAINT"), + // Complained without a delivery, so it is a complaint but not a delivered one. + recipient(DAY_ONE, "b@example.com", "SEND", "COMPLAINT"))); + + assertEquals(List.of(2L), SesMetricData.aggregate(emails, + query("COMPLAINT", DAY_ONE, DAY_TWO)).values()); + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + query("DELIVERY_COMPLAINT", DAY_ONE, DAY_TWO)).values()); + } + + @Test + void metricsThatNeedARealRecipientStayEmpty() { + List emails = List.of(email(DAY_ONE, "sender@example.com", null, + recipient(DAY_ONE, "a@example.com", "SEND", "DELIVERY"))); + + for (String metric : List.of("OPEN", "CLICK", "DELIVERY_OPEN", "DELIVERY_CLICK")) { + SesMetricData.Series series = SesMetricData.aggregate(emails, + query(metric, DAY_ONE, DAY_TWO)); + assertTrue(series.timestamps().isEmpty(), metric + " should report nothing"); + assertTrue(series.values().isEmpty(), metric + " should report nothing"); + } + } + + @Test + void identityDimensionMatchesTheAddressOrItsDomain() { + List emails = List.of(email(DAY_ONE, "sender@example.com", null, + recipient(DAY_ONE, "a@example.com", "SEND"))); + + for (String identity : List.of("sender@example.com", "example.com", "EXAMPLE.COM")) { + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("EMAIL_IDENTITY", identity), + DAY_ONE, DAY_TWO)).values(), identity); + } + assertTrue(SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("EMAIL_IDENTITY", "other.example.com"), + DAY_ONE, DAY_TWO)).values().isEmpty()); + } + + @Test + void identityDimensionMatchesASourceCarryingADisplayName() { + // The send path stores Source verbatim, so it can carry "Name ". + List emails = List.of(email(DAY_ONE, "Sender Name ", null, + recipient(DAY_ONE, "a@example.com", "SEND"))); + + for (String identity : List.of("sender@example.com", "example.com")) { + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("EMAIL_IDENTITY", identity), + DAY_ONE, DAY_TWO)).values(), identity); + } + } + + @Test + void configurationSetDimensionFiltersOnTheResolvedSet() { + List emails = List.of( + email(DAY_ONE, "sender@example.com", "tracked", + recipient(DAY_ONE, "a@example.com", "SEND")), + email(DAY_ONE, "sender@example.com", null, + recipient(DAY_ONE, "b@example.com", "SEND"))); + + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("CONFIGURATION_SET", "tracked"), + DAY_ONE, DAY_TWO)).values()); + assertTrue(SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("CONFIGURATION_SET", "absent"), + DAY_ONE, DAY_TWO)).values().isEmpty()); + } + + @Test + void tenantDimensionFiltersOnTheTenantTheSendNamed() { + // AWS accepts TENANT_NAME as a fourth dimension; the pinned SDK model still lists three. + List emails = List.of( + emailForTenant(DAY_ONE, "sender@example.com", null, "blue", + recipient(DAY_ONE, "a@example.com", "SEND")), + emailForTenant(DAY_ONE, "sender@example.com", null, null, + recipient(DAY_ONE, "b@example.com", "SEND"))); + + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("TENANT_NAME", "blue"), + DAY_ONE, DAY_TWO)).values()); + assertTrue(SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("TENANT_NAME", "green"), + DAY_ONE, DAY_TWO)).values().isEmpty()); + } + + @Test + void ispDimensionMatchesOnlyTheValueFlociReports() { + List emails = List.of(email(DAY_ONE, "sender@example.com", null, + recipient(DAY_ONE, "a@example.com", "SEND"))); + + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("ISP", "UNKNOWN_ISP"), + DAY_ONE, DAY_TWO)).values()); + assertTrue(SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", Map.of("ISP", "gmail.com"), + DAY_ONE, DAY_TWO)).values().isEmpty()); + } + + @Test + void severalDimensionsAllHaveToMatch() { + List emails = List.of(email(DAY_ONE, "sender@example.com", "tracked", + recipient(DAY_ONE, "a@example.com", "SEND"))); + + assertEquals(List.of(1L), SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", + Map.of("EMAIL_IDENTITY", "example.com", "CONFIGURATION_SET", "tracked", + "ISP", "UNKNOWN_ISP"), DAY_ONE, DAY_TWO)).values()); + assertTrue(SesMetricData.aggregate(emails, + new SesMetricData.Query("q", "SEND", + Map.of("EMAIL_IDENTITY", "example.com", "CONFIGURATION_SET", "other"), + DAY_ONE, DAY_TWO)).values().isEmpty()); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesMetricsV2IntegrationTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesMetricsV2IntegrationTest.java new file mode 100644 index 0000000000..ddb0e3cd52 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesMetricsV2IntegrationTest.java @@ -0,0 +1,515 @@ +package io.github.hectorvent.floci.services.ses; + +import io.quarkus.test.junit.QuarkusTest; +import io.restassured.response.Response; +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 java.nio.charset.StandardCharsets; +import java.time.Duration; +import java.time.Instant; +import java.util.Base64; +import java.util.List; + +import static io.restassured.RestAssured.given; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; + +/** + * Integration tests for {@code POST /v2/email/metrics/batch} (BatchGetMetricData). + * + *

The wire behaviour asserted here is probe-confirmed against real SES (2026-09-23, us-east-1): + * the VDM gate, daily buckets, a sparse series, the 60 day interval cap, and the order in which the + * request is validated. The exclusive end of the range is pinned in {@code SesMetricDataTest}, + * which can put an event on the boundary instant. Uses an isolated signing region so no other + * test's sends reach these counts. + */ +@QuarkusTest +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +class SesMetricsV2IntegrationTest { + + private static final String AUTH = + "AWS4-HMAC-SHA256 Credential=AKID/20260101/sa-east-1/ses/aws4_request"; + private static final String SENDER = "metrics-sender@example.com"; + + private static final long DAY = Duration.ofDays(1).toSeconds(); + + // One reading of the clock for the whole class: two calls a second apart would turn the + // equal-dates case below into a valid one-second interval. + private static final long NOW = Instant.now().getEpochSecond(); + + private static long start() { + return NOW - Duration.ofDays(1).toSeconds(); + } + + private static long end() { + return NOW + Duration.ofDays(1).toSeconds(); + } + + private static String query(String id, String metric) { + return """ + {"Id": "%s", "Namespace": "VDM", "Metric": "%s", "StartDate": %d, "EndDate": %d} + """.formatted(id, metric, start(), end()); + } + + private static Response post(String queriesJson) { + return given().contentType("application/json").header("Authorization", AUTH) + .body("{\"Queries\": [" + queriesJson + "]}") + .when().post("/v2/email/metrics/batch"); + } + + private static void badValue(String dimension, String value, String message) { + dimensionQuery(dimension, value).then().statusCode(400) + .body("message", equalTo(message)); + } + + private static void goodValue(String dimension, String value) { + dimensionQuery(dimension, value).then().statusCode(200); + } + + private static Response dimensionQuery(String dimension, String value) { + return given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "v", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"%s": "%s"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(dimension, value, start(), end())) + .when().post("/v2/email/metrics/batch"); + } + + private static int total(Response response, String id) { + List values = response.path("Results.find { it.Id == '" + id + "' }.Values"); + return values.stream().mapToInt(Integer::intValue).sum(); + } + + private static void send(String destination) { + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + { + "FromEmailAddress": "%s", + "Destination": {"ToAddresses": ["%s"]}, + "Content": {"Simple": {"Subject": {"Data": "metrics"}, + "Body": {"Text": {"Data": "hi"}}}} + } + """.formatted(SENDER, destination)) + .when().post("/v2/email/outbound-emails").then().statusCode(200); + } + + @Test + @Order(0) + void batchGetMetricData_vdmDisabled_reportsTheGateBeforeItValidatesTheRequest() { + // The body is invalid in three ways; with VDM off none of that is reached. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"Queries\": []}") + .when().post("/v2/email/metrics/batch").then().statusCode(404) + .body("message", containsString( + "To use this feature you must enable Virtual Deliverability Manager")); + } + + @Test + @Order(1) + void enableVdmAndVerifyTheSender() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"VdmAttributes\":{\"VdmEnabled\":\"ENABLED\"}}") + .when().put("/v2/email/account/vdm").then().statusCode(200); + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"EmailIdentity\":\"" + SENDER + "\"}") + .when().post("/v2/email/identities").then().statusCode(200); + } + + @Test + @Order(2) + void sendOneOfEachOutcome() { + send("ordinary@example.com"); + send("bounce@simulator.amazonses.com"); + send("complaint@simulator.amazonses.com"); + } + + @Test + @Order(3) + void batchGetMetricData_countsEachOutcomeOnce() { + Response response = post(String.join(",", + query("send", "SEND"), + query("delivery", "DELIVERY"), + query("bounce", "PERMANENT_BOUNCE"), + query("complaint", "COMPLAINT"), + query("delivered", "DELIVERY_COMPLAINT"))); + response.then().statusCode(200); + + // Summed across buckets, since the three sends can straddle UTC midnight. Results come + // back keyed by Id, in an order AWS does not promise. + assertEquals(3, total(response, "send")); + assertEquals(2, total(response, "delivery")); + assertEquals(1, total(response, "bounce")); + assertEquals(1, total(response, "complaint")); + assertEquals(1, total(response, "delivered")); + } + + @Test + @Order(4) + void batchGetMetricData_aMetricWithNoDataKeepsItsMembers() { + // Probed: an empty series is a Results entry with empty arrays, never an omission or a zero, + // and Errors is present on every response even though nothing ever populates it. + post(String.join(",", query("open", "OPEN"), query("transient", "TRANSIENT_BOUNCE"))) + .then().statusCode(200) + .body("Results", hasSize(2)) + .body("Results.find { it.Id == 'open' }.Timestamps", empty()) + .body("Results.find { it.Id == 'open' }.Values", empty()) + .body("Results.find { it.Id == 'transient' }.Values", empty()) + .body("Errors", empty()); + } + + @Test + @Order(5) + void batchGetMetricData_bucketsByDay() { + Response response = post(query("buckets", "SEND")); + response.then().statusCode(200); + List timestamps = response.path("Results[0].Timestamps"); + List values = response.path("Results[0].Values"); + + // The boundary is read from the response rather than the wall clock, so the assertions + // hold even when the suite crosses UTC midnight between the sends and this test. + for (Float timestamp : timestamps) { + assertEquals(0L, timestamp.longValue() % DAY, "bucket is not a whole UTC day"); + } + assertEquals(3, values.stream().mapToInt(Integer::intValue).sum()); + + // A window ending at the first bucket stops before it. + long firstBucket = timestamps.get(0).longValue(); + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "before", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}]} + """.formatted(firstBucket - DAY, firstBucket)) + .when().post("/v2/email/metrics/batch").then().statusCode(200) + .body("Results[0].Values", empty()); + } + + @Test + @Order(6) + void batchGetMetricData_filtersOnTheIdentityDimension() { + Response response = given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "mine", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"EMAIL_IDENTITY": "example.com"}, + "StartDate": %d, "EndDate": %d}, + {"Id": "theirs", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"EMAIL_IDENTITY": "elsewhere.example"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end(), start(), end())) + .when().post("/v2/email/metrics/batch"); + response.then().statusCode(200) + .body("Results.find { it.Id == 'theirs' }.Values", empty()); + // Summed, since the sends can straddle UTC midnight and land in two buckets. + assertEquals(3, total(response, "mine")); + } + + @Test + @Order(7) + void batchGetMetricData_filtersOnTheConfigurationSetEverySendPathResolves() { + // One send down each of the three paths, all naming the same set. A regression in any one + // of the three assignments shows up here as a count below three. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"ConfigurationSetName\": \"metrics-set\"}") + .when().post("/v2/email/configuration-sets").then().statusCode(200); + + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"FromEmailAddress": "%s", "ConfigurationSetName": "metrics-set", + "Destination": {"ToAddresses": ["simple@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "s"}, + "Body": {"Text": {"Data": "hi"}}}}} + """.formatted(SENDER)) + .when().post("/v2/email/outbound-emails").then().statusCode(200); + + String raw = "From: " + SENDER + "\r\nTo: raw@example.com\r\nSubject: r\r\n\r\nhi\r\n"; + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"FromEmailAddress": "%s", "ConfigurationSetName": "metrics-set", + "Destination": {"ToAddresses": ["raw@example.com"]}, + "Content": {"Raw": {"Data": "%s"}}} + """.formatted(SENDER, Base64.getEncoder() + .encodeToString(raw.getBytes(StandardCharsets.UTF_8)))) + .when().post("/v2/email/outbound-emails").then().statusCode(200); + + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"TemplateName": "metrics-cvet", "FromEmailAddress": "%s", + "TemplateSubject": "verify", "TemplateContent": "

verify

", + "SuccessRedirectionURL": "https://example.com/ok", + "FailureRedirectionURL": "https://example.com/no"} + """.formatted(SENDER)) + .when().post("/v2/email/custom-verification-email-templates").then().statusCode(200); + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"EmailAddress": "cvet@example.com", "TemplateName": "metrics-cvet", + "ConfigurationSetName": "metrics-set"} + """) + .when().post("/v2/email/outbound-custom-verification-emails").then().statusCode(200); + + Response response = given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "set", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"CONFIGURATION_SET": "metrics-set"}, + "StartDate": %d, "EndDate": %d}, + {"Id": "other", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"CONFIGURATION_SET": "no-such-set"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end(), start(), end())) + .when().post("/v2/email/metrics/batch"); + response.then().statusCode(200).body("Results.find { it.Id == 'other' }.Values", empty()); + + assertEquals(3, total(response, "set")); + + // A send naming a blank set resolves to no set, so it must not join any set's count. A + // blank dimension value cannot be used to check that: AWS refuses one outright. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"FromEmailAddress": "%s", "ConfigurationSetName": "", + "Destination": {"ToAddresses": ["blank@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "b"}, + "Body": {"Text": {"Data": "hi"}}}}} + """.formatted(SENDER)) + .when().post("/v2/email/outbound-emails").then().statusCode(200); + + Response after = given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "set", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"CONFIGURATION_SET": "metrics-set"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end())) + .when().post("/v2/email/metrics/batch"); + after.then().statusCode(200); + assertEquals(3, total(after, "set")); + } + + @Test + @Order(8) + void batchGetMetricData_filtersOnTheTenantTheSendNamed() { + // Without a send that actually names a tenant, dropping the setTenantName assignments + // would leave every positive tenant metric at zero and the suite green. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"TenantName\": \"metrics-tenant\"}") + .when().post("/v2/email/tenants").then().statusCode(200); + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"TenantName": "metrics-tenant", + "ResourceArn": "arn:aws:ses:sa-east-1:000000000000:identity/%s"} + """.formatted(SENDER)) + .when().post("/v2/email/tenants/resources").then().statusCode(200); + + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"FromEmailAddress": "%s", "TenantName": "metrics-tenant", + "Destination": {"ToAddresses": ["tenant@example.com"]}, + "Content": {"Simple": {"Subject": {"Data": "t"}, + "Body": {"Text": {"Data": "hi"}}}}} + """.formatted(SENDER)) + .when().post("/v2/email/outbound-emails").then().statusCode(200); + + // The raw path stores the tenant too, and only a raw send covers its own assignment. + String raw = "From: " + SENDER + "\r\nTo: tenant-raw@example.com\r\nSubject: t\r\n\r\nhi\r\n"; + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"FromEmailAddress": "%s", "TenantName": "metrics-tenant", + "Destination": {"ToAddresses": ["tenant-raw@example.com"]}, + "Content": {"Raw": {"Data": "%s"}}} + """.formatted(SENDER, Base64.getEncoder() + .encodeToString(raw.getBytes(StandardCharsets.UTF_8)))) + .when().post("/v2/email/outbound-emails").then().statusCode(200); + + Response response = given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "mine", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"TENANT_NAME": "metrics-tenant"}, + "StartDate": %d, "EndDate": %d}, + {"Id": "theirs", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"TENANT_NAME": "another-tenant"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end(), start(), end())) + .when().post("/v2/email/metrics/batch"); + response.then().statusCode(200) + .body("Results.find { it.Id == 'theirs' }.Values", empty()); + assertEquals(2, total(response, "mine")); + } + + @Test + @Order(9) + void batchGetMetricData_validatesInTheProbedOrder() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"Queries\": []}") + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("1 validation error detected: Value at 'queries' failed " + + "to satisfy constraint: Member must have length greater than or equal to 1")); + + StringBuilder eleven = new StringBuilder(query("q0", "SEND")); + for (int i = 1; i < 11; i++) { + eleven.append(",").append(query("q" + i, "SEND")); + } + post(eleven.toString()).then().statusCode(400) + .body("message", containsString("Member must have length less than or equal to 10")); + + // A duplicate id outranks both the reversed dates and the interval cap. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "same", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}, + {"Id": "same", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}]} + """.formatted(end(), start(), end(), start())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("All queries must have a unique identifier.")); + + // TENANT_NAME is a valid dimension on AWS even though the pinned SDK model omits it, so it + // must not be refused as unsupported. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "tenant", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"TENANT_NAME": "no-such-tenant"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end())) + .when().post("/v2/email/metrics/batch").then().statusCode(200) + .body("Results[0].Values", empty()); + + // An unknown dimension name outranks the member constraints. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "d", "Namespace": "VDM", "Metric": "NOT_A_METRIC", + "Dimensions": {"NOT_A_DIMENSION": "x"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("Unsupported dimension provided.")); + + // Member constraints aggregate into one message, metric first, then namespace, then id. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "", "Namespace": "NOT_VDM", "Metric": "NOT_A_METRIC", + "StartDate": %d, "EndDate": %d}]} + """.formatted(end(), start())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", containsString("3 validation errors detected:")) + .body("message", containsString("'queries.1.member.metric'")) + .body("message", containsString("'queries.1.member.namespace'")) + .body("message", containsString("'queries.1.member.id'")); + + // Each dimension validates its own value, with its own message; all probe-confirmed. + badValue("EMAIL_IDENTITY", "", "Invalid email identity provided."); + badValue("EMAIL_IDENTITY", "*", "Invalid email identity provided."); + badValue("EMAIL_IDENTITY", "user@", "Invalid email identity provided."); + badValue("ISP", "", "Invalid ISP name provided."); + badValue("ISP", "Gmail.com", "Invalid ISP name provided."); + badValue("CONFIGURATION_SET", "", "The configuration set name must be specified."); + badValue("CONFIGURATION_SET", "has space", "Invalid configuration set name : " + + "only alphanumeric ASCII characters, '_', and '-' are allowed."); + badValue("TENANT_NAME", "", "TenantName cannot be empty"); + badValue("TENANT_NAME", "*", "Invalid tenant name <*>: " + + "only alphanumeric ASCII characters, '_', and '-' are allowed."); + // The shapes the probe accepted still pass. + goodValue("EMAIL_IDENTITY", "UPPER@Example.COM"); + goodValue("ISP", "Not-An_Isp"); + + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "same-instant", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), start())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("Expected start date to be before the end date.")); + + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "wide", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}]} + """.formatted(end() - 61 * DAY, end())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("Invalid interval, can request at most 60 days. " + + "Please partition your requested interval.")); + + // Four keys became reachable when TENANT_NAME was added; the model caps the map at three. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "four", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"ISP": "UNKNOWN_ISP", "EMAIL_IDENTITY": "example.com", + "CONFIGURATION_SET": "s", "TENANT_NAME": "t"}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("1 validation error detected: Value at " + + "'queries.1.member.dimensions' failed to satisfy constraint: " + + "Member must have length less than or equal to 3")); + + // Probed request-wide: a bad dimension value in any query outranks a bad date in any other. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "dates", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}, + {"Id": "value", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": {"EMAIL_IDENTITY": ""}, + "StartDate": %d, "EndDate": %d}]} + """.formatted(end(), start(), start(), end())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("Invalid email identity provided.")); + + // A timestamp outside Instant's range is the caller's problem, not a server error. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "huge", "Namespace": "VDM", "Metric": "SEND", + "StartDate": 1, "EndDate": 999999999999999999}]} + """) + .when().post("/v2/email/metrics/batch").then().statusCode(400); + + // A window that parses but sits within a day of Instant's upper bound must still answer, + // rather than overflowing while the aggregation window is widened. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "edge", "Namespace": "VDM", "Metric": "SEND", + "StartDate": "+1000000000-12-01T00:00:00Z", + "EndDate": "+1000000000-12-31T00:00:00Z"}]} + """) + .when().post("/v2/email/metrics/batch").then().statusCode(200) + .body("Results[0].Values", empty()); + + // A list member that is not an array is a deserialization failure too, not an empty list. + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"Queries\": {}}") + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("Start of structure or map found where not expected.")); + + // A map member that is not an object is a deserialization failure, not an absent filter. + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "shape", "Namespace": "VDM", "Metric": "SEND", + "Dimensions": [], + "StartDate": %d, "EndDate": %d}]} + """.formatted(start(), end())) + .when().post("/v2/email/metrics/batch").then().statusCode(400) + .body("message", equalTo("Start of list found where not expected")); + + // Sixty days exactly is accepted. Both ends come from one reading of the clock, so a + // second boundary between two calls cannot turn this into sixty days and one second. + long capEnd = end(); + given().contentType("application/json").header("Authorization", AUTH) + .body(""" + {"Queries": [{"Id": "cap", "Namespace": "VDM", "Metric": "SEND", + "StartDate": %d, "EndDate": %d}]} + """.formatted(capEnd - 60 * DAY, capEnd)) + .when().post("/v2/email/metrics/batch").then().statusCode(200); + } + + @Test + @Order(10) + void cleanUpVdmAndIdentity() { + given().contentType("application/json").header("Authorization", AUTH) + .body("{\"VdmAttributes\":{\"VdmEnabled\":\"DISABLED\"}}") + .when().put("/v2/email/account/vdm"); + given().header("Authorization", AUTH) + .when().delete("/v2/email/identities/" + SENDER); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java index 4f5acdf5f7..2070f34b0c 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceListManagementTest.java @@ -65,7 +65,7 @@ class SesServiceListManagementTest { private void send(List to, String topicName) { service.sendEmail(FROM, to, null, null, null, null, "Subject", "body", null, - null, List.of(), List.of(), new ListManagementOptions(LIST, topicName), REGION); + null, List.of(), List.of(), new ListManagementOptions(LIST, topicName), null, REGION); } private SmtpRelay.RelayMessage capturedRelay() { @@ -124,7 +124,7 @@ class SesServiceListManagementTest { AwsException ex = assertThrows(AwsException.class, () -> service.sendEmail(FROM, List.of("sportsin@example.com"), null, null, null, null, "Subject", "body", null, null, List.of(), List.of(), - new ListManagementOptions("ghost-list", null), REGION)); + new ListManagementOptions("ghost-list", null), null, REGION)); assertEquals(404, ex.getHttpStatus()); } @@ -134,7 +134,7 @@ class SesServiceListManagementTest { AwsException ex = assertThrows(AwsException.class, () -> service.sendEmail(FROM, List.of("sportsin@example.com"), null, null, null, null, "Subject", "body", null, null, List.of(), List.of(), - new ListManagementOptions(LIST, "GhostTopic"), REGION)); + new ListManagementOptions(LIST, "GhostTopic"), null, REGION)); assertEquals(400, ex.getHttpStatus()); } @@ -143,7 +143,7 @@ class SesServiceListManagementTest { // Without ListManagementOptions the contact list is never consulted: an unsubscribed contact // is not suppressed and no contact is auto-created. service.sendEmail(FROM, List.of("unsub@example.com"), null, null, null, null, - "Subject", "body", null, null, List.of(), List.of(), null, REGION); + "Subject", "body", null, null, List.of(), List.of(), null, null, REGION); assertEquals(List.of("unsub@example.com"), capturedRelayTo()); } @@ -152,7 +152,7 @@ class SesServiceListManagementTest { // newbie is not a contact (auto-created, Sports defaults OPT_IN) so the send reaches the relay. service.sendEmail(FROM, List.of("newbie@example.com"), null, null, null, null, "Subject", "text", "

Unsub: {{amazonSESUnsubscribeUrl}}

", - null, List.of(), List.of(), new ListManagementOptions(LIST, "Sports"), REGION); + null, List.of(), List.of(), new ListManagementOptions(LIST, "Sports"), null, REGION); String html = capturedRelayBodyHtml(); assertTrue(html.contains("http://localhost:4566/_aws/ses/unsubscribe?"), html); assertTrue(html.contains("contactList=" + LIST), html); @@ -164,7 +164,7 @@ class SesServiceListManagementTest { void multiRecipient_doesNotReplacePlaceholder() { service.sendEmail(FROM, List.of("newbie@example.com", "sportsin@example.com"), null, null, null, null, "Subject", "text", "

Unsub: {{amazonSESUnsubscribeUrl}}

", - null, List.of(), List.of(), new ListManagementOptions(LIST, "Sports"), REGION); + null, List.of(), List.of(), new ListManagementOptions(LIST, "Sports"), null, REGION); assertTrue(capturedRelayBodyHtml().contains("{{amazonSESUnsubscribeUrl}}"), "multi-recipient send must not inject the unsubscribe link"); } @@ -217,7 +217,7 @@ class SesServiceListManagementTest { void singleRecipient_addsListUnsubscribeHeadersToRelay() { service.sendEmail(FROM, List.of("newbie@example.com"), null, null, null, null, "Subject", "text", "

x

", null, List.of(), List.of(), - new ListManagementOptions(LIST, "Sports"), REGION); + new ListManagementOptions(LIST, "Sports"), null, REGION); List headers = capturedRelayHeaders(); assertTrue(headers.stream().anyMatch(h -> "List-Unsubscribe".equals(h.name()) && h.value().contains("/_aws/ses/unsubscribe")), "List-Unsubscribe header must reach the relay"); @@ -230,7 +230,7 @@ class SesServiceListManagementTest { service.sendEmail(FROM, List.of("newbie@example.com"), null, null, null, null, "Subject", "text", "

x

", null, List.of(), List.of(new MessageHeader("List-Unsubscribe", "")), - new ListManagementOptions(LIST, "Sports"), REGION); + new ListManagementOptions(LIST, "Sports"), null, REGION); long count = capturedRelayHeaders().stream() .filter(h -> "List-Unsubscribe".equalsIgnoreCase(h.name())).count(); assertEquals(1L, count); diff --git a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java index 2374db7ffb..db730da624 100644 --- a/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/ses/SesServiceSmtpTest.java @@ -129,7 +129,7 @@ class SesServiceSmtpTest { List.of("bcc@example.com"), List.of("reply@example.com"), null, - "Subject", "text body", "

html

", null, List.of(), List.of(), null, "us-east-1"); + "Subject", "text body", "

html

", null, List.of(), List.of(), null, null, "us-east-1"); SmtpRelay.RelayMessage relayed = capturedRelay(); assertEquals("from@example.com", relayed.from()); @@ -148,7 +148,7 @@ class SesServiceSmtpTest { void sendEmail_storesAndRelays() { String messageId = service.sendEmail("from@example.com", List.of("to@example.com"), null, null, null, null, - "Subject", "text", null, null, List.of(), List.of(), null, "us-east-1"); + "Subject", "text", null, null, List.of(), List.of(), null, null, "us-east-1"); assertNotNull(messageId); assertFalse(emailStore.scan(k -> true).isEmpty()); @@ -159,7 +159,7 @@ class SesServiceSmtpTest { void sendEmail_noReturnPath_fallsBackToSource() { service.sendEmail("from@example.com", List.of("to@example.com"), null, null, null, null, - "Subject", "text", null, null, List.of(), List.of(), null, "us-east-1"); + "Subject", "text", null, null, List.of(), List.of(), null, null, "us-east-1"); assertEquals("from@example.com", capturedRelay().returnPath()); } @@ -168,7 +168,7 @@ class SesServiceSmtpTest { void sendEmail_explicitReturnPath_isRelayedAndStored() { String messageId = service.sendEmail("from@example.com", List.of("to@example.com"), null, null, null, "bounces@example.com", - "Subject", "text", null, null, List.of(), List.of(), null, "us-east-1"); + "Subject", "text", null, null, List.of(), List.of(), null, null, "us-east-1"); assertEquals("bounces@example.com", capturedRelay().returnPath()); assertEquals("bounces@example.com", storedEmail(messageId).getReturnPath()); @@ -182,7 +182,7 @@ class SesServiceSmtpTest { + "Subject: " + SesContentScan.signature() + "\r\n\r\nbody\r\n"; String messageId = service.sendRawEmail("from@example.com", List.of("to@example.com"), raw, - "bounces@example.com", null, List.of(), null, "us-east-1"); + "bounces@example.com", null, List.of(), null, null, "us-east-1"); assertEquals("bounces@example.com", storedEmail(messageId).getReturnPath()); assertEquals("Bad content", storedEmail(messageId).getRejectReason()); @@ -196,7 +196,7 @@ class SesServiceSmtpTest { String raw = "Content-Type: message/rfc822\r\n\r\n".repeat(100_000) + "Subject: deep\r\n\r\nx\r\n"; AwsException e = assertThrows(AwsException.class, () -> service.sendRawEmail("from@example.com", - List.of("to@example.com"), raw, null, null, List.of(), null, "us-east-1")); + List.of("to@example.com"), raw, null, null, List.of(), null, null, "us-east-1")); assertEquals(400, e.getHttpStatus()); assertTrue(sentEmails.listAll().isEmpty()); @@ -206,7 +206,7 @@ class SesServiceSmtpTest { @Test void sendRawEmail_callsRelayRaw() { String messageId = service.sendRawEmail("from@example.com", - List.of("to@example.com"), "raw MIME", null, null, List.of(), null, "us-east-1"); + List.of("to@example.com"), "raw MIME", null, null, List.of(), null, null, "us-east-1"); SmtpRelay.RawRelayMessage relayed = capturedRawRelay(); assertEquals("from@example.com", relayed.from()); @@ -223,7 +223,7 @@ class SesServiceSmtpTest { + "Subject: x\r\n\r\nbody"; String messageId = service.sendRawEmail("from@example.com", List.of("to@example.com"), raw, - "request-bounces@example.com", null, List.of(), null, "us-east-1"); + "request-bounces@example.com", null, List.of(), null, null, "us-east-1"); assertEquals("mime-bounces@example.com", capturedRawRelay().returnPath()); assertEquals("mime-bounces@example.com", storedEmail(messageId).getReturnPath()); @@ -234,7 +234,7 @@ class SesServiceSmtpTest { String raw = "From: from@example.com\r\nTo: to@example.com\r\nSubject: x\r\n\r\nbody"; service.sendRawEmail("from@example.com", List.of("to@example.com"), raw, - "request-bounces@example.com", null, List.of(), null, "us-east-1"); + "request-bounces@example.com", null, List.of(), null, null, "us-east-1"); assertEquals("request-bounces@example.com", capturedRawRelay().returnPath()); } @@ -242,7 +242,7 @@ class SesServiceSmtpTest { @Test void sendRawEmail_storesAndRelays() { String messageId = service.sendRawEmail("from@example.com", - List.of("to@example.com"), "raw", null, null, List.of(), null, "us-east-1"); + List.of("to@example.com"), "raw", null, null, List.of(), null, null, "us-east-1"); assertNotNull(messageId); assertFalse(emailStore.scan(k -> true).isEmpty()); @@ -254,7 +254,7 @@ class SesServiceSmtpTest { service.sendEmail("from@example.com", List.of("to@example.com"), null, null, null, null, - "Subject", null, "

html only

", null, List.of(), List.of(), null, "us-east-1"); + "Subject", null, "

html only

", null, List.of(), List.of(), null, null, "us-east-1"); SmtpRelay.RelayMessage relayed = capturedRelay(); assertEquals(List.of("to@example.com"), relayed.to()); @@ -274,7 +274,7 @@ class SesServiceSmtpTest { List.of("to@example.com"), List.of("cc@example.com"), null, null, null, - "Subject", "text body", null, null, List.of(), List.of(), null, "us-east-1"); + "Subject", "text body", null, null, List.of(), List.of(), null, null, "us-east-1"); assertNotNull(messageId); assertFalse(emailStore.scan(k -> true).isEmpty(), @@ -291,7 +291,7 @@ class SesServiceSmtpTest { List.of("to@example.com", "suppressed@example.com"), List.of("cc-keep@example.com"), null, null, null, - "Subject", "text body", null, null, List.of(), List.of(), null, "us-east-1"); + "Subject", "text body", null, null, List.of(), List.of(), null, null, "us-east-1"); SmtpRelay.RelayMessage relayed = capturedRelay(); assertEquals(List.of("to@example.com"), relayed.to()); @@ -303,7 +303,7 @@ class SesServiceSmtpTest { suppression.putSuppressedDestination("us-east-1", "to@example.com", "BOUNCE"); String messageId = service.sendRawEmail("from@example.com", - List.of("to@example.com"), "raw MIME", null, null, List.of(), null, "us-east-1"); + List.of("to@example.com"), "raw MIME", null, null, List.of(), null, null, "us-east-1"); assertNotNull(messageId); assertFalse(emailStore.scan(k -> true).isEmpty()); @@ -316,7 +316,7 @@ class SesServiceSmtpTest { service.sendRawEmail("from@example.com", List.of("to@example.com", "suppressed@example.com"), - "raw MIME", null, null, List.of(), null, "us-east-1"); + "raw MIME", null, null, List.of(), null, null, "us-east-1"); assertEquals(List.of("to@example.com"), capturedRawRelay().destinations()); } @@ -335,7 +335,7 @@ class SesServiceSmtpTest { service.sendEmail("from@example.com", List.of("to@example.com"), null, null, null, null, - "Subject", "text body", null, "cs-no-suppression", List.of(), List.of(), null, "us-east-1"); + "Subject", "text body", null, "cs-no-suppression", List.of(), List.of(), null, null, "us-east-1"); assertEquals(List.of("to@example.com"), capturedRelay().to()); } @@ -351,7 +351,7 @@ class SesServiceSmtpTest { service.sendEmail("from@example.com", List.of("complainer@example.com"), null, null, null, null, - "Subject", "text body", null, "cs-bounce-only", List.of(), List.of(), null, "us-east-1"); + "Subject", "text body", null, "cs-bounce-only", List.of(), List.of(), null, null, "us-east-1"); assertEquals(List.of("complainer@example.com"), capturedRelay().to()); } @@ -366,7 +366,7 @@ class SesServiceSmtpTest { service.sendEmail("from@example.com", List.of("to@example.com"), null, null, null, null, - "Subject", "text body", null, "cs-default", List.of(), List.of(), null, "us-east-1"); + "Subject", "text body", null, "cs-default", List.of(), List.of(), null, null, "us-east-1"); verify(smtpRelay, never()).relay(any(SmtpRelay.RelayMessage.class)); } @@ -378,7 +378,7 @@ class SesServiceSmtpTest { configSets.putSuppressionOptions("cs-no-suppression-raw", List.of(), "us-east-1"); service.sendRawEmail("from@example.com", - List.of("to@example.com"), "raw MIME", null, "cs-no-suppression-raw", List.of(), null, + List.of("to@example.com"), "raw MIME", null, "cs-no-suppression-raw", List.of(), null, null, "us-east-1"); assertEquals(List.of("to@example.com"), capturedRawRelay().destinations());