Move donation/backup configuration to own service

Also, add an etag so configuration can be sent only if it changes
This commit is contained in:
ravi-signal authored and GitHub committed 2026-07-01 12:19:00 -05:00
1 parent 67a03f81b9
commit 6b67604fe7
23 files changed
+676 -447

No files matched your search

@@ -72,14 +72,12 @@ import org.whispersystems.textsecuregcm.attachments.TusAttachmentGenerator;
import org.whispersystems.textsecuregcm.auth.AuthenticatedBackupUser;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentials;
import org.whispersystems.textsecuregcm.auth.ExternalServiceCredentialsGenerator;
import org.whispersystems.textsecuregcm.configuration.BackupConfiguration;
import org.whispersystems.textsecuregcm.configuration.SecureValueRecoveryConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicBackupConfiguration;
import org.whispersystems.textsecuregcm.configuration.dynamic.DynamicConfiguration;
import org.whispersystems.textsecuregcm.controllers.RateLimitExceededException;
import org.whispersystems.textsecuregcm.limits.RateLimiter;
import org.whispersystems.textsecuregcm.limits.RateLimiters;
import org.whispersystems.textsecuregcm.securevaluerecovery.SecureValueRecoveryClient;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
import org.whispersystems.textsecuregcm.storage.DynamoDbExtension;
import org.whispersystems.textsecuregcm.storage.DynamoDbExtensionSchema;
import org.whispersystems.textsecuregcm.util.AttributeValues;
@@ -114,7 +112,7 @@ public class BackupManagerTest {
private final RemoteStorageManager remoteStorageManager = mock(RemoteStorageManager.class);
private final byte[] backupKey = TestRandomUtil.nextBytes(32);
private final UUID aci = UUID.randomUUID();
private final DynamicBackupConfiguration backupConfiguration = new DynamicBackupConfiguration(
private final BackupConfiguration backupConfiguration = new BackupConfiguration(
3, 4, 5, Duration.ofSeconds(30), MAX_TOTAL_MEDIA_BYTES);
@@ -146,11 +144,6 @@ public class BackupManagerTest {
DYNAMO_DB_EXTENSION.getDynamoDbAsyncClient(),
DynamoDbExtensionSchema.Tables.BACKUPS.tableName(),
testClock);
@SuppressWarnings("unchecked") final DynamicConfigurationManager<DynamicConfiguration> dynamicConfigurationManager =
mock(DynamicConfigurationManager.class);
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
when(dynamicConfiguration.getBackupConfiguration()).thenReturn(backupConfiguration);
when(dynamicConfigurationManager.getConfiguration()).thenReturn(dynamicConfiguration);
this.backupManager = new BackupManager(
backupsDb,
@@ -162,7 +155,7 @@ public class BackupManagerTest {
svrbCredentialGenerator,
svrbClient,
testClock,
dynamicConfigurationManager);
backupConfiguration);
}
@ParameterizedTest
@@ -61,15 +61,12 @@ import org.signal.libsignal.zkgroup.receipts.ServerZkReceiptOperations;
import org.whispersystems.textsecuregcm.auth.AuthenticatedDevice;
import org.whispersystems.textsecuregcm.badges.BadgeTranslator;
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.SubscriptionController.GetBankMandateResponse;
import org.whispersystems.textsecuregcm.controllers.SubscriptionController.GetSubscriptionConfigurationResponse;
import org.whispersystems.textsecuregcm.entities.Badge;
import org.whispersystems.textsecuregcm.entities.BadgeSvg;
import org.whispersystems.textsecuregcm.mappers.CompletionExceptionMapper;
import org.whispersystems.textsecuregcm.mappers.SubscriptionExceptionMapper;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
import org.whispersystems.textsecuregcm.storage.PaymentTime;
import org.whispersystems.textsecuregcm.storage.SubscriptionManager;
import org.whispersystems.textsecuregcm.storage.Subscriptions;
@@ -112,12 +109,11 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
private static final ServerZkReceiptOperations ZK_OPS = mock(ServerZkReceiptOperations.class);
private static final BadgeTranslator BADGE_TRANSLATOR = mock(BadgeTranslator.class);
private static final BankMandateTranslator BANK_MANDATE_TRANSLATOR = mock(BankMandateTranslator.class);
private static final DynamicConfigurationManager<DynamicConfiguration> DYNAMIC_CONFIGURATION_MANAGER = mock(DynamicConfigurationManager.class);
private final static SubscriptionController SUBSCRIPTION_CONTROLLER = new SubscriptionController(CLOCK,
SUBSCRIPTION_CONFIG, ONETIME_CONFIG,
new SubscriptionManager(SUBSCRIPTIONS, List.of(STRIPE_MANAGER, BRAINTREE_MANAGER, PLAY_MANAGER, APPSTORE_MANAGER),
ZK_OPS, ISSUED_RECEIPTS_MANAGER), STRIPE_MANAGER, BRAINTREE_MANAGER, PLAY_MANAGER, APPSTORE_MANAGER,
BADGE_TRANSLATOR, BANK_MANDATE_TRANSLATOR, DONATION_PERMITS_MANAGER, DYNAMIC_CONFIGURATION_MANAGER);
BADGE_TRANSLATOR, BANK_MANDATE_TRANSLATOR, DONATION_PERMITS_MANAGER, MAX_TOTAL_BACKUP_MEDIA_BYTES);
private static final ResourceExtension RESOURCE_EXTENSION = ResourceExtension.builder()
.addProperty(ServerProperties.UNWRAP_COMPLETION_STAGE_IN_WRITER_ENABLE, Boolean.TRUE)
.addProvider(AuthHelper.getAuthFilter())
@@ -139,10 +135,6 @@ class SubscriptionControllerTest extends AbstractV1SubscriptionControllerTest {
when(STRIPE_MANAGER.getProvider()).thenReturn(PaymentProvider.STRIPE);
when(BRAINTREE_MANAGER.getProvider()).thenReturn(PaymentProvider.BRAINTREE);
final DynamicConfiguration dynamicConfiguration = mock(DynamicConfiguration.class);
when(dynamicConfiguration.getBackupConfiguration())
.thenReturn(new DynamicBackupConfiguration(null, null, null, null, MAX_TOTAL_BACKUP_MEDIA_BYTES));
when(DYNAMIC_CONFIGURATION_MANAGER.getConfiguration()).thenReturn(dynamicConfiguration);
List.of(STRIPE_MANAGER, BRAINTREE_MANAGER)
.forEach(manager -> when(manager.supportsPaymentMethod(any()))
@@ -31,17 +31,17 @@ import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.Mock;
import org.signal.chat.one_time_donations.ConfirmPayPalBoostRequest;
import org.signal.chat.one_time_donations.ConfirmPayPalBoostResponse;
import org.signal.chat.one_time_donations.CreateBoostReceiptCredentialsRequest;
import org.signal.chat.one_time_donations.CreateBoostReceiptCredentialsResponse;
import org.signal.chat.one_time_donations.CreateBoostRequest;
import org.signal.chat.one_time_donations.CreateBoostResponse;
import org.signal.chat.one_time_donations.CreatePayPalBoostRequest;
import org.signal.chat.one_time_donations.CreatePayPalBoostResponse;
import org.signal.chat.one_time_donations.OneTimeDonationsGrpc;
import org.signal.chat.subscriptions.PaymentMethod;
import org.signal.chat.subscriptions.PaymentProvider;
import org.signal.chat.purchase.ConfirmPayPalBoostRequest;
import org.signal.chat.purchase.ConfirmPayPalBoostResponse;
import org.signal.chat.purchase.CreateBoostReceiptCredentialsRequest;
import org.signal.chat.purchase.CreateBoostReceiptCredentialsResponse;
import org.signal.chat.purchase.CreateBoostRequest;
import org.signal.chat.purchase.CreateBoostResponse;
import org.signal.chat.purchase.CreatePayPalBoostRequest;
import org.signal.chat.purchase.CreatePayPalBoostResponse;
import org.signal.chat.purchase.OneTimeDonationsGrpc;
import org.signal.chat.purchase.PaymentMethod;
import org.signal.chat.purchase.PaymentProvider;
import org.signal.libsignal.zkgroup.ServerSecretParams;
import org.signal.libsignal.zkgroup.VerificationFailedException;
import org.signal.libsignal.zkgroup.donation.DonationPermit;
@@ -0,0 +1,220 @@
/*
* Copyright 2026 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.whispersystems.textsecuregcm.grpc;
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.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import com.google.common.net.InetAddresses;
import com.google.protobuf.ByteString;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.signal.chat.purchase.BackupLevelConfiguration;
import org.signal.chat.purchase.Configuration;
import org.signal.chat.purchase.CurrencyConfiguration;
import org.signal.chat.purchase.GetConfigurationRequest;
import org.signal.chat.purchase.GetConfigurationResponse;
import org.signal.chat.purchase.PaymentMethod;
import org.signal.chat.purchase.ProductConfigurationGrpc;
import org.whispersystems.textsecuregcm.badges.BadgeTranslator;
import org.whispersystems.textsecuregcm.configuration.OneTimeDonationConfiguration;
import org.whispersystems.textsecuregcm.configuration.SubscriptionConfiguration;
import org.whispersystems.textsecuregcm.entities.Badge;
import org.whispersystems.textsecuregcm.entities.BadgeSvg;
import org.whispersystems.textsecuregcm.subscriptions.BraintreeManager;
import org.whispersystems.textsecuregcm.subscriptions.StripeManager;
import org.whispersystems.textsecuregcm.tests.util.SubscriptionConfigTestHelper;
public class ProductConfigurationGrpcServiceTest extends
SimpleBaseGrpcTest<ProductConfigurationGrpcService, ProductConfigurationGrpc.ProductConfigurationBlockingStub> {
private final SubscriptionConfiguration subscriptionConfiguration =
SubscriptionConfigTestHelper.getSubscriptionConfig();
private final OneTimeDonationConfiguration oneTimeDonationConfiguration =
SubscriptionConfigTestHelper.getOneTimeConfig();
@Mock
private StripeManager stripeManager;
@Mock
private BraintreeManager braintreeManager;
@Mock
private BadgeTranslator badgeTranslator;
@Override
protected ProductConfigurationGrpcService createServiceBeforeEachTest() {
getMockRequestAttributesInterceptor().setRequestAttributes(
new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, "en-us"));
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"))));
when(badgeTranslator.resolveLocale(any())).thenReturn(Locale.US);
return new ProductConfigurationGrpcService(subscriptionConfiguration, oneTimeDonationConfiguration,
List.of(stripeManager, braintreeManager), badgeTranslator, 1234L);
}
private void checkConfiguration(final Configuration configuration) {
assertEquals("10000", configuration.getSepaMaximumEuros());
assertEquals(30L, configuration.getBackup().getFreeTierMediaDays());
final BackupLevelConfiguration backupLevel =
configuration.getBackup().getLevelsOrThrow(201L);
assertEquals(1234L, backupLevel.getStorageAllowanceBytes());
assertEquals("testPlayProductId", backupLevel.getPlayProductId());
assertEquals(40L, backupLevel.getMediaTtlDays());
final CurrencyConfiguration usd = configuration.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 CurrencyConfiguration jpy = configuration.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 CurrencyConfiguration bif = configuration.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 CurrencyConfiguration eur = configuration.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", configuration.getBadgeLevelsOrThrow(5L).getBadge().getId());
assertEquals(0L, configuration.getBadgeLevelsOrThrow(5L).getBadgeDurationSeconds());
assertEquals("B2", configuration.getBadgeLevelsOrThrow(15L).getBadge().getId());
assertEquals("B3", configuration.getBadgeLevelsOrThrow(35L).getBadge().getId());
assertEquals("BOOST", configuration.getBadgeLevelsOrThrow(1L).getBadge().getId());
assertTrue(configuration.getBadgeLevelsOrThrow(1L).getBadgeDurationSeconds() > 0);
assertEquals("GIFT", configuration.getBadgeLevelsOrThrow(100L).getBadge().getId());
assertTrue(configuration.getBadgeLevelsOrThrow(100L).getBadgeDurationSeconds() > 0);
}
@Test
void getConfigurationNoEtag() {
final GetConfigurationResponse response = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
assertFalse(response.hasEtagMatched());
checkConfiguration(response.getTaggedConfiguration().getConfiguration());
}
@Test
void getConfigurationWrongEtag() {
final GetConfigurationResponse response = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().setEtag(ByteString.copyFrom(new byte[32])).build());
assertFalse(response.hasEtagMatched());
checkConfiguration(response.getTaggedConfiguration().getConfiguration());
}
@Test
void getConfigurationCorrectEtag() {
final GetConfigurationResponse response = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
final ByteString etag = response.getTaggedConfiguration().getEtag();
final GetConfigurationResponse withCorrectEtag = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().setEtag(etag).build());
assertTrue(withCorrectEtag.hasEtagMatched());
assertTrue(withCorrectEtag.getEtagMatched());
assertFalse(withCorrectEtag.hasTaggedConfiguration());
}
@Test
void getConfigurationComputesOnce() {
final RequestAttributes frAttributes =
new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, "fr-fr");
final List<Locale> frLocales = List.of(Locale.FRANCE);
final RequestAttributes caAttributes =
new RequestAttributes(InetAddresses.forString("127.0.0.1"), null, "en-ca");
final List<Locale> caLocales = List.of(Locale.CANADA);
when(badgeTranslator.resolveLocale(frLocales)).thenReturn(Locale.FRANCE);
when(badgeTranslator.resolveLocale(caLocales)).thenReturn(Locale.CANADA);
// return slightly different values based on the language so the two configurations have different etags
when(badgeTranslator.translate(eq(frLocales), 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(eq(caLocales), eq("B1")))
.thenReturn(new Badge("B1", "dog1", "name1", "desc1", List.of("l", "m", "h", "x", "xx", "xxx"), "SVG", List.of(new BadgeSvg("sl", "sd"))));
getMockRequestAttributesInterceptor().setRequestAttributes(caAttributes);
final GetConfigurationResponse caResponse =
unauthenticatedServiceStub().getConfiguration(GetConfigurationRequest.newBuilder().build());
final GetConfigurationResponse caResponseCached = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder()
.setEtag(caResponse.getTaggedConfiguration().getEtag())
.build());
assertTrue(caResponseCached.hasEtagMatched());
verify(badgeTranslator, times(1)).translate(any(), eq("B1"));
getMockRequestAttributesInterceptor().setRequestAttributes(frAttributes);
final GetConfigurationResponse frResponse =
unauthenticatedServiceStub().getConfiguration(GetConfigurationRequest.newBuilder().build());
assertFalse(frResponse.hasEtagMatched());
verify(badgeTranslator, times(2)).translate(any(), eq("B1"));
}
}
@@ -24,7 +24,6 @@ import java.time.Duration;
import java.time.Instant;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
@@ -35,30 +34,28 @@ 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.CreatePayPalPaymentMethodRequest;
import org.signal.chat.subscriptions.CreatePayPalPaymentMethodResponse;
import org.signal.chat.subscriptions.CreatePaymentMethodRequest;
import org.signal.chat.subscriptions.CreatePaymentMethodResponse;
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.chat.purchase.CreatePayPalPaymentMethodRequest;
import org.signal.chat.purchase.CreatePayPalPaymentMethodResponse;
import org.signal.chat.purchase.CreatePaymentMethodRequest;
import org.signal.chat.purchase.CreatePaymentMethodResponse;
import org.signal.chat.purchase.DeleteSubscriberRequest;
import org.signal.chat.purchase.DeleteSubscriberResponse;
import org.signal.chat.purchase.GetBankMandateRequest;
import org.signal.chat.purchase.GetBankMandateResponse;
import org.signal.chat.purchase.GetReceiptCredentialsRequest;
import org.signal.chat.purchase.GetReceiptCredentialsResponse;
import org.signal.chat.purchase.GetSubscriptionInformationRequest;
import org.signal.chat.purchase.GetSubscriptionInformationResponse;
import org.signal.chat.purchase.PaymentMethod;
import org.signal.chat.purchase.SetDefaultPaymentMethodRequest;
import org.signal.chat.purchase.SetDefaultPaymentMethodResponse;
import org.signal.chat.purchase.SetIapSubscriptionRequest;
import org.signal.chat.purchase.SetIapSubscriptionResponse;
import org.signal.chat.purchase.SetSubscriptionLevelRequest;
import org.signal.chat.purchase.SetSubscriptionLevelResponse;
import org.signal.chat.purchase.SubscriptionsGrpc;
import org.signal.chat.purchase.UpdateSubscriberRequest;
import org.signal.chat.purchase.UpdateSubscriberResponse;
import org.signal.libsignal.zkgroup.ServerSecretParams;
import org.signal.libsignal.zkgroup.VerificationFailedException;
import org.signal.libsignal.zkgroup.donation.DonationPermit;
@@ -66,17 +63,10 @@ import org.signal.libsignal.zkgroup.donation.DonationPermitRequest;
import org.signal.libsignal.zkgroup.donation.DonationPermitRequestContext;
import org.signal.libsignal.zkgroup.donation.DonationPermitResponse;
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.DonationPermits;
import org.whispersystems.textsecuregcm.storage.DonationPermitsManager;
import org.whispersystems.textsecuregcm.storage.DynamicConfigurationManager;
import org.whispersystems.textsecuregcm.storage.SubscriptionManager;
import org.whispersystems.textsecuregcm.storage.Subscriptions;
import org.whispersystems.textsecuregcm.subscriptions.AppleAppStoreManager;
@@ -118,9 +108,6 @@ public class SubscriptionsGrpcServiceTest extends
private final SubscriptionConfiguration subscriptionConfiguration =
SubscriptionConfigTestHelper.getSubscriptionConfig();
private final OneTimeDonationConfiguration oneTimeDonationConfiguration =
SubscriptionConfigTestHelper.getOneTimeConfig();
@Mock
private DonationPermits donationPermits;
@@ -142,15 +129,9 @@ public class SubscriptionsGrpcServiceTest extends
@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";
@@ -167,9 +148,8 @@ public class SubscriptionsGrpcServiceTest extends
when(donationPermits.spend(any(byte[].class), any(Instant.class)))
.thenAnswer(answer -> spent.add(new String(answer.getArgument(0, byte[].class))));
return new SubscriptionsGrpcService(clock, subscriptionConfiguration, oneTimeDonationConfiguration,
subscriptionManager, donationPermitsManager, stripeManager, braintreeManager, googlePlayBillingManager,
appleAppStoreManager, badgeTranslator, bankMandateTranslator, dynamicConfigurationManager);
return new SubscriptionsGrpcService(clock, subscriptionConfiguration, subscriptionManager, donationPermitsManager,
stripeManager, braintreeManager, googlePlayBillingManager, appleAppStoreManager, bankMandateTranslator);
}
@Test
@@ -267,7 +247,7 @@ public class SubscriptionsGrpcServiceTest extends
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,
assertEquals(org.signal.chat.purchase.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getResult().getPaymentProvider());
assertEquals("test-client-secret", response.getResult().getClientSecret());
}
@@ -540,7 +520,7 @@ public class SubscriptionsGrpcServiceTest extends
.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,
assertEquals(org.signal.chat.purchase.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getChargeFailure().getProcessor());
assertEquals("card_declined", response.getChargeFailure().getCode());
assertEquals("Insufficient funds", response.getChargeFailure().getMessage());
@@ -573,7 +553,7 @@ public class SubscriptionsGrpcServiceTest extends
assertEquals(1000L, response.getSuccess().getEndOfCurrentPeriod());
assertTrue(response.getSuccess().getActive());
assertFalse(response.getSuccess().getCancelAtPeriodEnd());
assertEquals(org.signal.chat.subscriptions.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
assertEquals(org.signal.chat.purchase.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getSuccess().getProcessor());
assertEquals(PaymentMethod.PAYMENT_METHOD_CARD, response.getSuccess().getPaymentMethod());
}
@@ -640,7 +620,7 @@ public class SubscriptionsGrpcServiceTest extends
.setReceiptCredentialRequest(ByteString.copyFrom(TestRandomUtil.nextBytes(97)))
.build());
assertEquals(GetReceiptCredentialsResponse.ResponseCase.PAYMENT_REQUIRED, response.getResponseCase());
assertEquals(org.signal.chat.subscriptions.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
assertEquals(org.signal.chat.purchase.PaymentProvider.PAYMENT_PROVIDER_STRIPE,
response.getPaymentRequired().getChargeFailure().getProcessor());
assertEquals("card_declined", response.getPaymentRequired().getChargeFailure().getCode());
}
@@ -703,104 +683,12 @@ public class SubscriptionsGrpcServiceTest extends
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)
.setBankTransferType(org.signal.chat.purchase.BankTransferType.BANK_TRANSFER_TYPE_SEPA_DEBIT)
.build());
assertEquals("test-mandate", response.getMandate());
}