Improve payment withdrawals.

This commit is contained in:
Greyson Parrelli
2022-03-04 12:14:42 -05:00
committed by Alex Hart
parent eae6a971e6
commit 427e73f7fd
7 changed files with 232 additions and 63 deletions

View File

@@ -0,0 +1,158 @@
package org.thoughtcrime.securesms.keyvalue
import android.app.Application
import androidx.test.core.app.ApplicationProvider
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.powermock.api.mockito.PowerMockito
import org.powermock.core.classloader.annotations.PowerMockIgnore
import org.powermock.core.classloader.annotations.PrepareForTest
import org.powermock.modules.junit4.rule.PowerMockRule
import org.robolectric.RobolectricTestRunner
import org.robolectric.annotation.Config
import org.thoughtcrime.securesms.dependencies.ApplicationDependencies
import org.thoughtcrime.securesms.dependencies.MockApplicationDependencyProvider
import org.thoughtcrime.securesms.util.FeatureFlags
@RunWith(RobolectricTestRunner::class)
@Config(manifest = Config.NONE, application = Application::class)
@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "android.*", "androidx.*", "org.powermock.*")
@PrepareForTest(FeatureFlags::class)
class PaymentsValuesTest {
@get:Rule
val powerMockRule = PowerMockRule()
@Before
fun setup() {
if (!ApplicationDependencies.isInitialized()) {
ApplicationDependencies.init(ApplicationProvider.getApplicationContext(), MockApplicationDependencyProvider())
}
PowerMockito.mockStatic(FeatureFlags::class.java)
}
@Test
fun `when unregistered, expect NOT_IN_REGION`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, false)
}
)
assertEquals(PaymentsAvailability.NOT_IN_REGION, SignalStore.paymentsValues().paymentsAvailability)
}
@Test
fun `when flag disabled and no account, expect DISABLED_REMOTELY`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, true)
putString(AccountValues.KEY_E164, "+15551234567")
putBoolean(PaymentsValues.MOB_PAYMENTS_ENABLED, false)
}
)
PowerMockito.`when`(FeatureFlags.payments()).thenReturn(false)
PowerMockito.`when`(FeatureFlags.paymentsCountryBlocklist()).thenReturn("")
assertEquals(PaymentsAvailability.DISABLED_REMOTELY, SignalStore.paymentsValues().paymentsAvailability)
}
@Test
fun `when flag disabled but has account, expect WITHDRAW_ONLY`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, true)
putString(AccountValues.KEY_E164, "+15551234567")
putBoolean(PaymentsValues.MOB_PAYMENTS_ENABLED, true)
}
)
PowerMockito.`when`(FeatureFlags.payments()).thenReturn(false)
PowerMockito.`when`(FeatureFlags.paymentsCountryBlocklist()).thenReturn("")
assertEquals(PaymentsAvailability.WITHDRAW_ONLY, SignalStore.paymentsValues().paymentsAvailability)
}
@Test
fun `when flag enabled and no account, expect REGISTRATION_AVAILABLE`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, true)
putString(AccountValues.KEY_E164, "+15551234567")
putBoolean(PaymentsValues.MOB_PAYMENTS_ENABLED, false)
}
)
PowerMockito.`when`(FeatureFlags.payments()).thenReturn(true)
PowerMockito.`when`(FeatureFlags.paymentsCountryBlocklist()).thenReturn("")
assertEquals(PaymentsAvailability.REGISTRATION_AVAILABLE, SignalStore.paymentsValues().paymentsAvailability)
}
@Test
fun `when flag enabled and has account, expect WITHDRAW_AND_SEND`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, true)
putString(AccountValues.KEY_E164, "+15551234567")
putBoolean(PaymentsValues.MOB_PAYMENTS_ENABLED, true)
}
)
PowerMockito.`when`(FeatureFlags.payments()).thenReturn(true)
PowerMockito.`when`(FeatureFlags.paymentsCountryBlocklist()).thenReturn("")
assertEquals(PaymentsAvailability.WITHDRAW_AND_SEND, SignalStore.paymentsValues().paymentsAvailability)
}
@Test
fun `when flag enabled and no account and in the country blocklist, expect NOT_IN_REGION`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, true)
putString(AccountValues.KEY_E164, "+15551234567")
putBoolean(PaymentsValues.MOB_PAYMENTS_ENABLED, false)
}
)
PowerMockito.`when`(FeatureFlags.payments()).thenReturn(true)
PowerMockito.`when`(FeatureFlags.paymentsCountryBlocklist()).thenReturn("1")
assertEquals(PaymentsAvailability.NOT_IN_REGION, SignalStore.paymentsValues().paymentsAvailability)
}
@Test
fun `when flag enabled and has account and in the country blocklist, expect WITHDRAW_ONLY`() {
setupStore(
KeyValueDataSet().apply {
putBoolean(AccountValues.KEY_IS_REGISTERED, true)
putString(AccountValues.KEY_E164, "+15551234567")
putBoolean(PaymentsValues.MOB_PAYMENTS_ENABLED, true)
}
)
PowerMockito.`when`(FeatureFlags.payments()).thenReturn(true)
PowerMockito.`when`(FeatureFlags.paymentsCountryBlocklist()).thenReturn("1")
assertEquals(PaymentsAvailability.WITHDRAW_ONLY, SignalStore.paymentsValues().paymentsAvailability)
}
/**
* Account values will overwrite some values upon first access, so this takes care of that
*/
private fun setupStore(dataset: KeyValueDataSet) {
val store = KeyValueStore(
MockKeyValuePersistentStorage.withDataSet(
dataset.apply {
putString(AccountValues.KEY_ACI, "")
}
)
)
SignalStore.inject(store)
}
}

View File

@@ -2,46 +2,52 @@ package org.thoughtcrime.securesms.payments;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.signal.core.util.logging.Log;
import org.thoughtcrime.securesms.BuildConfig;
import org.thoughtcrime.securesms.testutil.EmptyLogger;
import org.thoughtcrime.securesms.util.FeatureFlags;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assume.assumeFalse;
import static org.junit.Assume.assumeTrue;
@RunWith(PowerMockRunner.class)
@PrepareForTest(FeatureFlags.class)
public final class GeographicalRestrictionsTest {
@Before
public void setup() {
Log.initialize(new EmptyLogger());
PowerMockito.mockStatic(FeatureFlags.class);
}
@Test
public void bad_number_not_allowed() {
assertFalse(GeographicalRestrictions.e164Allowed("bad_number"));
public void e164Allowed_general() {
PowerMockito.when(FeatureFlags.paymentsCountryBlocklist()).thenReturn("");
assertTrue(GeographicalRestrictions.e164Allowed("+15551234567"));
PowerMockito.when(FeatureFlags.paymentsCountryBlocklist()).thenReturn("1");
assertFalse(GeographicalRestrictions.e164Allowed("+15551234567"));
PowerMockito.when(FeatureFlags.paymentsCountryBlocklist()).thenReturn("1,44");
assertFalse(GeographicalRestrictions.e164Allowed("+15551234567"));
assertFalse(GeographicalRestrictions.e164Allowed("+445551234567"));
assertTrue(GeographicalRestrictions.e164Allowed("+525551234567"));
PowerMockito.when(FeatureFlags.paymentsCountryBlocklist()).thenReturn("1 234,44");
assertFalse(GeographicalRestrictions.e164Allowed("+12341234567"));
assertTrue(GeographicalRestrictions.e164Allowed("+15551234567"));
assertTrue(GeographicalRestrictions.e164Allowed("+525551234567"));
assertTrue(GeographicalRestrictions.e164Allowed("+2345551234567"));
}
@Test
public void null_not_allowed() {
public void e164Allowed_nullNotAllowed() {
assertFalse(GeographicalRestrictions.e164Allowed(null));
}
@Test
public void uk_allowed() {
assertTrue(GeographicalRestrictions.e164Allowed("+441617151234"));
}
@Test
public void crimea_not_allowed() {
assertFalse(GeographicalRestrictions.e164Allowed("+79782222222"));
}
@Test
public void blacklist_not_allowed() {
for (int code : BuildConfig.MOBILE_COIN_BLACKLIST) {
assertFalse(GeographicalRestrictions.regionAllowed(code));
}
}
}