Add currentPackageToken on create flow (#1751)

* Add currentPackageToken on create flow

* Change to Truth8 assertion

* Add check for specified renewal behavior
This commit is contained in:
sarahcaseybot 2022-08-23 14:47:41 -04:00 committed by GitHub
parent ad50d6b0c7
commit c118af0d6a
6 changed files with 50 additions and 1 deletions

View file

@ -390,6 +390,11 @@ public final class DomainCreateFlow implements TransactionalFlow {
.addGracePeriod( .addGracePeriod(
GracePeriod.forBillingEvent(GracePeriodStatus.ADD, repoId, createBillingEvent)) GracePeriod.forBillingEvent(GracePeriodStatus.ADD, repoId, createBillingEvent))
.build(); .build();
if (allocationToken.isPresent()
&& allocationToken.get().getTokenType().equals(TokenType.PACKAGE)) {
domain =
domain.asBuilder().setCurrentPackageToken(allocationToken.get().createVKey()).build();
}
DomainHistory domainHistory = DomainHistory domainHistory =
buildDomainHistory(domain, registry, now, period, registry.getAddGracePeriodLength()); buildDomainHistory(domain, registry, now, period, registry.getAddGracePeriodLength());
if (reservationTypes.contains(NAME_COLLISION)) { if (reservationTypes.contains(NAME_COLLISION)) {

View file

@ -105,6 +105,7 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
/** Single-use tokens are invalid after use. Infinite-use tokens, predictably, are not. */ /** Single-use tokens are invalid after use. Infinite-use tokens, predictably, are not. */
public enum TokenType { public enum TokenType {
PACKAGE,
SINGLE_USE, SINGLE_USE,
UNLIMITED_USE UNLIMITED_USE
} }
@ -274,6 +275,10 @@ public class AllocationToken extends BackupGroupRoot implements Buildable {
public AllocationToken build() { public AllocationToken build() {
checkArgumentNotNull(getInstance().tokenType, "Token type must be specified"); checkArgumentNotNull(getInstance().tokenType, "Token type must be specified");
checkArgument(!Strings.isNullOrEmpty(getInstance().token), "Token must not be null or empty"); checkArgument(!Strings.isNullOrEmpty(getInstance().token), "Token must not be null or empty");
checkArgument(
!getInstance().tokenType.equals(TokenType.PACKAGE)
|| getInstance().renewalPriceBehavior.equals(RenewalPriceBehavior.SPECIFIED),
"Package tokens must have renewalPriceBehavior set to SPECIFIED");
checkArgument( checkArgument(
getInstance().domainName == null || TokenType.SINGLE_USE.equals(getInstance().tokenType), getInstance().domainName == null || TokenType.SINGLE_USE.equals(getInstance().tokenType),
"Domain name can only be specified for SINGLE_USE tokens"); "Domain name can only be specified for SINGLE_USE tokens");

View file

@ -25,6 +25,7 @@ import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.DE
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.NONPREMIUM; import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.NONPREMIUM;
import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.SPECIFIED; import static google.registry.model.billing.BillingEvent.RenewalPriceBehavior.SPECIFIED;
import static google.registry.model.domain.fee.Fee.FEE_EXTENSION_URIS; import static google.registry.model.domain.fee.Fee.FEE_EXTENSION_URIS;
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.SINGLE_USE;
import static google.registry.model.domain.token.AllocationToken.TokenType.UNLIMITED_USE; import static google.registry.model.domain.token.AllocationToken.TokenType.UNLIMITED_USE;
import static google.registry.model.eppcommon.StatusValue.PENDING_DELETE; import static google.registry.model.eppcommon.StatusValue.PENDING_DELETE;
@ -70,6 +71,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedMap; import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
import com.google.common.collect.Ordering; import com.google.common.collect.Ordering;
import com.google.common.truth.Truth8;
import com.googlecode.objectify.Key; import com.googlecode.objectify.Key;
import google.registry.config.RegistryConfig; import google.registry.config.RegistryConfig;
import google.registry.flows.EppException; import google.registry.flows.EppException;
@ -3126,4 +3128,25 @@ class DomainCreateFlowTest extends ResourceFlowTestCase<DomainCreateFlow, Domain
ImmutableMap.of("DOMAIN", "example-one.tld", "YEARS", "2")); ImmutableMap.of("DOMAIN", "example-one.tld", "YEARS", "2"));
assertThrows(AllocationTokenNotValidForDomainException.class, this::runFlow); assertThrows(AllocationTokenNotValidForDomainException.class, this::runFlow);
} }
@Test
void testSuccess_packageToken_addsTokenToDomain() throws Exception {
AllocationToken token =
persistResource(
new AllocationToken.Builder()
.setToken("abc123")
.setTokenType(PACKAGE)
.setAllowedTlds(ImmutableSet.of("tld"))
.setRenewalPriceBehavior(SPECIFIED)
.build());
persistContactsAndHosts();
setEppInput(
"domain_create_allocationtoken.xml",
ImmutableMap.of("DOMAIN", "example.tld", "YEARS", "2"));
runFlowAssertResponse(
loadFile("domain_create_response.xml", ImmutableMap.of("DOMAIN", "example.tld")));
Domain domain = reloadResourceByForeignKey();
assertThat(domain.getCurrentPackageToken()).isPresent();
Truth8.assertThat(domain.getCurrentPackageToken()).hasValue(token.createVKey());
}
} }

View file

@ -213,6 +213,20 @@ public class AllocationTokenTest extends EntityTestCase {
assertThat(thrown).hasMessageThat().isEqualTo("Token type can only be set once"); assertThat(thrown).hasMessageThat().isEqualTo("Token type can only be set once");
} }
@Test
void testFail_packageTokenNotSpecifiedRenewalBehavior() {
AllocationToken.Builder builder =
new AllocationToken.Builder()
.setToken("abc123")
.setTokenType(TokenType.PACKAGE)
.setRenewalPriceBehavior(RenewalPriceBehavior.DEFAULT);
IllegalArgumentException thrown =
assertThrows(IllegalArgumentException.class, () -> builder.build());
assertThat(thrown)
.hasMessageThat()
.isEqualTo("Package tokens must have renewalPriceBehavior set to SPECIFIED");
}
@Test @Test
void testBuild_DomainNameWithLessThanTwoParts() { void testBuild_DomainNameWithLessThanTwoParts() {
IllegalArgumentException thrown = IllegalArgumentException thrown =

View file

@ -402,7 +402,8 @@ class GenerateAllocationTokensCommandTest extends CommandTestCase<GenerateAlloca
() -> runCommand("--number", "999", "--type", "INVALID_TYPE")); () -> runCommand("--number", "999", "--type", "INVALID_TYPE"));
assertThat(thrown) assertThat(thrown)
.hasMessageThat() .hasMessageThat()
.isEqualTo("Invalid value for -t parameter. Allowed values:[SINGLE_USE, UNLIMITED_USE]"); .isEqualTo(
"Invalid value for -t parameter. Allowed values:[PACKAGE, SINGLE_USE, UNLIMITED_USE]");
} }
@Test @Test

View file

@ -258,6 +258,7 @@ enum google.registry.model.domain.token.AllocationToken$TokenStatus {
VALID; VALID;
} }
enum google.registry.model.domain.token.AllocationToken$TokenType { enum google.registry.model.domain.token.AllocationToken$TokenType {
PACKAGE;
SINGLE_USE; SINGLE_USE;
UNLIMITED_USE; UNLIMITED_USE;
} }