Check token type of currentPackageToken (#1825)

* Check currentPackageToken TokenType

* Check TokenType of currentPackageToken

* Check that token already exists
This commit is contained in:
sarahcaseybot 2022-10-25 12:39:33 -04:00 committed by GitHub
parent aaa311ec40
commit 0746d28e0c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 72 additions and 14 deletions

View file

@ -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<AllocationToken> 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();
}

View file

@ -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.<DateTime, TokenStatus>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.

View file

@ -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();
}
}