From 0746d28e0c06bfdcf2df8e875d443d978f8ffcb7 Mon Sep 17 00:00:00 2001 From: sarahcaseybot Date: Tue, 25 Oct 2022 12:39:33 -0400 Subject: [PATCH] Check token type of currentPackageToken (#1825) * Check currentPackageToken TokenType * Check TokenType of currentPackageToken * Check that token already exists --- .../registry/model/domain/DomainBase.java | 16 ++++++ .../registry/model/domain/DomainSqlTest.java | 20 +++----- .../registry/model/domain/DomainTest.java | 50 +++++++++++++++++++ 3 files changed, 72 insertions(+), 14 deletions(-) diff --git a/core/src/main/java/google/registry/model/domain/DomainBase.java b/core/src/main/java/google/registry/model/domain/DomainBase.java index 01b397f02..e42eadf64 100644 --- a/core/src/main/java/google/registry/model/domain/DomainBase.java +++ b/core/src/main/java/google/registry/model/domain/DomainBase.java @@ -55,6 +55,7 @@ import google.registry.model.domain.launch.LaunchNotice; import google.registry.model.domain.rgp.GracePeriodStatus; import google.registry.model.domain.secdns.DomainDsData; import google.registry.model.domain.token.AllocationToken; +import google.registry.model.domain.token.AllocationToken.TokenType; import google.registry.model.eppcommon.StatusValue; import google.registry.model.host.Host; import google.registry.model.poll.PollMessage; @@ -919,6 +920,21 @@ public class DomainBase extends EppResource } public B setCurrentPackageToken(@Nullable VKey currentPackageToken) { + if (currentPackageToken == null) { + getInstance().currentPackageToken = currentPackageToken; + return thisCastToDerived(); + } + AllocationToken token = + tm().transact(() -> tm().loadByKeyIfPresent(currentPackageToken)) + .orElseThrow( + () -> + new IllegalArgumentException( + String.format( + "The package token %s does not exist", + currentPackageToken.getSqlKey()))); + checkArgument( + token.getTokenType().equals(TokenType.PACKAGE), + "The currentPackageToken must have a PACKAGE TokenType"); getInstance().currentPackageToken = currentPackageToken; return thisCastToDerived(); } diff --git a/core/src/test/java/google/registry/model/domain/DomainSqlTest.java b/core/src/test/java/google/registry/model/domain/DomainSqlTest.java index 14f18bcd6..10b3481aa 100644 --- a/core/src/test/java/google/registry/model/domain/DomainSqlTest.java +++ b/core/src/test/java/google/registry/model/domain/DomainSqlTest.java @@ -17,7 +17,7 @@ package google.registry.model.domain; import static com.google.common.truth.Truth.assertThat; import static google.registry.model.ImmutableObjectSubject.assertAboutImmutableObjects; import static google.registry.model.domain.token.AllocationToken.TokenStatus.NOT_STARTED; -import static google.registry.model.domain.token.AllocationToken.TokenType.UNLIMITED_USE; +import static google.registry.model.domain.token.AllocationToken.TokenType.PACKAGE; import static google.registry.persistence.transaction.TransactionManagerFactory.jpaTm; import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.insertInDb; @@ -39,6 +39,7 @@ import com.googlecode.objectify.Key; import google.registry.model.billing.BillingEvent; import google.registry.model.billing.BillingEvent.Flag; import google.registry.model.billing.BillingEvent.Reason; +import google.registry.model.billing.BillingEvent.RenewalPriceBehavior; import google.registry.model.contact.Contact; import google.registry.model.domain.DesignatedContact.Type; import google.registry.model.domain.launch.LaunchNotice; @@ -144,13 +145,11 @@ public class DomainSqlTest { allocationToken = new AllocationToken.Builder() .setToken("abc123Unlimited") - .setTokenType(UNLIMITED_USE) + .setTokenType(PACKAGE) .setCreationTimeForTest(DateTime.parse("2010-11-12T05:00:00Z")) .setAllowedTlds(ImmutableSet.of("dev", "app")) - .setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar, NewRegistrar")) - .setDiscountFraction(0.5) - .setDiscountPremiums(true) - .setDiscountYears(3) + .setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar")) + .setRenewalPriceBehavior(RenewalPriceBehavior.SPECIFIED) .setTokenStatusTransitions( ImmutableSortedMap.naturalOrder() .put(START_OF_TIME, NOT_STARTED) @@ -168,8 +167,8 @@ public class DomainSqlTest { @Test void testDomainBasePersistenceWithCurrentPackageToken() { - domain = domain.asBuilder().setCurrentPackageToken(allocationToken.createVKey()).build(); persistResource(allocationToken); + domain = domain.asBuilder().setCurrentPackageToken(allocationToken.createVKey()).build(); persistDomain(); assertEqualDomainExcept(loadByKey(domain.createVKey())); } @@ -180,13 +179,6 @@ public class DomainSqlTest { assertThrowForeignKeyViolation(() -> insertInDb(contact, contact2, domain)); } - @Test - void testCurrentPackageTokenForeignKeyConstraints() { - // Persist the domain without the associated allocation token object. - domain = domain.asBuilder().setCurrentPackageToken(allocationToken.createVKey()).build(); - assertThrowForeignKeyViolation(() -> persistDomain()); - } - @Test void testContactForeignKeyConstraints() { // Persist the domain without the associated contact objects. diff --git a/core/src/test/java/google/registry/model/domain/DomainTest.java b/core/src/test/java/google/registry/model/domain/DomainTest.java index 4c3770a70..d5ce67deb 100644 --- a/core/src/test/java/google/registry/model/domain/DomainTest.java +++ b/core/src/test/java/google/registry/model/domain/DomainTest.java @@ -19,6 +19,8 @@ import static com.google.common.collect.Iterables.getOnlyElement; import static com.google.common.truth.Truth.assertThat; import static com.google.common.truth.Truth8.assertThat; import static google.registry.model.EppResourceUtils.loadByForeignKey; +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.testing.DatabaseHelper.cloneAndSetAutoTimestamps; import static google.registry.testing.DatabaseHelper.createTld; import static google.registry.testing.DatabaseHelper.insertInDb; @@ -46,11 +48,13 @@ import google.registry.model.ImmutableObjectSubject; import google.registry.model.billing.BillingEvent; import google.registry.model.billing.BillingEvent.Flag; import google.registry.model.billing.BillingEvent.Reason; +import google.registry.model.billing.BillingEvent.RenewalPriceBehavior; import google.registry.model.contact.Contact; import google.registry.model.domain.DesignatedContact.Type; import google.registry.model.domain.launch.LaunchNotice; import google.registry.model.domain.rgp.GracePeriodStatus; import google.registry.model.domain.secdns.DomainDsData; +import google.registry.model.domain.token.AllocationToken; import google.registry.model.eppcommon.AuthInfo.PasswordAuth; import google.registry.model.eppcommon.StatusValue; import google.registry.model.eppcommon.Trid; @@ -978,4 +982,50 @@ public class DomainTest { assertThat(domain.getBillingContact()).isNull(); assertThat(domain.getTechContact()).isNull(); } + + @Test + void testFail_currentPackageTokenWrongPackageType() { + AllocationToken allocationToken = + persistResource( + new AllocationToken.Builder().setToken("abc123").setTokenType(SINGLE_USE).build()); + IllegalArgumentException thrown = + assertThrows( + IllegalArgumentException.class, + () -> domain.asBuilder().setCurrentPackageToken(allocationToken.createVKey()).build()); + assertThat(thrown) + .hasMessageThat() + .isEqualTo("The currentPackageToken must have a PACKAGE TokenType"); + } + + @Test + void testFailure_packageTokenDoesNotExist() { + AllocationToken allocationToken = + new AllocationToken.Builder() + .setToken("abc123") + .setTokenType(PACKAGE) + .setRenewalPriceBehavior(RenewalPriceBehavior.SPECIFIED) + .setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar")) + .build(); + IllegalArgumentException thrown = + assertThrows( + IllegalArgumentException.class, + () -> domain.asBuilder().setCurrentPackageToken(allocationToken.createVKey()).build()); + assertThat(thrown).hasMessageThat().isEqualTo("The package token abc123 does not exist"); + } + + @Test + void testSuccess_removeCurrentPackageToken() { + AllocationToken allocationToken = + persistResource( + new AllocationToken.Builder() + .setToken("abc123") + .setTokenType(PACKAGE) + .setRenewalPriceBehavior(RenewalPriceBehavior.SPECIFIED) + .setAllowedRegistrarIds(ImmutableSet.of("TheRegistrar")) + .build()); + domain = domain.asBuilder().setCurrentPackageToken(allocationToken.createVKey()).build(); + assertThat(domain.getCurrentPackageToken().get()).isEqualTo(allocationToken.createVKey()); + domain = domain.asBuilder().setCurrentPackageToken(null).build(); + assertThat(domain.getCurrentPackageToken()).isEmpty(); + } }