refactor(ses): extract the identity domain into SesIdentityService (#2984)

* refactor(ses): extract the identity domain into SesIdentityService

* fix(ses): persist identity mutations under the request-derived key
This commit is contained in:
Kenshin Okinaka authored and GitHub committed 2026-09-03 15:22:24 -04:00
1 parent ad0f4a1613
commit 8440e201e1
5 files changed
+850 -591

No files matched your search

@@ -0,0 +1,149 @@
package io.github.hectorvent.floci.services.ses;
import io.github.hectorvent.floci.core.common.AwsException;
import io.github.hectorvent.floci.core.storage.InMemoryStorage;
import io.github.hectorvent.floci.services.ses.model.Identity;
import io.github.hectorvent.floci.services.ses.model.Tag;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.time.Clock;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
/**
* Unit test for the extracted identity domain: store ownership, key validation, verification
* (email and domain), listing, MAIL FROM, the CVET pending registration, identity tags, and the
* find/save escape hatches the facade's notification flows rely on. The DKIM state machine moved
* here too and keeps its behavioral coverage in the existing facade-level unit and integration
* tests, which now exercise it through the delegation; only its creation-time surface is pinned
* here.
*/
class SesIdentityServiceTest {
private static final String REGION = "us-east-1";
private SesIdentityService service;
@BeforeEach
void setUp() {
service = new SesIdentityService(new InMemoryStorage<>(), null, Clock.systemUTC());
}
@Test
void verifyEmailIdentity_createsOnce_returnsExistingOnRepeat() {
Identity first = service.verifyEmailIdentity("alice@example.com", REGION);
assertEquals("EmailAddress", first.getIdentityType());
first.setVerificationStatus("Success");
service.save(first, REGION);
Identity second = service.verifyEmailIdentity("alice@example.com", REGION);
assertEquals("Success", second.getVerificationStatus());
}
@Test
void verifyEmailIdentity_rejectsBlankAndWhitespace() {
AwsException blank = assertThrows(AwsException.class,
() -> service.verifyEmailIdentity(" ", REGION));
assertEquals("Email address is required.", blank.getMessage());
AwsException padded = assertThrows(AwsException.class,
() -> service.verifyEmailIdentity(" alice@example.com", REGION));
assertEquals("Email address must not contain leading or trailing whitespace.",
padded.getMessage());
}
@Test
void verifyDomainIdentity_generatesDkimTokens_reportsNotStarted() {
Identity domain = service.verifyDomainIdentity("example.com", REGION);
assertEquals("Domain", domain.getIdentityType());
assertEquals("Pending", domain.getVerificationStatus());
assertEquals("NotStarted", domain.getDkimVerificationStatus());
assertEquals(3, domain.getDkimTokens().size());
// Tokens are stable across repeated calls.
assertEquals(domain.getDkimTokens(),
service.verifyDomainIdentity("example.com", REGION).getDkimTokens());
assertEquals(domain.getDkimTokens(), service.verifyDomainDkim("example.com", REGION));
}
@Test
void listIdentities_filtersByType_andRegion() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.save(new Identity("example.com", "Domain"), REGION);
service.verifyEmailIdentity("other@example.com", "eu-west-1");
assertEquals(2, service.listIdentities(null, REGION).size());
assertEquals(List.of("example.com"),
service.listIdentities("Domain", REGION).stream().map(Identity::getIdentity).toList());
}
@Test
void getVerifiedEmailAddresses_onlySuccessfulEmails() {
Identity verified = service.verifyEmailIdentity("alice@example.com", REGION);
verified.setVerificationStatus("Success");
service.save(verified, REGION);
service.markPendingEmailIdentity("pending@example.com", REGION);
Identity domain = new Identity("example.com", "Domain");
domain.setVerificationStatus("Success");
service.save(domain, REGION);
assertEquals(List.of("alice@example.com"), service.getVerifiedEmailAddresses(REGION));
}
@Test
void delete_removesTheRecord() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.delete("alice@example.com", REGION);
assertTrue(service.find("alice@example.com", REGION).isEmpty());
}
@Test
void setMailFromDomain_setClearAndValidate() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.setMailFromDomain("alice@example.com", "mail.example.com", "RejectMessage", REGION);
Identity attrs = service.getMailFromAttributes("alice@example.com", REGION);
assertEquals("mail.example.com", attrs.getMailFromDomain());
assertEquals("Success", attrs.getMailFromDomainStatus());
assertEquals("RejectMessage", attrs.getBehaviorOnMxFailure());
service.setMailFromDomain("alice@example.com", "", null, REGION);
attrs = service.getMailFromAttributes("alice@example.com", REGION);
assertEquals(null, attrs.getMailFromDomain());
assertEquals("UseDefaultValue", attrs.getBehaviorOnMxFailure());
assertThrows(AwsException.class, () -> service.setMailFromDomain(
"alice@example.com", "mail.example.com", "BadEnum", REGION));
AwsException missing = assertThrows(AwsException.class, () -> service.setMailFromDomain(
"ghost@example.com", "mail.example.com", null, REGION));
assertEquals("Identity <ghost@example.com> does not exist.", missing.getMessage());
}
@Test
void markPendingEmailIdentity_registersOnce_neverDowngrades() {
Identity verified = service.verifyEmailIdentity("alice@example.com", REGION);
verified.setVerificationStatus("Success");
service.save(verified, REGION);
service.markPendingEmailIdentity("alice@example.com", REGION);
assertEquals("Success",
service.find("alice@example.com", REGION).orElseThrow().getVerificationStatus());
service.markPendingEmailIdentity("new@example.com", REGION);
assertEquals("Pending",
service.find("new@example.com", REGION).orElseThrow().getVerificationStatus());
}
@Test
void tags_lifecycle_andNotFoundMessage() {
service.verifyEmailIdentity("alice@example.com", REGION);
service.tag("alice@example.com", REGION, List.of(new Tag("team", "floci")));
assertEquals(1, service.listTags("alice@example.com", REGION).size());
service.setTags("alice@example.com", REGION,
List.of(new Tag("team", "floci"), new Tag("env", "dev")));
service.untag("alice@example.com", REGION, List.of("team"));
assertEquals(List.of("env"),
service.listTags("alice@example.com", REGION).stream().map(Tag::key).toList());
AwsException e = assertThrows(AwsException.class,
() -> service.listTags("ghost@example.com", REGION));
assertEquals("No EmailIdentity present with name: ghost@example.com", e.getMessage());
}
}
@@ -53,9 +53,9 @@ final class SesServiceTestBuilder {
new InMemoryStorage<>();
private SmtpRelay smtpRelay = mock(SmtpRelay.class);
// Default null, matching the production null-Route53 case: SesService treats a null Route53Service
// as "DNS lookup disabled" for DKIM record checks. Tests that exercise the lookup opt in via
// route53Service(...).
// Default null, matching the production null-Route53 case: SesIdentityService treats a null
// Route53Service as "DNS lookup disabled" for DKIM record checks. Tests that exercise the lookup
// opt in via route53Service(...).
private Route53Service route53Service = null;
private ObjectMapper objectMapper = new ObjectMapper();
private Clock clock = Clock.systemUTC();
@@ -116,7 +116,7 @@ final class SesServiceTestBuilder {
SesService build() {
return new SesService(
identityStore,
new SesIdentityService(identityStore, route53Service, clock),
new SesSentEmailService(emailStore),
new SesAccountService(accountSettingsStore, accountVdmStore, accountDetailsStore),
new SesTemplateService(templateStore),
@@ -129,8 +129,6 @@ final class SesServiceTestBuilder {
new SesCvetService(cvetStore),
new SesTenantService(tenantStore, tenantAssociationStore, clock, new SecureRandom()),
smtpRelay,
objectMapper,
route53Service,
clock);
objectMapper);
}
}