diff --git a/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java b/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java index af48dc2e196..592254100d7 100644 --- a/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java +++ b/core/src/main/java/google/registry/flows/domain/token/AllocationTokenFlowUtils.java @@ -280,7 +280,7 @@ private static AllocationToken loadAndValidateToken( VKey tokenKey = VKey.create(AllocationToken.class, token); AllocationToken tokenEntity = AllocationToken.get(tokenKey).orElseThrow(NonexistentAllocationTokenException::new); - if (tokenEntity.getTokenType().equals(AllocationToken.TokenType.SINGLE_USE)) { + if (tokenEntity.getTokenType().isOneTimeUse()) { // Reload the token to avoid possible cache race conditions where the token may have already // been redeemed tokenEntity = tm().loadByKey(tokenKey); diff --git a/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java b/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java index 69a7ea8fe20..d0cd1b3aa86 100644 --- a/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java +++ b/core/src/test/java/google/registry/flows/domain/DomainCreateFlowTest.java @@ -2137,6 +2137,44 @@ void testSuccess_blockedByBsa_hasRegisterBsaToken() throws Exception { assertSuccessfulCreate("tld", ImmutableSet.of(), allocationToken); } + @Test + void testFailure_blockedByBsa_registerBsaTokenAlreadyRedeemedInDatabaseWithStaleCache() + throws Exception { + AllocationToken.setCacheForTest(Optional.of(Duration.ofMinutes(10))); + try { + enrollTldInBsa(); + AllocationToken token = + persistResource( + new AllocationToken.Builder() + .setToken("abc123") + .setTokenType(REGISTER_BSA) + .setDomainName("example.tld") + .build()); + persistBsaLabel("example"); + persistHosts(); + setEppInput( + "domain_create_allocationtoken.xml", + ImmutableMap.of("DOMAIN", "example.tld", "YEARS", "2")); + runFlow(); + assertSuccessfulCreate("tld", ImmutableSet.of(), token); + assertAllocationTokenWasRedeemed("abc123"); + // Cache still holds the unredeemed REGISTER_BSA token within its 10-minute TTL. + assertThat(AllocationToken.get(token.createVKey()).get().isRedeemed()).isFalse(); + + // Delete the created domain so a second registration of example.tld can be attempted. + Instant deletionTime = Instant.parse("1999-04-03T22:01:00.000Z"); + persistResource( + reloadResourceByForeignKey().asBuilder().setDeletionTime(deletionTime).build()); + clock.setTo(deletionTime.plusSeconds(1)); + + EppException thrown = + assertThrows(AlreadyRedeemedAllocationTokenException.class, this::runFlow); + assertAboutEppExceptions().that(thrown).marshalsToXml(); + } finally { + AllocationToken.setCacheForTest(Optional.empty()); + } + } + @Test void testSuccess_blockedByBsa_reservedDomain_viaAllocationTokenExtension() throws Exception { enrollTldInBsa(); diff --git a/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java b/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java index df8ab722bdd..1417cacf43c 100644 --- a/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java +++ b/core/src/test/java/google/registry/flows/domain/token/AllocationTokenFlowUtilsTest.java @@ -20,6 +20,7 @@ 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.DEFAULT_PROMO; +import static google.registry.model.domain.token.AllocationToken.TokenType.REGISTER_BSA; 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.persistence.transaction.TransactionManagerFactory.tm; @@ -307,6 +308,29 @@ void testFailure_loadFromExtension_singleUseTokenStaleCacheReloadsFromDatabase() assertLoadTokenFromExtensionThrowsException(AlreadyRedeemedAllocationTokenException.class); } + @Test + void testFailure_loadFromExtension_registerBsaTokenStaleCacheReloadsFromDatabase() { + when(allocationTokenExtension.getAllocationToken()).thenReturn("tokeN"); + AllocationToken token = + persistResource( + new AllocationToken.Builder() + .setToken("tokeN") + .setTokenType(REGISTER_BSA) + .setDomainName("example.tld") + .build()); + assertThat(AllocationToken.get(token.createVKey())).hasValue(token); + tm().transact( + () -> + tm().put( + token + .asBuilder() + .setRedemptionHistoryId(new HistoryEntryId("repoId", 1L)) + .build())); + // Cache still returns the old unredeemed REGISTER_BSA token. + assertThat(AllocationToken.get(token.createVKey()).get().isRedeemed()).isFalse(); + assertLoadTokenFromExtensionThrowsException(AlreadyRedeemedAllocationTokenException.class); + } + @Test void testFailure_tokenInvalidForRegistrar() { persistResource(