Prevent ending package tokens with active domains (#1919)

* Prevent ending package tokens with active domains

* Fix bad formatting in comments

* Fix lots of nits
This commit is contained in:
sarahcaseybot
2023-01-30 16:13:23 -05:00
committed by GitHub
parent 07b87bbb4d
commit 5e081f4692
4 changed files with 132 additions and 2 deletions
@@ -395,6 +395,26 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
.isInstanceOf(IllegalArgumentException.class);
}
@Test
void testFailure_invalidPackageTokenStatusTransition() {
assertThat(
assertThrows(
IllegalArgumentException.class,
() ->
runCommand(
"--number",
"999",
"--type",
"PACKAGE",
String.format(
"--token_status_transitions=\"%s=NOT_STARTED,%s=VALID,%s=ENDED\"",
START_OF_TIME, fakeClock.nowUtc(), fakeClock.nowUtc().plusDays(1)))))
.hasMessageThat()
.isEqualTo(
"PACKAGE tokens should not be generated with ENDED or CANCELLED in their transition"
+ " map");
}
@Test
void testFailure_lengthOfZero() {
IllegalArgumentException thrown =
@@ -22,9 +22,12 @@ import static google.registry.model.domain.token.AllocationToken.TokenStatus.CAN
import static google.registry.model.domain.token.AllocationToken.TokenStatus.ENDED;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.NOT_STARTED;
import static google.registry.model.domain.token.AllocationToken.TokenStatus.VALID;
import static google.registry.model.domain.token.AllocationToken.TokenType.PACKAGE;
import static google.registry.model.domain.token.AllocationToken.TokenType.SINGLE_USE;
import static google.registry.model.domain.token.AllocationToken.TokenType.UNLIMITED_USE;
import static google.registry.testing.DatabaseHelper.createTld;
import static google.registry.testing.DatabaseHelper.loadByEntity;
import static google.registry.testing.DatabaseHelper.persistActiveDomain;
import static google.registry.testing.DatabaseHelper.persistResource;
import static google.registry.util.DateTimeUtils.START_OF_TIME;
import static org.joda.time.DateTimeZone.UTC;
@@ -255,7 +258,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
@Test
void testUpdateStatusTransitions() throws Exception {
DateTime now = DateTime.now(UTC);
DateTime now = fakeClock.nowUtc();
AllocationToken token = persistResource(builderWithPromo().build());
runCommandForced(
"--prefix",
@@ -270,7 +273,7 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
@Test
void testUpdateStatusTransitions_badTransitions() {
DateTime now = DateTime.now(UTC);
DateTime now = fakeClock.nowUtc();
persistResource(builderWithPromo().build());
IllegalArgumentException thrown =
assertThrows(
@@ -288,6 +291,73 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase<UpdateAllocation
.isEqualTo("tokenStatusTransitions map cannot transition from NOT_STARTED to ENDED.");
}
@Test
void testUpdateStatusTransitions_endPackageTokenNoDomains() throws Exception {
DateTime now = fakeClock.nowUtc();
AllocationToken token =
persistResource(
new AllocationToken.Builder()
.setToken("token")
.setTokenType(PACKAGE)
.setRenewalPriceBehavior(SPECIFIED)
.setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar"))
.setTokenStatusTransitions(
ImmutableSortedMap.<DateTime, TokenStatus>naturalOrder()
.put(START_OF_TIME, NOT_STARTED)
.put(now.minusDays(1), VALID)
.build())
.build());
runCommandForced(
"--prefix",
"token",
"--token_status_transitions",
String.format(
"\"%s=NOT_STARTED,%s=VALID,%s=ENDED\"", START_OF_TIME, now.minusDays(1), now));
token = reloadResource(token);
assertThat(token.getTokenStatusTransitions().toValueMap())
.containsExactly(START_OF_TIME, NOT_STARTED, now.minusDays(1), VALID, now, ENDED);
}
@Test
void testUpdateStatusTransitions_endPackageTokenWithActiveDomainsFails() throws Exception {
DateTime now = fakeClock.nowUtc();
AllocationToken token =
persistResource(
new AllocationToken.Builder()
.setToken("token")
.setTokenType(PACKAGE)
.setRenewalPriceBehavior(SPECIFIED)
.setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar"))
.setTokenStatusTransitions(
ImmutableSortedMap.<DateTime, TokenStatus>naturalOrder()
.put(START_OF_TIME, NOT_STARTED)
.put(now.minusDays(1), VALID)
.build())
.build());
createTld("tld");
persistResource(
persistActiveDomain("example.tld")
.asBuilder()
.setCurrentPackageToken(token.createVKey())
.build());
IllegalArgumentException thrown =
assertThrows(
IllegalArgumentException.class,
() ->
runCommandForced(
"--prefix",
"token",
"--token_status_transitions",
String.format(
"\"%s=NOT_STARTED,%s=VALID,%s=ENDED\"",
START_OF_TIME, now.minusDays(1), now)));
assertThat(thrown)
.hasMessageThat()
.isEqualTo(
"Package token token can not end its promotion because it still has 1 domains in the"
+ " package");
}
@Test
void testUpdate_onlyWithPrefix() throws Exception {
AllocationToken token =