Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import google.registry.util.Clock;
import jakarta.inject.Inject;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

/**
Expand All @@ -56,7 +57,9 @@ public class CheckBulkComplianceAction implements Runnable {
private final String bulkPricingPackageDomainLimitWarningEmailBody;
private final String bulkPricingPackageDomainLimitUpgradeEmailBody;
private final String registrySupportEmail;
// Registrars receive a 30-day cure window after an active-domain overage warning before upgrade.
private static final int THIRTY_DAYS = 30;
// Notifications 40+ days old indicate the prior 30-39 day upgrade window passed without overage.
private static final int FORTY_DAYS = 40;

@Inject
Expand Down Expand Up @@ -182,18 +185,26 @@ private void handleActiveDomainOverage(ImmutableMap<BulkPricingPackage, Long> ov
logger.atInfo().log(
"Found %d bulk pricing packages over their active domains limit.", overageList.size());
for (BulkPricingPackage bulkPricingPackage : overageList.keySet()) {
int daysSinceLastNotification =
bulkPricingPackage
.getLastNotificationSent()
.map(sentDate -> (int) Duration.between(sentDate, clock.now()).toDays())
.orElse(Integer.MAX_VALUE);
// Send a warning email if 30-39 days since last notification and an upgrade email if 40+ days
if (daysSinceLastNotification >= THIRTY_DAYS) {
sendActiveDomainOverageEmail(
/* warning= */ daysSinceLastNotification >= FORTY_DAYS,
bulkPricingPackage,
overageList.get(bulkPricingPackage));
long activeDomains = overageList.get(bulkPricingPackage);
Optional<Instant> lastNotificationSent = bulkPricingPackage.getLastNotificationSent();
if (lastNotificationSent.isEmpty()) {
// First active-domain overage for this package: send initial 30-day cure warning.
sendActiveDomainOverageEmail(/* warning= */ true, bulkPricingPackage, activeDomains);
continue;
}
long daysSinceLastNotification =
Duration.between(lastNotificationSent.get(), clock.now()).toDays();
if (daysSinceLastNotification < THIRTY_DAYS) {
// Still within the 30-day cure window after a warning; do not send another email yet.
continue;
}
// Because lastNotificationSent is not cleared when a package returns below maxDomains,
// an elapsed interval of 30-39 days means the warning went uncured and requires an upgrade
// notice, whereas 40+ days means the prior violation was cured during days 30-39 and this
// is a new overage episode warranting a fresh 30-day warning.
boolean isNewViolationEpisode = daysSinceLastNotification >= FORTY_DAYS;
sendActiveDomainOverageEmail(
/* warning= */ isNewViolationEpisode, bulkPricingPackage, activeDomains);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
/** Unit tests for {@link CheckBulkComplianceAction}. */
public class CheckBulkComplianceActionTest {
// This is the default creation time for test data.
private final FakeClock clock = new FakeClock(Instant.parse("2012-03-25T00:00:00Z"));
private static final Instant START_TIME = Instant.parse("2012-03-25T00:00:00Z");
private final FakeClock clock = new FakeClock(START_TIME);
private static final String CREATE_LIMIT_EMAIL_SUBJECT = "create limit subject";
private static final String DOMAIN_LIMIT_WARNING_EMAIL_SUBJECT = "domain limit warning subject";
private static final String DOMAIN_LIMIT_UPGRADE_EMAIL_SUBJECT = "domain limit upgrade subject";
Expand Down Expand Up @@ -580,4 +581,172 @@ void testSuccess_multipleBulkPricingPackagesOverActiveDomainsLimit() {
tm().transact(() -> BulkPricingPackage.loadByTokenString(token.getToken()).get());
assertThat(packageAfterCheck.getLastNotificationSent().get()).isEqualTo(clock.now());
}

@Test
void testSuccess_activeDomainOverage_neverNotified_sendsWarningEmail() throws Exception {
bulkPricingPackage =
bulkPricingPackage
.asBuilder()
.setMaxCreates(4)
.setMaxDomains(1)
.setLastNotificationSent(null)
.build();
tm().transact(() -> tm().put(bulkPricingPackage));
persistEppResource(
DatabaseHelper.newDomain("foo.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());
persistEppResource(
DatabaseHelper.newDomain("buzz.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());

action.run();

verify(gmailClient).sendEmail(emailCaptor.capture());
EmailMessage emailMessage = emailCaptor.getValue();
assertThat(emailMessage.subject()).isEqualTo(DOMAIN_LIMIT_WARNING_EMAIL_SUBJECT);
assertThat(emailMessage.body())
.isEqualTo(
String.format(DOMAIN_LIMIT_WARNING_EMAIL_BODY, 1, "abc123", "The Registrar", 1, 2));
BulkPricingPackage packageAfterCheck =
tm().transact(() -> BulkPricingPackage.loadByTokenString(token.getToken()).get());
assertThat(packageAfterCheck.getLastNotificationSent()).hasValue(START_TIME);
}

@Test
void testSuccess_activeDomainOverage_notified29DaysAgo_doesNotSendEmail() {
Instant twentyNineDaysAgo = minusDays(START_TIME, 29);
bulkPricingPackage =
bulkPricingPackage
.asBuilder()
.setMaxCreates(4)
.setMaxDomains(1)
.setLastNotificationSent(twentyNineDaysAgo)
.build();
tm().transact(() -> tm().put(bulkPricingPackage));
persistEppResource(
DatabaseHelper.newDomain("foo.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());
persistEppResource(
DatabaseHelper.newDomain("buzz.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());

action.run();

verifyNoInteractions(gmailClient);
BulkPricingPackage packageAfterCheck =
tm().transact(() -> BulkPricingPackage.loadByTokenString(token.getToken()).get());
assertThat(packageAfterCheck.getLastNotificationSent()).hasValue(twentyNineDaysAgo);
}

@Test
void testSuccess_activeDomainOverage_notifiedExactly30DaysAgo_sendsUpgradeEmail()
throws Exception {
bulkPricingPackage =
bulkPricingPackage
.asBuilder()
.setMaxCreates(4)
.setMaxDomains(1)
.setLastNotificationSent(minusDays(START_TIME, 30))
.build();
tm().transact(() -> tm().put(bulkPricingPackage));
persistEppResource(
DatabaseHelper.newDomain("foo.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());
persistEppResource(
DatabaseHelper.newDomain("buzz.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());

action.run();

verify(gmailClient).sendEmail(emailCaptor.capture());
EmailMessage emailMessage = emailCaptor.getValue();
assertThat(emailMessage.subject()).isEqualTo(DOMAIN_LIMIT_UPGRADE_EMAIL_SUBJECT);
assertThat(emailMessage.body())
.isEqualTo(
String.format(DOMAIN_LIMIT_UPGRADE_EMAIL_BODY, 1, "abc123", "The Registrar", 1, 2));
BulkPricingPackage packageAfterCheck =
tm().transact(() -> BulkPricingPackage.loadByTokenString(token.getToken()).get());
assertThat(packageAfterCheck.getLastNotificationSent()).hasValue(START_TIME);
}

@Test
void testSuccess_activeDomainOverage_notified39DaysAgo_sendsUpgradeEmail() throws Exception {
bulkPricingPackage =
bulkPricingPackage
.asBuilder()
.setMaxCreates(4)
.setMaxDomains(1)
.setLastNotificationSent(minusDays(START_TIME, 39))
.build();
tm().transact(() -> tm().put(bulkPricingPackage));
persistEppResource(
DatabaseHelper.newDomain("foo.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());
persistEppResource(
DatabaseHelper.newDomain("buzz.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());

action.run();

verify(gmailClient).sendEmail(emailCaptor.capture());
EmailMessage emailMessage = emailCaptor.getValue();
assertThat(emailMessage.subject()).isEqualTo(DOMAIN_LIMIT_UPGRADE_EMAIL_SUBJECT);
assertThat(emailMessage.body())
.isEqualTo(
String.format(DOMAIN_LIMIT_UPGRADE_EMAIL_BODY, 1, "abc123", "The Registrar", 1, 2));
BulkPricingPackage packageAfterCheck =
tm().transact(() -> BulkPricingPackage.loadByTokenString(token.getToken()).get());
assertThat(packageAfterCheck.getLastNotificationSent()).hasValue(START_TIME);
}

@Test
void testSuccess_activeDomainOverage_notifiedExactly40DaysAgo_sendsWarningEmail()
throws Exception {
bulkPricingPackage =
bulkPricingPackage
.asBuilder()
.setMaxCreates(4)
.setMaxDomains(1)
.setLastNotificationSent(minusDays(START_TIME, 40))
.build();
tm().transact(() -> tm().put(bulkPricingPackage));
persistEppResource(
DatabaseHelper.newDomain("foo.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());
persistEppResource(
DatabaseHelper.newDomain("buzz.tld")
.asBuilder()
.setCurrentBulkToken(token.createVKey())
.build());

action.run();

verify(gmailClient).sendEmail(emailCaptor.capture());
EmailMessage emailMessage = emailCaptor.getValue();
assertThat(emailMessage.subject()).isEqualTo(DOMAIN_LIMIT_WARNING_EMAIL_SUBJECT);
assertThat(emailMessage.body())
.isEqualTo(
String.format(DOMAIN_LIMIT_WARNING_EMAIL_BODY, 1, "abc123", "The Registrar", 1, 2));
BulkPricingPackage packageAfterCheck =
tm().transact(() -> BulkPricingPackage.loadByTokenString(token.getToken()).get());
assertThat(packageAfterCheck.getLastNotificationSent()).hasValue(START_TIME);
}
}
Loading