Remove badges from product configuration

This commit is contained in:
Ravi Khadiwala authored and ravi-signal committed 2026-07-28 16:20:05 -05:00
1 parent 8e9253181c
commit 888b746294
6 files changed
+57 -232

No files matched your search

@@ -6,34 +6,24 @@
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;
@@ -53,9 +43,6 @@ public class ProductConfigurationGrpcServiceTest extends
@Mock
private BraintreeManager braintreeManager;
@Mock
private BadgeTranslator badgeTranslator;
@Override
protected ProductConfigurationGrpcService createServiceBeforeEachTest() {
getMockRequestAttributesInterceptor().setRequestAttributes(
@@ -76,25 +63,16 @@ public class ProductConfigurationGrpcServiceTest extends
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);
List.of(stripeManager, braintreeManager), 1234L);
}
private void checkConfiguration(final Configuration configuration) {
@Test
void getConfiguration() {
final GetConfigurationResponse configuration = unauthenticatedServiceStub()
.getConfiguration(GetConfigurationRequest.newBuilder().build());
assertEquals("10000", configuration.getSepaMaximumEuros());
assertEquals(30L, configuration.getBackup().getFreeTierMediaDays());
@@ -141,80 +119,15 @@ public class ProductConfigurationGrpcServiceTest extends
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("B1", configuration.getBadgeLevelsOrThrow(5L).getBadgeId());
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());
assertEquals("B2", configuration.getBadgeLevelsOrThrow(15L).getBadgeId());
assertEquals("B3", configuration.getBadgeLevelsOrThrow(35L).getBadgeId());
assertEquals("BOOST", configuration.getBadgeLevelsOrThrow(1L).getBadgeId());
assertTrue(configuration.getBadgeLevelsOrThrow(1L).getBadgeDurationSeconds() > 0);
assertEquals("GIFT", configuration.getBadgeLevelsOrThrow(100L).getBadge().getId());
assertEquals("GIFT", configuration.getBadgeLevelsOrThrow(100L).getBadgeId());
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"));
}
}