diff --git a/core/src/main/java/google/registry/tools/GenerateAllocationTokensCommand.java b/core/src/main/java/google/registry/tools/GenerateAllocationTokensCommand.java index d0c4e673a..631114fc1 100644 --- a/core/src/main/java/google/registry/tools/GenerateAllocationTokensCommand.java +++ b/core/src/main/java/google/registry/tools/GenerateAllocationTokensCommand.java @@ -15,6 +15,7 @@ package google.registry.tools; import static com.google.common.base.Preconditions.checkArgument; +import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.common.collect.ImmutableSet.toImmutableSet; import static com.google.common.collect.Sets.difference; import static google.registry.model.billing.BillingBase.RenewalPriceBehavior.DEFAULT; @@ -46,6 +47,9 @@ import google.registry.model.domain.token.AllocationToken; import google.registry.model.domain.token.AllocationToken.RegistrationBehavior; import google.registry.model.domain.token.AllocationToken.TokenStatus; import google.registry.model.domain.token.AllocationToken.TokenType; +import google.registry.model.registrar.Registrar; +import google.registry.model.tld.Tld; +import google.registry.model.tld.Tlds; import google.registry.persistence.VKey; import google.registry.tools.params.MoneyParameter; import google.registry.tools.params.TransitionListParameter.TokenStatusTransitions; @@ -291,10 +295,12 @@ class GenerateAllocationTokensCommand implements Command { !ImmutableList.of("").equals(allowedClientIds), "Either omit --allowed_client_ids if all registrars are allowed, or include a" + " comma-separated list"); + verifyAllRegistrarIdsExist(allowedClientIds); checkArgument( !ImmutableList.of("").equals(allowedTlds), "Either omit --allowed_tlds if all TLDs are allowed, or include a comma-separated list"); + verifyAllTldsExist(allowedTlds); if (ImmutableList.of("").equals(allowedEppActions)) { allowedEppActions = ImmutableList.of(); @@ -326,6 +332,34 @@ class GenerateAllocationTokensCommand implements Command { } } + static void verifyAllRegistrarIdsExist(@Nullable List allowedClientIds) { + // a null/empty list means that all registrars are allowed + if (isNullOrEmpty(allowedClientIds)) { + return; + } + ImmutableSet allRegistrarIds = + Registrar.loadAllKeysCached().stream() + .map(VKey::getKey) + .map(Object::toString) + .collect(toImmutableSet()); + ImmutableList badRegistrarIds = + allowedClientIds.stream() + .filter(id -> !allRegistrarIds.contains(id)) + .collect(toImmutableList()); + checkArgument(badRegistrarIds.isEmpty(), "Unknown registrar ID(s) %s", badRegistrarIds); + } + + static void verifyAllTldsExist(@Nullable List allowedTlds) { + // a null/empty list means that all TLDs are allowed + if (isNullOrEmpty(allowedTlds)) { + return; + } + ImmutableSet allTlds = Tlds.getTldsOfType(Tld.TldType.REAL); + ImmutableList badTlds = + allowedTlds.stream().filter(tld -> !allTlds.contains(tld)).collect(toImmutableList()); + checkArgument(badTlds.isEmpty(), "Unknown REAL TLD(s) %s", badTlds); + } + private void verifyTokenStringsDoNotExist() { ImmutableSet existingTokenStrings = getExistingTokenStrings(ImmutableSet.copyOf(tokenStrings)); diff --git a/core/src/main/java/google/registry/tools/UpdateAllocationTokensCommand.java b/core/src/main/java/google/registry/tools/UpdateAllocationTokensCommand.java index 4c9d32a29..b2584f84a 100644 --- a/core/src/main/java/google/registry/tools/UpdateAllocationTokensCommand.java +++ b/core/src/main/java/google/registry/tools/UpdateAllocationTokensCommand.java @@ -157,6 +157,9 @@ final class UpdateAllocationTokensCommand extends UpdateOrDeleteAllocationTokens endToken = true; } + GenerateAllocationTokensCommand.verifyAllRegistrarIdsExist(allowedClientIds); + GenerateAllocationTokensCommand.verifyAllTldsExist(allowedTlds); + tokensToSave = tm().transact( () -> diff --git a/core/src/test/java/google/registry/tools/GenerateAllocationTokensCommandTest.java b/core/src/test/java/google/registry/tools/GenerateAllocationTokensCommandTest.java index 070f78c00..b5baa101c 100644 --- a/core/src/test/java/google/registry/tools/GenerateAllocationTokensCommandTest.java +++ b/core/src/test/java/google/registry/tools/GenerateAllocationTokensCommandTest.java @@ -39,6 +39,7 @@ import google.registry.model.domain.fee.FeeQueryCommandExtensionItem.CommandName import google.registry.model.domain.token.AllocationToken; import google.registry.model.domain.token.AllocationToken.TokenStatus; import google.registry.model.reporting.HistoryEntry.HistoryEntryId; +import google.registry.testing.DatabaseHelper; import google.registry.testing.DeterministicStringGenerator; import google.registry.testing.DeterministicStringGenerator.Rule; import google.registry.util.StringGenerator.Alphabets; @@ -56,6 +57,7 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase runCommand("--number", "10", "--allowed_tlds", "badtld"))) + .hasMessageThat() + .isEqualTo("Unknown REAL TLD(s) [badtld]"); + } + + @Test + void testFailure_badRegistrar() { + assertThat( + assertThrows( + IllegalArgumentException.class, + () -> runCommand("--number", "10", "--allowed_client_ids", "badregistrar"))) + .hasMessageThat() + .isEqualTo("Unknown registrar ID(s) [badregistrar]"); + } + private AllocationToken createToken( String token, @Nullable HistoryEntryId redemptionHistoryEntryId, diff --git a/core/src/test/java/google/registry/tools/UpdateAllocationTokensCommandTest.java b/core/src/test/java/google/registry/tools/UpdateAllocationTokensCommandTest.java index 56b8c8d52..15c69e8a6 100644 --- a/core/src/test/java/google/registry/tools/UpdateAllocationTokensCommandTest.java +++ b/core/src/test/java/google/registry/tools/UpdateAllocationTokensCommandTest.java @@ -40,14 +40,21 @@ import google.registry.model.domain.fee.FeeQueryCommandExtensionItem.CommandName import google.registry.model.domain.token.AllocationToken; import google.registry.model.domain.token.AllocationToken.RegistrationBehavior; import google.registry.model.domain.token.AllocationToken.TokenStatus; +import google.registry.testing.DatabaseHelper; import org.joda.money.CurrencyUnit; import org.joda.money.Money; import org.joda.time.DateTime; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; /** Unit tests for {@link UpdateAllocationTokensCommand}. */ class UpdateAllocationTokensCommandTest extends CommandTestCase { + @BeforeEach + void beforeEach() { + DatabaseHelper.createTlds("tld", "example"); + } + @Test void testUpdateTlds_setTlds() throws Exception { AllocationToken token = @@ -64,14 +71,24 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase runCommandForced("--allowed_tlds=badtld"))) + .hasMessageThat() + .isEqualTo("Unknown REAL TLD(s) [badtld]"); + } + @Test void testUpdateClientIds_setClientIds() throws Exception { AllocationToken token = persistResource( builderWithPromo().setAllowedRegistrarIds(ImmutableSet.of("toRemove")).build()); - runCommandForced("--prefix", "token", "--allowed_client_ids", "clientone,clienttwo"); + runCommandForced("--prefix", "token", "--allowed_client_ids", "TheRegistrar,NewRegistrar"); assertThat(reloadResource(token).getAllowedRegistrarIds()) - .containsExactly("clientone", "clienttwo"); + .containsExactly("TheRegistrar", "NewRegistrar"); } @Test @@ -83,6 +100,17 @@ class UpdateAllocationTokensCommandTest extends CommandTestCase runCommandForced("--allowed_client_ids=badregistrar"))) + .hasMessageThat() + .isEqualTo("Unknown registrar ID(s) [badregistrar]"); + } + @Test void testUpdateEppActions_setEppActions() throws Exception { AllocationToken token =