Skip to content

Commit

Permalink
Add and update tests
Browse files Browse the repository at this point in the history
Add tests in `RealCreateInstantDebitsResultTest` to ensure the endpoint is called correctly.
  • Loading branch information
tillh-stripe committed Dec 9, 2024
1 parent 3be015b commit 2d0ba4c
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ internal object ApiKeyFixtures {
)

fun consumerSessionSignup() = ConsumerSessionSignup(
accountId = "acct_123",
publishableKey = "pk_123",
consumerSession = consumerSession().copy(
verificationSessions = listOf(
Expand All @@ -163,7 +164,10 @@ internal object ApiKeyFixtures {
),
)

fun cachedConsumerSession() = CachedConsumerSession(
fun cachedConsumerSession(
accountId: String? = null,
) = CachedConsumerSession(
accountId = accountId,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(***) *** **12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ internal class PollAttachPaymentAccountTest {
)

private fun cachedConsumerSession() = CachedConsumerSession(
accountId = null,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
isVerified = true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
package com.stripe.android.financialconnections.domain

import com.google.common.truth.Truth.assertThat
import com.stripe.android.financialconnections.FinancialConnectionsSheet.ElementsSessionContext
import com.stripe.android.financialconnections.FinancialConnectionsSheet.ElementsSessionContext.BillingDetails
import com.stripe.android.financialconnections.repository.CachedConsumerSession
import com.stripe.android.financialconnections.repository.FinancialConnectionsConsumerSessionRepository
import com.stripe.android.financialconnections.repository.FinancialConnectionsRepository
import com.stripe.android.model.ConsumerPaymentDetails
import com.stripe.android.model.IncentiveEligibilitySession
import com.stripe.android.model.LinkConsumerIncentive
import com.stripe.android.model.LinkMode
import com.stripe.android.model.SharePaymentDetails
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.mockito.kotlin.any
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.doReturn
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.verifyNoInteractions
import org.mockito.kotlin.whenever

class RealCreateInstantDebitsResultTest {

Expand Down Expand Up @@ -167,6 +173,96 @@ class RealCreateInstantDebitsResultTest {
)
}

@Test
fun `Skips checking available incentives if not incentive eligible`() = runTest {
val consumerRepository = makeConsumerSessionRepository()
val repository = makeRepository()

val createInstantDebitResult = RealCreateInstantDebitsResult(
consumerRepository = consumerRepository,
repository = repository,
consumerSessionProvider = { makeCachedConsumerSession("acct_789") },
elementsSessionContext = makeElementsSessionContext(
linkMode = LinkMode.LinkPaymentMethod,
incentiveEligibilitySession = null,
),
)

val result = createInstantDebitResult("bank_account_id_001")

verify(consumerRepository, never()).updateAvailableIncentives(
sessionId = any(),
consumerAccount = any(),
)

assertThat(result.eligibleForIncentive).isFalse()
}

@Test
fun `Skips checking available incentives if no signup happened in the flow`() = runTest {
val consumerRepository = makeConsumerSessionRepository()
val repository = makeRepository()

val createInstantDebitResult = RealCreateInstantDebitsResult(
consumerRepository = consumerRepository,
repository = repository,
consumerSessionProvider = {
// A missing account ID means no signup happened
makeCachedConsumerSession(accountId = null)
},
elementsSessionContext = makeElementsSessionContext(
linkMode = LinkMode.LinkPaymentMethod,
incentiveEligibilitySession = IncentiveEligibilitySession.PaymentIntent("pi_123"),
),
)

val result = createInstantDebitResult("bank_account_id_001")

verify(consumerRepository, never()).updateAvailableIncentives(
sessionId = any(),
consumerAccount = any(),
)

assertThat(result.eligibleForIncentive).isFalse()
}

@Test
fun `Checks available incentives if eligible`() = runTest {
val consumerRepository = makeConsumerSessionRepository()
val repository = makeRepository()

val createInstantDebitResult = RealCreateInstantDebitsResult(
consumerRepository = consumerRepository,
repository = repository,
consumerSessionProvider = { makeCachedConsumerSession("acct_789") },
elementsSessionContext = makeElementsSessionContext(
linkMode = LinkMode.LinkPaymentMethod,
incentiveEligibilitySession = IncentiveEligibilitySession.PaymentIntent("pi_123"),
),
)

whenever(
consumerRepository.updateAvailableIncentives(
sessionId = eq("pi_123"),
consumerAccount = eq("acct_789"),
)
).thenReturn(
UpdateAvailableIncentives(
data = listOf(
LinkConsumerIncentive(
incentiveParams = LinkConsumerIncentive.IncentiveParams(
paymentMethod = "link_instant_debits",
),
incentiveDisplayText = "$5",
)
)
)
)

val result = createInstantDebitResult("bank_account_id_001")
assertThat(result.eligibleForIncentive).isTrue()
}

private fun makeConsumerSessionRepository(): FinancialConnectionsConsumerSessionRepository {
val consumerPaymentDetails = ConsumerPaymentDetails(
paymentDetails = listOf(
Expand Down Expand Up @@ -199,8 +295,11 @@ class RealCreateInstantDebitsResultTest {
}
}

private fun makeCachedConsumerSession(): CachedConsumerSession {
private fun makeCachedConsumerSession(
accountId: String? = null,
): CachedConsumerSession {
return CachedConsumerSession(
accountId = accountId,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(***) *** **12",
Expand All @@ -212,6 +311,7 @@ class RealCreateInstantDebitsResultTest {
private fun makeElementsSessionContext(
linkMode: LinkMode?,
billingDetails: BillingDetails? = null,
incentiveEligibilitySession: IncentiveEligibilitySession? = null,
): ElementsSessionContext {
return ElementsSessionContext(
amount = 100L,
Expand All @@ -223,7 +323,7 @@ class RealCreateInstantDebitsResultTest {
phone = null,
phoneCountryCode = null,
),
incentiveEligibilitySession = null,
incentiveEligibilitySession = incentiveEligibilitySession,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ internal class AccountPickerViewModelTest {
@Test
fun `Saves selected accounts to Link if we have a consumer session`() = runTest {
val consumerSession = CachedConsumerSession(
accountId = null,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(***) *** **12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class FinancialConnectionsSheetForInstantDebitsLauncherTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "1234",
bankName = "Bank of America",
eligibleForIncentive = false,
)
)

Expand All @@ -46,6 +47,7 @@ class FinancialConnectionsSheetForInstantDebitsLauncherTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "1234",
bankName = "Bank of America",
eligibleForIncentive = false,
)
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,7 @@ internal class FinancialConnectionsSheetNativeViewModelTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "4242",
bankName = "Stripe Bank",
eligibleForIncentive = false,
)
},
)
Expand All @@ -339,6 +340,7 @@ internal class FinancialConnectionsSheetNativeViewModelTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "4242",
bankName = "Stripe Bank",
eligibleForIncentive = false,
),
)
)
Expand Down Expand Up @@ -382,6 +384,7 @@ internal class FinancialConnectionsSheetNativeViewModelTest {
encodedPaymentMethod = encodedPaymentMethod,
last4 = "4242",
bankName = "Stripe Bank",
eligibleForIncentive = false,
)
},
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class FinancialConnectionsConsumerSessionRepositoryImplTest {
)

val consumerSessionSignup = ConsumerSessionSignup(
accountId = "acct_123",
consumerSession = consumerSession,
publishableKey = "pk_123",
)
Expand Down Expand Up @@ -111,6 +112,7 @@ class FinancialConnectionsConsumerSessionRepositoryImplTest {
// ensures there's a cached consumer session after the signup call.
assertThat(repository.getCachedConsumerSession()).isEqualTo(
CachedConsumerSession(
accountId = "acct_123",
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(•••) ••• ••12",
Expand All @@ -132,6 +134,7 @@ class FinancialConnectionsConsumerSessionRepositoryImplTest {
)

val consumerSessionSignup = ConsumerSessionSignup(
accountId = "acct_123",
consumerSession = consumerSession,
publishableKey = "pk_123",
)
Expand Down Expand Up @@ -212,6 +215,7 @@ class FinancialConnectionsConsumerSessionRepositoryImplTest {
// ensures there's a cached consumer session after the lookup call.
assertThat(repository.getCachedConsumerSession()).isEqualTo(
CachedConsumerSession(
accountId = null,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(•••) ••• ••12",
Expand Down Expand Up @@ -267,6 +271,7 @@ class FinancialConnectionsConsumerSessionRepositoryImplTest {
// ensures there's a cached consumer session after the start-verification call.
assertThat(repository.getCachedConsumerSession()).isEqualTo(
CachedConsumerSession(
accountId = null,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(•••) ••• ••12",
Expand Down Expand Up @@ -316,6 +321,7 @@ class FinancialConnectionsConsumerSessionRepositoryImplTest {
// ensures there's a cached consumer session after the confirm-verification call.
assertThat(repository.getCachedConsumerSession()).isEqualTo(
CachedConsumerSession(
accountId = null,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(•••) ••• ••12",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class RealConsumerSessionRepositoryTest {
val cachedSession = store.provideConsumerSession()
assertThat(cachedSession).isEqualTo(
CachedConsumerSession(
accountId = null,
emailAddress = "[email protected]",
phoneNumber = "(•••) •••-1234",
clientSecret = "abc_123",
Expand All @@ -42,6 +43,7 @@ class RealConsumerSessionRepositoryTest {
val cachedSession = store.provideConsumerSession()
assertThat(cachedSession).isEqualTo(
CachedConsumerSession(
accountId = null,
emailAddress = "[email protected]",
phoneNumber = "(•••) •••-1234",
clientSecret = "abc_123",
Expand All @@ -52,7 +54,7 @@ class RealConsumerSessionRepositoryTest {
}

@Test
fun `Keeps existing publishable key when storing updated consumer session`() {
fun `Keeps existing publishable key and account ID when storing updated consumer session`() {
val store = RealConsumerSessionRepository(savedStateHandle = SavedStateHandle())

val session1 = makeConsumerSession(
Expand All @@ -65,12 +67,13 @@ class RealConsumerSessionRepositoryTest {
isVerified = true,
)

store.storeNewConsumerSession(session1, publishableKey = "pk_123")
store.storeNewConsumerSession(session1, publishableKey = "pk_123", accountId = "acct_123")
store.updateConsumerSession(session2)

val cachedSession = store.provideConsumerSession()
assertThat(cachedSession).isEqualTo(
CachedConsumerSession(
accountId = "acct_123",
emailAddress = "[email protected]",
phoneNumber = "(•••) •••-1234",
clientSecret = "abc_123",
Expand All @@ -94,12 +97,13 @@ class RealConsumerSessionRepositoryTest {
isVerified = false,
)

store.storeNewConsumerSession(session1, publishableKey = "pk_123")
store.storeNewConsumerSession(session2, publishableKey = "pk_456")
store.storeNewConsumerSession(session1, publishableKey = "pk_123", accountId = "acct_123")
store.storeNewConsumerSession(session2, publishableKey = "pk_456", accountId = "acct_456")

val cachedSession = store.provideConsumerSession()
assertThat(cachedSession).isEqualTo(
CachedConsumerSession(
accountId = "acct_456",
emailAddress = "[email protected]",
phoneNumber = "(•••) •••-1234",
clientSecret = "abc_456",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ class RealProvideApiRequestOptionsTest {
publishableKey: String?,
): CachedConsumerSession {
return CachedConsumerSession(
accountId = null,
clientSecret = "clientSecret",
emailAddress = "[email protected]",
phoneNumber = "(***) *** **12",
Expand Down
2 changes: 2 additions & 0 deletions link/src/test/java/com/stripe/android/link/TestFactory.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal object TestFactory {

const val EMAIL = "[email protected]"
const val CLIENT_SECRET = "client_secret"
const val ACCOUNT_ID = "acct_123"
const val PUBLISHABLE_KEY = "publishable_key"
const val MERCHANT_NAME = "merchantName"
const val CUSTOMER_EMAIL = "[email protected]"
Expand Down Expand Up @@ -55,6 +56,7 @@ internal object TestFactory {
)

val CONSUMER_SESSION_SIGN_UP = ConsumerSessionSignup(
accountId = ACCOUNT_ID,
consumerSession = CONSUMER_SESSION,
publishableKey = PUBLISHABLE_KEY
)
Expand Down
Loading

0 comments on commit 2d0ba4c

Please sign in to comment.