Simplify SubscriptionExceptions

This commit is contained in:
ravi-signal
2025-09-04 13:50:51 -05:00
committed by GitHub
parent 89b37015c6
commit a5423b6e21
27 changed files with 462 additions and 404 deletions

View File

@@ -21,15 +21,12 @@ import static org.whispersystems.textsecuregcm.util.AttributeValues.n;
import static org.whispersystems.textsecuregcm.util.AttributeValues.s;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.stripe.exception.ApiException;
import com.stripe.model.PaymentIntent;
import io.dropwizard.auth.AuthValueFactoryProvider;
import io.dropwizard.testing.junit5.DropwizardExtensionsSupport;
import io.dropwizard.testing.junit5.ResourceExtension;
import jakarta.ws.rs.client.Entity;
import jakarta.ws.rs.core.Response;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Clock;
@@ -79,7 +76,8 @@ import org.whispersystems.textsecuregcm.mappers.SubscriptionExceptionMapper;
import org.whispersystems.textsecuregcm.storage.IssuedReceiptsManager;
import org.whispersystems.textsecuregcm.storage.OneTimeDonationsManager;
import org.whispersystems.textsecuregcm.storage.PaymentTime;
import org.whispersystems.textsecuregcm.storage.SubscriptionException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionChargeFailurePaymentRequiredException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionException;
import org.whispersystems.textsecuregcm.storage.SubscriptionManager;
import org.whispersystems.textsecuregcm.storage.Subscriptions;
import org.whispersystems.textsecuregcm.subscriptions.AppleAppStoreManager;
@@ -95,6 +93,13 @@ import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
import org.whispersystems.textsecuregcm.subscriptions.PaymentStatus;
import org.whispersystems.textsecuregcm.subscriptions.ProcessorCustomer;
import org.whispersystems.textsecuregcm.subscriptions.StripeManager;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionInvalidArgumentsException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionNotFoundException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequiredException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequiresActionException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorConflictException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptRequestedForOpenPaymentException;
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
import org.whispersystems.textsecuregcm.util.MockUtils;
import org.whispersystems.textsecuregcm.util.SystemMapper;
@@ -338,7 +343,7 @@ class SubscriptionControllerTest {
when(BRAINTREE_MANAGER.captureOneTimePayment(anyString(), anyString(), anyString(), anyString(), anyLong(),
anyLong(), any()))
.thenReturn(CompletableFuture.failedFuture(new SubscriptionException.ProcessorException(PaymentProvider.BRAINTREE,
.thenReturn(CompletableFuture.failedFuture(new SubscriptionProcessorException(PaymentProvider.BRAINTREE,
new ChargeFailure("2046", "Declined", null, null, null))));
final Response response = RESOURCE_EXTENSION.target("/v1/subscription/boost/paypal/confirm")
@@ -417,7 +422,7 @@ class SubscriptionControllerTest {
@Test
void createSubscriptionProcessorDeclined() throws SubscriptionException {
when(STRIPE_MANAGER.createSubscription(any(), any(), anyLong(), anyLong()))
.thenThrow(new SubscriptionException.ProcessorException(PaymentProvider.STRIPE,
.thenThrow(new SubscriptionProcessorException(PaymentProvider.STRIPE,
new ChargeFailure("card_declined", "Insufficient funds", null, null, null)));
final String level = String.valueOf(levelId);
@@ -491,9 +496,9 @@ class SubscriptionControllerTest {
@Test
void stripePaymentIntentRequiresAction()
throws SubscriptionException.InvalidArguments, SubscriptionException.ProcessorException {
throws SubscriptionInvalidArgumentsException, SubscriptionProcessorException {
when(STRIPE_MANAGER.createSubscription(any(), any(), anyLong(), anyLong()))
.thenThrow(new SubscriptionException.PaymentRequiresAction());
.thenThrow(new SubscriptionPaymentRequiresActionException());
final String level = String.valueOf(levelId);
final String idempotencyKey = UUID.randomUUID().toString();
@@ -688,7 +693,7 @@ class SubscriptionControllerTest {
"201, M4",
})
void setSubscriptionLevel(long levelId, String expectedProcessorId)
throws SubscriptionException.ProcessorConflict, SubscriptionException.ProcessorException {
throws SubscriptionProcessorConflictException, SubscriptionProcessorException {
// set up record
final byte[] subscriberUserAndKey = new byte[32];
Arrays.fill(subscriberUserAndKey, (byte) 1);
@@ -735,7 +740,7 @@ class SubscriptionControllerTest {
@MethodSource
void setSubscriptionLevelExistingSubscription(final String existingCurrency, final long existingLevel,
final String requestCurrency, final long requestLevel, final boolean expectUpdate)
throws SubscriptionException.ProcessorConflict, SubscriptionException.ProcessorException {
throws SubscriptionProcessorConflictException, SubscriptionProcessorException {
// set up record
final byte[] subscriberUserAndKey = new byte[32];
@@ -854,7 +859,7 @@ class SubscriptionControllerTest {
@Test
public void setAppStoreTransactionId()
throws SubscriptionException.InvalidArguments, SubscriptionException.PaymentRequired, RateLimitExceededException, SubscriptionException.NotFound {
throws SubscriptionInvalidArgumentsException, SubscriptionPaymentRequiredException, RateLimitExceededException, SubscriptionNotFoundException {
final String originalTxId = "aTxId";
final byte[] subscriberUserAndKey = new byte[32];
Arrays.fill(subscriberUserAndKey, (byte) 1);
@@ -1003,7 +1008,7 @@ class SubscriptionControllerTest {
b(new ProcessorCustomer("customer", PaymentProvider.STRIPE).toDynamoBytes()),
Subscriptions.KEY_SUBSCRIPTION_ID, s("subscriptionId"))))));
when(STRIPE_MANAGER.getReceiptItem(any()))
.thenThrow(new SubscriptionException.ChargeFailurePaymentRequired(
.thenThrow(new SubscriptionChargeFailurePaymentRequiredException(
PaymentProvider.STRIPE,
new ChargeFailure("card_declined", "Insufficient funds", null, null, null)));
@@ -1027,7 +1032,7 @@ class SubscriptionControllerTest {
@ParameterizedTest
@CsvSource({"5, P45D", "201, P13D"})
public void createReceiptCredential(long level, Duration expectedExpirationWindow)
throws InvalidInputException, VerificationFailedException, SubscriptionException.ChargeFailurePaymentRequired, SubscriptionException.ReceiptRequestedForOpenPayment {
throws InvalidInputException, VerificationFailedException, SubscriptionChargeFailurePaymentRequiredException, SubscriptionReceiptRequestedForOpenPaymentException {
final byte[] subscriberUserAndKey = new byte[32];
Arrays.fill(subscriberUserAndKey, (byte) 1);
final String subscriberId = Base64.getEncoder().encodeToString(subscriberUserAndKey);

View File

@@ -34,16 +34,11 @@ import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.storage.SubscriptionException;
class AppleAppStoreManagerTest {
@@ -101,7 +96,7 @@ class AppleAppStoreManagerTest {
public void generateReceiptExpired()
throws VerificationException, APIException, IOException {
mockSubscription(Status.EXPIRED, AutoRenewStatus.ON);
assertThatExceptionOfType(SubscriptionException.PaymentRequired.class)
assertThatExceptionOfType(SubscriptionPaymentRequiredException.class)
.isThrownBy(() -> appleAppStoreManager.getReceiptItem(ORIGINAL_TX_ID));
}
@@ -196,7 +191,7 @@ class AppleAppStoreManagerTest {
@EnumSource(mode = EnumSource.Mode.EXCLUDE, names = {"EXPIRED", "REVOKED"})
public void cancelFailsForActiveSubscription(Status status) throws APIException, VerificationException, IOException {
mockSubscription(status, AutoRenewStatus.ON);
assertThatExceptionOfType(SubscriptionException.InvalidArguments.class)
assertThatExceptionOfType(SubscriptionInvalidArgumentsException.class)
.isThrownBy(() -> appleAppStoreManager.cancelAllActiveSubscriptions(ORIGINAL_TX_ID));
}

View File

@@ -41,7 +41,6 @@ import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.storage.SubscriptionException;
import org.whispersystems.textsecuregcm.util.MockUtils;
import org.whispersystems.textsecuregcm.util.MutableClock;
@@ -133,7 +132,7 @@ class GooglePlayBillingManagerTest {
switch (subscriptionState) {
case ACTIVE, IN_GRACE_PERIOD, CANCELED -> assertThatNoException()
.isThrownBy(() -> googlePlayBillingManager.validateToken(PURCHASE_TOKEN));
default -> assertThatExceptionOfType(SubscriptionException.PaymentRequired.class)
default -> assertThatExceptionOfType(SubscriptionPaymentRequiredException.class)
.isThrownBy(() -> googlePlayBillingManager.validateToken(PURCHASE_TOKEN));
}
}
@@ -234,7 +233,7 @@ class GooglePlayBillingManagerTest {
// next second should be expired
clock.setTimeInstant(day10.plus(Duration.ofSeconds(1)));
assertThatExceptionOfType(SubscriptionException.PaymentRequired.class)
assertThatExceptionOfType(SubscriptionPaymentRequiredException.class)
.isThrownBy(() -> googlePlayBillingManager.getReceiptItem(PURCHASE_TOKEN));
}
@@ -272,8 +271,8 @@ class GooglePlayBillingManagerTest {
public static Stream<Arguments> tokenErrors() {
return Stream.of(
Arguments.of(404, SubscriptionException.NotFound.class),
Arguments.of(410, SubscriptionException.NotFound.class),
Arguments.of(404, SubscriptionNotFoundException.class),
Arguments.of(410, SubscriptionNotFoundException.class),
Arguments.of(400, HttpResponseException.class)
);
}

View File

@@ -8,18 +8,11 @@ package org.whispersystems.textsecuregcm.subscriptions;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.braintreegateway.BraintreeGateway;
import com.braintreegateway.Customer;
import com.braintreegateway.CustomerGateway;
import com.google.cloud.pubsub.v1.Publisher;
import com.stripe.StripeClient;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@@ -32,8 +25,6 @@ import com.stripe.service.SubscriptionService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.whispersystems.textsecuregcm.currency.CurrencyConversionManager;
import org.whispersystems.textsecuregcm.storage.SubscriptionException;
class StripeManagerTest {
@@ -67,7 +58,7 @@ class StripeManagerTest {
when(subscriptionService.create(any(), any())).thenThrow(stripeException);
when(stripeClient.subscriptions()).thenReturn(subscriptionService);
assertThatExceptionOfType(SubscriptionException.PaymentRequiresAction.class).isThrownBy(() ->
assertThatExceptionOfType(SubscriptionPaymentRequiresActionException.class).isThrownBy(() ->
stripeManager.createSubscription("customerId", "priceId", 1, 0));
}
}