Port SubscriptionController to gRPC

This commit is contained in:
Ameya Lokare authored and Jon Chambers committed 2026-06-18 08:27:38 -04:00
1 parent 3cfe8b9f6c
commit 1876b71d39
21 files changed
+2279 -426

No files matched your search

@@ -8,25 +8,21 @@ package org.whispersystems.textsecuregcm.controllers;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.Clock;
import org.signal.libsignal.zkgroup.receipts.ServerZkReceiptOperations;
import org.whispersystems.textsecuregcm.configuration.OneTimeDonationConfiguration;
import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration;
import org.whispersystems.textsecuregcm.storage.IssuedReceiptsManager;
import org.whispersystems.textsecuregcm.subscriptions.BraintreeManager;
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
import org.whispersystems.textsecuregcm.subscriptions.StripeManager;
import org.whispersystems.textsecuregcm.tests.util.SubscriptionConfigTestHelper;
import org.whispersystems.textsecuregcm.util.MockUtils;
import org.whispersystems.textsecuregcm.util.SystemMapper;
class AbstractV1SubscriptionControllerTest {
static final Clock CLOCK = mock(Clock.class);
private static final ObjectMapper YAML_MAPPER = SystemMapper.yamlMapper();
static final OneTimeDonationConfiguration ONETIME_CONFIG = ConfigHelper.getOneTimeConfig();
static final OneTimeDonationConfiguration ONETIME_CONFIG = SubscriptionConfigTestHelper.getOneTimeConfig();
static final StripeManager STRIPE_MANAGER = MockUtils.buildMock(StripeManager.class, mgr ->
when(mgr.getProvider()).thenReturn(PaymentProvider.STRIPE));
static final BraintreeManager BRAINTREE_MANAGER = MockUtils.buildMock(BraintreeManager.class, mgr ->
@@ -35,183 +31,5 @@ class AbstractV1SubscriptionControllerTest {
static final ServerZkReceiptOperations ZK_OPS = mock(ServerZkReceiptOperations.class);
/**
* Encapsulates {@code static} configuration, to keep the class header simpler and avoid illegal forward references
*/
record ConfigHelper() {
static SubscriptionConfiguration getSubscriptionConfig() {
return readValue(SUBSCRIPTION_CONFIG_YAML, SubscriptionConfiguration.class);
}
static OneTimeDonationConfiguration getOneTimeConfig() {
return readValue(ONETIME_CONFIG_YAML, OneTimeDonationConfiguration.class);
}
private static <T> T readValue(String yaml, Class<T> type) {
try {
return YAML_MAPPER.readValue(yaml, type);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private static final String SUBSCRIPTION_CONFIG_YAML = """
badgeExpiration: P30D
badgeGracePeriod: P15D
backupExpiration: P3D
backupGracePeriod: P10D
backupFreeTierMediaDuration: P30D
backupLevels:
201:
playProductId: testPlayProductId
mediaTtl: P40D
prices:
usd:
amount: '5'
processorIds:
STRIPE: R4
BRAINTREE: M4
jpy:
amount: '500'
processorIds:
STRIPE: Q4
BRAINTREE: N4
bif:
amount: '5000'
processorIds:
STRIPE: S4
BRAINTREE: O4
eur:
amount: '5'
processorIds:
STRIPE: A4
BRAINTREE: B4
levels:
5:
badge: B1
prices:
usd:
amount: '5'
processorIds:
STRIPE: R1
BRAINTREE: M1
jpy:
amount: '500'
processorIds:
STRIPE: Q1
BRAINTREE: N1
bif:
amount: '5000'
processorIds:
STRIPE: S1
BRAINTREE: O1
eur:
amount: '5'
processorIds:
STRIPE: A1
BRAINTREE: B1
15:
badge: B2
prices:
usd:
amount: '15'
processorIds:
STRIPE: R2
BRAINTREE: M2
jpy:
amount: '1500'
processorIds:
STRIPE: Q2
BRAINTREE: N2
bif:
amount: '15000'
processorIds:
STRIPE: S2
BRAINTREE: O2
eur:
amount: '15'
processorIds:
STRIPE: A2
BRAINTREE: B2
35:
badge: B3
prices:
usd:
amount: '35'
processorIds:
STRIPE: R3
BRAINTREE: M3
jpy:
amount: '3500'
processorIds:
STRIPE: Q3
BRAINTREE: N3
bif:
amount: '35000'
processorIds:
STRIPE: S3
BRAINTREE: O3
eur:
amount: '35'
processorIds:
STRIPE: A3
BRAINTREE: B3
""";
private static final String ONETIME_CONFIG_YAML = """
boost:
level: 1
expiration: P45D
badge: BOOST
gift:
level: 100
expiration: P60D
badge: GIFT
currencies:
usd:
minimum: '2.50' # fractional to test BigDecimal conversion
gift: '20'
boosts:
- '5.50'
- '6'
- '7'
- '8'
- '9'
- '10'
eur:
minimum: '3'
gift: '5'
boosts:
- '5'
- '10'
- '20'
- '30'
- '50'
- '100'
jpy:
minimum: '250'
gift: '2000'
boosts:
- '550'
- '600'
- '700'
- '800'
- '900'
- '1000'
bif:
minimum: '2500'
gift: '20000'
boosts:
- '5500'
- '6000'
- '7000'
- '8000'
- '9000'
- '10000'
sepaMaximumEuros: '10000'
""";
}
}
@@ -79,6 +79,7 @@ import org.whispersystems.textsecuregcm.subscriptions.BankMandateTranslator;
import org.whispersystems.textsecuregcm.subscriptions.ChargeFailure;
import org.whispersystems.textsecuregcm.subscriptions.CustomerAwareSubscriptionPaymentProcessor;
import org.whispersystems.textsecuregcm.subscriptions.GooglePlayBillingManager;
import org.whispersystems.textsecuregcm.subscriptions.LevelConfiguration;
import org.whispersystems.textsecuregcm.subscriptions.PaymentMethod;
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
import org.whispersystems.textsecuregcm.subscriptions.ProcessorCustomer;
@@ -91,7 +92,9 @@ import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequire
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorConflictException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptRequestedForOpenPaymentException;
import org.whispersystems.textsecuregcm.storage.WriteConflictException;
import org.whispersystems.textsecuregcm.tests.util.AuthHelper;
import org.whispersystems.textsecuregcm.tests.util.SubscriptionConfigTestHelper;
import org.whispersystems.textsecuregcm.util.MockUtils;
import org.whispersystems.textsecuregcm.util.SystemMapper;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
@@ -100,7 +103,7 @@ import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
private static final long MAX_TOTAL_BACKUP_MEDIA_BYTES = 1234L;
private static final SubscriptionConfiguration SUBSCRIPTION_CONFIG = ConfigHelper.getSubscriptionConfig();
private static final SubscriptionConfiguration SUBSCRIPTION_CONFIG = SubscriptionConfigTestHelper.getSubscriptionConfig();
private static final Subscriptions SUBSCRIPTIONS = mock(Subscriptions.class);
private static final GooglePlayBillingManager PLAY_MANAGER = MockUtils.buildMock(GooglePlayBillingManager.class,
mgr -> when(mgr.getProvider()).thenReturn(PaymentProvider.GOOGLE_PLAY_BILLING));
@@ -913,95 +916,95 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
assertThat(currency.minimum()).isEqualByComparingTo(
BigDecimal.valueOf(2.5).setScale(2, RoundingMode.HALF_EVEN));
assertThat(currency.oneTime()).isEqualTo(
Map.of("1",
Map.of(1L,
List.of(BigDecimal.valueOf(5.5).setScale(2, RoundingMode.HALF_EVEN), BigDecimal.valueOf(6),
BigDecimal.valueOf(7), BigDecimal.valueOf(8),
BigDecimal.valueOf(9), BigDecimal.valueOf(10)), "100",
BigDecimal.valueOf(9), BigDecimal.valueOf(10)), 100L,
List.of(BigDecimal.valueOf(20))));
assertThat(currency.subscription()).isEqualTo(
Map.of("5", BigDecimal.valueOf(5), "15", BigDecimal.valueOf(15), "35", BigDecimal.valueOf(35)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of("201", BigDecimal.valueOf(5)));
assertThat(currency.supportedPaymentMethods()).isEqualTo(List.of("CARD", "PAYPAL"));
Map.of(5L, BigDecimal.valueOf(5), 15L, BigDecimal.valueOf(15), 35L, BigDecimal.valueOf(35)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of(201L, BigDecimal.valueOf(5)));
assertThat(currency.supportedPaymentMethods()).isEqualTo(List.of(PaymentMethod.CARD, PaymentMethod.PAYPAL));
});
assertThat(currencyMap).extractingByKey("jpy").satisfies(currency -> {
assertThat(currency.minimum()).isEqualByComparingTo(
BigDecimal.valueOf(250));
assertThat(currency.oneTime()).isEqualTo(
Map.of("1",
Map.of(1L,
List.of(BigDecimal.valueOf(550), BigDecimal.valueOf(600),
BigDecimal.valueOf(700), BigDecimal.valueOf(800),
BigDecimal.valueOf(900), BigDecimal.valueOf(1000)), "100",
BigDecimal.valueOf(900), BigDecimal.valueOf(1000)), 100L,
List.of(BigDecimal.valueOf(2000))));
assertThat(currency.subscription()).isEqualTo(
Map.of("5", BigDecimal.valueOf(500), "15", BigDecimal.valueOf(1500), "35", BigDecimal.valueOf(3500)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of("201", BigDecimal.valueOf(500)));
assertThat(currency.supportedPaymentMethods()).isEqualTo(List.of("CARD", "PAYPAL"));
Map.of(5L, BigDecimal.valueOf(500), 15L, BigDecimal.valueOf(1500), 35L, BigDecimal.valueOf(3500)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of(201L, BigDecimal.valueOf(500)));
assertThat(currency.supportedPaymentMethods()).isEqualTo(List.of(PaymentMethod.CARD, PaymentMethod.PAYPAL));
});
assertThat(currencyMap).extractingByKey("bif").satisfies(currency -> {
assertThat(currency.minimum()).isEqualByComparingTo(
BigDecimal.valueOf(2500));
assertThat(currency.oneTime()).isEqualTo(
Map.of("1",
Map.of(1L,
List.of(BigDecimal.valueOf(5500), BigDecimal.valueOf(6000),
BigDecimal.valueOf(7000), BigDecimal.valueOf(8000),
BigDecimal.valueOf(9000), BigDecimal.valueOf(10000)), "100",
BigDecimal.valueOf(9000), BigDecimal.valueOf(10000)), 100L,
List.of(BigDecimal.valueOf(20000))));
assertThat(currency.subscription()).isEqualTo(
Map.of("5", BigDecimal.valueOf(5000), "15", BigDecimal.valueOf(15000), "35", BigDecimal.valueOf(35000)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of("201", BigDecimal.valueOf(5000)));
assertThat(currency.supportedPaymentMethods()).isEqualTo(List.of("CARD"));
Map.of(5L, BigDecimal.valueOf(5000), 15L, BigDecimal.valueOf(15000), 35L, BigDecimal.valueOf(35000)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of(201L, BigDecimal.valueOf(5000)));
assertThat(currency.supportedPaymentMethods()).isEqualTo(List.of(PaymentMethod.CARD));
});
assertThat(currencyMap).extractingByKey("eur").satisfies(currency -> {
assertThat(currency.minimum()).isEqualByComparingTo(
BigDecimal.valueOf(3));
assertThat(currency.oneTime()).isEqualTo(
Map.of("1",
Map.of(1L,
List.of(BigDecimal.valueOf(5), BigDecimal.valueOf(10),
BigDecimal.valueOf(20), BigDecimal.valueOf(30), BigDecimal.valueOf(50), BigDecimal.valueOf(100)), "100",
BigDecimal.valueOf(20), BigDecimal.valueOf(30), BigDecimal.valueOf(50), BigDecimal.valueOf(100)), 100L,
List.of(BigDecimal.valueOf(5))));
assertThat(currency.subscription()).isEqualTo(
Map.of("5", BigDecimal.valueOf(5), "15", BigDecimal.valueOf(15), "35", BigDecimal.valueOf(35)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of("201", BigDecimal.valueOf(5)));
final List<String> expectedPaymentMethods = List.of("CARD", "SEPA_DEBIT", "IDEAL");
Map.of(5L, BigDecimal.valueOf(5), 15L, BigDecimal.valueOf(15), 35L, BigDecimal.valueOf(35)));
assertThat(currency.backupSubscription()).isEqualTo(Map.of(201L, BigDecimal.valueOf(5)));
final List<PaymentMethod> expectedPaymentMethods = List.of(PaymentMethod.CARD, PaymentMethod.SEPA_DEBIT, PaymentMethod.IDEAL);
assertThat(currency.supportedPaymentMethods()).isEqualTo(expectedPaymentMethods);
});
});
assertThat(response.levels()).containsKeys("1", "5", "15", "35", "100").satisfies(levelsMap -> {
assertThat(levelsMap).extractingByKey("1").satisfies(
level -> assertThat(level).extracting(SubscriptionController.LevelConfiguration::badge)
assertThat(response.levels()).containsKeys(1L, 5L, 15L, 35L, 100L).satisfies(levelsMap -> {
assertThat(levelsMap).extractingByKey(1L).satisfies(
level -> assertThat(level).extracting(LevelConfiguration::badge)
.satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("BOOST");
assertThat(badge.getName()).isEqualTo("boost1");
}));
assertThat(levelsMap).extractingByKey("100").satisfies(
level -> assertThat(level).extracting(SubscriptionController.LevelConfiguration::badge)
assertThat(levelsMap).extractingByKey(100L).satisfies(
level -> assertThat(level).extracting(LevelConfiguration::badge)
.satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("GIFT");
assertThat(badge.getName()).isEqualTo("gift1");
}));
assertThat(levelsMap).extractingByKey("5").satisfies(level -> {
assertThat(level).extracting(SubscriptionController.LevelConfiguration::badge)
assertThat(levelsMap).extractingByKey(5L).satisfies(level -> {
assertThat(level).extracting(LevelConfiguration::badge)
.satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("B1");
assertThat(badge.getName()).isEqualTo("name1");
});
});
assertThat(levelsMap).extractingByKey("15").satisfies(level ->
assertThat(level).extracting(SubscriptionController.LevelConfiguration::badge)
assertThat(levelsMap).extractingByKey(15L).satisfies(level ->
assertThat(level).extracting(LevelConfiguration::badge)
.satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("B2");
assertThat(badge.getName()).isEqualTo("name2");
}));
assertThat(levelsMap).extractingByKey("35").satisfies(level -> {
assertThat(level).extracting(SubscriptionController.LevelConfiguration::badge)
assertThat(levelsMap).extractingByKey(35L).satisfies(level -> {
assertThat(level).extracting(LevelConfiguration::badge)
.satisfies(badge -> {
assertThat(badge.getId()).isEqualTo("B3");
assertThat(badge.getName()).isEqualTo("name3");
@@ -0,0 +1,724 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.grpc;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import com.google.common.net.InetAddresses;
import com.google.protobuf.ByteString;
import java.math.BigDecimal;
import java.time.Clock;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.signal.chat.subscriptions.CreatePaymentMethodRequest;
import org.signal.chat.subscriptions.CreatePaymentMethodResponse;
import org.signal.chat.subscriptions.CreatePayPalPaymentMethodRequest;
import org.signal.chat.subscriptions.CreatePayPalPaymentMethodResponse;
import org.signal.chat.subscriptions.DeleteSubscriberRequest;
import org.signal.chat.subscriptions.DeleteSubscriberResponse;
import org.signal.chat.subscriptions.GetBankMandateRequest;
import org.signal.chat.subscriptions.GetBankMandateResponse;
import org.signal.chat.subscriptions.GetConfigurationRequest;
import org.signal.chat.subscriptions.GetConfigurationResponse;
import org.signal.chat.subscriptions.GetReceiptCredentialsRequest;
import org.signal.chat.subscriptions.GetReceiptCredentialsResponse;
import org.signal.chat.subscriptions.GetSubscriptionInformationRequest;
import org.signal.chat.subscriptions.GetSubscriptionInformationResponse;
import org.signal.chat.subscriptions.PaymentMethod;
import org.signal.chat.subscriptions.SetDefaultPaymentMethodRequest;
import org.signal.chat.subscriptions.SetDefaultPaymentMethodResponse;
import org.signal.chat.subscriptions.SetIapSubscriptionRequest;
import org.signal.chat.subscriptions.SetIapSubscriptionResponse;
import org.signal.chat.subscriptions.SetSubscriptionLevelRequest;
import org.signal.chat.subscriptions.SetSubscriptionLevelResponse;
import org.signal.chat.subscriptions.SubscriptionsGrpc;
import org.signal.chat.subscriptions.UpdateSubscriberRequest;
import org.signal.chat.subscriptions.UpdateSubscriberResponse;
import org.signal.libsignal.zkgroup.receipts.ReceiptCredentialResponse;
import org.whispersystems.textsecuregcm.badges.BadgeTranslator;
import org.whispersystems.textsecuregcm.configuration.OneTimeDonationConfiguration;
import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicBackupConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.entities.Badge;
import org.whispersystems.textsecuregcm.entities.BadgeSvg;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
import org.whispersystems.textsecuregcm.storage.SubscriptionManager;
import org.whispersystems.textsecuregcm.storage.Subscriptions;
import org.whispersystems.textsecuregcm.subscriptions.AppleAppStoreManager;
import org.whispersystems.textsecuregcm.subscriptions.BankMandateTranslator;
import org.whispersystems.textsecuregcm.subscriptions.BankTransferType;
import org.whispersystems.textsecuregcm.subscriptions.BraintreeManager;
import org.whispersystems.textsecuregcm.subscriptions.ChargeFailure;
import org.whispersystems.textsecuregcm.subscriptions.GooglePlayBillingManager;
import org.whispersystems.textsecuregcm.subscriptions.PaymentProvider;
import org.whispersystems.textsecuregcm.subscriptions.ProcessorCustomer;
import org.whispersystems.textsecuregcm.subscriptions.StripeManager;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionChargeFailurePaymentRequiredException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionForbiddenException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionInformation;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionInvalidArgumentsException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionInvalidIdempotencyKeyException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionInvalidLevelException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionNotFoundException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequiredException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPaymentRequiresActionException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionPrice;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorConflictException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionProcessorException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptAlreadyRedeemedException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionReceiptRequestedForOpenPaymentException;
import org.whispersystems.textsecuregcm.subscriptions.SubscriptionStatus;
import org.whispersystems.textsecuregcm.tests.util.SubscriptionConfigTestHelper;
import org.whispersystems.textsecuregcm.util.TestClock;
import org.whispersystems.textsecuregcm.util.TestRandomUtil;
public class SubscriptionsGrpcServiceTest extends
SimpleBaseGrpcTest<SubscriptionsGrpcService, SubscriptionsGrpc.SubscriptionsBlockingStub> {
private final Clock clock = TestClock.pinned(Instant.ofEpochSecond(100));
private final SubscriptionConfiguration subscriptionConfiguration =
SubscriptionConfigTestHelper.getSubscriptionConfig();
private final OneTimeDonationConfiguration oneTimeDonationConfiguration =
SubscriptionConfigTestHelper.getOneTimeConfig();
@Mock
private SubscriptionManager subscriptionManager;
@Mock
private StripeManager stripeManager;
@Mock
private BraintreeManager braintreeManager;
@Mock
private GooglePlayBillingManager googlePlayBillingManager;
@Mock
private AppleAppStoreManager appleAppStoreManager;
@Mock
private BadgeTranslator badgeTranslator;
@Mock
private BankMandateTranslator bankMandateTranslator;
@Mock
private DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager;
private static final ByteString SUBSCRIBER_ID = ByteString.copyFrom(TestRandomUtil.nextBytes(32));
private static final long LEVEL = 5L;
private static final String CURRENCY = "usd";
@Override
protected SubscriptionsGrpcService createServiceBeforeEachTest() {
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, "en-us"));
return new SubscriptionsGrpcService(clock, subscriptionConfiguration, oneTimeDonationConfiguration,
subscriptionManager, stripeManager, braintreeManager, googlePlayBillingManager, appleAppStoreManager,
badgeTranslator, bankMandateTranslator, dynamicConfigurationManager);
}
@Test
void updateSubscriber() {
final UpdateSubscriberResponse response = unauthenticatedServiceStub().updateSubscriber(
UpdateSubscriberRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(UpdateSubscriberResponse.ResponseCase.SUCCESS, response.getResponseCase());
}
@Test
void updateSubscriberIdMismatch() throws SubscriptionForbiddenException {
doThrow(new SubscriptionForbiddenException("subscriberId mismatch"))
.when(subscriptionManager).updateSubscriber(any());
final UpdateSubscriberResponse response = unauthenticatedServiceStub().updateSubscriber(
UpdateSubscriberRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(UpdateSubscriberResponse.ResponseCase.SUBSCRIBER_ID_MISMATCH, response.getResponseCase());
}
@Test
void deleteSubscriber() {
final DeleteSubscriberResponse response = unauthenticatedServiceStub().deleteSubscriber(
DeleteSubscriberRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(DeleteSubscriberResponse.ResponseCase.SUCCESS, response.getResponseCase());
}
@Test
void deleteSubscriberNotFound()
throws SubscriptionNotFoundException, SubscriptionInvalidArgumentsException, RateLimitExceededException {
doThrow(new SubscriptionNotFoundException())
.when(subscriptionManager).deleteSubscriber(any());
final DeleteSubscriberResponse response = unauthenticatedServiceStub().deleteSubscriber(
DeleteSubscriberRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(DeleteSubscriberResponse.ResponseCase.SUBSCRIBER_NOT_FOUND, response.getResponseCase());
}
@Test
void deleteSubscriberCannotCancel()
throws SubscriptionNotFoundException, SubscriptionInvalidArgumentsException, RateLimitExceededException {
doThrow(new SubscriptionInvalidArgumentsException("cannot cancel subscription"))
.when(subscriptionManager).deleteSubscriber(any());
final DeleteSubscriberResponse response = unauthenticatedServiceStub().deleteSubscriber(
DeleteSubscriberRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(DeleteSubscriberResponse.ResponseCase.CANNOT_CANCEL_SUBSCRIPTION, response.getResponseCase());
}
@ParameterizedTest
@EnumSource(value = PaymentMethod.class, names = {"PAYMENT_METHOD_CARD", "PAYMENT_METHOD_SEPA_DEBIT",
"PAYMENT_METHOD_IDEAL"})
void createPaymentMethod(final PaymentMethod paymentMethod)
throws SubscriptionForbiddenException, SubscriptionNotFoundException, SubscriptionProcessorConflictException {
when(subscriptionManager.addPaymentMethodToCustomer(any(), any(), any(), any())).thenReturn("test-client-secret");
when(stripeManager.getProvider()).thenReturn(PaymentProvider.STRIPE);
final CreatePaymentMethodRequest.Builder builder = CreatePaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setPaymentMethod(paymentMethod);
final CreatePaymentMethodResponse response = unauthenticatedServiceStub().createPaymentMethod(builder.build());
assertEquals(CreatePaymentMethodResponse.ResponseCase.RESULT, response.getResponseCase());
// Currently, only stripe is chosen as the payment provider for all supported payment methods
assertEquals(org.signal.chat.subscriptions.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getResult().getPaymentProvider());
assertEquals("test-client-secret", response.getResult().getClientSecret());
}
@Test
void createPaymentMethodUnsupportedPaymentMethod() {
GrpcTestUtils.assertStatusInvalidArgument(
() -> unauthenticatedServiceStub().createPaymentMethod(
CreatePaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setPaymentMethod(PaymentMethod.PAYMENT_METHOD_PAYPAL)
.build()));
}
@Test
void createPaymentMethodProcessorConflict()
throws SubscriptionForbiddenException, SubscriptionNotFoundException, SubscriptionProcessorConflictException {
doThrow(new SubscriptionProcessorConflictException())
.when(subscriptionManager).addPaymentMethodToCustomer(any(), any(), any(), any());
final CreatePaymentMethodResponse response = unauthenticatedServiceStub().createPaymentMethod(
CreatePaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setPaymentMethod(PaymentMethod.PAYMENT_METHOD_CARD)
.build());
assertEquals(CreatePaymentMethodResponse.ResponseCase.SUBSCRIPTION_PROCESSOR_CONFLICT, response.getResponseCase());
}
@Test
void createPayPalPaymentMethod()
throws SubscriptionForbiddenException, SubscriptionNotFoundException, SubscriptionProcessorConflictException {
final BraintreeManager.PayPalBillingAgreementApprovalDetails details =
new BraintreeManager.PayPalBillingAgreementApprovalDetails("https://fake-approval", "test-billing-token");
when(subscriptionManager.addPaymentMethodToCustomer(any(), any(), any(), any()))
.thenReturn(CompletableFuture.completedFuture(details));
final CreatePayPalPaymentMethodResponse response = unauthenticatedServiceStub().createPayPalPaymentMethod(
CreatePayPalPaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setReturnUrl("https://fake-return")
.setCancelUrl("https://fake-cancel")
.build());
assertEquals(CreatePayPalPaymentMethodResponse.ResponseCase.RESULT, response.getResponseCase());
assertEquals("https://fake-approval", response.getResult().getApprovalUrl());
assertEquals("test-billing-token", response.getResult().getToken());
}
@Test
void createPayPalPaymentMethodProcessorConflict()
throws SubscriptionForbiddenException, SubscriptionNotFoundException, SubscriptionProcessorConflictException {
doThrow(new SubscriptionProcessorConflictException())
.when(subscriptionManager).addPaymentMethodToCustomer(any(), any(), any(), any());
final CreatePayPalPaymentMethodResponse response = unauthenticatedServiceStub().createPayPalPaymentMethod(
CreatePayPalPaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setReturnUrl("https://fake-return")
.setCancelUrl("https://fake-cancel")
.build());
assertEquals(CreatePayPalPaymentMethodResponse.ResponseCase.SUBSCRIPTION_PROCESSOR_CONFLICT,
response.getResponseCase());
}
@ParameterizedTest
@EnumSource(value = SetDefaultPaymentMethodRequest.RequestCase.class, names = {"STRIPE", "BRAINTREE", "SEPA"})
void setDefaultPaymentMethod(final SetDefaultPaymentMethodRequest.RequestCase requestCase)
throws SubscriptionNotFoundException, SubscriptionForbiddenException {
final PaymentProvider provider = requestCase == SetDefaultPaymentMethodRequest.RequestCase.BRAINTREE
? PaymentProvider.BRAINTREE : PaymentProvider.STRIPE;
final Subscriptions.Record record = mock(Subscriptions.Record.class);
when(record.getProcessorCustomer()).thenReturn(Optional.of(new ProcessorCustomer("test-customer", provider)));
when(subscriptionManager.getSubscriber(any())).thenReturn(record);
if (requestCase == SetDefaultPaymentMethodRequest.RequestCase.SEPA) {
when(stripeManager.getGeneratedSepaIdFromSetupIntent(any()))
.thenReturn(CompletableFuture.completedFuture("sepa-id"));
}
final SetDefaultPaymentMethodRequest.Builder builder = SetDefaultPaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID);
switch (requestCase) {
case STRIPE -> builder.setStripe(SetDefaultPaymentMethodRequest.StripePaymentMethod.newBuilder()
.setPaymentMethodToken("test-stripe-token").build());
case BRAINTREE -> builder.setBraintree(SetDefaultPaymentMethodRequest.BraintreePaymentMethod.newBuilder()
.setPaymentMethodToken("test-braintree-token").build());
case SEPA -> builder.setSepa(SetDefaultPaymentMethodRequest.SepaPaymentMethod.newBuilder()
.setSetupIntentId("test-setup-intent-id").build());
default -> throw new IllegalArgumentException("Unexpected case: " + requestCase);
}
final SetDefaultPaymentMethodResponse response =
unauthenticatedServiceStub().setDefaultPaymentMethod(builder.build());
assertEquals(SetDefaultPaymentMethodResponse.ResponseCase.SUCCESS, response.getResponseCase());
}
@Test
void setDefaultPaymentMethodNoRequest() {
GrpcTestUtils.assertStatusInvalidArgument(
() -> unauthenticatedServiceStub().setDefaultPaymentMethod(
SetDefaultPaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build()));
}
@Test
void setDefaultPaymentMethodSubscriptionNotFound()
throws SubscriptionNotFoundException, SubscriptionForbiddenException {
doThrow(new SubscriptionNotFoundException())
.when(subscriptionManager).getSubscriber(any());
final SetDefaultPaymentMethodResponse response = unauthenticatedServiceStub().setDefaultPaymentMethod(
SetDefaultPaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setStripe(SetDefaultPaymentMethodRequest.StripePaymentMethod.newBuilder()
.setPaymentMethodToken("test-token").build())
.build());
assertEquals(SetDefaultPaymentMethodResponse.ResponseCase.SUBSCRIBER_NOT_FOUND, response.getResponseCase());
}
@Test
void setDefaultPaymentMethodNoCustomer() throws SubscriptionNotFoundException, SubscriptionForbiddenException {
final Subscriptions.Record record = mock(Subscriptions.Record.class);
when(record.getProcessorCustomer()).thenReturn(Optional.empty());
when(subscriptionManager.getSubscriber(any())).thenReturn(record);
final SetDefaultPaymentMethodResponse response = unauthenticatedServiceStub().setDefaultPaymentMethod(
SetDefaultPaymentMethodRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setStripe(SetDefaultPaymentMethodRequest.StripePaymentMethod.newBuilder()
.setPaymentMethodToken("test-token").build())
.build());
assertEquals(SetDefaultPaymentMethodResponse.ResponseCase.PAYMENT_METHOD_NOT_SET_UP, response.getResponseCase());
}
private void mockValidSubscription() throws SubscriptionNotFoundException, SubscriptionForbiddenException {
final Subscriptions.Record record = mock(Subscriptions.Record.class);
when(record.getProcessorCustomer()).thenReturn(
Optional.of(new ProcessorCustomer("test-customer", PaymentProvider.STRIPE)));
when(subscriptionManager.getSubscriber(any())).thenReturn(record);
}
@Test
void setSubscriptionLevel() throws SubscriptionException {
mockValidSubscription();
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(LEVEL)
.setCurrency(CURRENCY)
.setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(SetSubscriptionLevelResponse.ResponseCase.SUCCESS, response.getResponseCase());
assertEquals(LEVEL, response.getSuccess().getLevel());
}
@Test
void setSubscriptionLevelNotFound() throws SubscriptionNotFoundException, SubscriptionForbiddenException {
doThrow(new SubscriptionNotFoundException())
.when(subscriptionManager).getSubscriber(any());
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(LEVEL).setCurrency(CURRENCY).setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(SetSubscriptionLevelResponse.ResponseCase.SUBSCRIBER_NOT_FOUND, response.getResponseCase());
}
@Test
void setSubscriptionLevelNoCustomer() throws SubscriptionNotFoundException, SubscriptionForbiddenException {
final Subscriptions.Record record = mock(Subscriptions.Record.class);
when(record.getProcessorCustomer()).thenReturn(Optional.empty());
when(subscriptionManager.getSubscriber(any())).thenReturn(record);
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(LEVEL).setCurrency(CURRENCY).setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(SetSubscriptionLevelResponse.ResponseCase.PAYMENT_METHOD_NOT_SET_UP, response.getResponseCase());
}
@Test
void setSubscriptionLevelUnsupportedLevel() throws SubscriptionNotFoundException, SubscriptionForbiddenException {
final Subscriptions.Record record = mock(Subscriptions.Record.class);
when(record.getProcessorCustomer()).thenReturn(
Optional.of(new ProcessorCustomer("test-customer", PaymentProvider.STRIPE)));
when(subscriptionManager.getSubscriber(any())).thenReturn(record);
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(999L).setCurrency(CURRENCY).setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(SetSubscriptionLevelResponse.ResponseCase.UNSUPPORTED_LEVEL, response.getResponseCase());
}
@Test
void setSubscriptionLevelUnsupportedCurrency() throws SubscriptionNotFoundException, SubscriptionForbiddenException {
final Subscriptions.Record record = mock(Subscriptions.Record.class);
when(record.getProcessorCustomer()).thenReturn(
Optional.of(new ProcessorCustomer("test-customer", PaymentProvider.STRIPE)));
when(subscriptionManager.getSubscriber(any())).thenReturn(record);
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(LEVEL).setCurrency("xyz").setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(SetSubscriptionLevelResponse.ResponseCase.UNSUPPORTED_CURRENCY, response.getResponseCase());
}
@ParameterizedTest
@MethodSource
void setSubscriptionLevelUpdateExceptions(final SubscriptionException exception,
final SetSubscriptionLevelResponse.ResponseCase expectedCase)
throws SubscriptionException {
mockValidSubscription();
doThrow(exception).when(subscriptionManager).updateSubscriptionLevelForCustomer(
any(), any(), any(), anyLong(), any(), any(), any(), any());
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(LEVEL).setCurrency(CURRENCY).setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(expectedCase, response.getResponseCase());
}
static Stream<Arguments> setSubscriptionLevelUpdateExceptions() {
return Stream.of(
Arguments.of(new SubscriptionInvalidIdempotencyKeyException("bad key"),
SetSubscriptionLevelResponse.ResponseCase.INVALID_IDEMPOTENCY_KEY),
Arguments.of(new SubscriptionPaymentRequiresActionException(),
SetSubscriptionLevelResponse.ResponseCase.PAYMENT_REQUIRES_ACTION),
Arguments.of(new SubscriptionInvalidLevelException(),
SetSubscriptionLevelResponse.ResponseCase.INVALID_LEVEL_TRANSITION),
Arguments.of(new SubscriptionProcessorConflictException(),
SetSubscriptionLevelResponse.ResponseCase.SUBSCRIPTION_PROCESSOR_CONFLICT)
);
}
@Test
void setSubscriptionLevelChargeFailure()
throws SubscriptionException {
mockValidSubscription();
doThrow(new SubscriptionProcessorException(PaymentProvider.STRIPE,
new ChargeFailure("card_declined", "Insufficient funds", null, null, null)))
.when(subscriptionManager).updateSubscriptionLevelForCustomer(
any(), any(), any(), anyLong(), any(), any(), any(), any());
final SetSubscriptionLevelResponse response = unauthenticatedServiceStub().setSubscriptionLevel(
SetSubscriptionLevelRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setLevel(LEVEL).setCurrency(CURRENCY).setIdempotencyKey("test-idempotency-key")
.build());
assertEquals(SetSubscriptionLevelResponse.ResponseCase.CHARGE_FAILURE, response.getResponseCase());
assertEquals(org.signal.chat.subscriptions.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getChargeFailure().getProcessor());
assertEquals("card_declined", response.getChargeFailure().getCode());
assertEquals("Insufficient funds", response.getChargeFailure().getMessage());
}
@Test
void getSubscriptionInformation()
throws SubscriptionNotFoundException, SubscriptionForbiddenException, RateLimitExceededException {
final SubscriptionInformation info = new SubscriptionInformation(
new SubscriptionPrice(CURRENCY, BigDecimal.valueOf(500)),
LEVEL,
Instant.ofEpochSecond(100),
Instant.ofEpochSecond(1000),
true,
false,
SubscriptionStatus.ACTIVE,
PaymentProvider.STRIPE,
org.whispersystems.textsecuregcm.subscriptions.PaymentMethod.CARD,
false,
null);
when(subscriptionManager.getSubscriptionInformation(any())).thenReturn(Optional.of(info));
final GetSubscriptionInformationResponse response = unauthenticatedServiceStub().getSubscriptionInformation(
GetSubscriptionInformationRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(GetSubscriptionInformationResponse.ResponseCase.SUCCESS, response.getResponseCase());
assertEquals(LEVEL, response.getSuccess().getLevel());
assertEquals(CURRENCY, response.getSuccess().getCurrency());
assertEquals(500L, response.getSuccess().getAmount());
assertEquals(1000L, response.getSuccess().getEndOfCurrentPeriod());
assertTrue(response.getSuccess().getActive());
assertFalse(response.getSuccess().getCancelAtPeriodEnd());
assertEquals(org.signal.chat.subscriptions.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getSuccess().getProcessor());
assertEquals(PaymentMethod.PAYMENT_METHOD_CARD, response.getSuccess().getPaymentMethod());
}
@Test
void getSubscriptionInformationNoSubscription()
throws SubscriptionNotFoundException, SubscriptionForbiddenException, RateLimitExceededException {
when(subscriptionManager.getSubscriptionInformation(any())).thenReturn(Optional.empty());
final GetSubscriptionInformationResponse response = unauthenticatedServiceStub().getSubscriptionInformation(
GetSubscriptionInformationRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build());
assertEquals(GetSubscriptionInformationResponse.ResponseCase.NO_SUBSCRIPTION, response.getResponseCase());
}
@Test
void getReceiptCredentials() throws Exception {
final ReceiptCredentialResponse receiptCredentialResponse = mock(ReceiptCredentialResponse.class);
final byte[] responseBytes = TestRandomUtil.nextBytes(16);
when(receiptCredentialResponse.serialize()).thenReturn(responseBytes);
when(subscriptionManager.createReceiptCredentials(any(), any(), any()))
.thenReturn(new SubscriptionManager.ReceiptResult(receiptCredentialResponse, null, PaymentProvider.STRIPE));
final GetReceiptCredentialsResponse response = unauthenticatedServiceStub().getReceiptCredentials(
GetReceiptCredentialsRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setReceiptCredentialRequest(ByteString.copyFrom(TestRandomUtil.nextBytes(97)))
.build());
assertEquals(GetReceiptCredentialsResponse.ResponseCase.SUCCESS, response.getResponseCase());
assertArrayEquals(responseBytes, response.getSuccess().getReceiptCredentialResponse().toByteArray());
}
static Stream<Arguments> getReceiptCredentialsExceptions() {
return Stream.of(
Arguments.of(new SubscriptionReceiptRequestedForOpenPaymentException(),
GetReceiptCredentialsResponse.ResponseCase.NO_PAID_INVOICE),
Arguments.of(new SubscriptionPaymentRequiredException(),
GetReceiptCredentialsResponse.ResponseCase.PAYMENT_REQUIRED),
Arguments.of(new SubscriptionReceiptAlreadyRedeemedException(),
GetReceiptCredentialsResponse.ResponseCase.ALREADY_REDEEMED)
);
}
@ParameterizedTest
@MethodSource
void getReceiptCredentialsExceptions(final SubscriptionException exception,
final GetReceiptCredentialsResponse.ResponseCase expectedCase) throws Exception {
doThrow(exception).when(subscriptionManager).createReceiptCredentials(any(), any(), any());
final GetReceiptCredentialsResponse response = unauthenticatedServiceStub().getReceiptCredentials(
GetReceiptCredentialsRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setReceiptCredentialRequest(ByteString.copyFrom(TestRandomUtil.nextBytes(97)))
.build());
assertEquals(expectedCase, response.getResponseCase());
}
@Test
void getReceiptCredentialsChargeFailure() throws Exception {
doThrow(new SubscriptionChargeFailurePaymentRequiredException(PaymentProvider.STRIPE,
new ChargeFailure("card_declined", "Insufficient funds", null, null, null)))
.when(subscriptionManager).createReceiptCredentials(any(), any(), any());
final GetReceiptCredentialsResponse response = unauthenticatedServiceStub().getReceiptCredentials(
GetReceiptCredentialsRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setReceiptCredentialRequest(ByteString.copyFrom(TestRandomUtil.nextBytes(97)))
.build());
assertEquals(GetReceiptCredentialsResponse.ResponseCase.PAYMENT_REQUIRED, response.getResponseCase());
assertEquals(org.signal.chat.subscriptions.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getPaymentRequired().getChargeFailure().getProcessor());
assertEquals("card_declined", response.getPaymentRequired().getChargeFailure().getCode());
}
@ParameterizedTest
@EnumSource(value = SetIapSubscriptionRequest.RequestCase.class, names = {"APP_STORE", "PLAY_BILLING"})
void setIapSubscription(final SetIapSubscriptionRequest.RequestCase requestCase)
throws SubscriptionException, RateLimitExceededException {
when(subscriptionManager.updateAppStoreTransactionId(any(), any(), any())).thenReturn(LEVEL);
when(subscriptionManager.updatePlayBillingPurchaseToken(any(), any(), any())).thenReturn(LEVEL);
final SetIapSubscriptionRequest.Builder builder = SetIapSubscriptionRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID);
switch (requestCase) {
case APP_STORE -> builder.setAppStore(SetIapSubscriptionRequest.AppStorePurchase.newBuilder()
.setOriginalTransactionId("test-transaction-id").build());
case PLAY_BILLING -> builder.setPlayBilling(SetIapSubscriptionRequest.PlayBillingPurchase.newBuilder()
.setPurchaseToken("test-purchase-token").build());
default -> throw new IllegalArgumentException("Unexpected case: " + requestCase);
}
final SetIapSubscriptionResponse response =
unauthenticatedServiceStub().setIapSubscription(builder.build());
assertEquals(SetIapSubscriptionResponse.ResponseCase.SUCCESS, response.getResponseCase());
assertEquals(LEVEL, response.getSuccess().getLevel());
}
@Test
void setIapSubscriptionNoRequest() {
GrpcTestUtils.assertStatusInvalidArgument(
() -> unauthenticatedServiceStub().setIapSubscription(
SetIapSubscriptionRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.build()));
}
static Stream<Arguments> setIapSubscriptionExceptions() {
return Stream.of(
Arguments.of(new SubscriptionNotFoundException(),
SetIapSubscriptionResponse.ResponseCase.SUBSCRIBER_NOT_FOUND),
Arguments.of(new SubscriptionProcessorConflictException(),
SetIapSubscriptionResponse.ResponseCase.SUBSCRIPTION_PROCESSOR_CONFLICT),
Arguments.of(new SubscriptionPaymentRequiredException(),
SetIapSubscriptionResponse.ResponseCase.PAYMENT_REQUIRED),
Arguments.of(new SubscriptionInvalidArgumentsException("invalid"),
SetIapSubscriptionResponse.ResponseCase.INVALID_TRANSACTION)
);
}
@ParameterizedTest
@MethodSource
void setIapSubscriptionExceptions(final SubscriptionException exception,
final SetIapSubscriptionResponse.ResponseCase expectedCase)
throws SubscriptionException, RateLimitExceededException {
doThrow(exception).when(subscriptionManager).updateAppStoreTransactionId(any(), any(), any());
final SetIapSubscriptionResponse response = unauthenticatedServiceStub().setIapSubscription(
SetIapSubscriptionRequest.newBuilder()
.setSubscriberId(SUBSCRIBER_ID)
.setAppStore(SetIapSubscriptionRequest.AppStorePurchase.newBuilder()
.setOriginalTransactionId("test-transaction-id").build())
.build());
assertEquals(expectedCase, response.getResponseCase());
}
@Test
void getConfiguration() {
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
when(dynamicConfiguration.getBackupConfiguration())
.thenReturn(new DynamicBackupConfiguration(null, null, null, null, 1234L));
when(stripeManager.supportsPaymentMethod(any())).thenCallRealMethod();
when(stripeManager.getSupportedCurrenciesForPaymentMethod(
org.whispersystems.textsecuregcm.subscriptions.PaymentMethod.CARD))
.thenReturn(Set.of("usd", "jpy", "bif", "eur"));
when(stripeManager.getSupportedCurrenciesForPaymentMethod(
org.whispersystems.textsecuregcm.subscriptions.PaymentMethod.SEPA_DEBIT))
.thenReturn(Set.of("eur"));
when(stripeManager.getSupportedCurrenciesForPaymentMethod(
org.whispersystems.textsecuregcm.subscriptions.PaymentMethod.IDEAL))
.thenReturn(Set.of("eur"));
when(braintreeManager.supportsPaymentMethod(any())).thenCallRealMethod();
when(braintreeManager.getSupportedCurrenciesForPaymentMethod(
org.whispersystems.textsecuregcm.subscriptions.PaymentMethod.PAYPAL))
.thenReturn(Set.of("usd", "jpy"));
when(badgeTranslator.translate(any(), eq("B1"))).thenReturn(new Badge("B1", "cat1", "name1", "desc1",
List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"))));
when(badgeTranslator.translate(any(), eq("B2"))).thenReturn(new Badge("B2", "cat2", "name2", "desc2",
List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"))));
when(badgeTranslator.translate(any(), eq("B3"))).thenReturn(new Badge("B3", "cat3", "name3", "desc3",
List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"))));
when(badgeTranslator.translate(any(), eq("BOOST"))).thenReturn(new Badge("BOOST", "boost1", "boost1", "boost1",
List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"))));
when(badgeTranslator.translate(any(), eq("GIFT"))).thenReturn(new Badge("GIFT", "gift1", "gift1", "gift1",
List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"))));
final GetConfigurationResponse response = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
assertEquals("10000", response.getSepaMaximumEuros());
assertEquals(30L, response.getBackup().getFreeTierMediaDays());
final GetConfigurationResponse.BackupLevelConfiguration backupLevel =
response.getBackup().getLevelsOrThrow(201L);
assertEquals(1234L, backupLevel.getStorageAllowanceBytes());
assertEquals("testPlayProductId", backupLevel.getPlayProductId());
assertEquals(40L, backupLevel.getMediaTtlDays());
final GetConfigurationResponse.CurrencyConfiguration usd = response.getCurrenciesOrThrow("usd");
assertEquals("2.50", usd.getMinimum());
assertEquals(List.of(PaymentMethod.PAYMENT_METHOD_CARD, PaymentMethod.PAYMENT_METHOD_PAYPAL),
usd.getSupportedPaymentMethodsList());
assertEquals(List.of("5.50", "6", "7", "8", "9", "10"), usd.getOneTimeOrThrow(1L).getAmountsList());
assertEquals(List.of("20"), usd.getOneTimeOrThrow(100L).getAmountsList());
assertEquals(Map.of(5L, "5", 15L, "15", 35L, "35"), usd.getSubscriptionMap());
assertEquals(Map.of(201L, "5"), usd.getBackupSubscriptionMap());
final GetConfigurationResponse.CurrencyConfiguration jpy = response.getCurrenciesOrThrow("jpy");
assertEquals("250", jpy.getMinimum());
assertEquals(List.of(PaymentMethod.PAYMENT_METHOD_CARD, PaymentMethod.PAYMENT_METHOD_PAYPAL),
jpy.getSupportedPaymentMethodsList());
assertEquals(List.of("550", "600", "700", "800", "900", "1000"), jpy.getOneTimeOrThrow(1L).getAmountsList());
assertEquals(List.of("2000"), jpy.getOneTimeOrThrow(100L).getAmountsList());
assertEquals(Map.of(5L, "500", 15L, "1500", 35L, "3500"), jpy.getSubscriptionMap());
assertEquals(Map.of(201L, "500"), jpy.getBackupSubscriptionMap());
final GetConfigurationResponse.CurrencyConfiguration bif = response.getCurrenciesOrThrow("bif");
assertEquals("2500", bif.getMinimum());
assertEquals(List.of(PaymentMethod.PAYMENT_METHOD_CARD), bif.getSupportedPaymentMethodsList());
assertEquals(List.of("5500", "6000", "7000", "8000", "9000", "10000"), bif.getOneTimeOrThrow(1L).getAmountsList());
assertEquals(List.of("20000"), bif.getOneTimeOrThrow(100L).getAmountsList());
assertEquals(Map.of(5L, "5000", 15L, "15000", 35L, "35000"), bif.getSubscriptionMap());
assertEquals(Map.of(201L, "5000"), bif.getBackupSubscriptionMap());
final GetConfigurationResponse.CurrencyConfiguration eur = response.getCurrenciesOrThrow("eur");
assertEquals("3", eur.getMinimum());
assertEquals(
List.of(PaymentMethod.PAYMENT_METHOD_CARD, PaymentMethod.PAYMENT_METHOD_SEPA_DEBIT,
PaymentMethod.PAYMENT_METHOD_IDEAL),
eur.getSupportedPaymentMethodsList());
assertEquals(List.of("5", "10", "20", "30", "50", "100"), eur.getOneTimeOrThrow(1L).getAmountsList());
assertEquals(List.of("5"), eur.getOneTimeOrThrow(100L).getAmountsList());
assertEquals(Map.of(5L, "5", 15L, "15", 35L, "35"), eur.getSubscriptionMap());
assertEquals(Map.of(201L, "5"), eur.getBackupSubscriptionMap());
assertEquals("B1", response.getLevelsOrThrow(5L).getBadge().getBadge().getId());
assertEquals(0L, response.getLevelsOrThrow(5L).getBadge().getDurationSeconds());
assertEquals("B2", response.getLevelsOrThrow(15L).getBadge().getBadge().getId());
assertEquals("B3", response.getLevelsOrThrow(35L).getBadge().getBadge().getId());
assertEquals("BOOST", response.getLevelsOrThrow(1L).getBadge().getBadge().getId());
assertTrue(response.getLevelsOrThrow(1L).getBadge().getDurationSeconds() > 0);
assertEquals("GIFT", response.getLevelsOrThrow(100L).getBadge().getBadge().getId());
assertTrue(response.getLevelsOrThrow(100L).getBadge().getDurationSeconds() > 0);
}
@Test
void getBankMandate() {
when(bankMandateTranslator.translate(any(), eq(BankTransferType.SEPA_DEBIT))).thenReturn("test-mandate");
final GetBankMandateResponse response = unauthenticatedServiceStub().getBankMandate(
GetBankMandateRequest.newBuilder()
.setBankTransferType(org.signal.chat.subscriptions.BankTransferType.BANK_TRANSFER_TYPE_SEPA_DEBIT)
.build());
assertEquals("test-mandate", response.getMandate());
}
}
@@ -0,0 +1,190 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.tests.util;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.whispersystems.textsecuregcm.configuration.OneTimeDonationConfiguration;
import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration;
import org.whispersystems.textsecuregcm.util.SystemMapper;
public final class SubscriptionConfigTestHelper {
private static final ObjectMapper YAML_MAPPER = SystemMapper.yamlMapper();
public static SubscriptionConfiguration getSubscriptionConfig() {
return readValue(SUBSCRIPTION_CONFIG_YAML, SubscriptionConfiguration.class);
}
public static OneTimeDonationConfiguration getOneTimeConfig() {
return readValue(ONETIME_CONFIG_YAML, OneTimeDonationConfiguration.class);
}
private static <T> T readValue(final String yaml, final Class<T> type) {
try {
return YAML_MAPPER.readValue(yaml, type);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
private static final String SUBSCRIPTION_CONFIG_YAML = """
badgeExpiration: P30D
badgeGracePeriod: P15D
backupExpiration: P3D
backupGracePeriod: P10D
backupFreeTierMediaDuration: P30D
backupLevels:
201:
playProductId: testPlayProductId
mediaTtl: P40D
prices:
usd:
amount: '5'
processorIds:
STRIPE: R4
BRAINTREE: M4
jpy:
amount: '500'
processorIds:
STRIPE: Q4
BRAINTREE: N4
bif:
amount: '5000'
processorIds:
STRIPE: S4
BRAINTREE: O4
eur:
amount: '5'
processorIds:
STRIPE: A4
BRAINTREE: B4
levels:
5:
badge: B1
prices:
usd:
amount: '5'
processorIds:
STRIPE: R1
BRAINTREE: M1
jpy:
amount: '500'
processorIds:
STRIPE: Q1
BRAINTREE: N1
bif:
amount: '5000'
processorIds:
STRIPE: S1
BRAINTREE: O1
eur:
amount: '5'
processorIds:
STRIPE: A1
BRAINTREE: B1
15:
badge: B2
prices:
usd:
amount: '15'
processorIds:
STRIPE: R2
BRAINTREE: M2
jpy:
amount: '1500'
processorIds:
STRIPE: Q2
BRAINTREE: N2
bif:
amount: '15000'
processorIds:
STRIPE: S2
BRAINTREE: O2
eur:
amount: '15'
processorIds:
STRIPE: A2
BRAINTREE: B2
35:
badge: B3
prices:
usd:
amount: '35'
processorIds:
STRIPE: R3
BRAINTREE: M3
jpy:
amount: '3500'
processorIds:
STRIPE: Q3
BRAINTREE: N3
bif:
amount: '35000'
processorIds:
STRIPE: S3
BRAINTREE: O3
eur:
amount: '35'
processorIds:
STRIPE: A3
BRAINTREE: B3
""";
private static final String ONETIME_CONFIG_YAML = """
boost:
level: 1
expiration: P45D
badge: BOOST
gift:
level: 100
expiration: P60D
badge: GIFT
currencies:
usd:
minimum: '2.50' # fractional to test BigDecimal conversion
gift: '20'
boosts:
- '5.50'
- '6'
- '7'
- '8'
- '9'
- '10'
eur:
minimum: '3'
gift: '5'
boosts:
- '5'
- '10'
- '20'
- '30'
- '50'
- '100'
jpy:
minimum: '250'
gift: '2000'
boosts:
- '550'
- '600'
- '700'
- '800'
- '900'
- '1000'
bif:
minimum: '2500'
gift: '20000'
boosts:
- '5500'
- '6000'
- '7000'
- '8000'
- '9000'
- '10000'
sepaMaximumEuros: '10000'
""";
private SubscriptionConfigTestHelper() {}
}